2 * Copyright (c) 2008, 2009 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;
75 ds_put_char_multiple(struct ds *ds, char c, size_t n)
77 memset(ds_put_uninit(ds, n), c, n);
81 ds_put_buffer(struct ds *ds, const char *s, size_t n)
83 memcpy(ds_put_uninit(ds, n), s, n);
87 ds_put_cstr(struct ds *ds, const char *s)
89 size_t s_len = strlen(s);
90 memcpy(ds_put_uninit(ds, s_len), s, s_len);
94 ds_put_format(struct ds *ds, const char *format, ...)
98 va_start(args, format);
99 ds_put_format_valist(ds, format, args);
104 ds_put_format_valist(struct ds *ds, const char *format, va_list args_)
110 va_copy(args, args_);
111 available = ds->string ? ds->allocated - ds->length + 1 : 0;
112 needed = vsnprintf(&ds->string[ds->length], available, format, args);
115 if (needed < available) {
116 ds->length += needed;
120 ds_reserve(ds, ds->length + needed);
122 va_copy(args, args_);
123 available = ds->allocated - ds->length + 1;
124 needed = vsnprintf(&ds->string[ds->length], available, format, args);
127 assert(needed < available);
128 ds->length += needed;
133 ds_put_printable(struct ds *ds, const char *s, size_t n)
135 ds_reserve(ds, ds->length + n);
137 unsigned char c = *s++;
138 if (c < 0x20 || c > 0x7e || c == '\\' || c == '"') {
139 ds_put_format(ds, "\\%03o", (int) c);
147 ds_put_strftime(struct ds *ds, const char *template, const struct tm *tm)
150 time_t now = time_now();
151 tm = localtime(&now);
154 size_t avail = ds->string ? ds->allocated - ds->length + 1 : 0;
155 size_t used = strftime(&ds->string[ds->length], avail, template, tm);
160 ds_reserve(ds, ds->length + (avail < 32 ? 64 : 2 * avail));
165 ds_get_line(struct ds *ds, FILE *file)
171 return ds->length ? 0 : EOF;
172 } else if (c == '\n') {
181 ds_cstr(struct ds *ds)
186 ds->string[ds->length] = '\0';
190 /* Returns a null-terminated string representing the current contents of 'ds',
191 * which the caller is expected to free with free(), then clears the contents
194 ds_steal_cstr(struct ds *ds)
196 char *s = ds_cstr(ds);
202 ds_destroy(struct ds *ds)
207 /* Writes the 'size' bytes in 'buf' to 'string' as hex bytes arranged 16 per
208 * line. Numeric offsets are also included, starting at 'ofs' for the first
209 * byte in 'buf'. If 'ascii' is true then the corresponding ASCII characters
210 * are also rendered alongside. */
212 ds_put_hex_dump(struct ds *ds, const void *buf_, size_t size,
213 uintptr_t ofs, bool ascii)
215 const uint8_t *buf = buf_;
216 const size_t per_line = 16; /* Maximum bytes per line. */
220 size_t start, end, n;
223 /* Number of bytes on this line. */
224 start = ofs % per_line;
226 if (end - start > size)
231 ds_put_format(ds, "%08jx ", (uintmax_t) ROUND_DOWN(ofs, per_line));
232 for (i = 0; i < start; i++)
233 ds_put_format(ds, " ");
235 ds_put_format(ds, "%02hhx%c",
236 buf[i - start], i == per_line / 2 - 1? '-' : ' ');
239 for (; i < per_line; i++)
240 ds_put_format(ds, " ");
241 ds_put_format(ds, "|");
242 for (i = 0; i < start; i++)
243 ds_put_format(ds, " ");
244 for (; i < end; i++) {
245 int c = buf[i - start];
246 ds_put_char(ds, c >= 32 && c < 127 ? c : '.');
248 for (; i < per_line; i++)
249 ds_put_format(ds, " ");
250 ds_put_format(ds, "|");
252 ds_put_format(ds, "\n");
261 ds_last(const struct ds *ds)
263 return ds->length > 0 ? (unsigned char) ds->string[ds->length - 1] : EOF;
267 ds_chomp(struct ds *ds, int c)
269 if (ds->length > 0 && ds->string[ds->length - 1] == (char) c) {
270 ds->string[--ds->length] = '\0';