]> git.evergreen-ils.org Git - Evergreen.git/blob - OpenSRF/src/utils/utils.h
1dc004d6a3688def0653b5fc5b4eb2b3bc9f2fd1
[Evergreen.git] / OpenSRF / src / utils / utils.h
1 /*
2 Copyright (C) 2005  Georgia Public Library Service 
3 Bill Erickson <highfalutin@gmail.com>
4 Mike Rylander <mrylander@gmail.com>
5
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; either version 2
9 of the License, or (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15 */
16
17 #ifndef UTILS_H
18 #define UTILS_H
19
20 #include <stdio.h>
21 #include <stdarg.h>
22 #include <fcntl.h>
23 #include <unistd.h>
24 #include <sys/time.h>
25 #include <sys/stat.h>
26 #include <fcntl.h>
27 #include <sys/types.h>
28 #include <stdlib.h>
29 #include <string.h>
30 //#include <sys/timeb.h>
31
32
33 #define BUFFER_MAX_SIZE 10485760 
34
35 int daemonize();
36
37 void* safe_malloc(int size);
38
39 // ---------------------------------------------------------------------------------
40 // Generic growing buffer. Add data all you want
41 // ---------------------------------------------------------------------------------
42 struct growing_buffer_struct {
43         char *buf;
44         int n_used;
45         int size;
46 };
47 typedef struct growing_buffer_struct growing_buffer;
48
49 growing_buffer* buffer_init( int initial_num_bytes);
50
51 // XXX This isn't defined in utils.c!! removing for now...
52 //int buffer_addchar(growing_buffer* gb, char c);
53
54 int buffer_add(growing_buffer* gb, char* c);
55 int buffer_fadd(growing_buffer* gb, const char* format, ... );
56 int buffer_reset( growing_buffer* gb);
57 char* buffer_data( growing_buffer* gb);
58 int buffer_free( growing_buffer* gb );
59 int buffer_add_char(growing_buffer* gb, char c);
60
61 /* returns the size needed to fill in the vsnprintf buffer.  
62         * ! this calls va_end on the va_list argument*
63         */
64 long va_list_size(const char* format, va_list);
65
66 /* turns a va list into a string, caller must free the 
67         allocated char */
68 char* va_list_to_string(const char* format, ...);
69
70
71 /* string escape utility method.  escapes unicode embeded characters.
72         escapes the usual \n, \t, etc. 
73         for example, if you provide a string like so:
74
75         hello,
76                 you
77
78         you would get back:
79         hello,\n\tyou
80  
81  */
82 char* uescape( const char* string, int size, int full_escape );
83
84 /* utility methods */
85 int set_fl( int fd, int flags );
86 int clr_fl( int fd, int flags );
87
88
89
90 // Utility method
91 double get_timestamp_millis();
92
93
94 /* returns true if the whole string is a number */
95 int stringisnum(char* s);
96
97 /* reads a file and returns the string version of the file
98         user is responsible for freeing the returned char*
99         */
100 char* file_to_string(char* filename);
101
102
103
104
105
106 #endif