]> git.proxmox.com Git - mirror_ovs.git/blame - lib/dynamic-string.h
hash: Replace primary hash functions by murmurhash.
[mirror_ovs.git] / lib / dynamic-string.h
CommitLineData
064af421 1/*
e0edde6f 2 * Copyright (c) 2008, 2009, 2010, 2011, 2012 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>
25#include "compiler.h"
26
27struct tm;
28
541bc79f
BP
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.
31 *
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. */
064af421
BP
36struct ds {
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. */
40};
41
42#define DS_EMPTY_INITIALIZER { NULL, 0, 0 }
43
44void ds_init(struct ds *);
45void ds_clear(struct ds *);
46void ds_truncate(struct ds *, size_t new_length);
47void ds_reserve(struct ds *, size_t min_length);
48char *ds_put_uninit(struct ds *, size_t n);
36c501fe 49static inline void ds_put_char(struct ds *, char);
f38b84ea 50void ds_put_utf8(struct ds *, int uc);
064af421
BP
51void ds_put_char_multiple(struct ds *, char, size_t n);
52void ds_put_buffer(struct ds *, const char *, size_t n);
53void ds_put_cstr(struct ds *, const char *);
1fd13cde 54void ds_put_and_free_cstr(struct ds *, char *);
064af421
BP
55void ds_put_format(struct ds *, const char *, ...) PRINTF_FORMAT(2, 3);
56void ds_put_format_valist(struct ds *, const char *, va_list)
57 PRINTF_FORMAT(2, 0);
58void ds_put_printable(struct ds *, const char *, size_t);
b5d29991 59void ds_put_strftime(struct ds *, const char *, bool utc)
064af421
BP
60 STRFTIME_FORMAT(2);
61void ds_put_hex_dump(struct ds *ds, const void *buf_, size_t size,
62 uintptr_t ofs, bool ascii);
63int ds_get_line(struct ds *, FILE *);
dd8101bc 64int ds_get_preprocessed_line(struct ds *, FILE *);
06d7ae7d 65int ds_get_test_line(struct ds *, FILE *);
064af421
BP
66
67char *ds_cstr(struct ds *);
5f98eed4 68const char *ds_cstr_ro(const struct ds *);
48718554 69char *ds_steal_cstr(struct ds *);
064af421 70void ds_destroy(struct ds *);
e83fd213 71void ds_swap(struct ds *, struct ds *);
064af421
BP
72
73int ds_last(const struct ds *);
74void ds_chomp(struct ds *, int c);
36c501fe
BP
75\f
76/* Inline functions. */
77
78void ds_put_char__(struct ds *, char);
79
80static inline void
81ds_put_char(struct ds *ds, char c)
82{
83 if (ds->length < ds->allocated) {
84 ds->string[ds->length++] = c;
85 ds->string[ds->length] = '\0';
86 } else {
87 ds_put_char__(ds, c);
88 }
89}
064af421
BP
90
91#endif /* dynamic-string.h */