API changes to WRBUF. wrbuf_free removed; replaced by wrbuf_destroy. And
[yaz-moved-to-github.git] / src / wrbuf.c
index f361f6b..ef2be4c 100644 (file)
@@ -1,8 +1,8 @@
 /*
- * Copyright (C) 1995-2005, Index Data ApS
+ * Copyright (C) 1995-2007, Index Data ApS
  * See the file LICENSE for details.
  *
- * $Id: wrbuf.c,v 1.11 2006-07-07 10:31:26 marc Exp $
+ * $Id: wrbuf.c,v 1.18 2007-03-19 14:40:07 adam Exp $
  */
 
 /**
@@ -20,6 +20,7 @@
 #include <stdarg.h>
 
 #include <yaz/wrbuf.h>
+#include <yaz/snprintf.h>
 #include <yaz/yaz-iconv.h>
 
 WRBUF wrbuf_alloc(void)
@@ -34,10 +35,9 @@ WRBUF wrbuf_alloc(void)
     return n;
 }
 
-void wrbuf_free(WRBUF b, int free_buf)
+void wrbuf_destroy(WRBUF b)
 {
-    if (free_buf && b->buf)
-        xfree(b->buf);
+    xfree(b->buf);
     xfree(b);
 }
 
@@ -76,23 +76,21 @@ int wrbuf_write(WRBUF b, const char *buf, int size)
 
 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;
 }
 
 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;
 }
 
@@ -101,7 +99,6 @@ void wrbuf_chop_right(WRBUF b)
     while (b->pos && b->buf[b->pos-1] == ' ')
     {
         (b->pos)--;
-        b->buf[b->pos] = '\0';
     }
 }
 
@@ -143,8 +140,6 @@ int wrbuf_xmlputs_n(WRBUF b, const char *cp, int size)
         }
         cp++;
     }
-    wrbuf_putc(b, 0);
-    (b->pos)--;
     return 0;
 }
 
@@ -154,16 +149,7 @@ void wrbuf_printf(WRBUF b, const char *fmt, ...)
     char buf[4096];
 
     va_start(ap, fmt);
-#ifdef WIN32
-    _vsnprintf(buf, sizeof(buf)-1, fmt, ap);
-#else
-/* !WIN32 */
-#if HAVE_VSNPRINTF
-    vsnprintf(buf, sizeof(buf)-1, fmt, ap);
-#else
-    vsprintf(buf, fmt, ap);
-#endif
-#endif
+    yaz_vsnprintf(buf, sizeof(buf)-1, fmt, ap);
     wrbuf_puts (b, buf);
 
     va_end(ap);
@@ -215,11 +201,32 @@ int wrbuf_iconv_puts(WRBUF b, yaz_iconv_t cd, const char *strz)
     return wrbuf_iconv_write(b, cd, strz, strlen(strz));
 }
 
+int wrbuf_iconv_putchar(WRBUF b, yaz_iconv_t cd, int ch)
+{
+    char buf[1];
+    buf[0] = ch;
+    return wrbuf_iconv_write(b, cd, buf, 1);
+}
+
 int wrbuf_iconv_write_cdata(WRBUF b, yaz_iconv_t cd, const char *buf, int size)
 {
     return wrbuf_iconv_write_x(b, cd, buf, size, 1);
 }
 
+const char *wrbuf_cstr(WRBUF b)
+{
+    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;
+}
+
 /*
  * Local variables:
  * c-basic-offset: 4