2 * Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira, Inc.
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"
26 /* Initializes 'ds' as an empty string buffer. */
28 ds_init(struct ds *ds)
35 /* Sets 'ds''s length to 0, effectively clearing any existing content. Does
36 * not free any memory. */
38 ds_clear(struct ds *ds)
43 /* Reduces 'ds''s length to no more than 'new_length'. (If its length is
44 * already 'new_length' or less, does nothing.) */
46 ds_truncate(struct ds *ds, size_t new_length)
48 if (ds->length > new_length) {
49 ds->length = new_length;
50 ds->string[new_length] = '\0';
54 /* Ensures that at least 'min_length + 1' bytes (including space for a null
55 * terminator) are allocated for ds->string, allocating or reallocating memory
58 ds_reserve(struct ds *ds, size_t min_length)
60 if (min_length > ds->allocated || !ds->string) {
61 ds->allocated += MAX(min_length, ds->allocated);
62 ds->allocated = MAX(8, ds->allocated);
63 ds->string = xrealloc(ds->string, ds->allocated + 1);
67 /* Appends space for 'n' bytes to the end of 'ds->string', increasing
68 * 'ds->length' by the same amount, and returns the first appended byte. The
69 * caller should fill in all 'n' bytes starting at the return value. */
71 ds_put_uninit(struct ds *ds, size_t n)
73 ds_reserve(ds, ds->length + n);
75 ds->string[ds->length] = '\0';
76 return &ds->string[ds->length - n];
80 ds_put_char__(struct ds *ds, char c)
82 *ds_put_uninit(ds, 1) = c;
85 /* Appends unicode code point 'uc' to 'ds' in UTF-8 encoding. */
87 ds_put_utf8(struct ds *ds, int uc)
91 } else if (uc <= 0x7ff) {
92 ds_put_char(ds, 0xc0 | (uc >> 6));
93 ds_put_char(ds, 0x80 | (uc & 0x3f));
94 } else if (uc <= 0xffff) {
95 ds_put_char(ds, 0xe0 | (uc >> 12));
96 ds_put_char(ds, 0x80 | ((uc >> 6) & 0x3f));
97 ds_put_char(ds, 0x80 | (uc & 0x3f));
98 } else if (uc <= 0x10ffff) {
99 ds_put_char(ds, 0xf0 | (uc >> 18));
100 ds_put_char(ds, 0x80 | ((uc >> 12) & 0x3f));
101 ds_put_char(ds, 0x80 | ((uc >> 6) & 0x3f));
102 ds_put_char(ds, 0x80 | (uc & 0x3f));
104 /* Invalid code point. Insert the Unicode general substitute
105 * REPLACEMENT CHARACTER. */
106 ds_put_utf8(ds, 0xfffd);
111 ds_put_char_multiple(struct ds *ds, char c, size_t n)
113 memset(ds_put_uninit(ds, n), c, n);
117 ds_put_buffer(struct ds *ds, const char *s, size_t n)
119 memcpy(ds_put_uninit(ds, n), s, n);
123 ds_put_cstr(struct ds *ds, const char *s)
125 size_t s_len = strlen(s);
126 memcpy(ds_put_uninit(ds, s_len), s, s_len);
130 ds_put_and_free_cstr(struct ds *ds, char *s)
137 ds_put_format(struct ds *ds, const char *format, ...)
141 va_start(args, format);
142 ds_put_format_valist(ds, format, args);
147 ds_put_format_valist(struct ds *ds, const char *format, va_list args_)
153 va_copy(args, args_);
154 available = ds->string ? ds->allocated - ds->length + 1 : 0;
155 needed = vsnprintf(&ds->string[ds->length], available, format, args);
158 if (needed < available) {
159 ds->length += needed;
161 ds_reserve(ds, ds->length + needed);
163 va_copy(args, args_);
164 available = ds->allocated - ds->length + 1;
165 needed = vsnprintf(&ds->string[ds->length], available, format, args);
168 assert(needed < available);
169 ds->length += needed;
174 ds_put_printable(struct ds *ds, const char *s, size_t n)
176 ds_reserve(ds, ds->length + n);
178 unsigned char c = *s++;
179 if (c < 0x20 || c > 0x7e || c == '\\' || c == '"') {
180 ds_put_format(ds, "\\%03o", (int) c);
187 /* Writes the current time to 'string' based on 'template'.
188 * The current time is either localtime or UTC based on 'utc'. */
190 ds_put_strftime(struct ds *ds, const char *template, bool utc)
193 time_t now = time_wall();
197 tm = localtime(&now);
201 size_t avail = ds->string ? ds->allocated - ds->length + 1 : 0;
202 size_t used = strftime(&ds->string[ds->length], avail, template, tm);
207 ds_reserve(ds, ds->length + (avail < 32 ? 64 : 2 * avail));
212 ds_get_line(struct ds *ds, FILE *file)
218 return ds->length ? 0 : EOF;
219 } else if (c == '\n') {
227 /* Reads a line from 'file' into 'ds', clearing anything initially in 'ds'.
228 * Deletes comments introduced by "#" and skips lines that contains only white
229 * space (after deleting comments).
231 * Returns 0 if successful, EOF if no non-blank line was found. */
233 ds_get_preprocessed_line(struct ds *ds, FILE *file)
235 while (!ds_get_line(ds, file)) {
236 char *line = ds_cstr(ds);
239 /* Delete comments. */
240 comment = strchr(line, '#');
245 /* Return successfully unless the line is all spaces. */
246 if (line[strspn(line, " \t\n")] != '\0') {
254 ds_cstr(struct ds *ds)
259 ds->string[ds->length] = '\0';
264 ds_cstr_ro(const struct ds *ds)
266 return ds_cstr((struct ds *) ds);
269 /* Returns a null-terminated string representing the current contents of 'ds',
270 * which the caller is expected to free with free(), then clears the contents
273 ds_steal_cstr(struct ds *ds)
275 char *s = ds_cstr(ds);
281 ds_destroy(struct ds *ds)
286 /* Swaps the content of 'a' and 'b'. */
288 ds_swap(struct ds *a, struct ds *b)
295 /* Writes the 'size' bytes in 'buf' to 'string' as hex bytes arranged 16 per
296 * line. Numeric offsets are also included, starting at 'ofs' for the first
297 * byte in 'buf'. If 'ascii' is true then the corresponding ASCII characters
298 * are also rendered alongside. */
300 ds_put_hex_dump(struct ds *ds, const void *buf_, size_t size,
301 uintptr_t ofs, bool ascii)
303 const uint8_t *buf = buf_;
304 const size_t per_line = 16; /* Maximum bytes per line. */
308 size_t start, end, n;
311 /* Number of bytes on this line. */
312 start = ofs % per_line;
314 if (end - start > size)
319 ds_put_format(ds, "%08jx ", (uintmax_t) ROUND_DOWN(ofs, per_line));
320 for (i = 0; i < start; i++)
321 ds_put_format(ds, " ");
323 ds_put_format(ds, "%02hhx%c",
324 buf[i - start], i == per_line / 2 - 1? '-' : ' ');
327 for (; i < per_line; i++)
328 ds_put_format(ds, " ");
329 ds_put_format(ds, "|");
330 for (i = 0; i < start; i++)
331 ds_put_format(ds, " ");
332 for (; i < end; i++) {
333 int c = buf[i - start];
334 ds_put_char(ds, c >= 32 && c < 127 ? c : '.');
336 for (; i < per_line; i++)
337 ds_put_format(ds, " ");
338 ds_put_format(ds, "|");
340 ds_put_format(ds, "\n");
349 ds_last(const struct ds *ds)
351 return ds->length > 0 ? (unsigned char) ds->string[ds->length - 1] : EOF;
355 ds_chomp(struct ds *ds, int c)
357 if (ds->length > 0 && ds->string[ds->length - 1] == (char) c) {
358 ds->string[--ds->length] = '\0';