2 Copyright (C) 2005 Georgia Public Library Service
3 Bill Erickson <highfalutin@gmail.com>
4 Mike Rylander <mrylander@gmail.com>
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.
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.
27 #include <sys/types.h>
30 //#include <sys/timeb.h>
34 /* turns a va_list into a string */
35 #define VA_LIST_TO_STRING(x) \
36 unsigned long len = 0;\
39 va_copy(a_copy, args); \
41 len = vsnprintf(NULL, 0, x, args); \
46 va_start(a_copy, x); \
47 vsnprintf(_b, len - 1, x, a_copy); \
51 /* turns a long into a string */
52 #define LONG_TO_STRING(l) \
53 unsigned int __len = snprintf(NULL, 0, "%ld", l) + 2;\
56 snprintf(__b, __len - 1, "%ld", l); \
59 #define DOUBLE_TO_STRING(l) \
60 unsigned int __len = snprintf(NULL, 0, "%lf", l) + 2; \
63 snprintf(__b, __len - 1, "%lf", l); \
64 char* DOUBLESTR = __b;
66 #define INT_TO_STRING(l) \
67 unsigned int __len = snprintf(NULL, 0, "%d", l) + 2; \
70 snprintf(__b, __len - 1, "%d", l); \
77 unsigned char digest[16];\
80 for ( i=0 ; i != strlen(text) ; i++ ) MD5_feed (&ctx, text[i]);\
81 MD5_stop (&ctx, digest);\
86 for ( i=0 ; i<16 ; i++ ) {\
87 sprintf(buf, "%02x", digest[i]);\
88 strcat( final, buf );\
97 #define BUFFER_MAX_SIZE 10485760
99 /* these are evil and should be condemned
100 ! Only use these if you are done with argv[].
101 call init_proc_title() first, then call
103 the title is only allowed to be as big as the
104 initial process name of the process (full size of argv[]).
105 truncation may occurr.
107 int init_proc_title( int argc, char* argv[] );
108 int set_proc_title( char* format, ... );
113 void* safe_malloc(int size);
115 // ---------------------------------------------------------------------------------
116 // Generic growing buffer. Add data all you want
117 // ---------------------------------------------------------------------------------
118 struct growing_buffer_struct {
123 typedef struct growing_buffer_struct growing_buffer;
125 growing_buffer* buffer_init( int initial_num_bytes);
127 // XXX This isn't defined in utils.c!! removing for now...
128 //int buffer_addchar(growing_buffer* gb, char c);
130 int buffer_add(growing_buffer* gb, char* c);
131 int buffer_fadd(growing_buffer* gb, const char* format, ... );
132 int buffer_reset( growing_buffer* gb);
133 char* buffer_data( growing_buffer* gb);
134 int buffer_free( growing_buffer* gb );
135 int buffer_add_char(growing_buffer* gb, char c);
137 /* returns the size needed to fill in the vsnprintf buffer.
138 * ! this calls va_end on the va_list argument*
140 long va_list_size(const char* format, va_list);
142 /* turns a va list into a string, caller must free the
144 char* va_list_to_string(const char* format, ...);
147 /* string escape utility method. escapes unicode embeded characters.
148 escapes the usual \n, \t, etc.
149 for example, if you provide a string like so:
158 char* uescape( const char* string, int size, int full_escape );
160 /* utility methods */
161 int set_fl( int fd, int flags );
162 int clr_fl( int fd, int flags );
167 double get_timestamp_millis();
170 /* returns true if the whole string is a number */
171 int stringisnum(char* s);
173 /* reads a file and returns the string version of the file
174 user is responsible for freeing the returned char*
176 char* file_to_string(const char* filename);
181 Calculates the md5 of the text provided.
182 The returned string must be freed by the caller.
184 char* md5sum( char* text, ... );