Line data Source code
1 : /*
2 : * lwan - web server
3 : * Copyright (c) 2012 L. A. F. Pereira <l@tia.mat.br>
4 : *
5 : * This program is free software; you can redistribute it and/or
6 : * modify it under the terms of the GNU General Public License
7 : * as published by the Free Software Foundation; either version 2
8 : * of the License, or any later version.
9 : *
10 : * This program is distributed in the hope that it will be useful,
11 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 : * GNU General Public License for more details.
14 : *
15 : * You should have received a copy of the GNU General Public License
16 : * along with this program; if not, write to the Free Software
17 : * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
18 : * USA.
19 : */
20 :
21 : #pragma once
22 :
23 : #include <stdarg.h>
24 : #include <stdbool.h>
25 : #include <stdio.h>
26 : #include <string.h>
27 :
28 : struct lwan_strbuf {
29 : char *buffer;
30 :
31 : /* `capacity` used to be derived from `used` by aligning it to the next
32 : * power of two, but this resulted in re-allocations after this strbuf
33 : * been reset between requests. It now always contains the capacity
34 : * allocated by `buffer`; resetting essentially only resets `used` and
35 : * writes `\0` to buffer[0]. */
36 : size_t capacity, used;
37 :
38 : unsigned int flags;
39 : };
40 :
41 : #define LWAN_STRBUF_STATIC_INIT \
42 : (struct lwan_strbuf) { .buffer = "" }
43 :
44 : bool lwan_strbuf_init_with_fixed_buffer(struct lwan_strbuf *buf,
45 : void *buffer,
46 : size_t size);
47 : bool lwan_strbuf_init_with_size(struct lwan_strbuf *buf, size_t size);
48 : bool lwan_strbuf_init(struct lwan_strbuf *buf);
49 : struct lwan_strbuf *lwan_strbuf_new_static(const char *str, size_t size);
50 : struct lwan_strbuf *lwan_strbuf_new_with_size(size_t size);
51 : struct lwan_strbuf *lwan_strbuf_new_with_fixed_buffer(size_t size);
52 : struct lwan_strbuf *lwan_strbuf_new(void);
53 : void lwan_strbuf_free(struct lwan_strbuf *s);
54 :
55 : void lwan_strbuf_reset(struct lwan_strbuf *s);
56 : void lwan_strbuf_reset_trim(struct lwan_strbuf *s, size_t trim_thresh);
57 :
58 : bool lwan_strbuf_append_char(struct lwan_strbuf *s, const char c);
59 :
60 : bool lwan_strbuf_append_str(struct lwan_strbuf *s1, const char *s2, size_t sz);
61 609 : static inline bool lwan_strbuf_append_strz(struct lwan_strbuf *s1,
62 : const char *s2)
63 : {
64 609 : return lwan_strbuf_append_str(s1, s2, strlen(s2));
65 : }
66 :
67 : bool lwan_strbuf_set_static(struct lwan_strbuf *s1, const char *s2, size_t sz);
68 3 : static inline bool lwan_strbuf_set_staticz(struct lwan_strbuf *s1,
69 : const char *s2)
70 : {
71 3 : return lwan_strbuf_set_static(s1, s2, strlen(s2));
72 : }
73 :
74 : bool lwan_strbuf_set(struct lwan_strbuf *s1, const char *s2, size_t sz);
75 : static inline bool lwan_strbuf_setz(struct lwan_strbuf *s1, const char *s2)
76 : {
77 : return lwan_strbuf_set(s1, s2, strlen(s2));
78 : }
79 :
80 : bool lwan_strbuf_append_printf(struct lwan_strbuf *s, const char *fmt, ...)
81 : __attribute__((format(printf, 2, 3)));
82 : bool lwan_strbuf_append_vprintf(struct lwan_strbuf *s,
83 : const char *fmt,
84 : va_list v);
85 :
86 : bool lwan_strbuf_printf(struct lwan_strbuf *s1, const char *fmt, ...)
87 : __attribute__((format(printf, 2, 3)));
88 : bool lwan_strbuf_vprintf(struct lwan_strbuf *s1, const char *fmt, va_list ap);
89 :
90 : bool lwan_strbuf_grow_to(struct lwan_strbuf *s, size_t new_size);
91 : bool lwan_strbuf_grow_by(struct lwan_strbuf *s, size_t offset);
92 :
93 9275 : static inline size_t lwan_strbuf_get_length(const struct lwan_strbuf *s)
94 : {
95 9275 : return s->used;
96 : }
97 :
98 20544 : static inline char *lwan_strbuf_get_buffer(const struct lwan_strbuf *s)
99 : {
100 20544 : return s->buffer;
101 : }
102 :
103 : bool lwan_strbuf_init_from_file(struct lwan_strbuf *s, const char *path);
104 : struct lwan_strbuf *lwan_strbuf_new_from_file(const char *path);
|