strbuf.h
Go to the documentation of this file.
1 /* strbuf - String buffer routines
2  *
3  * Copyright (c) 2010-2012 Mark Pulford <[email protected]>
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 /* Workaround for MSVC */
29 #ifdef _MSC_VER
30 #define inline __inline
31 #endif
32 
33 /* Size: Total bytes allocated to *buf
34  * Length: String length, excluding optional NULL terminator.
35  * Increment: Allocation increments when resizing the string buffer.
36  * Dynamic: True if created via strbuf_new()
37  */
38 
39 typedef struct {
40  char *buf;
41  int size;
42  int length;
43  int increment;
44  int dynamic;
45  int reallocs;
46  int debug;
47 } strbuf_t;
48 
49 #ifndef STRBUF_DEFAULT_SIZE
50 #define STRBUF_DEFAULT_SIZE 1023
51 #endif
52 #ifndef STRBUF_DEFAULT_INCREMENT
53 #define STRBUF_DEFAULT_INCREMENT -2
54 #endif
55 
56 /* Initialise */
57 extern strbuf_t *strbuf_new(int len);
58 extern void strbuf_init(strbuf_t *s, int len);
59 extern void strbuf_set_increment(strbuf_t *s, int increment);
60 
61 /* Release */
62 extern void strbuf_free(strbuf_t *s);
63 extern char *strbuf_free_to_string(strbuf_t *s, int *len);
64 
65 /* Management */
66 extern void strbuf_resize(strbuf_t *s, int len);
67 static int strbuf_empty_length(strbuf_t *s);
68 static int strbuf_length(strbuf_t *s);
69 static char *strbuf_string(strbuf_t *s, int *len);
70 static void strbuf_ensure_empty_length(strbuf_t *s, int len);
71 static char *strbuf_empty_ptr(strbuf_t *s);
72 static void strbuf_extend_length(strbuf_t *s, int len);
73 
74 /* Update */
75 extern void strbuf_append_fmt(strbuf_t *s, int len, const char *fmt, ...);
76 extern void strbuf_append_fmt_retry(strbuf_t *s, const char *format, ...);
77 static void strbuf_append_mem(strbuf_t *s, const char *c, int len);
78 extern void strbuf_append_string(strbuf_t *s, const char *str);
79 static void strbuf_append_char(strbuf_t *s, const char c);
80 static void strbuf_ensure_null(strbuf_t *s);
81 
82 /* Reset string for before use */
83 static inline void strbuf_reset(strbuf_t *s)
84 {
85  s->length = 0;
86 }
87 
88 static inline int strbuf_allocated(strbuf_t *s)
89 {
90  return s->buf != NULL;
91 }
92 
93 /* Return bytes remaining in the string buffer
94  * Ensure there is space for a NULL terminator. */
95 static inline int strbuf_empty_length(strbuf_t *s)
96 {
97  return s->size - s->length - 1;
98 }
99 
100 static inline void strbuf_ensure_empty_length(strbuf_t *s, int len)
101 {
102  if (len > strbuf_empty_length(s))
103  strbuf_resize(s, s->length + len);
104 }
105 
106 static inline char *strbuf_empty_ptr(strbuf_t *s)
107 {
108  return s->buf + s->length;
109 }
110 
111 static inline void strbuf_extend_length(strbuf_t *s, int len)
112 {
113  s->length += len;
114 }
115 
116 static inline int strbuf_length(strbuf_t *s)
117 {
118  return s->length;
119 }
120 
121 static inline void strbuf_append_char(strbuf_t *s, const char c)
122 {
123  strbuf_ensure_empty_length(s, 1);
124  s->buf[s->length++] = c;
125 }
126 
127 static inline void strbuf_append_char_unsafe(strbuf_t *s, const char c)
128 {
129  s->buf[s->length++] = c;
130 }
131 
132 static inline void strbuf_append_mem(strbuf_t *s, const char *c, int len)
133 {
134  strbuf_ensure_empty_length(s, len);
135  memcpy(s->buf + s->length, c, len);
136  s->length += len;
137 }
138 
139 static inline void strbuf_append_mem_unsafe(strbuf_t *s, const char *c, int len)
140 {
141  memcpy(s->buf + s->length, c, len);
142  s->length += len;
143 }
144 
145 static inline void strbuf_ensure_null(strbuf_t *s)
146 {
147  s->buf[s->length] = 0;
148 }
149 
150 static inline char *strbuf_string(strbuf_t *s, int *len)
151 {
152  if (len)
153  *len = s->length;
154 
155  return s->buf;
156 }
157 
158 /* vi:ai et sw=4 ts=4:
159  */
strbuf_free
void strbuf_free(strbuf_t *s)
Definition: strbuf.c:104
strbuf_set_increment
void strbuf_set_increment(strbuf_t *s, int increment)
Definition: strbuf.c:84
strbuf_t::buf
char * buf
Definition: strbuf.h:40
strbuf_t::dynamic
int dynamic
Definition: strbuf.h:44
strbuf_t::size
int size
Definition: strbuf.h:41
strbuf_t::reallocs
int reallocs
Definition: strbuf.h:45
strbuf_append_string
void strbuf_append_string(strbuf_t *s, const char *str)
Definition: strbuf.c:182
strbuf_resize
void strbuf_resize(strbuf_t *s, int len)
Definition: strbuf.c:164
strbuf_append_fmt_retry
void strbuf_append_fmt_retry(strbuf_t *s, const char *format,...)
Definition: strbuf.c:221
strbuf_t::length
int length
Definition: strbuf.h:42
strbuf_t
Definition: strbuf.h:39
s
char_u * s
Definition: eval.c:764
strbuf_new
strbuf_t * strbuf_new(int len)
Definition: strbuf.c:68
strbuf_t::debug
int debug
Definition: strbuf.h:46
strbuf_init
void strbuf_init(strbuf_t *s, int len)
Definition: strbuf.c:44
strbuf_t::increment
int increment
Definition: strbuf.h:43
stdlib.h
NULL
return NULL
Definition: eval.c:10355
strbuf_free_to_string
char * strbuf_free_to_string(strbuf_t *s, int *len)
Definition: strbuf.c:116
strbuf_append_fmt
void strbuf_append_fmt(strbuf_t *s, int len, const char *fmt,...)
Definition: strbuf.c:202
len
int len
Definition: helpers.c:1034