Import from old repository commit 61ef2b42a9c4ba8e1600f15bb0236765edc2ad45.
[cascardo/ovs.git] / lib / dynamic-string.h
1 /*
2  * Copyright (c) 2008 Nicira Networks.
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
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);
42 void ds_put_char(struct ds *, char);
43 void ds_put_char_multiple(struct ds *, char, size_t n);
44 void ds_put_buffer(struct ds *, const char *, size_t n);
45 void ds_put_cstr(struct ds *, const char *);
46 void ds_put_format(struct ds *, const char *, ...) PRINTF_FORMAT(2, 3);
47 void ds_put_format_valist(struct ds *, const char *, va_list)
48     PRINTF_FORMAT(2, 0);
49 void ds_put_printable(struct ds *, const char *, size_t);
50 void ds_put_strftime(struct ds *, const char *, const struct tm *)
51     STRFTIME_FORMAT(2);
52 void ds_put_hex_dump(struct ds *ds, const void *buf_, size_t size,
53                      uintptr_t ofs, bool ascii);
54 int ds_get_line(struct ds *, FILE *);
55
56 char *ds_cstr(struct ds *);
57 void ds_destroy(struct ds *);
58
59 int ds_last(const struct ds *);
60 void ds_chomp(struct ds *, int c);
61
62 #endif /* dynamic-string.h */