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