[libbdplus-devel] strutl: merge changes from other libs

anonymous git at videolan.org
Wed Apr 29 10:33:24 CEST 2015


libbdplus | branch: master | anonymous <anonymous at anonymous.org> | Tue Apr 28 13:54:18 2015 +0300| [7ae0830f2f821717117fa5d65418fe5a233c40b9] | committer: anonymous

strutl: merge changes from other libs

str_printf: return null when failed
str_skip_white: skip tabs
add const

> http://git.videolan.org/gitweb.cgi/libbdplus.git/?a=commit;h=7ae0830f2f821717117fa5d65418fe5a233c40b9
---

 src/libbdplus/bdplus_config.c |    4 ++--
 src/util/strutl.c             |   36 ++++++++++++++++++------------------
 src/util/strutl.h             |    4 ++--
 3 files changed, 22 insertions(+), 22 deletions(-)

diff --git a/src/libbdplus/bdplus_config.c b/src/libbdplus/bdplus_config.c
index 74f6a10..8e0ab14 100644
--- a/src/libbdplus/bdplus_config.c
+++ b/src/libbdplus/bdplus_config.c
@@ -66,9 +66,9 @@ static int _load_aes_keys(bdplus_aes_key_t *aes_keys, const char *base)
 
 static int _load_ecdsa_keys(bdplus_ecdsa_key_t *ecdsa_keys, const char *base)
 {
+    const char *p;
     char *path = str_printf("%s/" ECDSA_KEYS_FILE, base);
     char *cfg;
-    char *p;
     int   num_ecdsa_keys = 0;
 
     cfg = file_load(path, NULL);
@@ -190,9 +190,9 @@ static void _dev_free(bdplus_dev_t **pp)
 
 static int _load_memory(bdplus_ram_t **ram, const char *base)
 {
+    const char *p;
     char *path;
     char *cfg;
-    char *p;
 
     path = str_printf("%s/" MEMORY_MAP_FILE, base);
     cfg = file_load(path, NULL);
diff --git a/src/util/strutl.c b/src/util/strutl.c
index fd471d1..b1a4a21 100644
--- a/src/util/strutl.c
+++ b/src/util/strutl.c
@@ -31,22 +31,28 @@
 #include <stdlib.h>
 #include <string.h>
 
-char * str_dup(const char *str)
+char *str_dup(const char *str)
 {
-  return str ? strcpy (malloc(strlen(str) + 1), str) : NULL;
+    return str ? strcpy (malloc(strlen(str) + 1), str) : NULL;
 }
 
-char * str_printf(const char *fmt, ...)
+char *str_printf(const char *fmt, ...)
 {
     /* Guess we need no more than 100 bytes. */
-    int len;
     va_list ap;
-    int size = 100;
-    char *tmp, *str = NULL;
+    int     len;
+    int     size = 100;
+    char   *tmp, *str = NULL;
+
+    while (1) {
+
+        tmp = realloc(str, size);
+        if (tmp == NULL) {
+            X_FREE(str);
+            return NULL;
+        }
+        str = tmp;
 
-    str = malloc(size);
-    while (1) 
-    {
         /* Try to print in the allocated space. */
         va_start(ap, fmt);
         len = vsnprintf(str, size, fmt, ap);
@@ -62,16 +68,10 @@ char * str_printf(const char *fmt, ...)
             size = len+1; /* precisely what is needed */
         else           /* glibc 2.0 */
             size *= 2;  /* twice the old size */
-
-        tmp = realloc(str, size);
-        if (tmp == NULL) {
-            return str;
-        }
-        str = tmp;
     }
 }
 
-char *str_next_line(char *p)
+const char *str_next_line(const char *p)
 {
     while (*p && *p != '\r' && *p != '\n') {
         p++;
@@ -83,9 +83,9 @@ char *str_next_line(char *p)
     return p;
 }
 
-char *str_skip_white(char *p)
+const char *str_skip_white(const char *p)
 {
-    while (*p && (*p == '\r' || *p == '\n' || *p == ' ')) {
+    while (*p && (*p == ' ' || *p == '\r' || *p == '\n' || *p == '\t')) {
         p++;
     }
 
diff --git a/src/util/strutl.h b/src/util/strutl.h
index b9bfd35..23654a9 100644
--- a/src/util/strutl.h
+++ b/src/util/strutl.h
@@ -28,8 +28,8 @@
 BD_PRIVATE char * str_dup(const char *str) BD_ATTR_MALLOC;
 BD_PRIVATE char * str_printf(const char *fmt, ...) BD_ATTR_FORMAT_PRINTF(1,2) BD_ATTR_MALLOC;
 
-BD_PRIVATE char * str_skip_white(char *p);
-BD_PRIVATE char * str_next_line(char *p);
+BD_PRIVATE const char * str_skip_white    (const char *str);
+BD_PRIVATE const char * str_next_line     (const char *str);
 
 BD_PRIVATE char * str_print_hex(char *out, const uint8_t *str, int count);
 



More information about the libbdplus-devel mailing list