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.
22 #include <sys/timeb.h>
25 inline void* safe_malloc( int size ) {
26 void* ptr = (void*) malloc( size );
28 perror("safe_malloc(): Out of Memory" );
31 memset( ptr, 0, size );
35 /* utility method for profiling */
36 double get_timestamp_millis() {
40 (int)t.time + ( ((double)t.millitm) / 1000 ) );
45 /* setting/clearing file flags */
46 int set_fl( int fd, int flags ) {
50 if( (val = fcntl( fd, F_GETFL, 0) ) < 0 ) {
51 fprintf(stderr, "fcntl F_GETFL error");
57 if( fcntl( fd, F_SETFL, val ) < 0 ) {
58 fprintf(stderr, "fcntl F_SETFL error");
64 int clr_fl( int fd, int flags ) {
68 if( (val = fcntl( fd, F_GETFL, 0) ) < 0 ) {
69 fprintf(stderr, "fcntl F_GETFL error" );
75 if( fcntl( fd, F_SETFL, val ) < 0 ) {
76 fprintf( stderr, "fcntl F_SETFL error" );
82 // ---------------------------------------------------------------------------------
83 // Flesh out a ubiqitous growing string buffer
84 // ---------------------------------------------------------------------------------
86 growing_buffer* buffer_init(int num_initial_bytes) {
88 if( num_initial_bytes > BUFFER_MAX_SIZE ) {
93 size_t len = sizeof(growing_buffer);
95 growing_buffer* gb = (growing_buffer*) safe_malloc(len);
97 gb->n_used = 0;/* nothing stored so far */
98 gb->size = num_initial_bytes;
99 gb->buf = (char *) safe_malloc(gb->size + 1);
104 int buffer_fadd(growing_buffer* gb, const char* format, ... ) {
106 if(!gb || !format) return 0;
112 char* f_copy = strdup(format);
114 va_copy(a_copy,args);
116 va_start(a_copy, f_copy);
117 len = vsnprintf(NULL, 0, f_copy, a_copy);
125 va_start(args, format);
126 vsnprintf(buf, len - 1, format, args);
131 return buffer_add(gb, buf);
135 int buffer_add(growing_buffer* gb, char* data) {
138 if( ! gb || ! data ) { return 0; }
139 int data_len = strlen( data );
141 if( data_len == 0 ) { return 0; }
142 int total_len = data_len + gb->n_used;
144 while( total_len >= gb->size ) {
148 if( gb->size > BUFFER_MAX_SIZE ) {
149 fprintf(stderr, "Buffer reached MAX_SIZE of %d", BUFFER_MAX_SIZE );
154 char* new_data = (char*) safe_malloc( gb->size );
156 strcpy( new_data, gb->buf );
160 strcat( gb->buf, data );
161 gb->n_used = total_len;
166 int buffer_reset( growing_buffer *gb){
167 if( gb == NULL ) { return 0; }
168 if( gb->buf == NULL ) { return 0; }
169 memset( gb->buf, 0, gb->size );
174 int buffer_free( growing_buffer* gb ) {
182 char* buffer_data( growing_buffer *gb) {
183 return strdup( gb->buf );
187 int buffer_add_char(growing_buffer* gb, char c) {
197 char* uescape( const char* string, int size, int full_escape ) {
199 growing_buffer* buf = buffer_init(size + 64);
201 long unsigned int c = 0;
203 while (string[idx]) {
207 if ((string[idx] & 0xF0) == 0xF0) {
210 if( size - idx < 4 ) return NULL;
213 c |= (string[idx] & 0x3F)<<12;
216 c |= (string[idx] & 0x3F)<<6;
219 c |= (string[idx] & 0x3F);
223 buffer_fadd(buf, "\\u%0.4x", c);
225 } else if ((string[idx] & 0xE0) == 0xE0) {
227 if( size - idx < 3 ) return NULL;
230 c |= (string[idx] & 0x3F)<<6;
233 c |= (string[idx] & 0x3F);
237 buffer_fadd(buf, "\\u%0.4x", c);
239 } else if ((string[idx] & 0xC0) == 0xC0) {
242 if( size - idx < 2 ) return NULL;
245 c |= (string[idx] & 0x3F);
249 buffer_fadd(buf, "\\u%0.4x", c);
254 /* escape the usual suspects */
258 buffer_add_char(buf, '\\');
259 buffer_add_char(buf, '"');
263 buffer_add_char(buf, '\\');
264 buffer_add_char(buf, 'b');
268 buffer_add_char(buf, '\\');
269 buffer_add_char(buf, 'f');
273 buffer_add_char(buf, '\\');
274 buffer_add_char(buf, 't');
278 buffer_add_char(buf, '\\');
279 buffer_add_char(buf, 'n');
283 buffer_add_char(buf, '\\');
284 buffer_add_char(buf, 'r');
288 buffer_add_char(buf, c);
292 buffer_add_char(buf, c);
299 char* d = buffer_data(buf);