]> git.proxmox.com Git - mirror_ovs.git/blob - lib/dynamic-string.h
dpif-netdev: Store actions data and size contiguously.
[mirror_ovs.git] / lib / dynamic-string.h
1 /*
2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
3 *
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:
7 *
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.
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 <time.h>
26 #include "compiler.h"
27
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. */
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);
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 *, ...) OVS_PRINTF_FORMAT(2, 3);
55 void ds_put_format_valist(struct ds *, const char *, va_list)
56 OVS_PRINTF_FORMAT(2, 0);
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 *);
63
64 void ds_put_strftime_msec(struct ds *, const char *format, long long int when,
65 bool utc);
66 char *xastrftime_msec(const char *format, long long int when, bool utc);
67
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 *);
73
74 int ds_last(const struct ds *);
75 void ds_chomp(struct ds *, int c);
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 }
91
92 #endif /* dynamic-string.h */