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