Implement JSON parsing and serialization.
[openvswitch] / lib / dynamic-string.c
1 /*
2  * Copyright (c) 2008, 2009 Nicira Networks.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <config.h>
18 #include "dynamic-string.h"
19 #include <assert.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <time.h>
23 #include "timeval.h"
24 #include "util.h"
25
26 void
27 ds_init(struct ds *ds)
28 {
29     ds->string = NULL;
30     ds->length = 0;
31     ds->allocated = 0;
32 }
33
34 void
35 ds_clear(struct ds *ds) 
36 {
37     ds->length = 0;
38 }
39
40 void
41 ds_truncate(struct ds *ds, size_t new_length)
42 {
43     if (ds->length > new_length) {
44         ds->length = new_length;
45         ds->string[new_length] = '\0';
46     }
47 }
48
49 void
50 ds_reserve(struct ds *ds, size_t min_length)
51 {
52     if (min_length > ds->allocated || !ds->string) {
53         ds->allocated += MAX(min_length, ds->allocated);
54         ds->allocated = MAX(8, ds->allocated);
55         ds->string = xrealloc(ds->string, ds->allocated + 1);
56     }
57 }
58
59 char *
60 ds_put_uninit(struct ds *ds, size_t n)
61 {
62     ds_reserve(ds, ds->length + n);
63     ds->length += n;
64     ds->string[ds->length] = '\0';
65     return &ds->string[ds->length - n];
66 }
67
68 void
69 ds_put_char(struct ds *ds, char c)
70 {
71     *ds_put_uninit(ds, 1) = c;
72 }
73
74 /* Appends unicode code point 'uc' to 'ds' in UTF-8 encoding. */
75 void
76 ds_put_utf8(struct ds *ds, int uc)
77 {
78     if (uc <= 0x7f) {
79         ds_put_char(ds, uc);
80     } else if (uc <= 0x7ff) {
81         ds_put_char(ds, 0xc0 | (uc >> 6));
82         ds_put_char(ds, 0x80 | (uc & 0x3f));
83     } else if (uc <= 0xffff) {
84         ds_put_char(ds, 0xe0 | (uc >> 12));
85         ds_put_char(ds, 0x80 | ((uc >> 6) & 0x3f));
86         ds_put_char(ds, 0x80 | (uc & 0x3f));
87     } else if (uc <= 0x10ffff) {
88         ds_put_char(ds, 0xf0 | (uc >> 18));
89         ds_put_char(ds, 0x80 | ((uc >> 12) & 0x3f));
90         ds_put_char(ds, 0x80 | ((uc >> 6) & 0x3f));
91         ds_put_char(ds, 0x80 | (uc & 0x3f));
92     } else {
93         /* Invalid code point.  Insert the Unicode general substitute
94          * REPLACEMENT CHARACTER. */
95         ds_put_utf8(ds, 0xfffd);
96     }
97 }
98
99 void
100 ds_put_char_multiple(struct ds *ds, char c, size_t n)
101 {
102     memset(ds_put_uninit(ds, n), c, n);
103 }
104
105 void
106 ds_put_buffer(struct ds *ds, const char *s, size_t n)
107 {
108     memcpy(ds_put_uninit(ds, n), s, n);
109 }
110
111 void
112 ds_put_cstr(struct ds *ds, const char *s)
113 {
114     size_t s_len = strlen(s);
115     memcpy(ds_put_uninit(ds, s_len), s, s_len);
116 }
117
118 void
119 ds_put_format(struct ds *ds, const char *format, ...)
120 {
121     va_list args;
122
123     va_start(args, format);
124     ds_put_format_valist(ds, format, args);
125     va_end(args);
126 }
127
128 void
129 ds_put_format_valist(struct ds *ds, const char *format, va_list args_)
130 {
131     va_list args;
132     size_t available;
133     int needed;
134
135     va_copy(args, args_);
136     available = ds->string ? ds->allocated - ds->length + 1 : 0;
137     needed = vsnprintf(&ds->string[ds->length], available, format, args);
138     va_end(args);
139
140     if (needed < available) {
141         ds->length += needed;
142     } else {
143         size_t available;
144
145         ds_reserve(ds, ds->length + needed);
146
147         va_copy(args, args_);
148         available = ds->allocated - ds->length + 1;
149         needed = vsnprintf(&ds->string[ds->length], available, format, args);
150         va_end(args);
151
152         assert(needed < available);
153         ds->length += needed;
154     }
155 }
156
157 void
158 ds_put_printable(struct ds *ds, const char *s, size_t n) 
159 {
160     ds_reserve(ds, ds->length + n);
161     while (n-- > 0) {
162         unsigned char c = *s++;
163         if (c < 0x20 || c > 0x7e || c == '\\' || c == '"') {
164             ds_put_format(ds, "\\%03o", (int) c);
165         } else {
166             ds_put_char(ds, c);
167         }
168     }
169 }
170
171 void
172 ds_put_strftime(struct ds *ds, const char *template, const struct tm *tm)
173 {
174     if (!tm) {
175         time_t now = time_now();
176         tm = localtime(&now);
177     }
178     for (;;) {
179         size_t avail = ds->string ? ds->allocated - ds->length + 1 : 0;
180         size_t used = strftime(&ds->string[ds->length], avail, template, tm);
181         if (used) {
182             ds->length += used;
183             return;
184         }
185         ds_reserve(ds, ds->length + (avail < 32 ? 64 : 2 * avail)); 
186     }
187 }
188
189 int
190 ds_get_line(struct ds *ds, FILE *file)
191 {
192     ds_clear(ds);
193     for (;;) {
194         int c = getc(file);
195         if (c == EOF) {
196             return ds->length ? 0 : EOF;
197         } else if (c == '\n') {
198             return 0;
199         } else {
200             ds_put_char(ds, c);
201         }
202     }
203 }
204
205 char *
206 ds_cstr(struct ds *ds)
207 {
208     if (!ds->string) {
209         ds_reserve(ds, 0);
210     }
211     ds->string[ds->length] = '\0';
212     return ds->string;
213 }
214
215 /* Returns a null-terminated string representing the current contents of 'ds',
216  * which the caller is expected to free with free(), then clears the contents
217  * of 'ds'. */
218 char *
219 ds_steal_cstr(struct ds *ds)
220 {
221     char *s = ds_cstr(ds);
222     ds_init(ds);
223     return s;
224 }
225
226 void
227 ds_destroy(struct ds *ds)
228 {
229     free(ds->string);
230 }
231
232 /* Writes the 'size' bytes in 'buf' to 'string' as hex bytes arranged 16 per
233  * line.  Numeric offsets are also included, starting at 'ofs' for the first
234  * byte in 'buf'.  If 'ascii' is true then the corresponding ASCII characters
235  * are also rendered alongside. */
236 void
237 ds_put_hex_dump(struct ds *ds, const void *buf_, size_t size,
238                 uintptr_t ofs, bool ascii)
239 {
240   const uint8_t *buf = buf_;
241   const size_t per_line = 16; /* Maximum bytes per line. */
242
243   while (size > 0)
244     {
245       size_t start, end, n;
246       size_t i;
247
248       /* Number of bytes on this line. */
249       start = ofs % per_line;
250       end = per_line;
251       if (end - start > size)
252         end = start + size;
253       n = end - start;
254
255       /* Print line. */
256       ds_put_format(ds, "%08jx  ", (uintmax_t) ROUND_DOWN(ofs, per_line));
257       for (i = 0; i < start; i++)
258         ds_put_format(ds, "   ");
259       for (; i < end; i++)
260         ds_put_format(ds, "%02hhx%c",
261                 buf[i - start], i == per_line / 2 - 1? '-' : ' ');
262       if (ascii)
263         {
264           for (; i < per_line; i++)
265             ds_put_format(ds, "   ");
266           ds_put_format(ds, "|");
267           for (i = 0; i < start; i++)
268             ds_put_format(ds, " ");
269           for (; i < end; i++) {
270               int c = buf[i - start];
271               ds_put_char(ds, c >= 32 && c < 127 ? c : '.');
272           }
273           for (; i < per_line; i++)
274             ds_put_format(ds, " ");
275           ds_put_format(ds, "|");
276         }
277       ds_put_format(ds, "\n");
278
279       ofs += n;
280       buf += n;
281       size -= n;
282     }
283 }
284
285 int
286 ds_last(const struct ds *ds)
287 {
288     return ds->length > 0 ? (unsigned char) ds->string[ds->length - 1] : EOF;
289 }
290
291 void
292 ds_chomp(struct ds *ds, int c)
293 {
294     if (ds->length > 0 && ds->string[ds->length - 1] == (char) c) {
295         ds->string[--ds->length] = '\0';
296     }
297 }