]> git.saurik.com Git - redis.git/blame - deps/lua/src/strbuf.h
lua_cmsgpack.c added
[redis.git] / deps / lua / src / strbuf.h
CommitLineData
15108778 1/* strbuf - String buffer routines
2 *
3 * Copyright (c) 2010-2011 Mark Pulford <mark@kyne.com.au>
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25#include <stdlib.h>
26#include <stdarg.h>
27
28/* Size: Total bytes allocated to *buf
29 * Length: String length, excluding optional NULL terminator.
30 * Increment: Allocation increments when resizing the string buffer.
31 * Dynamic: True if created via strbuf_new()
32 */
33
34typedef struct {
35 char *buf;
36 int size;
37 int length;
38 int increment;
39 int dynamic;
40 int reallocs;
41 int debug;
42} strbuf_t;
43
44#ifndef STRBUF_DEFAULT_SIZE
45#define STRBUF_DEFAULT_SIZE 1023
46#endif
47#ifndef STRBUF_DEFAULT_INCREMENT
48#define STRBUF_DEFAULT_INCREMENT -2
49#endif
50
51/* Initialise */
52extern strbuf_t *strbuf_new(int len);
53extern void strbuf_init(strbuf_t *s, int len);
54extern void strbuf_set_increment(strbuf_t *s, int increment);
55
56/* Release */
57extern void strbuf_free(strbuf_t *s);
58extern char *strbuf_free_to_string(strbuf_t *s, int *len);
59
60/* Management */
61extern void strbuf_resize(strbuf_t *s, int len);
62static int strbuf_empty_length(strbuf_t *s);
63static int strbuf_length(strbuf_t *s);
64static char *strbuf_string(strbuf_t *s, int *len);
65static void strbuf_ensure_empty_length(strbuf_t *s, int len);
66
67/* Update */
68extern void strbuf_append_fmt(strbuf_t *s, int len, const char *fmt, ...);
69extern void strbuf_append_fmt_retry(strbuf_t *s, const char *format, ...);
70static void strbuf_append_mem(strbuf_t *s, const char *c, int len);
71extern void strbuf_append_string(strbuf_t *s, const char *str);
72static void strbuf_append_char(strbuf_t *s, const char c);
73static void strbuf_ensure_null(strbuf_t *s);
74
75/* Reset string for before use */
76static inline void strbuf_reset(strbuf_t *s)
77{
78 s->length = 0;
79}
80
81static inline int strbuf_allocated(strbuf_t *s)
82{
83 return s->buf != NULL;
84}
85
86/* Return bytes remaining in the string buffer
87 * Ensure there is space for a NULL terminator. */
88static inline int strbuf_empty_length(strbuf_t *s)
89{
90 return s->size - s->length - 1;
91}
92
93static inline void strbuf_ensure_empty_length(strbuf_t *s, int len)
94{
95 if (len > strbuf_empty_length(s))
96 strbuf_resize(s, s->length + len);
97}
98
99static inline int strbuf_length(strbuf_t *s)
100{
101 return s->length;
102}
103
104static inline void strbuf_append_char(strbuf_t *s, const char c)
105{
106 strbuf_ensure_empty_length(s, 1);
107 s->buf[s->length++] = c;
108}
109
110static inline void strbuf_append_char_unsafe(strbuf_t *s, const char c)
111{
112 s->buf[s->length++] = c;
113}
114
115static inline void strbuf_append_mem(strbuf_t *s, const char *c, int len)
116{
117 strbuf_ensure_empty_length(s, len);
118 memcpy(s->buf + s->length, c, len);
119 s->length += len;
120}
121
122static inline void strbuf_append_mem_unsafe(strbuf_t *s, const char *c, int len)
123{
124 memcpy(s->buf + s->length, c, len);
125 s->length += len;
126}
127
128static inline void strbuf_ensure_null(strbuf_t *s)
129{
130 s->buf[s->length] = 0;
131}
132
133static inline char *strbuf_string(strbuf_t *s, int *len)
134{
135 if (len)
136 *len = s->length;
137
138 return s->buf;
139}
140
141/* vi:ai et sw=4 ts=4:
142 */