]>
git.proxmox.com Git - mirror_ovs.git/blob - lib/dynamic-string.c
2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 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"
25 /* Initializes 'ds' as an empty string buffer. */
27 ds_init(struct ds
*ds
)
34 /* Sets 'ds''s length to 0, effectively clearing any existing content. Does
35 * not free any memory. */
37 ds_clear(struct ds
*ds
)
42 /* Reduces 'ds''s length to no more than 'new_length'. (If its length is
43 * already 'new_length' or less, does nothing.) */
45 ds_truncate(struct ds
*ds
, size_t new_length
)
47 if (ds
->length
> new_length
) {
48 ds
->length
= new_length
;
49 ds
->string
[new_length
] = '\0';
53 /* Ensures that at least 'min_length + 1' bytes (including space for a null
54 * terminator) are allocated for ds->string, allocating or reallocating memory
57 ds_reserve(struct ds
*ds
, size_t min_length
)
59 if (min_length
> ds
->allocated
|| !ds
->string
) {
60 ds
->allocated
+= MAX(min_length
, ds
->allocated
);
61 ds
->allocated
= MAX(8, ds
->allocated
);
62 ds
->string
= xrealloc(ds
->string
, ds
->allocated
+ 1);
66 /* Appends space for 'n' bytes to the end of 'ds->string', increasing
67 * 'ds->length' by the same amount, and returns the first appended byte. The
68 * caller should fill in all 'n' bytes starting at the return value. */
70 ds_put_uninit(struct ds
*ds
, size_t n
)
72 ds_reserve(ds
, ds
->length
+ n
);
74 ds
->string
[ds
->length
] = '\0';
75 return &ds
->string
[ds
->length
- n
];
79 ds_put_char__(struct ds
*ds
, char c
)
81 *ds_put_uninit(ds
, 1) = c
;
84 /* Appends unicode code point 'uc' to 'ds' in UTF-8 encoding. */
86 ds_put_utf8(struct ds
*ds
, int uc
)
90 } else if (uc
<= 0x7ff) {
91 ds_put_char(ds
, 0xc0 | (uc
>> 6));
92 ds_put_char(ds
, 0x80 | (uc
& 0x3f));
93 } else if (uc
<= 0xffff) {
94 ds_put_char(ds
, 0xe0 | (uc
>> 12));
95 ds_put_char(ds
, 0x80 | ((uc
>> 6) & 0x3f));
96 ds_put_char(ds
, 0x80 | (uc
& 0x3f));
97 } else if (uc
<= 0x10ffff) {
98 ds_put_char(ds
, 0xf0 | (uc
>> 18));
99 ds_put_char(ds
, 0x80 | ((uc
>> 12) & 0x3f));
100 ds_put_char(ds
, 0x80 | ((uc
>> 6) & 0x3f));
101 ds_put_char(ds
, 0x80 | (uc
& 0x3f));
103 /* Invalid code point. Insert the Unicode general substitute
104 * REPLACEMENT CHARACTER. */
105 ds_put_utf8(ds
, 0xfffd);
110 ds_put_char_multiple(struct ds
*ds
, char c
, size_t n
)
112 memset(ds_put_uninit(ds
, n
), c
, n
);
116 ds_put_buffer(struct ds
*ds
, const char *s
, size_t n
)
118 memcpy(ds_put_uninit(ds
, n
), s
, n
);
122 ds_put_cstr(struct ds
*ds
, const char *s
)
124 size_t s_len
= strlen(s
);
125 memcpy(ds_put_uninit(ds
, s_len
), s
, s_len
);
129 ds_put_and_free_cstr(struct ds
*ds
, char *s
)
136 ds_put_format(struct ds
*ds
, const char *format
, ...)
140 va_start(args
, format
);
141 ds_put_format_valist(ds
, format
, args
);
146 ds_put_format_valist(struct ds
*ds
, const char *format
, va_list args_
)
152 va_copy(args
, args_
);
153 available
= ds
->string
? ds
->allocated
- ds
->length
+ 1 : 0;
154 needed
= vsnprintf(&ds
->string
[ds
->length
], available
, format
, args
);
157 if (needed
< available
) {
158 ds
->length
+= needed
;
160 ds_reserve(ds
, ds
->length
+ needed
);
162 va_copy(args
, args_
);
163 available
= ds
->allocated
- ds
->length
+ 1;
164 needed
= vsnprintf(&ds
->string
[ds
->length
], available
, format
, args
);
167 ovs_assert(needed
< available
);
168 ds
->length
+= needed
;
173 ds_put_printable(struct ds
*ds
, const char *s
, size_t n
)
175 ds_reserve(ds
, ds
->length
+ n
);
177 unsigned char c
= *s
++;
178 if (c
< 0x20 || c
> 0x7e || c
== '\\' || c
== '"') {
179 ds_put_format(ds
, "\\%03o", (int) c
);
186 /* Writes time 'when' to 'string' based on 'template', in local time or UTC
189 ds_put_strftime(struct ds
*ds
, const char *template, time_t when
, bool utc
)
193 gmtime_r(&when
, &tm
);
195 localtime_r(&when
, &tm
);
199 size_t avail
= ds
->string
? ds
->allocated
- ds
->length
+ 1 : 0;
200 size_t used
= strftime(&ds
->string
[ds
->length
], avail
, template, &tm
);
205 ds_reserve(ds
, ds
->length
+ (avail
< 32 ? 64 : 2 * avail
));
209 /* Returns a malloc()'d string for time 'when' based on 'template', in local
210 * time or UTC based on 'utc'. */
212 xastrftime(const char *template, time_t when
, bool utc
)
217 ds_put_strftime(&s
, template, when
, utc
);
222 ds_get_line(struct ds
*ds
, FILE *file
)
228 return ds
->length
? 0 : EOF
;
229 } else if (c
== '\n') {
237 /* Reads a line from 'file' into 'ds', clearing anything initially in 'ds'.
238 * Deletes comments introduced by "#" and skips lines that contains only white
239 * space (after deleting comments).
241 * Returns 0 if successful, EOF if no non-blank line was found. */
243 ds_get_preprocessed_line(struct ds
*ds
, FILE *file
)
245 while (!ds_get_line(ds
, file
)) {
246 char *line
= ds_cstr(ds
);
249 /* Delete comments. */
250 comment
= strchr(line
, '#');
255 /* Return successfully unless the line is all spaces. */
256 if (line
[strspn(line
, " \t\n")] != '\0') {
263 /* Reads a line from 'file' into 'ds' and does some preprocessing on it:
265 * - If the line begins with #, prints it on stdout and reads the next line.
267 * - Otherwise, if the line contains an # somewhere else, strips it and
268 * everything following it (as a comment).
270 * - If (after comment removal) the line contains only white space, prints
271 * a blank line on stdout and reads the next line.
273 * - Otherwise, returns the line to the caller.
275 * This is useful in some of the OVS tests, where we want to check that parsing
276 * and then re-formatting some kind of data does not change it, but we also
277 * want to be able to put comments in the input.
279 * Returns 0 if successful, EOF if no non-blank line was found. */
281 ds_get_test_line(struct ds
*ds
, FILE *file
)
287 retval
= ds_get_line(ds
, file
);
298 comment
= strchr(s
, '#');
302 if (s
[strspn(s
, " \t\n")] == '\0') {
312 ds_cstr(struct ds
*ds
)
317 ds
->string
[ds
->length
] = '\0';
322 ds_cstr_ro(const struct ds
*ds
)
324 return ds_cstr(CONST_CAST(struct ds
*, ds
));
327 /* Returns a null-terminated string representing the current contents of 'ds',
328 * which the caller is expected to free with free(), then clears the contents
331 ds_steal_cstr(struct ds
*ds
)
333 char *s
= ds_cstr(ds
);
339 ds_destroy(struct ds
*ds
)
344 /* Swaps the content of 'a' and 'b'. */
346 ds_swap(struct ds
*a
, struct ds
*b
)
353 /* Writes the 'size' bytes in 'buf' to 'string' as hex bytes arranged 16 per
354 * line. Numeric offsets are also included, starting at 'ofs' for the first
355 * byte in 'buf'. If 'ascii' is true then the corresponding ASCII characters
356 * are also rendered alongside. */
358 ds_put_hex_dump(struct ds
*ds
, const void *buf_
, size_t size
,
359 uintptr_t ofs
, bool ascii
)
361 const uint8_t *buf
= buf_
;
362 const size_t per_line
= 16; /* Maximum bytes per line. */
366 size_t start
, end
, n
;
369 /* Number of bytes on this line. */
370 start
= ofs
% per_line
;
372 if (end
- start
> size
)
377 ds_put_format(ds
, "%08jx ", (uintmax_t) ROUND_DOWN(ofs
, per_line
));
378 for (i
= 0; i
< start
; i
++)
379 ds_put_format(ds
, " ");
381 ds_put_format(ds
, "%02hhx%c",
382 buf
[i
- start
], i
== per_line
/ 2 - 1? '-' : ' ');
385 for (; i
< per_line
; i
++)
386 ds_put_format(ds
, " ");
387 ds_put_format(ds
, "|");
388 for (i
= 0; i
< start
; i
++)
389 ds_put_format(ds
, " ");
390 for (; i
< end
; i
++) {
391 int c
= buf
[i
- start
];
392 ds_put_char(ds
, c
>= 32 && c
< 127 ? c
: '.');
394 for (; i
< per_line
; i
++)
395 ds_put_format(ds
, " ");
396 ds_put_format(ds
, "|");
398 ds_put_format(ds
, "\n");
407 ds_last(const struct ds
*ds
)
409 return ds
->length
> 0 ? (unsigned char) ds
->string
[ds
->length
- 1] : EOF
;
413 ds_chomp(struct ds
*ds
, int c
)
415 if (ds
->length
> 0 && ds
->string
[ds
->length
- 1] == (char) c
) {
416 ds
->string
[--ds
->length
] = '\0';