2 Copyright (C) 2005 Georgia Public Library Service
3 Bill Erickson <highfalutin@gmail.com>
4 Mike Rylander <mrylander@gmail.com>
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.
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.
27 #include <sys/types.h>
30 //#include <sys/timeb.h>
34 #define OSRF_MALLOC(ptr, size) \
36 ptr = (void*) malloc( size ); \
38 perror("OSRF_MALLOC(): Out of Memory" );\
41 memset( ptr, 0, size );\
45 // The original ... replace with noop once no more errors occur in NDEBUG mode
46 #define osrf_clearbuf( s, n ) memset( s, 0, n )
48 #define osrf_clearbuf( s, n ) \
50 char * clearbuf_temp_s = (s); \
51 size_t clearbuf_temp_n = (n); \
52 memset( clearbuf_temp_s, '!', clearbuf_temp_n ); \
53 clearbuf_temp_s[ clearbuf_temp_n - 1 ] = '\0'; \
57 #define OSRF_BUFFER_ADD(gb, data) \
61 __tl = strlen(data) + gb->n_used;\
62 if( __tl < gb->size ) {\
63 strcpy( gb->buf + gb->n_used, data ); \
65 } else { buffer_add(gb, data); }\
69 #define OSRF_BUFFER_ADD_N(gb, data, n) \
71 growing_buffer* gb__ = gb; \
72 const char* data__ = data; \
75 int tl__ = n__ + gb__->n_used;\
76 if( tl__ < gb__->size ) {\
77 memcpy( gb__->buf + gb__->n_used, data__, n__ ); \
78 gb__->buf[tl__] = '\0'; \
79 gb__->n_used = tl__; \
80 } else { buffer_add_n(gb__, data__, n__); }\
84 #define OSRF_BUFFER_ADD_CHAR(gb, c)\
87 if(gb->n_used < gb->size - 1) {\
88 gb->buf[gb->n_used++] = c;\
89 gb->buf[gb->n_used] = '\0';\
92 buffer_add_char(gb, c);\
96 #define OSRF_BUFFER_RESET(gb) \
97 memset(gb->buf, 0, gb->size);\
103 /* turns a va_list into a string */
104 #define VA_LIST_TO_STRING(x) \
105 unsigned long __len = 0;\
108 va_copy(a_copy, args); \
110 __len = vsnprintf(NULL, 0, x, args); \
115 va_start(a_copy, x); \
116 vsnprintf(_b, __len - 1, x, a_copy); \
120 /* turns a long into a string */
121 #define LONG_TO_STRING(l) \
122 unsigned int __len = snprintf(NULL, 0, "%ld", l) + 2;\
125 snprintf(__b, __len - 1, "%ld", l); \
128 #define DOUBLE_TO_STRING(l) \
129 unsigned int __len = snprintf(NULL, 0, "%f", l) + 2; \
132 snprintf(__b, __len - 1, "%f", l); \
133 char* DOUBLESTR = __b;
135 #define LONG_DOUBLE_TO_STRING(l) \
136 unsigned int __len = snprintf(NULL, 0, "%Lf", l) + 2; \
139 snprintf(__b, __len - 1, "%Lf", l); \
140 char* LONGDOUBLESTR = __b;
143 #define INT_TO_STRING(l) \
144 unsigned int __len = snprintf(NULL, 0, "%d", l) + 2; \
147 snprintf(__b, __len - 1, "%d", l); \
153 struct md5_ctx ctx; \
154 unsigned char digest[16];\
157 for ( i=0 ; i != strlen(text) ; i++ ) MD5_feed (&ctx, text[i]);\
158 MD5_stop (&ctx, digest);\
162 memset(final,0,256);\
163 for ( i=0 ; i<16 ; i++ ) {\
164 sprintf(buf, "%02x", digest[i]);\
165 strcat( final, buf );\
167 char* MD5STR = final;
174 #define BUFFER_MAX_SIZE 10485760
176 /* these are evil and should be condemned
177 ! Only use these if you are done with argv[].
178 call init_proc_title() first, then call
180 the title is only allowed to be as big as the
181 initial process name of the process (full size of argv[]).
182 truncation may occurr.
184 int init_proc_title( int argc, char* argv[] );
185 int set_proc_title( const char* format, ... );
188 int daemonize( void );
190 void* safe_malloc(int size);
191 void* safe_calloc(int size);
193 // ---------------------------------------------------------------------------------
194 // Generic growing buffer. Add data all you want
195 // ---------------------------------------------------------------------------------
196 struct growing_buffer_struct {
201 typedef struct growing_buffer_struct growing_buffer;
203 #define buffer_length(x) (x)->n_used
205 growing_buffer* buffer_init( int initial_num_bytes);
207 // XXX This isn't defined in utils.c!! removing for now...
208 //int buffer_addchar(growing_buffer* gb, char c);
210 int buffer_add(growing_buffer* gb, const char* c);
211 int buffer_add_n(growing_buffer* gb, const char* data, size_t n);
212 int buffer_fadd(growing_buffer* gb, const char* format, ... );
213 int buffer_reset( growing_buffer* gb);
214 char* buffer_data( const growing_buffer* gb);
215 char* buffer_release( growing_buffer* gb );
216 int buffer_free( growing_buffer* gb );
217 int buffer_add_char(growing_buffer* gb, char c);
218 char buffer_chomp(growing_buffer* gb); // removes the last character from the buffer
220 /* returns the size needed to fill in the vsnprintf buffer.
221 * ! this calls va_end on the va_list argument*
223 long va_list_size(const char* format, va_list);
225 /* turns a va list into a string, caller must free the
227 char* va_list_to_string(const char* format, ...);
230 /* string escape utility method. escapes unicode embedded characters.
231 escapes the usual \n, \t, etc.
232 for example, if you provide a string like so:
241 char* uescape( const char* string, int size, int full_escape );
243 /* utility methods */
244 int set_fl( int fd, int flags );
245 int clr_fl( int fd, int flags );
250 double get_timestamp_millis( void );
253 /* returns true if the whole string is a number */
254 int stringisnum(const char* s);
256 /* reads a file and returns the string version of the file
257 user is responsible for freeing the returned char*
259 char* file_to_string(const char* filename);
264 Calculates the md5 of the text provided.
265 The returned string must be freed by the caller.
267 char* md5sum( const char* text, ... );
271 Checks the validity of the file descriptor
272 returns -1 if the file descriptor is invalid
273 returns 0 if the descriptor is OK
275 int osrfUtilsCheckFileDescriptor( int fd );