]>
Commit | Line | Data |
---|---|---|
064af421 | 1 | /* |
3e78870d | 2 | * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 Nicira, Inc. |
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> | |
3e78870d | 25 | #include <time.h> |
064af421 BP |
26 | #include "compiler.h" |
27 | ||
541bc79f BP |
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. | |
30 | * | |
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. */ | |
064af421 BP |
35 | struct ds { |
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. */ | |
39 | }; | |
40 | ||
41 | #define DS_EMPTY_INITIALIZER { NULL, 0, 0 } | |
42 | ||
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); | |
36c501fe | 48 | static inline void ds_put_char(struct ds *, char); |
f38b84ea | 49 | void ds_put_utf8(struct ds *, int uc); |
064af421 BP |
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 *); | |
1fd13cde | 53 | void ds_put_and_free_cstr(struct ds *, char *); |
cab50449 | 54 | void ds_put_format(struct ds *, const char *, ...) OVS_PRINTF_FORMAT(2, 3); |
064af421 | 55 | void ds_put_format_valist(struct ds *, const char *, va_list) |
cab50449 | 56 | OVS_PRINTF_FORMAT(2, 0); |
064af421 | 57 | void ds_put_printable(struct ds *, const char *, size_t); |
064af421 BP |
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 *); | |
bdda5aca | 61 | int ds_get_preprocessed_line(struct ds *, FILE *, int *line_number); |
06d7ae7d | 62 | int ds_get_test_line(struct ds *, FILE *); |
064af421 | 63 | |
4052e3ff | 64 | void ds_put_strftime_msec(struct ds *, const char *format, long long int when, |
2b31d8e7 | 65 | bool utc); |
4052e3ff | 66 | char *xastrftime_msec(const char *format, long long int when, bool utc); |
3e78870d | 67 | |
064af421 | 68 | char *ds_cstr(struct ds *); |
5f98eed4 | 69 | const char *ds_cstr_ro(const struct ds *); |
48718554 | 70 | char *ds_steal_cstr(struct ds *); |
064af421 | 71 | void ds_destroy(struct ds *); |
e83fd213 | 72 | void ds_swap(struct ds *, struct ds *); |
064af421 BP |
73 | |
74 | int ds_last(const struct ds *); | |
75 | void ds_chomp(struct ds *, int c); | |
36c501fe BP |
76 | \f |
77 | /* Inline functions. */ | |
78 | ||
79 | void ds_put_char__(struct ds *, char); | |
80 | ||
81 | static inline void | |
82 | ds_put_char(struct ds *ds, char c) | |
83 | { | |
84 | if (ds->length < ds->allocated) { | |
85 | ds->string[ds->length++] = c; | |
86 | ds->string[ds->length] = '\0'; | |
87 | } else { | |
88 | ds_put_char__(ds, c); | |
89 | } | |
90 | } | |
064af421 BP |
91 | |
92 | #endif /* dynamic-string.h */ |