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