diff th_string.c @ 126:9ae3d87a686f

Sync.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 29 Oct 2010 19:54:42 +0300
parents fe4d5f3b486c
children f741718d13c5
line wrap: on
line diff
--- a/th_string.c	Fri Oct 29 16:59:35 2010 +0300
+++ b/th_string.c	Fri Oct 29 19:54:42 2010 +0300
@@ -10,10 +10,6 @@
 #endif
 #include "th_string.h"
 
-#define LPREV (pNode->pPrev)
-#define LNEXT (pNode->pNext)
-
-
 /* strdup with a NULL check
  */
 char *th_strdup(const char *s)
@@ -58,114 +54,32 @@
 }
 
 
-#ifdef STRDUP_PRINTF
-/*
+/* Simulate a sprintf() that allocates memory
  */
-enum {
-    TH_PAD_RIGHT = 1,
-    TH_PAD_ZERO = 2
-};
-
-
-static size_t th_printbuf_str(char **buf, const char *str, size_t width, int flags)
-{
-    size_t len = strlen(str);
-    char *out = (buf != NULL) ? *buf : NULL;
-    char pad = ' ';
-
-    if (width > 0) {
-        width = (len >= width) ? 0 : width - len;
-        if (flags & TH_PAD_ZERO)
-            pad = '0';
-    }
-
-    if ((flags & TH_PAD_RIGHT) == 0 && out != NULL) {
-        while (width-- > 0)
-            *out++ = pad;
-    }
-
-    if (out != NULL) {
-        while (*str)
-            *out++ = *str++;
-    }
-
-    if (flags & TH_PAD_RIGHT && out != NULL) {
-        while (width-- > 0)
-            *out++ = pad;
-    }
-
-    if (buf != NULL)
-        *buf = out;
-
-    return len + width;
-}
-
-#define TH_INTBUF_LEN (32)
-
-static size_t th_printbuf_int(char **buf, int i, int b, int sg, int width, int pad, int letbase)
-{
-    char tmpbuf[TH_INTBUF_LEN], *s;
-    int t, neg = 0, pc = 0;
-    unsigned int u = i;
-
-    if (i == 0) {
-        tmpbuf[0] = '0';
-        tmpbuf[1] = 0;
-        return th_printbuf_str(buf, tmpbuf, width, pad);
-    }
-
-    if (sg && b == 10 && i < 0) {
-        neg = 1;
-        u = -i;
-    }
-    
-    s = tmpbuf + TH_INTBUF_LEN - 1;
-    *s = 0;
-
-    while (u) {
-        t = u % b;
-        if (t >= 10)
-            t += letbase - '0' - 10;
-        
-        *--s = t + '0';
-        u /= b;
-    }
-    
-    if (neg) {
-        if (width && (pad & PAD_ZERO)) {
-            printchar (out, '-');
-            ++pc;
-            --width;
-        } else {
-            *--s = '-';
-        }
-    }
-
-    return pc + th_printbuf_str(buf, s, width, pad);
-}
-
-
 char * th_strdup_vprintf(const char *fmt, va_list args)
 {
-    const char *s = fmt;
-    char *res;
-    size_t len = 0;
+    ssize_t size = 100;
+    char *buf, *nbuf = NULL;
 
-    /* 1. Determine required space for final string */
-    while (*s) {
-        if (*s == '%') {
-            s++;
-        } else {
-            s++;
-            len++;
+    if ((buf = th_malloc(size)) == NULL)
+        return NULL;
+    
+    while (1) {
+        ssize_t n = vsnprintf(buf, size, fmt, args);
+        if (n > -1 && n < size)
+            return buf;
+        if (n > -1)
+            size = n + 1;
+        else
+            size *= 2;
+        
+        if ((nbuf = th_realloc(nbuf, size)) == NULL) {
+            th_free(buf);
+            return NULL;
         }
+        
+        buf = nbuf;
     }
-    
-    /* 2. Allocate space */
-    
-    /* 3. Create final string */
-    
-    return res;
 }
 
 
@@ -180,7 +94,7 @@
 
     return res;
 }
-#endif
+
 
 /* Compare two strings ignoring case [strcasecmp, strncasecmp]
  */