YAZ  5.23.1
wrbuf.c
Go to the documentation of this file.
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) Index Data
3  * See the file LICENSE for details.
4  */
5 
11 #if HAVE_CONFIG_H
12 #include <config.h>
13 #endif
14 
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <stdarg.h>
19 #include <assert.h>
20 
21 #include <yaz/wrbuf.h>
22 #include <yaz/snprintf.h>
23 #include <yaz/yaz-iconv.h>
24 
26 {
27  WRBUF n;
28 
29  if (!(n = (WRBUF)xmalloc(sizeof(*n))))
30  abort();
31  n->buf = 0;
32  n->size = 0;
33  n->pos = 0;
34  wrbuf_grow(n, 1);
35  return n;
36 }
37 
39 {
40  if (b)
41  {
42  xfree(b->buf);
43  xfree(b);
44  }
45 }
46 
48 {
49  b->pos = 0;
50 }
51 
52 int wrbuf_grow(WRBUF b, size_t minsize)
53 {
54  size_t togrow;
55 
56  if (!b->size)
57  togrow = 1024;
58  else
59  togrow = b->size;
60  if (togrow < minsize)
61  togrow = minsize;
62  b->buf = (char *) xrealloc(b->buf, 1 + (b->size += togrow));
63  if (!b->buf)
64  abort();
65  return 0;
66 }
67 
68 void wrbuf_write(WRBUF b, const char *buf, size_t size)
69 {
70  if (size <= 0)
71  return;
72  if (b->pos + size >= b->size)
73  wrbuf_grow(b, size);
74  memcpy(b->buf + b->pos, buf, size);
75  b->pos += size;
76 }
77 
78 void wrbuf_insert(WRBUF b, size_t pos, const char *buf, size_t size)
79 {
80  if (size <= 0 || pos > b->pos)
81  return;
82  if (b->pos + size >= b->size)
83  wrbuf_grow(b, size);
84  memmove(b->buf + pos + size, b->buf + pos, b->pos - pos);
85  memcpy(b->buf + pos, buf, size);
86  b->pos += size;
87 }
88 
89 void wrbuf_puts(WRBUF b, const char *buf)
90 {
91  wrbuf_write(b, buf, strlen(buf));
92 }
93 
94 void wrbuf_vp_puts(const char *buf, void *client_data)
95 {
96  WRBUF b = (WRBUF) client_data;
97  wrbuf_puts(b, buf);
98 }
99 
100 void wrbuf_puts_replace_char(WRBUF b, const char *buf,
101  const char from, const char to)
102 {
103  while(*buf)
104  {
105  if (*buf == from)
106  wrbuf_putc(b, to);
107  else
108  wrbuf_putc(b, *buf);
109  buf++;
110  }
111 }
112 
113 void wrbuf_puts_replace_str(WRBUF b, const char *buf,
114  const char *from, const char *to)
115 {
116  const char *cp0 = buf;
117  for (;;)
118  {
119  const char *cp1 = strstr(cp0, from);
120  if (!cp1)
121  break;
122  if (cp1 != cp0)
123  wrbuf_write(b, cp0, cp1 - cp0);
124  wrbuf_puts(b, to);
125  cp0 = cp1 + strlen(from);
126  }
127  wrbuf_puts(b, cp0);
128 }
129 
131 {
132  while (b->pos && b->buf[b->pos-1] == ' ')
133  {
134  (b->pos)--;
135  }
136 }
137 
138 void wrbuf_xmlputs(WRBUF b, const char *cp)
139 {
140  wrbuf_xmlputs_n(b, cp, strlen(cp));
141 }
142 
143 void wrbuf_xmlputs_n(WRBUF b, const char *cp, size_t size)
144 {
145  for (; size; size--)
146  {
147  /* only TAB,CR,LF of ASCII CTRL are allowed in XML 1.0! */
148  if (*cp >= 0 && *cp <= 31)
149  if (*cp != 9 && *cp != 10 && *cp != 13)
150  {
151  cp++; /* we silently ignore (delete) these.. */
152  continue;
153  }
154  switch(*cp)
155  {
156  case '<':
157  wrbuf_puts(b, "&lt;");
158  break;
159  case '>':
160  wrbuf_puts(b, "&gt;");
161  break;
162  case '&':
163  wrbuf_puts(b, "&amp;");
164  break;
165  case '"':
166  wrbuf_puts(b, "&quot;");
167  break;
168  case '\'':
169  wrbuf_puts(b, "&apos;");
170  break;
171  default:
172  wrbuf_putc(b, *cp);
173  }
174  cp++;
175  }
176 }
177 
178 void wrbuf_printf(WRBUF b, const char *fmt, ...)
179 {
180  va_list ap;
181  char buf[4096];
182 
183  va_start(ap, fmt);
184  yaz_vsnprintf(buf, sizeof(buf)-1, fmt, ap);
185  wrbuf_puts (b, buf);
186 
187  va_end(ap);
188 }
189 
190 int wrbuf_iconv_write2(WRBUF b, yaz_iconv_t cd, const char *buf,
191  size_t size,
192  void (*wfunc)(WRBUF, const char *, size_t))
193 {
194  int ret = 0;
195  if (cd)
196  {
197  char outbuf[128];
198  size_t inbytesleft = size;
199  const char *inp = buf;
200  while (inbytesleft)
201  {
202  size_t outbytesleft = sizeof(outbuf);
203  char *outp = outbuf;
204  size_t r = yaz_iconv(cd, (char**) &inp, &inbytesleft,
205  &outp, &outbytesleft);
206  if (r == (size_t) (-1))
207  {
208  int e = yaz_iconv_error(cd);
209  if (e != YAZ_ICONV_E2BIG)
210  {
211  ret = -1;
212  break;
213  }
214  }
215  (*wfunc)(b, outbuf, outp - outbuf);
216  }
217  }
218  else
219  (*wfunc)(b, buf, size);
220  return ret;
221 }
222 
223 int wrbuf_iconv_write_x(WRBUF b, yaz_iconv_t cd, const char *buf,
224  size_t size, int cdata)
225 {
226  return wrbuf_iconv_write2(b, cd, buf, size,
227  cdata ? wrbuf_xmlputs_n : wrbuf_write);
228 }
229 
230 void wrbuf_iconv_write(WRBUF b, yaz_iconv_t cd, const char *buf, size_t size)
231 {
232  wrbuf_iconv_write2(b, cd, buf, size, wrbuf_write);
233 }
234 
235 void wrbuf_iconv_puts(WRBUF b, yaz_iconv_t cd, const char *strz)
236 {
237  wrbuf_iconv_write(b, cd, strz, strlen(strz));
238 }
239 
241 {
242  char buf[1];
243  buf[0] = ch;
244  wrbuf_iconv_write(b, cd, buf, 1);
245 }
246 
247 void wrbuf_iconv_write_cdata(WRBUF b, yaz_iconv_t cd, const char *buf, size_t size)
248 {
249  wrbuf_iconv_write2(b, cd, buf, size, wrbuf_xmlputs_n);
250 }
251 
252 void wrbuf_iconv_puts_cdata(WRBUF b, yaz_iconv_t cd, const char *strz)
253 {
254  wrbuf_iconv_write2(b, cd, strz, strlen(strz), wrbuf_xmlputs_n);
255 }
256 
258  const char *buf, size_t size)
259 {
260  wrbuf_iconv_write2(b, cd, buf, size, wrbuf_json_write);
261 }
262 
263 void wrbuf_iconv_json_puts(WRBUF b, yaz_iconv_t cd, const char *strz)
264 {
265  wrbuf_iconv_write2(b, cd, strz, strlen(strz), wrbuf_json_write);
266 }
267 
269 {
270  if (cd)
271  {
272  char outbuf[16];
273  size_t outbytesleft = sizeof(outbuf);
274  char *outp = outbuf;
275  size_t r = yaz_iconv(cd, 0, 0, &outp, &outbytesleft);
276  if (r != (size_t) (-1))
277  wrbuf_write(b, outbuf, outp - outbuf);
278  }
279 }
280 
281 const char *wrbuf_cstr(WRBUF b)
282 {
283  assert(b && b->pos <= b->size);
284  b->buf[b->pos] = '\0';
285  return b->buf;
286 }
287 
288 const char *wrbuf_cstr_null(WRBUF b)
289 {
290  if (!b || b->pos == 0)
291  return 0;
292  assert(b->pos <= b->size);
293  b->buf[b->pos] = '\0';
294  return b->buf;
295 }
296 
297 void wrbuf_cut_right(WRBUF b, size_t no_to_remove)
298 {
299  if (no_to_remove > b->pos)
300  no_to_remove = b->pos;
301  b->pos = b->pos - no_to_remove;
302 }
303 
304 void wrbuf_puts_escaped(WRBUF b, const char *str)
305 {
306  wrbuf_write_escaped(b, str, strlen(str));
307 }
308 
309 void wrbuf_write_escaped(WRBUF b, const char *str, size_t len)
310 {
311  size_t i;
312  for (i = 0; i < len; i++)
313  if (str[i] < ' ' || str[i] > 126)
314  wrbuf_printf(b, "\\x%02X", str[i] & 0xff);
315  else
316  wrbuf_putc(b, str[i]);
317 }
318 
319 void wrbuf_json_write(WRBUF b, const char *cp, size_t sz)
320 {
321  size_t i;
322  for (i = 0; i < sz; i++)
323  {
324  if (cp[i] > 0 && cp[i] < 32)
325  {
326  wrbuf_putc(b, '\\');
327  switch (cp[i])
328  {
329  case '\b': wrbuf_putc(b, 'b'); break;
330  case '\f': wrbuf_putc(b, 'f'); break;
331  case '\n': wrbuf_putc(b, 'n'); break;
332  case '\r': wrbuf_putc(b, 'r'); break;
333  case '\t': wrbuf_putc(b, 't'); break;
334  default:
335  wrbuf_printf(b, "u%04x", cp[i]);
336  }
337  }
338  else if (cp[i] == '"')
339  {
340  wrbuf_putc(b, '\\'); wrbuf_putc(b, '"');
341  }
342  else if (cp[i] == '\\')
343  {
344  wrbuf_putc(b, '\\'); wrbuf_putc(b, '\\');
345  }
346  else
347  { /* leave encoding as raw UTF-8 */
348  wrbuf_putc(b, cp[i]);
349  }
350  }
351 
352 }
353 
354 void wrbuf_json_puts(WRBUF b, const char *str)
355 {
356  wrbuf_json_write(b, str, strlen(str));
357 }
358 
359 /*
360  * Local variables:
361  * c-basic-offset: 4
362  * c-file-style: "Stroustrup"
363  * indent-tabs-mode: nil
364  * End:
365  * vim: shiftwidth=4 tabstop=8 expandtab
366  */
367 
void yaz_vsnprintf(char *buf, size_t size, const char *fmt, va_list ap)
Definition: snprintf.c:17
int wrbuf_iconv_write_x(WRBUF b, yaz_iconv_t cd, const char *buf, size_t size, int cdata)
writer of string using iconv and cdata
Definition: wrbuf.c:223
void wrbuf_iconv_puts_cdata(WRBUF b, yaz_iconv_t cd, const char *strz)
iconv converts C-string and appends to WRBUF as XML CDATA
Definition: wrbuf.c:252
#define xrealloc(o, x)
utility macro which calls xrealloc_f
Definition: xmalloc.h:47
void wrbuf_json_puts(WRBUF b, const char *str)
writes JSON text to WRBUF with escaping
Definition: wrbuf.c:354
Header for WRBUF (growing buffer)
void wrbuf_json_write(WRBUF b, const char *cp, size_t sz)
writes JSON text to WRBUF with escaping
Definition: wrbuf.c:319
const char * wrbuf_cstr(WRBUF b)
returns WRBUF content as C-string
Definition: wrbuf.c:281
int yaz_iconv_error(yaz_iconv_t cd)
returns last error - like errno for iconv(3)
Definition: siconv.c:279
void wrbuf_puts(WRBUF b, const char *buf)
appends C-string to WRBUF
Definition: wrbuf.c:89
void wrbuf_write(WRBUF b, const char *buf, size_t size)
append constant size buffer to WRBUF
Definition: wrbuf.c:68
string buffer
Definition: wrbuf.h:42
void wrbuf_insert(WRBUF b, size_t pos, const char *buf, size_t size)
inserts buffer into WRBUF at some position
Definition: wrbuf.c:78
void wrbuf_cut_right(WRBUF b, size_t no_to_remove)
cut size of WRBUF
Definition: wrbuf.c:297
void wrbuf_rewind(WRBUF b)
empty WRBUF content (length of buffer set to 0)
Definition: wrbuf.c:47
void wrbuf_xmlputs_n(WRBUF b, const char *cp, size_t size)
writes buffer of certain size to WRBUF and XML encode (as CDATA)
Definition: wrbuf.c:143
void wrbuf_iconv_json_write(WRBUF b, yaz_iconv_t cd, const char *buf, size_t size)
Definition: wrbuf.c:257
size_t pos
Definition: wrbuf.h:45
#define xfree(x)
utility macro which calls xfree_f
Definition: xmalloc.h:53
void wrbuf_printf(WRBUF b, const char *fmt,...)
writes printf result to WRBUF
Definition: wrbuf.c:178
size_t size
Definition: wrbuf.h:46
void wrbuf_destroy(WRBUF b)
destroy WRBUF and its buffer
Definition: wrbuf.c:38
void wrbuf_iconv_json_puts(WRBUF b, yaz_iconv_t cd, const char *strz)
Definition: wrbuf.c:263
void wrbuf_puts_replace_char(WRBUF b, const char *buf, const char from, const char to)
puts buf to WRBUF and replaces a single char
Definition: wrbuf.c:100
void wrbuf_vp_puts(const char *buf, void *client_data)
appends C-string to WRBUF - void pointer variant
Definition: wrbuf.c:94
size_t yaz_iconv(yaz_iconv_t cd, char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft)
just like iconv(3)
Definition: siconv.c:146
int wrbuf_iconv_write2(WRBUF b, yaz_iconv_t cd, const char *buf, size_t size, void(*wfunc)(WRBUF, const char *, size_t))
General writer of string using iconv and cdata.
Definition: wrbuf.c:190
const char * wrbuf_cstr_null(WRBUF b)
returns WRBUF content as C-string or NULL
Definition: wrbuf.c:288
void wrbuf_write_escaped(WRBUF b, const char *str, size_t len)
writes buffer to WRBUF and escape non-ASCII characters
Definition: wrbuf.c:309
void wrbuf_chop_right(WRBUF b)
chips traling blanks away from WRBUF
Definition: wrbuf.c:130
int wrbuf_grow(WRBUF b, size_t minsize)
grow WRBUF larger
Definition: wrbuf.c:52
void wrbuf_iconv_write(WRBUF b, yaz_iconv_t cd, const char *buf, size_t size)
Converts buffer using iconv and appends to WRBUF.
Definition: wrbuf.c:230
void wrbuf_iconv_reset(WRBUF b, yaz_iconv_t cd)
iconv reset(flush) to WRBUF
Definition: wrbuf.c:268
#define wrbuf_putc(b, c)
Definition: wrbuf.h:268
#define xmalloc(x)
utility macro which calls malloc_f
Definition: xmalloc.h:49
void wrbuf_puts_replace_str(WRBUF b, const char *buf, const char *from, const char *to)
puts buf to WRBUF and replaces a string with another
Definition: wrbuf.c:113
Header for YAZ iconv interface.
void wrbuf_iconv_puts(WRBUF b, yaz_iconv_t cd, const char *strz)
iconv converts C-string and appends to WRBUF
Definition: wrbuf.c:235
void wrbuf_iconv_write_cdata(WRBUF b, yaz_iconv_t cd, const char *buf, size_t size)
Converts buffer using iconv and appends to WRBUF as XML CDATA.
Definition: wrbuf.c:247
#define YAZ_ICONV_E2BIG
error code: Not sufficient room for output buffer
Definition: yaz-iconv.h:47
void wrbuf_xmlputs(WRBUF b, const char *cp)
writes C-String to WRBUF and XML encode (as CDATA)
Definition: wrbuf.c:138
struct wrbuf * WRBUF
char * buf
Definition: wrbuf.h:44
void wrbuf_iconv_putchar(WRBUF b, yaz_iconv_t cd, int ch)
iconv converts character and appends to WRBUF
Definition: wrbuf.c:240
WRBUF wrbuf_alloc(void)
construct WRBUF
Definition: wrbuf.c:25
void wrbuf_puts_escaped(WRBUF b, const char *str)
writes C-string to WRBUF and escape non-ASCII characters
Definition: wrbuf.c:304
Header for config file reading utilities.