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.
17 #ifndef DYNAMIC_STRING_H
18 #define DYNAMIC_STRING_H 1
28 /* A "dynamic string", that is, a buffer that can be used to construct a
29 * string across a series of operations that extend or modify it.
31 * The 'string' member does not always point to a null-terminated string.
32 * Initially it is NULL, and even when it is nonnull, some operations do not
33 * ensure that it is null-terminated. Use ds_cstr() to ensure that memory is
34 * allocated for the string and that it is null-terminated. */
36 char *string; /* Null-terminated string. */
37 size_t length; /* Bytes used, not including null terminator. */
38 size_t allocated; /* Bytes allocated, not including null terminator. */
41 #define DS_EMPTY_INITIALIZER { NULL, 0, 0 }
43 void ds_init(struct ds *);
44 void ds_clear(struct ds *);
45 void ds_truncate(struct ds *, size_t new_length);
46 void ds_reserve(struct ds *, size_t min_length);
47 char *ds_put_uninit(struct ds *, size_t n);
48 static inline void ds_put_char(struct ds *, char);
49 void ds_put_utf8(struct ds *, int uc);
50 void ds_put_char_multiple(struct ds *, char, size_t n);
51 void ds_put_buffer(struct ds *, const char *, size_t n);
52 void ds_put_cstr(struct ds *, const char *);
53 void ds_put_and_free_cstr(struct ds *, char *);
54 void ds_put_format(struct ds *, const char *, ...) PRINTF_FORMAT(2, 3);
55 void ds_put_format_valist(struct ds *, const char *, va_list)
57 void ds_put_printable(struct ds *, const char *, size_t);
58 void ds_put_hex_dump(struct ds *ds, const void *buf_, size_t size,
59 uintptr_t ofs, bool ascii);
60 int ds_get_line(struct ds *, FILE *);
61 int ds_get_preprocessed_line(struct ds *, FILE *, int *line_number);
62 int ds_get_test_line(struct ds *, FILE *);
64 void ds_put_strftime_msec(struct ds *, const char *template, long long int when,
66 char *xastrftime_msec(const char *template, long long int when, bool utc);
68 char *ds_cstr(struct ds *);
69 const char *ds_cstr_ro(const struct ds *);
70 char *ds_steal_cstr(struct ds *);
71 void ds_destroy(struct ds *);
72 void ds_swap(struct ds *, struct ds *);
74 int ds_last(const struct ds *);
75 void ds_chomp(struct ds *, int c);
77 /* Inline functions. */
79 void ds_put_char__(struct ds *, char);
82 ds_put_char(struct ds *ds, char c)
84 if (ds->length < ds->allocated) {
85 ds->string[ds->length++] = c;
86 ds->string[ds->length] = '\0';
92 #endif /* dynamic-string.h */