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