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;
110 va_start(args, format);
111 int len = vsnprintf(NULL, 0, format, args);
117 va_start(args, format);
118 vsnprintf(buf, len - 1, format, args);
121 return buffer_add(gb, buf);
125 int buffer_add(growing_buffer* gb, char* data) {
128 if( ! gb || ! data ) { return 0; }
129 int data_len = strlen( data );
131 if( data_len == 0 ) { return 0; }
132 int total_len = data_len + gb->n_used;
134 while( total_len >= gb->size ) {
138 if( gb->size > BUFFER_MAX_SIZE ) {
139 fprintf(stderr, "Buffer reached MAX_SIZE of %d", BUFFER_MAX_SIZE );
144 char* new_data = (char*) safe_malloc( gb->size );
146 strcpy( new_data, gb->buf );
150 strcat( gb->buf, data );
151 gb->n_used = total_len;
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 );
164 int buffer_free( growing_buffer* gb ) {
172 char* buffer_data( growing_buffer *gb) {
173 return strdup( gb->buf );
177 int buffer_add_char(growing_buffer* gb, char c) {
187 char* uescape( const char* string, int size, int full_escape ) {
189 growing_buffer* buf = buffer_init(size + 64);
191 long unsigned int c = 0;
193 while (string[idx]) {
197 if ((string[idx] & 0xF0) == 0xF0) {
200 if( size - idx < 4 ) return NULL;
203 c |= (string[idx] & 0x3F)<<12;
206 c |= (string[idx] & 0x3F)<<6;
209 c |= (string[idx] & 0x3F);
213 buffer_fadd(buf, "\\u%0.4x", c);
215 } else if ((string[idx] & 0xE0) == 0xE0) {
217 if( size - idx < 3 ) return NULL;
220 c |= (string[idx] & 0x3F)<<6;
223 c |= (string[idx] & 0x3F);
227 buffer_fadd(buf, "\\u%0.4x", c);
229 } else if ((string[idx] & 0xC0) == 0xC0) {
232 if( size - idx < 2 ) return NULL;
235 c |= (string[idx] & 0x3F);
239 buffer_fadd(buf, "\\u%0.4x", c);
244 /* escape the usual suspects */
248 buffer_add_char(buf, '\\');
249 buffer_add_char(buf, '"');
253 buffer_add_char(buf, '\\');
254 buffer_add_char(buf, 'b');
258 buffer_add_char(buf, '\\');
259 buffer_add_char(buf, 'f');
263 buffer_add_char(buf, '\\');
264 buffer_add_char(buf, 't');
268 buffer_add_char(buf, '\\');
269 buffer_add_char(buf, 'n');
273 buffer_add_char(buf, '\\');
274 buffer_add_char(buf, 'r');
278 buffer_add_char(buf, c);
282 buffer_add_char(buf, c);
289 char* d = buffer_data(buf);