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.
19 #include <sys/types.h>
24 //#include <sys/timeb.h>
27 inline void* safe_malloc( int size ) {
28 void* ptr = (void*) malloc( size );
30 perror("safe_malloc(): Out of Memory" );
33 memset( ptr, 0, size );
37 /* utility method for profiling */
38 double get_timestamp_millis() {
41 gettimeofday(&tv, NULL);
43 // double time = (int)t.time + ( ((double)t.millitm) / 1000 ) + ( ((double)tv.tv_usec / 1000000) );
44 double time = (int)tv.tv_sec + ( ((double)tv.tv_usec / 1000000) );
49 /* setting/clearing file flags */
50 int set_fl( int fd, int flags ) {
54 if( (val = fcntl( fd, F_GETFL, 0) ) < 0 ) {
55 fprintf(stderr, "fcntl F_GETFL error");
61 if( fcntl( fd, F_SETFL, val ) < 0 ) {
62 fprintf(stderr, "fcntl F_SETFL error");
68 int clr_fl( int fd, int flags ) {
72 if( (val = fcntl( fd, F_GETFL, 0) ) < 0 ) {
73 fprintf(stderr, "fcntl F_GETFL error" );
79 if( fcntl( fd, F_SETFL, val ) < 0 ) {
80 fprintf( stderr, "fcntl F_SETFL error" );
86 long va_list_size(const char* format, va_list args) {
88 len = vsnprintf(NULL, 0, format, args);
95 // ---------------------------------------------------------------------------------
96 // Flesh out a ubiqitous growing string buffer
97 // ---------------------------------------------------------------------------------
99 growing_buffer* buffer_init(int num_initial_bytes) {
101 if( num_initial_bytes > BUFFER_MAX_SIZE ) return NULL;
103 size_t len = sizeof(growing_buffer);
105 growing_buffer* gb = (growing_buffer*) safe_malloc(len);
107 gb->n_used = 0;/* nothing stored so far */
108 gb->size = num_initial_bytes;
109 gb->buf = (char *) safe_malloc(gb->size + 1);
115 int buffer_fadd(growing_buffer* gb, const char* format, ... ) {
117 if(!gb || !format) return 0;
123 va_copy(a_copy, args);
125 va_start(args, format);
126 len = va_list_size(format, args);
131 va_start(a_copy, format);
132 vsnprintf(buf, len - 1, format, a_copy);
135 return buffer_add(gb, buf);
139 int buffer_add(growing_buffer* gb, char* data) {
142 if( ! gb || ! data ) { return 0; }
143 int data_len = strlen( data );
145 if( data_len == 0 ) { return 0; }
146 int total_len = data_len + gb->n_used;
148 while( total_len >= gb->size ) {
152 if( gb->size > BUFFER_MAX_SIZE ) {
153 fprintf(stderr, "Buffer reached MAX_SIZE of %d", BUFFER_MAX_SIZE );
158 char* new_data = (char*) safe_malloc( gb->size );
160 strcpy( new_data, gb->buf );
164 strcat( gb->buf, data );
165 gb->n_used = total_len;
170 int buffer_reset( growing_buffer *gb){
171 if( gb == NULL ) { return 0; }
172 if( gb->buf == NULL ) { return 0; }
173 memset( gb->buf, 0, gb->size );
178 int buffer_free( growing_buffer* gb ) {
186 char* buffer_data( growing_buffer *gb) {
187 return strdup( gb->buf );
191 int buffer_add_char(growing_buffer* gb, char c) {
201 char* uescape( const char* string, int size, int full_escape ) {
203 growing_buffer* buf = buffer_init(size + 64);
205 long unsigned int c = 0;
207 while (string[idx]) {
211 if ((string[idx] & 0xF0) == 0xF0) {
214 if( size - idx < 4 ) return NULL;
217 c |= (string[idx] & 0x3F)<<12;
220 c |= (string[idx] & 0x3F)<<6;
223 c |= (string[idx] & 0x3F);
227 buffer_fadd(buf, "\\u%0.4x", c);
229 } else if ((string[idx] & 0xE0) == 0xE0) {
231 if( size - idx < 3 ) return NULL;
234 c |= (string[idx] & 0x3F)<<6;
237 c |= (string[idx] & 0x3F);
241 buffer_fadd(buf, "\\u%0.4x", c);
243 } else if ((string[idx] & 0xC0) == 0xC0) {
246 if( size - idx < 2 ) return NULL;
249 c |= (string[idx] & 0x3F);
253 buffer_fadd(buf, "\\u%0.4x", c);
258 /* escape the usual suspects */
262 buffer_add_char(buf, '\\');
263 buffer_add_char(buf, '"');
267 buffer_add_char(buf, '\\');
268 buffer_add_char(buf, 'b');
272 buffer_add_char(buf, '\\');
273 buffer_add_char(buf, 'f');
277 buffer_add_char(buf, '\\');
278 buffer_add_char(buf, 't');
282 buffer_add_char(buf, '\\');
283 buffer_add_char(buf, 'n');
287 buffer_add_char(buf, '\\');
288 buffer_add_char(buf, 'r');
292 buffer_add_char(buf, c);
296 buffer_add_char(buf, c);
303 char* d = buffer_data(buf);
309 // A function to turn a process into a daemon and set it's process name in ps/top
314 perror("Failed to fork!");
317 } else if (f == 0) { // We're in the child now...
321 } else { // We're in the parent...
326 int stringisnum(char* s) {
327 char* w = (char*) malloc(strlen(s) * sizeof(char*));
337 char* file_to_string(char* filename) {
339 if(!filename) return NULL;
344 growing_buffer* gb = buffer_init(len);
346 FILE* file = fopen(filename, "r");
348 perror("Unable to open file in json_parse_file()");
352 while(fgets(buf, len - 1, file)) {
357 char* data = buffer_data(gb);