s3: Call printer_list_parent_init in the parent
[Samba/gbeck.git] / source3 / lib / cbuf.c
blobe9763ae403fc97cb09f3fd6fb95768c184b62883
1 /*
2 * Samba Unix/Linux SMB client library
4 * Copyright (C) Gregor Beck 2010
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 /**
21 * @file cbuf.c
22 * @author Gregor Beck <gb@sernet.de>
23 * @date Aug 2010
25 * @brief A talloced character buffer.
30 #include "includes.h"
31 #include "cbuf.h"
34 struct cbuf {
35 char* buf;
36 size_t pos;
37 size_t size;
41 cbuf* cbuf_clear(cbuf* b)
43 cbuf_setpos(b, 0);
44 return b;
47 cbuf* cbuf_new(const void* ctx)
49 cbuf* s = talloc(ctx, cbuf);
50 if (s == NULL)
51 return NULL;
52 s->size = 32;
53 s->buf = (char *)talloc_size(s, s->size);
54 if (s->size && (s->buf == NULL)) {
55 talloc_free(s);
56 return NULL;
58 return cbuf_clear(s);
61 cbuf* cbuf_copy(const cbuf* b)
63 cbuf* s = talloc(talloc_parent(b), cbuf);
64 if (s == NULL) {
65 return NULL;
68 s->buf = (char *)talloc_memdup(s, b->buf, b->size); /* only up to pos? */
70 /* XXX shallow did not work, because realloc */
71 /* fails with multiple references */
72 /* s->buf = talloc_reference(s, b->buf); */
74 if (s->buf == NULL) {
75 cbuf_delete(s);
76 return NULL;
78 s->size = b->size;
79 s->pos = b->pos;
80 return s;
83 void cbuf_delete(cbuf* b)
85 talloc_free(b);
88 #define SWAP(A,B,T) do { \
89 T tmp = A; A = B; B = tmp; \
90 } while(0)
93 void cbuf_swap(cbuf* b1, cbuf* b2)
95 if (b1 == b2) {
96 return;
98 talloc_reparent(b1, b2, b1->buf);
99 talloc_reparent(b2, b1, b2->buf);
100 SWAP(b1->buf, b2->buf, char*);
101 SWAP(b1->pos, b2->pos, size_t);
102 SWAP(b1->size, b2->size, size_t);
107 cbuf* cbuf_takeover(cbuf* b1, cbuf* b2)
109 talloc_reparent(b2, b1, b2->buf);
110 b1->buf = b2->buf;
111 b1->pos = b2->pos;
112 b1->size = b2->size;
113 cbuf_delete(b2);
114 return b1;
117 cbuf* cbuf_swapptr(cbuf* b, char** ptr, size_t len)
119 SWAP(b->buf, *ptr, char*);
120 talloc_steal(b, b->buf);
121 b->size = talloc_get_size(b->buf);
122 b->pos = (len == -1) ? strlen(b->buf) : len;
124 assert(b->pos <= b->size);
125 return b;
128 cbuf* cbuf_resize(cbuf* b, size_t size)
130 char* save_buf = b->buf;
131 b->buf = talloc_realloc(b, b->buf, char, size);
132 if (b->buf == NULL) {
133 talloc_free(save_buf);
134 b->size = 0;
135 } else {
136 b->size = size;
138 b->pos = MIN(b->pos, b->size);
139 return b->buf ? b : NULL;
142 char* cbuf_reserve(cbuf* b, size_t len)
144 if(b->size < b->pos + len)
145 cbuf_resize(b, MAX(2*b->size, b->pos + len));
146 return b->buf ? b->buf + b->pos : NULL;
149 int cbuf_puts(cbuf* b, const char* str, size_t len)
151 char* dst;
153 if (b == NULL)
154 return 0;
156 if (len == -1) {
157 len=strlen(str);
160 dst = cbuf_reserve(b, len+1);
161 if (dst == NULL)
162 return -1;
164 memcpy(dst, str, len);
165 dst[len] = '\0'; /* just to ease debugging */
167 b->pos += len;
168 assert(b->pos < b->size);
170 return len;
173 int cbuf_putc(cbuf* b, char c) {
174 char* dst;
176 if (b == NULL)
177 return 0;
179 dst = cbuf_reserve(b, 2);
180 if (dst == NULL) {
181 return -1;
184 dst[0] = c;
185 dst[1] = '\0'; /* just to ease debugging */
187 b->pos++;
188 assert(b->pos < b->size);
190 return 1;
193 int cbuf_putdw(cbuf* b, uint32_t u) {
194 char* dst;
195 static const size_t LEN = sizeof(uint32_t);
197 if (b == NULL)
198 return 0;
200 dst = cbuf_reserve(b, LEN);
201 if (dst == NULL) {
202 return -1;
205 SIVAL(dst, 0, u);
207 b->pos += LEN;
208 assert(b->pos <= b->size); /* no NULL termination*/
210 return LEN;
213 size_t cbuf_getpos(const cbuf* b) {
214 assert(b->pos <= b->size);
215 return b->pos;
218 void cbuf_setpos(cbuf* b, size_t pos) {
219 assert(pos <= b->size);
220 b->pos = pos;
221 if (pos < b->size)
222 b->buf[pos] = '\0'; /* just to ease debugging */
225 char* cbuf_gets(cbuf* b, size_t idx) {
226 assert(idx <= b->pos);
228 if (cbuf_reserve(b, 1) == NULL)
229 return NULL;
231 b->buf[b->pos] = '\0';
232 return b->buf + idx;
235 int cbuf_printf(cbuf* b, const char* fmt, ...)
237 va_list args, args2;
238 int len;
239 char* dst = b->buf + b->pos;
240 const int avail = b->size - b->pos;
241 assert(avail >= 0);
243 va_start(args, fmt);
244 va_copy(args2, args);
246 len = vsnprintf(dst, avail, fmt, args);
248 if (len >= avail) {
249 dst = cbuf_reserve(b, len+1);
250 len = (dst != NULL) ? vsnprintf(dst, len+1, fmt, args2) : -1;
253 if (len > 0) {
254 b->pos += len;
257 va_end(args);
258 va_end(args2);
259 assert(b->pos <= b->size);
261 return len;
264 int cbuf_print_quoted_string(cbuf* ost, const char* s)
266 int n = 1;
267 cbuf_putc(ost,'"');
269 while(true) {
270 switch (*s) {
271 case '\0':
272 cbuf_putc(ost, '"');
273 return n+1;
275 case '"':
276 case '\\':
277 cbuf_putc(ost, '\\');
278 n++;
279 /* no break */
280 default:
281 cbuf_putc(ost, *s);
282 n++;
284 s++;