]> git.proxmox.com Git - mirror_ovs.git/blob - include/openvswitch/dynamic-string.h
ofp-monitor: Make OFP_FLOW_REMOVED_REASON_BUFSIZE public.
[mirror_ovs.git] / include / openvswitch / dynamic-string.h
1 /*
2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2015, 2016 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 OPENVSWITCH_DYNAMIC_STRING_H
18 #define OPENVSWITCH_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 "openvswitch/compiler.h"
26
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30
31 /* A "dynamic string", that is, a buffer that can be used to construct a
32 * string across a series of operations that extend or modify it.
33 *
34 * The 'string' member does not always point to a null-terminated string.
35 * Initially it is NULL, and even when it is nonnull, some operations do not
36 * ensure that it is null-terminated. Use ds_cstr() to ensure that memory is
37 * allocated for the string and that it is null-terminated. */
38 struct ds {
39 char *string; /* Null-terminated string. */
40 size_t length; /* Bytes used, not including null terminator. */
41 size_t allocated; /* Bytes allocated, not including null terminator. */
42 };
43
44 #define DS_EMPTY_INITIALIZER { NULL, 0, 0 }
45
46 void ds_init(struct ds *);
47 void ds_clear(struct ds *);
48 void ds_truncate(struct ds *, size_t new_length);
49 void ds_reserve(struct ds *, size_t min_length);
50 char *ds_put_uninit(struct ds *, size_t n);
51 static inline void ds_put_char(struct ds *, char);
52 void ds_put_utf8(struct ds *, int uc);
53 void ds_put_char_multiple(struct ds *, char, size_t n);
54 void ds_put_buffer(struct ds *, const char *, size_t n);
55 void ds_put_cstr(struct ds *, const char *);
56 void ds_put_and_free_cstr(struct ds *, char *);
57 void ds_put_format(struct ds *, const char *, ...) OVS_PRINTF_FORMAT(2, 3);
58 void ds_put_format_valist(struct ds *, const char *, va_list)
59 OVS_PRINTF_FORMAT(2, 0);
60 void ds_put_printable(struct ds *, const char *, size_t);
61 void ds_put_hex(struct ds *ds, const void *buf, size_t size);
62 void ds_put_hex_dump(struct ds *ds, const void *buf_, size_t size,
63 uintptr_t ofs, bool ascii);
64 int ds_get_line(struct ds *, FILE *);
65 int ds_get_preprocessed_line(struct ds *, FILE *, int *line_number);
66 int ds_get_test_line(struct ds *, FILE *);
67
68 void ds_put_strftime_msec(struct ds *, const char *format, long long int when,
69 bool utc);
70 char *xastrftime_msec(const char *format, long long int when, bool utc);
71
72 char *ds_cstr(struct ds *);
73 const char *ds_cstr_ro(const struct ds *);
74 char *ds_steal_cstr(struct ds *);
75 void ds_destroy(struct ds *);
76 void ds_swap(struct ds *, struct ds *);
77
78 int ds_last(const struct ds *);
79 bool ds_chomp(struct ds *, int c);
80 void ds_clone(struct ds *, struct ds *);
81 \f
82 /* Inline functions. */
83
84 void ds_put_char__(struct ds *, char);
85
86 static inline void
87 ds_put_char(struct ds *ds, char c)
88 {
89 if (ds->length < ds->allocated) {
90 ds->string[ds->length++] = c;
91 ds->string[ds->length] = '\0';
92 } else {
93 ds_put_char__(ds, c);
94 }
95 }
96
97 #ifdef __cplusplus
98 }
99 #endif
100
101 #endif /* dynamic-string.h */