-/*
- * Copyright (C) 1995-2007, Index Data ApS
+/* This file is part of the YAZ toolkit.
+ * Copyright (C) 1995-2008 Index Data
* See the file LICENSE for details.
- *
- * $Id: wrbuf.c,v 1.16 2007-02-23 10:15:01 adam Exp $
*/
/**
return n;
}
-void wrbuf_free(WRBUF b, int free_buf)
-{
- if (free_buf && b->buf)
- xfree(b->buf);
- xfree(b);
-}
-
void wrbuf_destroy(WRBUF b)
{
- wrbuf_free(b, 1);
+ xfree(b->buf);
+ xfree(b);
}
void wrbuf_rewind(WRBUF b)
int wrbuf_puts(WRBUF b, const char *buf)
{
- wrbuf_write(b, buf, strlen(buf)+1); /* '\0'-terminate as well */
- (b->pos)--; /* don't include '\0' in count */
+ wrbuf_write(b, buf, strlen(buf));
return 0;
}
+void wrbuf_vputs(const char *buf, void *client_data)
+{
+ wrbuf_write((WRBUF) client_data, buf, strlen(buf));
+}
+
int wrbuf_puts_replace_char(WRBUF b, const char *buf,
const char from, const char to)
{
- while(*buf){
+ while(*buf)
+ {
if (*buf == from)
wrbuf_putc(b, to);
else
wrbuf_putc(b, *buf);
buf++;
}
- wrbuf_putc(b, 0);
- (b->pos)--; /* don't include '\0' in count */
return 0;
}
while (b->pos && b->buf[b->pos-1] == ' ')
{
(b->pos)--;
- b->buf[b->pos] = '\0';
}
}
}
cp++;
}
- wrbuf_putc(b, 0);
- (b->pos)--;
return 0;
}
{
if (cd)
{
- char outbuf[12];
+ char outbuf[16];
size_t inbytesleft = size;
const char *inp = buf;
while (inbytesleft)
return wrbuf_iconv_write_x(b, cd, buf, size, 1);
}
+int wrbuf_iconv_puts_cdata(WRBUF b, yaz_iconv_t cd, const char *strz)
+{
+ return wrbuf_iconv_write_x(b, cd, strz, strlen(strz), 1);
+}
+
+void wrbuf_iconv_reset(WRBUF b, yaz_iconv_t cd)
+{
+ if (cd)
+ {
+ char outbuf[16];
+ size_t outbytesleft = sizeof(outbuf);
+ char *outp = outbuf;
+ size_t r = yaz_iconv(cd, 0, 0, &outp, &outbytesleft);
+ if (r != (size_t) (-1))
+ wrbuf_write(b, outbuf, outp - outbuf);
+ }
+}
+
const char *wrbuf_cstr(WRBUF b)
{
- wrbuf_write(b, "", 1); /* '\0'-terminate as well */
- (b->pos)--; /* don't include '\0' in count */
+ wrbuf_putc(b, '\0'); /* add '\0' */
+ (b->pos)--; /* don't include '\0' in count */
return b->buf;
}
+void wrbuf_cut_right(WRBUF b, size_t no_to_remove)
+{
+ if (no_to_remove > b->pos)
+ no_to_remove = b->pos;
+ b->pos = b->pos - no_to_remove;
+}
+
+void wrbuf_puts_escaped(WRBUF b, const char *str)
+{
+ wrbuf_write_escaped(b, str, strlen(str));
+}
+
+void wrbuf_write_escaped(WRBUF b, const char *str, size_t len)
+{
+ size_t i;
+ for (i = 0; i < len; i++)
+ if (str[i] < ' ' || str[i] > 126)
+ wrbuf_printf(b, "\\x%02X", str[i] & 0xff);
+ else
+ wrbuf_putc(b, str[i]);
+}
+
/*
* Local variables:
* c-basic-offset: 4