]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/utils/utils.c
added some license info and comments
[OpenSRF.git] / 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         int len = strlen(format) + 1024;
111         char buf[len];
112         memset(buf, 0, len);
113
114         va_start(args, format);
115         vsnprintf(buf, len - 1, format, args);
116         va_end(args);
117
118         return buffer_add(gb, buf);
119
120 }
121
122
123 int buffer_add(growing_buffer* gb, char* data) {
124
125
126         if( ! gb || ! data  ) { return 0; }
127         int data_len = strlen( data );
128
129         if( data_len == 0 ) { return 0; }
130         int total_len = data_len + gb->n_used;
131
132         while( total_len >= gb->size ) {
133                 gb->size *= 2;
134         }
135
136         if( gb->size > BUFFER_MAX_SIZE ) {
137                 fprintf(stderr, "Buffer reached MAX_SIZE of %d", BUFFER_MAX_SIZE );
138                 buffer_free( gb );
139                 return 0;
140         }
141
142         char* new_data = (char*) safe_malloc( gb->size );
143
144         strcpy( new_data, gb->buf );
145         free( gb->buf );
146         gb->buf = new_data;
147
148         strcat( gb->buf, data );
149         gb->n_used = total_len;
150         return total_len;
151 }
152
153
154 int buffer_reset( growing_buffer *gb){
155         if( gb == NULL ) { return 0; }
156         if( gb->buf == NULL ) { return 0; }
157         memset( gb->buf, 0, gb->size );
158         gb->n_used = 0;
159         return 1;
160 }
161
162 int buffer_free( growing_buffer* gb ) {
163         if( gb == NULL ) 
164                 return 0;
165         free( gb->buf );
166         free( gb );
167         return 1;
168 }
169
170 char* buffer_data( growing_buffer *gb) {
171         return strdup( gb->buf );
172 }
173
174
175 int buffer_add_char(growing_buffer* gb, char c) {
176         char buf[2];
177         buf[0] = c;
178         buf[1] = '\0';
179         buffer_add(gb, buf);
180         return 1;
181 }
182
183
184
185 char* uescape( const char* string, int size, int full_escape ) {
186
187         growing_buffer* buf = buffer_init(size + 64);
188         int idx = 0;
189         long unsigned int c = 0;
190
191         while (string[idx]) {
192         
193                 c ^= c;
194                 
195                 if ((string[idx] & 0xF0) == 0xF0) {
196                         c = string[idx]<<18;
197
198                         if( size - idx < 4 ) return NULL;
199                         
200                         idx++;
201                         c |= (string[idx] & 0x3F)<<12;
202                         
203                         idx++;
204                         c |= (string[idx] & 0x3F)<<6;
205                         
206                         idx++;
207                         c |= (string[idx] & 0x3F);
208                         
209                         c ^= 0xFF000000;
210                         
211                         buffer_fadd(buf, "\\u%0.4x", c);
212
213                 } else if ((string[idx] & 0xE0) == 0xE0) {
214                         c = string[idx]<<12;
215                         if( size - idx < 3 ) return NULL;
216                         
217                         idx++;
218                         c |= (string[idx] & 0x3F)<<6;
219                         
220                         idx++;
221                         c |= (string[idx] & 0x3F);
222                         
223                         c ^= 0xFFF80000;
224                         
225                         buffer_fadd(buf, "\\u%0.4x", c);
226
227                 } else if ((string[idx] & 0xC0) == 0xC0) {
228                         // Two byte char
229                         c = string[idx]<<6;
230                         if( size - idx < 2 ) return NULL;
231                         
232                         idx++;
233                         c |= (string[idx] & 0x3F);
234                         
235                         c ^= 0xFFFFF000;
236                         
237                         buffer_fadd(buf, "\\u%0.4x", c);
238
239                 } else {
240                         c = string[idx];
241
242                         /* escape the usual suspects */
243                         if(full_escape) {
244                                 switch(c) {
245                                         case '"':
246                                                 buffer_add_char(buf, '\\');
247                                                 buffer_add_char(buf, '"');
248                                                 break;
249         
250                                         case '\b':
251                                                 buffer_add_char(buf, '\\');
252                                                 buffer_add_char(buf, 'b');
253                                                 break;
254         
255                                         case '\f':
256                                                 buffer_add_char(buf, '\\');
257                                                 buffer_add_char(buf, 'f');
258                                                 break;
259         
260                                         case '\t':
261                                                 buffer_add_char(buf, '\\');
262                                                 buffer_add_char(buf, 't');
263                                                 break;
264         
265                                         case '\n':
266                                                 buffer_add_char(buf, '\\');
267                                                 buffer_add_char(buf, 'n');
268                                                 break;
269         
270                                         case '\r':
271                                                 buffer_add_char(buf, '\\');
272                                                 buffer_add_char(buf, 'r');
273                                                 break;
274
275                                         default:
276                                                 buffer_add_char(buf, c);
277                                 }
278
279                         } else {
280                                 buffer_add_char(buf, c);
281                         }
282                 }
283
284                 idx++;
285         }
286
287         char* d = buffer_data(buf);
288         buffer_free(buf);
289         return d;
290 }
291