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>
38 #define OSRF_MALLOC(ptr, size) \
40 size_t _size = size; \
41 void* p = malloc( _size ); \
43 perror("OSRF_MALLOC(): Out of Memory" ); \
46 memset( p, 0, _size ); \
51 // The original ... replace with noop once no more errors occur in NDEBUG mode
52 #define osrf_clearbuf( s, n ) memset( s, 0, n )
54 #define osrf_clearbuf( s, n ) \
56 char * clearbuf_temp_s = (s); \
57 size_t clearbuf_temp_n = (n); \
58 memset( clearbuf_temp_s, '!', clearbuf_temp_n ); \
59 clearbuf_temp_s[ clearbuf_temp_n - 1 ] = '\0'; \
63 #define OSRF_BUFFER_ADD(gb, data) \
66 growing_buffer* _gb = gb; \
67 const char* _data = data; \
69 _tl = strlen(_data) + _gb->n_used;\
70 if( _tl < _gb->size ) {\
71 strcpy( _gb->buf + _gb->n_used, _data ); \
73 } else { buffer_add(_gb, _data); }\
77 #define OSRF_BUFFER_ADD_N(gb, data, n) \
79 growing_buffer* gb__ = gb; \
80 const char* data__ = data; \
83 int tl__ = n__ + gb__->n_used;\
84 if( tl__ < gb__->size ) {\
85 memcpy( gb__->buf + gb__->n_used, data__, n__ ); \
86 gb__->buf[tl__] = '\0'; \
87 gb__->n_used = tl__; \
88 } else { buffer_add_n(gb__, data__, n__); }\
92 #define OSRF_BUFFER_ADD_CHAR(gb, c)\
94 growing_buffer* _gb = gb;\
97 if(_gb->n_used < _gb->size - 1) {\
98 _gb->buf[_gb->n_used++] = _c;\
99 _gb->buf[_gb->n_used] = '\0';\
102 buffer_add_char(_gb, _c);\
106 #define OSRF_BUFFER_RESET(gb) \
108 growing_buffer* _gb = gb;\
109 memset(_gb->buf, 0, _gb->size);\
113 #define OSRF_BUFFER_C_STR( x ) ((const char *) (x)->buf)
116 /* turns a va_list into a string */
117 #define VA_LIST_TO_STRING(x) \
118 unsigned long __len = 0;\
121 va_copy(a_copy, args); \
123 __len = vsnprintf(NULL, 0, x, args); \
128 va_start(a_copy, x); \
129 vsnprintf(_b, __len - 1, x, a_copy); \
133 /* turns a long into a string */
134 #define LONG_TO_STRING(l) \
135 unsigned int __len = snprintf(NULL, 0, "%ld", l) + 2;\
138 snprintf(__b, __len - 1, "%ld", l); \
141 #define DOUBLE_TO_STRING(l) \
142 unsigned int __len = snprintf(NULL, 0, "%f", l) + 2; \
145 snprintf(__b, __len - 1, "%f", l); \
146 char* DOUBLESTR = __b;
148 #define LONG_DOUBLE_TO_STRING(l) \
149 unsigned int __len = snprintf(NULL, 0, "%Lf", l) + 2; \
152 snprintf(__b, __len - 1, "%Lf", l); \
153 char* LONGDOUBLESTR = __b;
156 #define INT_TO_STRING(l) \
157 unsigned int __len = snprintf(NULL, 0, "%d", l) + 2; \
160 snprintf(__b, __len - 1, "%d", l); \
166 struct md5_ctx ctx; \
167 unsigned char digest[16];\
170 for ( i=0 ; i != strlen(text) ; i++ ) MD5_feed (&ctx, text[i]);\
171 MD5_stop (&ctx, digest);\
175 memset(final,0,256);\
176 for ( i=0 ; i<16 ; i++ ) {\
177 sprintf(buf, "%02x", digest[i]);\
178 strcat( final, buf );\
180 char* MD5STR = final;
187 #define BUFFER_MAX_SIZE 10485760
189 /* these are evil and should be condemned
190 ! Only use these if you are done with argv[].
191 call init_proc_title() first, then call
193 the title is only allowed to be as big as the
194 initial process name of the process (full size of argv[]).
195 truncation may occurr.
197 int init_proc_title( int argc, char* argv[] );
198 int set_proc_title( const char* format, ... );
201 int daemonize( void );
203 void* safe_malloc(int size);
204 void* safe_calloc(int size);
206 // ---------------------------------------------------------------------------------
207 // Generic growing buffer. Add data all you want
208 // ---------------------------------------------------------------------------------
209 struct growing_buffer_struct {
214 typedef struct growing_buffer_struct growing_buffer;
216 #define buffer_length(x) (x)->n_used
218 growing_buffer* buffer_init( int initial_num_bytes);
220 // XXX This isn't defined in utils.c!! removing for now...
221 //int buffer_addchar(growing_buffer* gb, char c);
223 int buffer_add(growing_buffer* gb, const char* c);
224 int buffer_add_n(growing_buffer* gb, const char* data, size_t n);
225 int buffer_fadd(growing_buffer* gb, const char* format, ... );
226 int buffer_reset( growing_buffer* gb);
227 char* buffer_data( const growing_buffer* gb);
228 char* buffer_release( growing_buffer* gb );
229 int buffer_free( growing_buffer* gb );
230 int buffer_add_char(growing_buffer* gb, char c);
231 char buffer_chomp(growing_buffer* gb); // removes the last character from the buffer
233 /* returns the size needed to fill in the vsnprintf buffer.
234 * ! this calls va_end on the va_list argument*
236 long va_list_size(const char* format, va_list);
238 /* turns a va list into a string, caller must free the
240 char* va_list_to_string(const char* format, ...);
243 /* string escape utility method. escapes unicode embedded characters.
244 escapes the usual \n, \t, etc.
245 for example, if you provide a string like so:
254 char* uescape( const char* string, int size, int full_escape );
256 /* utility methods */
257 int set_fl( int fd, int flags );
258 int clr_fl( int fd, int flags );
263 double get_timestamp_millis( void );
266 /* returns true if the whole string is a number */
267 int stringisnum(const char* s);
271 Calculates the md5 of the text provided.
272 The returned string must be freed by the caller.
274 char* md5sum( const char* text, ... );
278 Checks the validity of the file descriptor
279 returns -1 if the file descriptor is invalid
280 returns 0 if the descriptor is OK
282 int osrfUtilsCheckFileDescriptor( int fd );