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