2 * Copyright (c) 2008, 2009, 2010, 2011 Nicira Networks.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "dynamic-string.h"
27 ds_init(struct ds *ds)
35 ds_clear(struct ds *ds)
41 ds_truncate(struct ds *ds, size_t new_length)
43 if (ds->length > new_length) {
44 ds->length = new_length;
45 ds->string[new_length] = '\0';
50 ds_reserve(struct ds *ds, size_t min_length)
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);
60 ds_put_uninit(struct ds *ds, size_t n)
62 ds_reserve(ds, ds->length + n);
64 ds->string[ds->length] = '\0';
65 return &ds->string[ds->length - n];
69 ds_put_char__(struct ds *ds, char c)
71 *ds_put_uninit(ds, 1) = c;
74 /* Appends unicode code point 'uc' to 'ds' in UTF-8 encoding. */
76 ds_put_utf8(struct ds *ds, int 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));
93 /* Invalid code point. Insert the Unicode general substitute
94 * REPLACEMENT CHARACTER. */
95 ds_put_utf8(ds, 0xfffd);
100 ds_put_char_multiple(struct ds *ds, char c, size_t n)
102 memset(ds_put_uninit(ds, n), c, n);
106 ds_put_buffer(struct ds *ds, const char *s, size_t n)
108 memcpy(ds_put_uninit(ds, n), s, n);
112 ds_put_cstr(struct ds *ds, const char *s)
114 size_t s_len = strlen(s);
115 memcpy(ds_put_uninit(ds, s_len), s, s_len);
119 ds_put_and_free_cstr(struct ds *ds, char *s)
126 ds_put_format(struct ds *ds, const char *format, ...)
130 va_start(args, format);
131 ds_put_format_valist(ds, format, args);
136 ds_put_format_valist(struct ds *ds, const char *format, va_list args_)
142 va_copy(args, args_);
143 available = ds->string ? ds->allocated - ds->length + 1 : 0;
144 needed = vsnprintf(&ds->string[ds->length], available, format, args);
147 if (needed < available) {
148 ds->length += needed;
150 ds_reserve(ds, ds->length + needed);
152 va_copy(args, args_);
153 available = ds->allocated - ds->length + 1;
154 needed = vsnprintf(&ds->string[ds->length], available, format, args);
157 assert(needed < available);
158 ds->length += needed;
163 ds_put_printable(struct ds *ds, const char *s, size_t n)
165 ds_reserve(ds, ds->length + n);
167 unsigned char c = *s++;
168 if (c < 0x20 || c > 0x7e || c == '\\' || c == '"') {
169 ds_put_format(ds, "\\%03o", (int) c);
177 ds_put_strftime(struct ds *ds, const char *template, const struct tm *tm)
180 time_t now = time_wall();
181 tm = localtime(&now);
184 size_t avail = ds->string ? ds->allocated - ds->length + 1 : 0;
185 size_t used = strftime(&ds->string[ds->length], avail, template, tm);
190 ds_reserve(ds, ds->length + (avail < 32 ? 64 : 2 * avail));
195 ds_get_line(struct ds *ds, FILE *file)
201 return ds->length ? 0 : EOF;
202 } else if (c == '\n') {
210 /* Reads a line from 'file' into 'ds', clearing anything initially in 'ds'.
211 * Deletes comments introduced by "#" and skips lines that contains only white
212 * space (after deleting comments).
214 * Returns 0 if successful, EOF if no non-blank line was found. */
216 ds_get_preprocessed_line(struct ds *ds, FILE *file)
218 while (!ds_get_line(ds, file)) {
219 char *line = ds_cstr(ds);
222 /* Delete comments. */
223 comment = strchr(line, '#');
228 /* Return successfully unless the line is all spaces. */
229 if (line[strspn(line, " \t\n")] != '\0') {
237 ds_cstr(struct ds *ds)
242 ds->string[ds->length] = '\0';
247 ds_cstr_ro(const struct ds *ds)
249 return ds_cstr((struct ds *) ds);
252 /* Returns a null-terminated string representing the current contents of 'ds',
253 * which the caller is expected to free with free(), then clears the contents
256 ds_steal_cstr(struct ds *ds)
258 char *s = ds_cstr(ds);
264 ds_destroy(struct ds *ds)
269 /* Swaps the content of 'a' and 'b'. */
271 ds_swap(struct ds *a, struct ds *b)
278 /* Writes the 'size' bytes in 'buf' to 'string' as hex bytes arranged 16 per
279 * line. Numeric offsets are also included, starting at 'ofs' for the first
280 * byte in 'buf'. If 'ascii' is true then the corresponding ASCII characters
281 * are also rendered alongside. */
283 ds_put_hex_dump(struct ds *ds, const void *buf_, size_t size,
284 uintptr_t ofs, bool ascii)
286 const uint8_t *buf = buf_;
287 const size_t per_line = 16; /* Maximum bytes per line. */
291 size_t start, end, n;
294 /* Number of bytes on this line. */
295 start = ofs % per_line;
297 if (end - start > size)
302 ds_put_format(ds, "%08jx ", (uintmax_t) ROUND_DOWN(ofs, per_line));
303 for (i = 0; i < start; i++)
304 ds_put_format(ds, " ");
306 ds_put_format(ds, "%02hhx%c",
307 buf[i - start], i == per_line / 2 - 1? '-' : ' ');
310 for (; i < per_line; i++)
311 ds_put_format(ds, " ");
312 ds_put_format(ds, "|");
313 for (i = 0; i < start; i++)
314 ds_put_format(ds, " ");
315 for (; i < end; i++) {
316 int c = buf[i - start];
317 ds_put_char(ds, c >= 32 && c < 127 ? c : '.');
319 for (; i < per_line; i++)
320 ds_put_format(ds, " ");
321 ds_put_format(ds, "|");
323 ds_put_format(ds, "\n");
332 ds_last(const struct ds *ds)
334 return ds->length > 0 ? (unsigned char) ds->string[ds->length - 1] : EOF;
338 ds_chomp(struct ds *ds, int c)
340 if (ds->length > 0 && ds->string[ds->length - 1] == (char) c) {
341 ds->string[--ds->length] = '\0';