bugfix for buffer_fadd
[Evergreen.git] / OpenSRF / src / utils / utils.c
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 #include <stdio.h>
18 #include <unistd.h>
19 #include <stdlib.h>
20 #include <string.h>
21
22 #include <sys/timeb.h>
23 #include "utils.h"
24
25 inline void* safe_malloc( int size ) {
26         void* ptr = (void*) malloc( size );
27         if( ptr == NULL ) {
28                 perror("safe_malloc(): Out of Memory" );
29                 exit(99);
30         }
31         memset( ptr, 0, size );
32         return ptr;
33 }
34
35 /* utility method for profiling */
36 double get_timestamp_millis() {
37         struct timeb t;
38         ftime(&t);
39         double time     = ( 
40                 (int)t.time     + ( ((double)t.millitm) / 1000 ) );
41         return time;
42 }
43
44
45 /* setting/clearing file flags */
46 int set_fl( int fd, int flags ) {
47         
48         int val;
49
50         if( (val = fcntl( fd, F_GETFL, 0) ) < 0 ) {
51                 fprintf(stderr, "fcntl F_GETFL error");
52                 return -1;
53         }
54
55         val |= flags;
56
57         if( fcntl( fd, F_SETFL, val ) < 0 ) {
58                 fprintf(stderr, "fcntl F_SETFL error");
59                 return -1;
60         }
61         return 0;
62 }
63         
64 int clr_fl( int fd, int flags ) {
65         
66         int val;
67
68         if( (val = fcntl( fd, F_GETFL, 0) ) < 0 ) {
69                 fprintf(stderr, "fcntl F_GETFL error" );
70                 return -1;
71         }
72
73         val &= ~flags;
74
75         if( fcntl( fd, F_SETFL, val ) < 0 ) {
76                 fprintf( stderr, "fcntl F_SETFL error" );
77                 return -1;
78         }
79         return 0;
80 }
81
82 // ---------------------------------------------------------------------------------
83 // Flesh out a ubiqitous growing string buffer
84 // ---------------------------------------------------------------------------------
85
86 growing_buffer* buffer_init(int num_initial_bytes) {
87
88         if( num_initial_bytes > BUFFER_MAX_SIZE ) {
89                 return NULL;
90         }
91
92
93         size_t len = sizeof(growing_buffer);
94
95         growing_buffer* gb = (growing_buffer*) safe_malloc(len);
96
97         gb->n_used = 0;/* nothing stored so far */
98         gb->size = num_initial_bytes;
99         gb->buf = (char *) safe_malloc(gb->size + 1);
100
101         return gb;
102 }
103
104 int buffer_fadd(growing_buffer* gb, const char* format, ... ) {
105
106         if(!gb || !format) return 0; 
107
108         va_list args;
109
110         va_start(args, format);
111         int len = vsnprintf(NULL, 0, format, args);
112         len += 1;
113
114         char buf[len];
115         memset(buf, 0, len);
116
117         va_start(args, format);
118         vsnprintf(buf, len - 1, format, args);
119         va_end(args);
120
121         return buffer_add(gb, buf);
122
123 }
124
125 int buffer_add(growing_buffer* gb, char* data) {
126
127
128         if( ! gb || ! data  ) { return 0; }
129         int data_len = strlen( data );
130
131         if( data_len == 0 ) { return 0; }
132         int total_len = data_len + gb->n_used;
133
134         while( total_len >= gb->size ) {
135                 gb->size *= 2;
136         }
137
138         if( gb->size > BUFFER_MAX_SIZE ) {
139                 fprintf(stderr, "Buffer reached MAX_SIZE of %d", BUFFER_MAX_SIZE );
140                 buffer_free( gb );
141                 return 0;
142         }
143
144         char* new_data = (char*) safe_malloc( gb->size );
145
146         strcpy( new_data, gb->buf );
147         free( gb->buf );
148         gb->buf = new_data;
149
150         strcat( gb->buf, data );
151         gb->n_used = total_len;
152         return total_len;
153 }
154
155
156 int buffer_reset( growing_buffer *gb){
157         if( gb == NULL ) { return 0; }
158         if( gb->buf == NULL ) { return 0; }
159         memset( gb->buf, 0, gb->size );
160         gb->n_used = 0;
161         return 1;
162 }
163
164 int buffer_free( growing_buffer* gb ) {
165         if( gb == NULL ) 
166                 return 0;
167         free( gb->buf );
168         free( gb );
169         return 1;
170 }
171
172 char* buffer_data( growing_buffer *gb) {
173         return strdup( gb->buf );
174 }
175
176
177 int buffer_add_char(growing_buffer* gb, char c) {
178         char buf[2];
179         buf[0] = c;
180         buf[1] = '\0';
181         buffer_add(gb, buf);
182         return 1;
183 }
184
185
186
187 char* uescape( const char* string, int size, int full_escape ) {
188
189         growing_buffer* buf = buffer_init(size + 64);
190         int idx = 0;
191         long unsigned int c = 0;
192
193         while (string[idx]) {
194         
195                 c ^= c;
196                 
197                 if ((string[idx] & 0xF0) == 0xF0) {
198                         c = string[idx]<<18;
199
200                         if( size - idx < 4 ) return NULL;
201                         
202                         idx++;
203                         c |= (string[idx] & 0x3F)<<12;
204                         
205                         idx++;
206                         c |= (string[idx] & 0x3F)<<6;
207                         
208                         idx++;
209                         c |= (string[idx] & 0x3F);
210                         
211                         c ^= 0xFF000000;
212                         
213                         buffer_fadd(buf, "\\u%0.4x", c);
214
215                 } else if ((string[idx] & 0xE0) == 0xE0) {
216                         c = string[idx]<<12;
217                         if( size - idx < 3 ) return NULL;
218                         
219                         idx++;
220                         c |= (string[idx] & 0x3F)<<6;
221                         
222                         idx++;
223                         c |= (string[idx] & 0x3F);
224                         
225                         c ^= 0xFFF80000;
226                         
227                         buffer_fadd(buf, "\\u%0.4x", c);
228
229                 } else if ((string[idx] & 0xC0) == 0xC0) {
230                         // Two byte char
231                         c = string[idx]<<6;
232                         if( size - idx < 2 ) return NULL;
233                         
234                         idx++;
235                         c |= (string[idx] & 0x3F);
236                         
237                         c ^= 0xFFFFF000;
238                         
239                         buffer_fadd(buf, "\\u%0.4x", c);
240
241                 } else {
242                         c = string[idx];
243
244                         /* escape the usual suspects */
245                         if(full_escape) {
246                                 switch(c) {
247                                         case '"':
248                                                 buffer_add_char(buf, '\\');
249                                                 buffer_add_char(buf, '"');
250                                                 break;
251         
252                                         case '\b':
253                                                 buffer_add_char(buf, '\\');
254                                                 buffer_add_char(buf, 'b');
255                                                 break;
256         
257                                         case '\f':
258                                                 buffer_add_char(buf, '\\');
259                                                 buffer_add_char(buf, 'f');
260                                                 break;
261         
262                                         case '\t':
263                                                 buffer_add_char(buf, '\\');
264                                                 buffer_add_char(buf, 't');
265                                                 break;
266         
267                                         case '\n':
268                                                 buffer_add_char(buf, '\\');
269                                                 buffer_add_char(buf, 'n');
270                                                 break;
271         
272                                         case '\r':
273                                                 buffer_add_char(buf, '\\');
274                                                 buffer_add_char(buf, 'r');
275                                                 break;
276
277                                         default:
278                                                 buffer_add_char(buf, c);
279                                 }
280
281                         } else {
282                                 buffer_add_char(buf, c);
283                         }
284                 }
285
286                 idx++;
287         }
288
289         char* d = buffer_data(buf);
290         buffer_free(buf);
291         return d;
292 }
293