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