]> git.evergreen-ils.org Git - OpenSRF.git/blob - include/opensrf/utils.h
1. Replaced the innards of an osrfStringArray to include an
[OpenSRF.git] / include / opensrf / 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 #ifdef __cplusplus
33 extern "C" {
34 #endif
35
36 #include "md5.h"
37
38 #define OSRF_MALLOC(ptr, size) \
39         do {\
40                         size_t _size = size; \
41                         void* p = malloc( _size ); \
42                         if( p == NULL ) { \
43                                 perror("OSRF_MALLOC(): Out of Memory" ); \
44                                 exit(99); \
45                         } \
46                         memset( p, 0, _size ); \
47                         (ptr) = p; \
48                 } while(0)
49
50 #ifdef NDEBUG
51 // The original ... replace with noop once no more errors occur in NDEBUG mode
52 #define osrf_clearbuf( s, n ) memset( s, 0, n )
53 #else
54 #define osrf_clearbuf( s, n ) \
55         do { \
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'; \
60         } while( 0 )
61 #endif
62
63 #define OSRF_BUFFER_ADD(gb, data) \
64         do {\
65                 int _tl; \
66                 growing_buffer* _gb = gb; \
67                 const char* _data = data; \
68                 if(_gb && _data) {\
69                         _tl = strlen(_data) + _gb->n_used;\
70                         if( _tl < _gb->size ) {\
71                                 strcpy( _gb->buf + _gb->n_used, _data ); \
72                                 _gb->n_used = _tl; \
73                         } else { buffer_add(_gb, _data); }\
74                 }\
75         } while(0)
76
77 #define OSRF_BUFFER_ADD_N(gb, data, n) \
78         do {\
79                 growing_buffer* gb__ = gb; \
80                 const char* data__ = data; \
81                 size_t n__ = n; \
82                 if(gb__ && 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__); }\
89 }\
90 } while(0)
91
92 #define OSRF_BUFFER_ADD_CHAR(gb, c)\
93         do {\
94                 growing_buffer* _gb = gb;\
95                 char _c = c;\
96                 if(_gb) {\
97                         if(_gb->n_used < _gb->size - 1) {\
98                                 _gb->buf[_gb->n_used++] = _c;\
99                                 _gb->buf[_gb->n_used]   = '\0';\
100                         }\
101                         else\
102                                 buffer_add_char(_gb, _c);\
103                 }\
104         }while(0)
105
106 #define OSRF_BUFFER_RESET(gb) \
107         do {\
108                 growing_buffer* _gb = gb;\
109         memset(_gb->buf, 0, _gb->size);\
110         _gb->n_used = 0;\
111         }while(0)
112
113 #define OSRF_BUFFER_C_STR( x ) ((const char *) (x)->buf)
114
115
116 /* turns a va_list into a string */
117 #define VA_LIST_TO_STRING(x) \
118         unsigned long __len = 0;\
119         va_list args; \
120         va_list a_copy;\
121         va_copy(a_copy, args); \
122         va_start(args, x); \
123         __len = vsnprintf(NULL, 0, x, args); \
124         va_end(args); \
125         __len += 2; \
126         char _b[__len]; \
127         bzero(_b, __len); \
128         va_start(a_copy, x); \
129         vsnprintf(_b, __len - 1, x, a_copy); \
130         va_end(a_copy); \
131         char* VA_BUF = _b; \
132
133 /* turns a long into a string */
134 #define LONG_TO_STRING(l) \
135         unsigned int __len = snprintf(NULL, 0, "%ld", l) + 2;\
136         char __b[__len]; \
137         bzero(__b, __len); \
138         snprintf(__b, __len - 1, "%ld", l); \
139         char* LONGSTR = __b;
140
141 #define DOUBLE_TO_STRING(l) \
142         unsigned int __len = snprintf(NULL, 0, "%f", l) + 2; \
143         char __b[__len]; \
144         bzero(__b, __len); \
145         snprintf(__b, __len - 1, "%f", l); \
146         char* DOUBLESTR = __b;
147
148 #define LONG_DOUBLE_TO_STRING(l) \
149         unsigned int __len = snprintf(NULL, 0, "%Lf", l) + 2; \
150         char __b[__len]; \
151         bzero(__b, __len); \
152         snprintf(__b, __len - 1, "%Lf", l); \
153         char* LONGDOUBLESTR = __b;
154
155
156 #define INT_TO_STRING(l) \
157         unsigned int __len = snprintf(NULL, 0, "%d", l) + 2; \
158         char __b[__len]; \
159         bzero(__b, __len); \
160         snprintf(__b, __len - 1, "%d", l); \
161         char* INTSTR = __b;
162
163
164 /*
165 #define MD5SUM(s) \
166         struct md5_ctx ctx; \
167         unsigned char digest[16];\
168         MD5_start (&ctx);\
169         int i;\
170         for ( i=0 ; i != strlen(text) ; i++ ) MD5_feed (&ctx, text[i]);\
171         MD5_stop (&ctx, digest);\
172         char buf[16];\
173         memset(buf,0,16);\
174         char final[256];\
175         memset(final,0,256);\
176         for ( i=0 ; i<16 ; i++ ) {\
177                 sprintf(buf, "%02x", digest[i]);\
178                 strcat( final, buf );\
179         }\
180         char* MD5STR = final;
181         */
182
183
184         
185
186
187 #define BUFFER_MAX_SIZE 10485760 
188
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
192         set_proc_title. 
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.
196  */
197 int init_proc_title( int argc, char* argv[] );
198 int set_proc_title( const char* format, ... );
199
200
201 int daemonize( void );
202
203 void* safe_malloc(int size);
204 void* safe_calloc(int size);
205
206 // ---------------------------------------------------------------------------------
207 // Generic growing buffer. Add data all you want
208 // ---------------------------------------------------------------------------------
209 struct growing_buffer_struct {
210         char *buf;
211         int n_used;
212         int size;
213 };
214 typedef struct growing_buffer_struct growing_buffer;
215
216 #define buffer_length(x) (x)->n_used
217
218 growing_buffer* buffer_init( int initial_num_bytes);
219
220 // XXX This isn't defined in utils.c!! removing for now...
221 //int buffer_addchar(growing_buffer* gb, char c);
222
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
232
233 /* returns the size needed to fill in the vsnprintf buffer.  
234         * ! this calls va_end on the va_list argument*
235         */
236 long va_list_size(const char* format, va_list);
237
238 /* turns a va list into a string, caller must free the 
239         allocated char */
240 char* va_list_to_string(const char* format, ...);
241
242
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:
246
247         hello,
248                 you
249
250         you would get back:
251         hello,\n\tyou
252  
253  */
254 char* uescape( const char* string, int size, int full_escape );
255
256 /* utility methods */
257 int set_fl( int fd, int flags );
258 int clr_fl( int fd, int flags );
259
260
261
262 // Utility method
263 double get_timestamp_millis( void );
264
265
266 /* returns true if the whole string is a number */
267 int stringisnum(const char* s);
268
269
270 /** 
271   Calculates the md5 of the text provided.
272   The returned string must be freed by the caller.
273   */
274 char* md5sum( const char* text, ... );
275
276
277 /**
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
281   */
282 int osrfUtilsCheckFileDescriptor( int fd );
283
284 #ifdef __cplusplus
285 }
286 #endif
287
288 #endif