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