]> git.evergreen-ils.org Git - Evergreen.git/blob - OpenSRF/src/utils/utils.h
new json api changes
[Evergreen.git] / OpenSRF / src / utils / utils.h
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 #ifndef UTILS_H
18 #define UTILS_H
19
20 #include <stdio.h>
21 #include <stdarg.h>
22 #include <fcntl.h>
23 #include <unistd.h>
24 #include <sys/time.h>
25 #include <sys/stat.h>
26 #include <fcntl.h>
27 #include <sys/types.h>
28 #include <stdlib.h>
29 #include <string.h>
30 //#include <sys/timeb.h>
31
32
33 /* turns a va_list into a string */
34 #define VA_LIST_TO_STRING(x) \
35         unsigned long len = 0;\
36         va_list args; \
37         va_list a_copy;\
38         va_copy(a_copy, args); \
39         va_start(args, x); \
40         len = vsnprintf(NULL, 0, x, args); \
41         va_end(args); \
42         len += 2; \
43         char _b[len]; \
44         bzero(_b, len); \
45         va_start(a_copy, x); \
46         vsnprintf(_b, len - 1, x, a_copy); \
47         va_end(a_copy); \
48         char* VA_BUF = _b; \
49
50 /* turns a long into a string */
51 #define LONG_TO_STRING(l) \
52         unsigned int __len = snprintf(NULL, 0, "%ld", l) + 2;\
53         char __b[__len]; \
54         bzero(__b, __len); \
55         snprintf(__b, __len - 1, "%ld", l); \
56         char* LONGSTR = __b;
57
58 #define DOUBLE_TO_STRING(l) \
59         unsigned int __len = snprintf(NULL, 0, "%lf", l) + 2; \
60         char __b[__len]; \
61         bzero(__b, __len); \
62         snprintf(__b, __len - 1, "%lf", l); \
63         char* DOUBLESTR = __b;
64
65 #define INT_TO_STRING(l) \
66         unsigned int __len = snprintf(NULL, 0, "%d", l) + 2; \
67         char __b[__len]; \
68         bzero(__b, __len); \
69         snprintf(__b, __len - 1, "%d", l); \
70         char* INTSTR = __b;
71
72
73         
74
75
76 #define BUFFER_MAX_SIZE 10485760 
77
78 int daemonize();
79
80 void* safe_malloc(int size);
81
82 // ---------------------------------------------------------------------------------
83 // Generic growing buffer. Add data all you want
84 // ---------------------------------------------------------------------------------
85 struct growing_buffer_struct {
86         char *buf;
87         int n_used;
88         int size;
89 };
90 typedef struct growing_buffer_struct growing_buffer;
91
92 growing_buffer* buffer_init( int initial_num_bytes);
93
94 // XXX This isn't defined in utils.c!! removing for now...
95 //int buffer_addchar(growing_buffer* gb, char c);
96
97 int buffer_add(growing_buffer* gb, char* c);
98 int buffer_fadd(growing_buffer* gb, const char* format, ... );
99 int buffer_reset( growing_buffer* gb);
100 char* buffer_data( growing_buffer* gb);
101 int buffer_free( growing_buffer* gb );
102 int buffer_add_char(growing_buffer* gb, char c);
103
104 /* returns the size needed to fill in the vsnprintf buffer.  
105         * ! this calls va_end on the va_list argument*
106         */
107 long va_list_size(const char* format, va_list);
108
109 /* turns a va list into a string, caller must free the 
110         allocated char */
111 char* va_list_to_string(const char* format, ...);
112
113
114 /* string escape utility method.  escapes unicode embeded characters.
115         escapes the usual \n, \t, etc. 
116         for example, if you provide a string like so:
117
118         hello,
119                 you
120
121         you would get back:
122         hello,\n\tyou
123  
124  */
125 char* uescape( const char* string, int size, int full_escape );
126
127 /* utility methods */
128 int set_fl( int fd, int flags );
129 int clr_fl( int fd, int flags );
130
131
132
133 // Utility method
134 double get_timestamp_millis();
135
136
137 /* returns true if the whole string is a number */
138 int stringisnum(char* s);
139
140 /* reads a file and returns the string version of the file
141         user is responsible for freeing the returned char*
142         */
143 char* file_to_string(const char* filename);
144
145
146
147
148
149 #endif