]>
Commit | Line | Data |
---|---|---|
064af421 | 1 | /* |
e83fd213 | 2 | * Copyright (c) 2008, 2009, 2010 Nicira Networks. |
064af421 | 3 | * |
a14bc59f BP |
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: | |
064af421 | 7 | * |
a14bc59f BP |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * | |
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. | |
064af421 BP |
15 | */ |
16 | ||
17 | #ifndef DYNAMIC_STRING_H | |
18 | #define DYNAMIC_STRING_H 1 | |
19 | ||
20 | #include <stdarg.h> | |
21 | #include <stdbool.h> | |
22 | #include <stddef.h> | |
23 | #include <stdint.h> | |
24 | #include <stdio.h> | |
25 | #include "compiler.h" | |
26 | ||
27 | struct tm; | |
28 | ||
29 | struct ds { | |
30 | char *string; /* Null-terminated string. */ | |
31 | size_t length; /* Bytes used, not including null terminator. */ | |
32 | size_t allocated; /* Bytes allocated, not including null terminator. */ | |
33 | }; | |
34 | ||
35 | #define DS_EMPTY_INITIALIZER { NULL, 0, 0 } | |
36 | ||
37 | void ds_init(struct ds *); | |
38 | void ds_clear(struct ds *); | |
39 | void ds_truncate(struct ds *, size_t new_length); | |
40 | void ds_reserve(struct ds *, size_t min_length); | |
41 | char *ds_put_uninit(struct ds *, size_t n); | |
36c501fe | 42 | static inline void ds_put_char(struct ds *, char); |
f38b84ea | 43 | void ds_put_utf8(struct ds *, int uc); |
064af421 BP |
44 | void ds_put_char_multiple(struct ds *, char, size_t n); |
45 | void ds_put_buffer(struct ds *, const char *, size_t n); | |
46 | void ds_put_cstr(struct ds *, const char *); | |
1fd13cde | 47 | void ds_put_and_free_cstr(struct ds *, char *); |
064af421 BP |
48 | void ds_put_format(struct ds *, const char *, ...) PRINTF_FORMAT(2, 3); |
49 | void ds_put_format_valist(struct ds *, const char *, va_list) | |
50 | PRINTF_FORMAT(2, 0); | |
51 | void ds_put_printable(struct ds *, const char *, size_t); | |
52 | void ds_put_strftime(struct ds *, const char *, const struct tm *) | |
53 | STRFTIME_FORMAT(2); | |
54 | void ds_put_hex_dump(struct ds *ds, const void *buf_, size_t size, | |
55 | uintptr_t ofs, bool ascii); | |
56 | int ds_get_line(struct ds *, FILE *); | |
57 | ||
58 | char *ds_cstr(struct ds *); | |
5f98eed4 | 59 | const char *ds_cstr_ro(const struct ds *); |
48718554 | 60 | char *ds_steal_cstr(struct ds *); |
064af421 | 61 | void ds_destroy(struct ds *); |
e83fd213 | 62 | void ds_swap(struct ds *, struct ds *); |
064af421 BP |
63 | |
64 | int ds_last(const struct ds *); | |
65 | void ds_chomp(struct ds *, int c); | |
36c501fe BP |
66 | \f |
67 | /* Inline functions. */ | |
68 | ||
69 | void ds_put_char__(struct ds *, char); | |
70 | ||
71 | static inline void | |
72 | ds_put_char(struct ds *ds, char c) | |
73 | { | |
74 | if (ds->length < ds->allocated) { | |
75 | ds->string[ds->length++] = c; | |
76 | ds->string[ds->length] = '\0'; | |
77 | } else { | |
78 | ds_put_char__(ds, c); | |
79 | } | |
80 | } | |
064af421 BP |
81 | |
82 | #endif /* dynamic-string.h */ |