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>
33 /* turns a va_list into a string */
34 #define VA_LIST_TO_STRING(x) \
35 unsigned long len = 0;\
38 va_copy(a_copy, args); \
40 len = vsnprintf(NULL, 0, x, args); \
45 va_start(a_copy, x); \
46 vsnprintf(_b, len - 1, x, a_copy); \
50 /* turns a long into a string */
51 #define LONG_TO_STRING(l) \
52 unsigned int __len = snprintf(NULL, 0, "%ld", l) + 2;\
55 snprintf(__b, __len - 1, "%ld", l); \
58 #define DOUBLE_TO_STRING(l) \
59 unsigned int __len = snprintf(NULL, 0, "%lf", l) + 2; \
62 snprintf(__b, __len - 1, "%lf", l); \
63 char* DOUBLESTR = __b;
65 #define INT_TO_STRING(l) \
66 unsigned int __len = snprintf(NULL, 0, "%d", l) + 2; \
69 snprintf(__b, __len - 1, "%d", l); \
76 #define BUFFER_MAX_SIZE 10485760
78 /* these are evil and should be condemned
79 ! Only use these if you are done with argv[].
80 call init_proc_title() first, then call
82 the title is only allowed to be as big as the
83 initial process name of the process (full size of argv[]).
84 truncation may occurr.
86 int init_proc_title( int argc, char* argv[] );
87 int set_proc_title( char* format, ... );
92 void* safe_malloc(int size);
94 // ---------------------------------------------------------------------------------
95 // Generic growing buffer. Add data all you want
96 // ---------------------------------------------------------------------------------
97 struct growing_buffer_struct {
102 typedef struct growing_buffer_struct growing_buffer;
104 growing_buffer* buffer_init( int initial_num_bytes);
106 // XXX This isn't defined in utils.c!! removing for now...
107 //int buffer_addchar(growing_buffer* gb, char c);
109 int buffer_add(growing_buffer* gb, char* c);
110 int buffer_fadd(growing_buffer* gb, const char* format, ... );
111 int buffer_reset( growing_buffer* gb);
112 char* buffer_data( growing_buffer* gb);
113 int buffer_free( growing_buffer* gb );
114 int buffer_add_char(growing_buffer* gb, char c);
116 /* returns the size needed to fill in the vsnprintf buffer.
117 * ! this calls va_end on the va_list argument*
119 long va_list_size(const char* format, va_list);
121 /* turns a va list into a string, caller must free the
123 char* va_list_to_string(const char* format, ...);
126 /* string escape utility method. escapes unicode embeded characters.
127 escapes the usual \n, \t, etc.
128 for example, if you provide a string like so:
137 char* uescape( const char* string, int size, int full_escape );
139 /* utility methods */
140 int set_fl( int fd, int flags );
141 int clr_fl( int fd, int flags );
146 double get_timestamp_millis();
149 /* returns true if the whole string is a number */
150 int stringisnum(char* s);
152 /* reads a file and returns the string version of the file
153 user is responsible for freeing the returned char*
155 char* file_to_string(const char* filename);