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.
20 @brief Prototypes for various low-level utility functions, and related macros.
22 Many of these facilities concern the growing_buffer structure,
23 a sort of poor man's string class that allocates more space for
37 #include <sys/types.h>
40 //#include <sys/timeb.h>
48 @brief Macro version of safe_malloc()
49 @param ptr Pointer to be updated to point to newly allocated memory
50 @param size How many bytes to allocate
52 #define OSRF_MALLOC(ptr, size) \
54 size_t _size = size; \
55 void* p = malloc( _size ); \
57 perror("OSRF_MALLOC(): Out of Memory" ); \
60 memset( p, 0, _size ); \
65 #define osrf_clearbuf( s, n ) memset( s, 0, n )
68 @brief Fills a buffer with binary zeros (normal mode) or exclamation points (debugging mode)
69 @param s Pointer to buffer
70 @param n Length of buffer
72 This macro is used to help ferret out code that inappropriately assumes that a newly
73 allocated buffer is filled with binary zeros. No code should rely on it to do
74 anything in particular. Someday it may turn into a no-op.
76 #define osrf_clearbuf( s, n ) \
78 char * clearbuf_temp_s = (s); \
79 size_t clearbuf_temp_n = (n); \
80 memset( clearbuf_temp_s, '!', clearbuf_temp_n ); \
81 clearbuf_temp_s[ clearbuf_temp_n - 1 ] = '\0'; \
86 @brief Macro version of buffer_add()
87 @param gb Pointer to a growing_buffer
88 @param data Pointer to the string to be appended
91 #define OSRF_BUFFER_ADD(gb, data) \
94 growing_buffer* _gb = gb; \
95 const char* _data = data; \
97 _tl = strlen(_data) + _gb->n_used;\
98 if( _tl < _gb->size ) {\
99 strcpy( _gb->buf + _gb->n_used, _data ); \
101 } else { buffer_add(_gb, _data); }\
106 @brief Macro version of buffer_add_n()
107 @param gb Pointer to a growing_buffer
108 @param data Pointer to the bytes to be appended
109 @param n How many characters to append
111 #define OSRF_BUFFER_ADD_N(gb, data, n) \
113 growing_buffer* gb__ = gb; \
114 const char* data__ = data; \
116 if(gb__ && data__) {\
117 int tl__ = n__ + gb__->n_used;\
118 if( tl__ < gb__->size ) {\
119 memcpy( gb__->buf + gb__->n_used, data__, n__ ); \
120 gb__->buf[tl__] = '\0'; \
121 gb__->n_used = tl__; \
122 } else { buffer_add_n(gb__, data__, n__); }\
127 @brief Macro version of buffer_add_char()
128 @param gb Pointer to a growing buffer
129 @param c Character to be appended
131 #define OSRF_BUFFER_ADD_CHAR(gb, c)\
133 growing_buffer* _gb = gb;\
136 if(_gb->n_used < _gb->size - 1) {\
137 _gb->buf[_gb->n_used++] = _c;\
138 _gb->buf[_gb->n_used] = '\0';\
141 buffer_add_char(_gb, _c);\
146 @brief Macro version of buffer_reset()
147 @param gb Pointer to the growing_buffer to be reset
149 #define OSRF_BUFFER_RESET(gb) \
151 growing_buffer* _gb = gb;\
152 memset(_gb->buf, 0, _gb->size);\
157 @brief Resolves to a const pointer to the string inside a growing_buffer
158 @param x Pointer to a growing_buffier
160 #define OSRF_BUFFER_C_STR( x ) ((const char *) (x)->buf)
164 @brief Turn a printf-style format string and a va_list into a string.
165 @param x A printf-style format string.
167 This macro can work only in a variadic function.
169 The resulting string is constructed in a local buffer, whose address is
170 given by the pointer VA_BUF, This buffer is NOT allocated dynamically,
171 so don't try to free it.
173 #define VA_LIST_TO_STRING(x) \
174 unsigned long __len = 0;\
177 va_copy(a_copy, args); \
179 __len = vsnprintf(NULL, 0, x, args); \
184 va_start(a_copy, x); \
185 vsnprintf(_b, __len - 1, x, a_copy); \
190 @brief Format a long into a string.
193 The long is formatted into a local buffer whose address is given by the pointer
194 LONGSTR. This buffer is NOT allocated dynamically, so don't try to free it.
196 #define LONG_TO_STRING(l) \
197 unsigned int __len = snprintf(NULL, 0, "%ld", l) + 2;\
200 snprintf(__b, __len - 1, "%ld", l); \
204 @brief Format a double into a string.
207 The double is formatted into a local buffer whose address is given by the pointer
208 DOUBLESTR. This buffer is NOT allocated dynamically, so don't try to free it.
210 #define DOUBLE_TO_STRING(l) \
211 unsigned int __len = snprintf(NULL, 0, "%f", l) + 2; \
214 snprintf(__b, __len - 1, "%f", l); \
215 char* DOUBLESTR = __b;
218 @brief Format a long double into a string.
219 @param l A long double
221 The long double is formatted into a local buffer whose address is given by the pointer
222 LONGDOUBLESTR. This buffer is NOT allocated dynamically, so don't try to free it.
224 #define LONG_DOUBLE_TO_STRING(l) \
225 unsigned int __len = snprintf(NULL, 0, "%Lf", l) + 2; \
228 snprintf(__b, __len - 1, "%Lf", l); \
229 char* LONGDOUBLESTR = __b;
233 @brief Format an int into a string.
236 The int is formatted into a local buffer whose address is given by the pointer
237 INTSTR. This buffer is NOT allocated dynamically, so don't try to free it.
239 #define INT_TO_STRING(l) \
240 unsigned int __len = snprintf(NULL, 0, "%d", l) + 2; \
243 snprintf(__b, __len - 1, "%d", l); \
249 struct md5_ctx ctx; \
250 unsigned char digest[16];\
253 for ( i=0 ; i != strlen(text) ; i++ ) MD5_feed (&ctx, text[i]);\
254 MD5_stop (&ctx, digest);\
258 memset(final,0,256);\
259 for ( i=0 ; i<16 ; i++ ) {\
260 sprintf(buf, "%02x", digest[i]);\
261 strcat( final, buf );\
263 char* MD5STR = final;
268 @brief The maximum buffer size for a growing_buffer
270 #define BUFFER_MAX_SIZE 10485760
272 /* these are evil and should be condemned
273 ! Only use these if you are done with argv[].
274 call init_proc_title() first, then call
276 the title is only allowed to be as big as the
277 initial process name of the process (full size of argv[]).
278 truncation may occur.
280 int init_proc_title( int argc, char* argv[] );
281 int set_proc_title( const char* format, ... );
284 int daemonize( void );
286 void* safe_malloc(int size);
287 void* safe_calloc(int size);
289 // ---------------------------------------------------------------------------------
290 // Generic growing buffer. Add data all you want
291 // ---------------------------------------------------------------------------------
293 @brief A poor man's string class in C.
295 A growing_buffer stores a character string. Related functions append data
296 and otherwise manage the string, allocating more memory automatically as needed
297 when the string gets too big for its buffer.
299 A growing_buffer is designed for text, not binary data. In particular: if you
300 try to store embedded nuls in one, something bad will almost certainly happen.
302 struct growing_buffer_struct {
303 /** @brief Pointer to the internal buffer */
305 /** @brief Length of the stored string */
307 /** @brief Size of the internal buffer */
310 typedef struct growing_buffer_struct growing_buffer;
313 @brief The length of the string stored by a growing_buffer.
314 @param x A pointer to the growing buffer.
316 #define buffer_length(x) (x)->n_used
318 growing_buffer* buffer_init( int initial_num_bytes);
320 int buffer_add(growing_buffer* gb, const char* c);
321 int buffer_add_n(growing_buffer* gb, const char* data, size_t n);
322 int buffer_fadd(growing_buffer* gb, const char* format, ... );
323 int buffer_reset( growing_buffer* gb);
324 char* buffer_data( const growing_buffer* gb);
325 char* buffer_release( growing_buffer* gb );
326 int buffer_free( growing_buffer* gb );
327 int buffer_add_char(growing_buffer* gb, char c);
328 char buffer_chomp(growing_buffer* gb); // removes the last character from the buffer
331 returns the size needed to fill in the vsnprintf buffer.
332 this calls va_end on the va_list argument*
334 long va_list_size(const char* format, va_list);
336 /* turns a va list into a string, caller must free the
338 char* va_list_to_string(const char* format, ...);
341 /* string escape utility method. escapes unicode embedded characters.
342 escapes the usual \n, \t, etc.
343 for example, if you provide a string like so:
351 char* uescape( const char* string, int size, int full_escape );
353 /* utility methods */
354 int set_fl( int fd, int flags );
355 int clr_fl( int fd, int flags );
359 double get_timestamp_millis( void );
362 /* returns true if the whole string is a number */
363 int stringisnum(const char* s);
367 Calculates the md5 of the text provided.
368 The returned string must be freed by the caller.
370 char* md5sum( const char* text, ... );
374 Checks the validity of the file descriptor
375 returns -1 if the file descriptor is invalid
376 returns 0 if the descriptor is OK
378 int osrfUtilsCheckFileDescriptor( int fd );