9 inline void* safe_malloc( int size ) {
10 void* ptr = (void*) malloc( size );
12 perror("safe_malloc(): Out of Memory" );
15 memset( ptr, 0, size );
19 /* utility method for profiling */
20 double get_timestamp_millis() {
24 (int)t.time + ( ((double)t.millitm) / 1000 ) );
29 /* setting/clearing file flags */
30 int set_fl( int fd, int flags ) {
34 if( (val = fcntl( fd, F_GETFL, 0) ) < 0 ) {
35 fprintf(stderr, "fcntl F_GETFL error");
41 if( fcntl( fd, F_SETFL, val ) < 0 ) {
42 fprintf(stderr, "fcntl F_SETFL error");
48 int clr_fl( int fd, int flags ) {
52 if( (val = fcntl( fd, F_GETFL, 0) ) < 0 ) {
53 fprintf(stderr, "fcntl F_GETFL error" );
59 if( fcntl( fd, F_SETFL, val ) < 0 ) {
60 fprintf( stderr, "fcntl F_SETFL error" );
66 // ---------------------------------------------------------------------------------
67 // Flesh out a ubiqitous growing string buffer
68 // ---------------------------------------------------------------------------------
70 growing_buffer* buffer_init(int num_initial_bytes) {
72 if( num_initial_bytes > BUFFER_MAX_SIZE ) {
77 size_t len = sizeof(growing_buffer);
79 growing_buffer* gb = (growing_buffer*) safe_malloc(len);
81 gb->n_used = 0;/* nothing stored so far */
82 gb->size = num_initial_bytes;
83 gb->buf = (char *) safe_malloc(gb->size + 1);
88 int buffer_fadd(growing_buffer* gb, const char* format, ... ) {
90 if(!gb || !format) return 0;
94 int len = strlen(format) + 1024;
98 va_start(args, format);
99 vsnprintf(buf, len - 1, format, args);
102 return buffer_add(gb, buf);
107 int buffer_add(growing_buffer* gb, char* data) {
110 if( ! gb || ! data ) { return 0; }
111 int data_len = strlen( data );
113 if( data_len == 0 ) { return 0; }
114 int total_len = data_len + gb->n_used;
116 while( total_len >= gb->size ) {
120 if( gb->size > BUFFER_MAX_SIZE ) {
121 fprintf(stderr, "Buffer reached MAX_SIZE of %d", BUFFER_MAX_SIZE );
126 char* new_data = (char*) safe_malloc( gb->size );
128 strcpy( new_data, gb->buf );
132 strcat( gb->buf, data );
133 gb->n_used = total_len;
138 int buffer_reset( growing_buffer *gb){
139 if( gb == NULL ) { return 0; }
140 if( gb->buf == NULL ) { return 0; }
141 memset( gb->buf, 0, gb->size );
146 int buffer_free( growing_buffer* gb ) {
154 char* buffer_data( growing_buffer *gb) {
155 return strdup( gb->buf );
159 int buffer_add_char(growing_buffer* gb, char c) {
169 char* uescape( const char* string, int size, int full_escape ) {
171 growing_buffer* buf = buffer_init(size + 64);
173 long unsigned int c = 0;
175 while (string[idx]) {
179 if ((string[idx] & 0xF0) == 0xF0) {
182 if( size - idx < 4 ) return NULL;
185 c |= (string[idx] & 0x3F)<<12;
188 c |= (string[idx] & 0x3F)<<6;
191 c |= (string[idx] & 0x3F);
195 buffer_fadd(buf, "\\u%0.4x", c);
197 } else if ((string[idx] & 0xE0) == 0xE0) {
199 if( size - idx < 3 ) return NULL;
202 c |= (string[idx] & 0x3F)<<6;
205 c |= (string[idx] & 0x3F);
209 buffer_fadd(buf, "\\u%0.4x", c);
211 } else if ((string[idx] & 0xC0) == 0xC0) {
214 if( size - idx < 2 ) return NULL;
217 c |= (string[idx] & 0x3F);
221 buffer_fadd(buf, "\\u%0.4x", c);
226 /* escape the usual suspects */
230 buffer_add_char(buf, '\\');
231 buffer_add_char(buf, '"');
235 buffer_add_char(buf, '\\');
236 buffer_add_char(buf, 'b');
240 buffer_add_char(buf, '\\');
241 buffer_add_char(buf, 'f');
245 buffer_add_char(buf, '\\');
246 buffer_add_char(buf, 't');
250 buffer_add_char(buf, '\\');
251 buffer_add_char(buf, 'n');
255 buffer_add_char(buf, '\\');
256 buffer_add_char(buf, 'r');
260 buffer_add_char(buf, c);
264 buffer_add_char(buf, c);
271 char* d = buffer_data(buf);