changeset 377:9ad157feb99a

Sync th-libs.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 03 Oct 2011 00:04:33 +0300
parents 1617d0a0c58e
children afbc3bfd3e03
files th_args.c th_string.c
diffstat 2 files changed, 254 insertions(+), 150 deletions(-) [+]
line wrap: on
line diff
--- a/th_args.c	Tue Jun 28 21:46:18 2011 +0300
+++ b/th_args.c	Mon Oct 03 00:04:33 2011 +0300
@@ -128,20 +128,26 @@
  */
 static BOOL th_args_check_arg(optarg_t *o, char *optArg)
 {
-    if ((o->optFlags & OPT_ARGMASK) == OPT_ARGREQ && optArg == NULL) {
-        if (o->optShort != 0 && o->optLong != NULL) {
+    if ((o->optFlags & OPT_ARGMASK) == OPT_ARGREQ && optArg == NULL)
+    {
+        if (o->optShort != 0 && o->optLong != NULL)
+        {
             THERR("Option '-%c ARG' (--%s=ARG) requires an argument!\n",
-                o->optShort, o->optLong);
-        } else if (o->optShort != 0) {
+                  o->optShort, o->optLong);
+        }
+        else if (o->optShort != 0)
+        {
             THERR("Option '-%c ARG' requires an argument!\n", o->optShort);
-        } else if (o->optLong != NULL) {
+        }
+        else if (o->optLong != NULL)
+        {
             THERR("Option --%s=ARG requires an argument!\n", o->optLong);
         }
-        
+
         return FALSE;
-    } else
+    }
+    else
         return TRUE;
-
 }
 
 
@@ -155,39 +161,43 @@
     char *tmpArg = currArg, *optArg;
     int optN;
     BOOL isFound;
-    
+
     /* Short options can be combined: -a -b -c == -abc */
-    while (*tmpArg) {
-        
+    while (*tmpArg)
+    {
         for (optN = 0, isFound = FALSE; (optN < optListN) && !isFound; optN++)
-        if (*tmpArg == optList[optN].optShort) {
-            /* Get possible option argument, if needed */
-            if ((optList[optN].optFlags & OPT_ARGMASK) != 0 && (++(*newArgIndex) < argc))
-                optArg = argv[*newArgIndex];
-            else
-                optArg = NULL;
-            
-            /* Check if option argument is required */
-            if (!th_args_check_arg(&optList[optN], optArg))
-                return FALSE;            
-            else {
-                char tmpStr[2] = { 0, 0 };
+            if (*tmpArg == optList[optN].optShort)
+            {
+                /* Get possible option argument, if needed */
+                if ((optList[optN].optFlags & OPT_ARGMASK) != 0
+                    && (++(*newArgIndex) < argc))
+                    optArg = argv[*newArgIndex];
+                else
+                    optArg = NULL;
+
+                /* Check if option argument is required */
+                if (!th_args_check_arg(&optList[optN], optArg))
+                    return FALSE;
+                else
+                {
+                    char tmpStr[2] = { 0, 0 };
 
-                /* Option was given succesfully, try to handle it */
-                wasGiven[optN] = TRUE;
-                
-                tmpStr[0] = *tmpArg;
-                
-                if (!handleOpt(optList[optN].optID, optArg, tmpStr))
-                    return FALSE;
+                    /* Option was given succesfully, try to handle it */
+                    wasGiven[optN] = TRUE;
+
+                    tmpStr[0] = *tmpArg;
+
+                    if (!handleOpt(optList[optN].optID, optArg, tmpStr))
+                        return FALSE;
+                }
+
+                isFound = TRUE;
             }
-                            
-            isFound = TRUE;
-        }
-        
-        if (!isFound) {
+
+        if (!isFound)
+        {
             THERR("Unknown short option '%c' in argument '-%s'\n",
-                *tmpArg, currArg);
+                  *tmpArg, currArg);
             return FALSE;
         }
 
@@ -207,37 +217,46 @@
 {
     int optN, optLen, i;
     char *optArg;
-    
-    (void) argc; (void) argv; (void) newArgIndex;
-    
+
+    (void) argc;
+    (void) argv;
+    (void) newArgIndex;
+
     /* Long option */
     for (optN = -1, optLen = i = 0; (i < optListN) && (optN < 0); i++)
-    if (optList[i].optLong) {
-        optLen = strlen(optList[i].optLong);
-        if (strncmp(currArg, optList[i].optLong, optLen) == 0)
-            optN = i;
-    }
-    
+        if (optList[i].optLong)
+        {
+            optLen = strlen(optList[i].optLong);
+            if (strncmp(currArg, optList[i].optLong, optLen) == 0)
+                optN = i;
+        }
+
     /* Get possible option argument, if needed */
-    if (optN >= 0) {
-        if ((optList[optN].optFlags & OPT_ARGMASK) != 0) {
+    if (optN >= 0)
+    {
+        if ((optList[optN].optFlags & OPT_ARGMASK) != 0)
+        {
             if (currArg[optLen] == '=')
                 optArg = &currArg[optLen + 1];
             else
                 optArg = NULL;
-        } else
+        }
+        else
             optArg = NULL;
 
         /* Check if option argument is required */
         if (!th_args_check_arg(&optList[optN], optArg))
-            return FALSE;            
-        else {
+            return FALSE;
+        else
+        {
             /* Option was given succesfully, try to handle it */
             wasGiven[optN] = TRUE;
             if (!handleOpt(optList[optN].optID, optArg, currArg))
                 return FALSE;
         }
-    } else {
+    }
+    else
+    {
         THERR("Unknown long option '--%s'\n", currArg);
         return FALSE;
     }
@@ -250,9 +269,9 @@
  * calling the given callback functions.
  */
 BOOL th_args_process(int argc, char *argv[],
-             optarg_t optList[], int optListN,
-             BOOL (*handleOpt) (int, char *, char *),
-             BOOL (*handleNonOption) (char *), BOOL bailOut)
+                     optarg_t optList[], int optListN,
+                     BOOL(*handleOpt) (int, char *, char *),
+                     BOOL(*handleNonOption) (char *), BOOL bailOut)
 {
     BOOL endOptions, optionsOK;
     int argIndex, newArgIndex, i;
@@ -261,8 +280,10 @@
 
     /* Allocate wasGiven */
     wasGiven = (BOOL *) th_calloc(optListN, sizeof(BOOL));
-    if (!wasGiven) {
-        THERR("FATAL ERROR! Could not allocate wasGiven in th_args_process()!\n");
+    if (!wasGiven)
+    {
+        THERR
+            ("FATAL ERROR! Could not allocate wasGiven in th_args_process()!\n");
         exit(128);
     }
 
@@ -270,60 +291,73 @@
     argIndex = 1;
     optionsOK = TRUE;
     endOptions = FALSE;
-    while (argIndex < argc) {
+    while (argIndex < argc)
+    {
         currArg = argv[argIndex];
-        if ((currArg[0] == '-') && !endOptions) {
+        if ((currArg[0] == '-') && !endOptions)
+        {
             newArgIndex = argIndex;
             currArg++;
-            if (*currArg == '-') {
+            if (*currArg == '-')
+            {
                 /* Check for "--", which ends the options-list */
                 currArg++;
-                if (*currArg == 0) {
+                if (*currArg == 0)
+                {
                     endOptions = TRUE;
                     continue;
                 }
-                
+
                 /* Long options */
                 if (!th_args_process_long(currArg, &newArgIndex,
-                    wasGiven, argc, argv, optList, optListN,
-                    handleOpt))
+                                          wasGiven, argc, argv, optList,
+                                          optListN, handleOpt))
                     optionsOK = FALSE;
-            } else {
+            }
+            else
+            {
                 /* Short options */
                 if (!th_args_process_short(currArg, &newArgIndex,
-                    wasGiven, argc, argv, optList, optListN,
-                    handleOpt))
+                                           wasGiven, argc, argv, optList,
+                                           optListN, handleOpt))
                     optionsOK = FALSE;
             }
 
             argIndex = newArgIndex;
-        } else {
+        }
+        else
+        {
             /* Was not option argument */
-            if (handleNonOption == NULL || (handleNonOption != NULL && !handleNonOption(currArg))) {
+            if (handleNonOption == NULL
+                || (handleNonOption != NULL && !handleNonOption(currArg)))
+            {
                 THERR("Invalid argument '%s'\n", currArg);
                 optionsOK = FALSE;
             }
         }
-        
+
         /* Check if we bail out on invalid argument */
-        if (!optionsOK && bailOut) {
+        if (!optionsOK && bailOut)
+        {
             th_free(wasGiven);
             return FALSE;
         }
-        
+
         argIndex++;
     }
 
     /* Check wasGiven by isRequired */
     for (i = 0; i < optListN; i++)
-    if ((optList[i].optFlags & OPT_REQUIRED) != 0 && !wasGiven[i]) {
-        THERR("Option -%s (--%s) is required.\n",
-            optList[i].optShort, optList[i].optLong);
+        if ((optList[i].optFlags & OPT_REQUIRED) != 0 && !wasGiven[i])
+        {
+            THERR("Option -%s (--%s) is required.\n",
+                  optList[i].optShort, optList[i].optLong);
 
-        optionsOK = FALSE;
-        if (bailOut) break;
-    }
-    
+            optionsOK = FALSE;
+            if (bailOut)
+                break;
+        }
+
     th_free(wasGiven);
     return optionsOK;
 }
@@ -332,56 +366,66 @@
 /* Print help for commandline arguments/options
  */
 void th_args_help(FILE * outFile,
-    optarg_t optList[], int optListN,
-    char * progName, char * progUsage)
+                  optarg_t optList[], int optListN,
+                  char *progName, char *progUsage)
 {
     int i, nrequired;
 
     fprintf(outFile,
-        "\n%s v%s (%s)\n"
-        "%s\n"
-        "%s\n"
-        "Usage: %s %s\n",
-        th_prog_name, th_prog_version, th_prog_fullname,
-        th_prog_author, th_prog_license, progName, progUsage);
+            "\n%s v%s (%s)\n"
+            "%s\n"
+            "%s\n"
+            "Usage: %s %s\n",
+            th_prog_name, th_prog_version, th_prog_fullname,
+            th_prog_author, th_prog_license, progName, progUsage);
 
 
-    for (i = nrequired = 0; i < optListN; i++) {
+    for (i = nrequired = 0; i < optListN; i++)
+    {
         optarg_t *o = &optList[i];
-        
+
         /* Print short option */
         if (o->optShort != 0)
             fprintf(outFile, "  -%c,  ", o->optShort);
         else
             fprintf(outFile, "       ");
-        
+
         /* Print long option */
-        if (o->optLong) {
+        if (o->optLong)
+        {
             char tmpStr[64], *p;
-            
-            if ((o->optFlags & OPT_ARGMASK) == OPT_ARGOPT) {
-                snprintf(tmpStr, sizeof(tmpStr), "%s[=ARG]", optList[i].optLong);
+
+            if ((o->optFlags & OPT_ARGMASK) == OPT_ARGOPT)
+            {
+                snprintf(tmpStr, sizeof(tmpStr), "%s[=ARG]",
+                         optList[i].optLong);
                 p = tmpStr;
-            } else if ((o->optFlags & OPT_ARGMASK) == OPT_ARGREQ) {
-                snprintf(tmpStr, sizeof(tmpStr), "%s=ARG", optList[i].optLong);
+            }
+            else if ((o->optFlags & OPT_ARGMASK) == OPT_ARGREQ)
+            {
+                snprintf(tmpStr, sizeof(tmpStr), "%s=ARG",
+                         optList[i].optLong);
                 p = tmpStr;
-            } else
+            }
+            else
                 p = o->optLong;
-            
+
             fprintf(outFile, "--%-15s", p);
-        } else
+        }
+        else
             fprintf(outFile, "                 ");
 
         fprintf(outFile, "  %s.", optList[i].optDesc);
-        
-        if (o->optFlags & OPT_REQUIRED) {
+
+        if (o->optFlags & OPT_REQUIRED)
+        {
             fprintf(outFile, " [*]\n");
             nrequired++;
-        } else
+        }
+        else
             fprintf(outFile, "\n");
     }
-    
+
     if (nrequired > 0)
         fprintf(outFile, "(Options marked with [*] are required)\n");
 }
-
--- a/th_string.c	Tue Jun 28 21:46:18 2011 +0300
+++ b/th_string.c	Mon Oct 03 00:04:33 2011 +0300
@@ -36,13 +36,15 @@
 
     /* Copy to the destination */
     i = n;
-    while (*s && i > 0) {
+    while (*s && i > 0)
+    {
         *(d++) = *(s++);
         i--;
     }
 
     /* Fill rest of space with zeros */
-    while (i > 0) {
+    while (i > 0)
+    {
         *(d++) = 0;
         i--;
     }
@@ -64,7 +66,8 @@
     if ((buf = th_malloc(size)) == NULL)
         return NULL;
     
-    while (1) {
+    while (1)
+    {
         int n;
         va_list ap;
         va_copy(ap, args);
@@ -78,7 +81,8 @@
         else
             size *= 2;
 
-        if ((nbuf = th_realloc(nbuf, size)) == NULL) {
+        if ((nbuf = th_realloc(nbuf, size)) == NULL)
+        {
             th_free(buf);
             return NULL;
         }
@@ -133,7 +137,8 @@
     if (str1 == str2)
         return 0;
 
-    while (*s1 && *s2 && th_tolower(*s1) == th_tolower(*s2)) {
+    while (*s1 && *s2 && th_tolower(*s1) == th_tolower(*s2))
+    {
         s1++;
         s2++;
     }
@@ -151,7 +156,8 @@
     if (str1 == str2)
         return 0;
 
-    while (n > 0 && *s1 && *s2 && th_tolower(*s1) == th_tolower(*s2)) {
+    while (n > 0 && *s1 && *s2 && th_tolower(*s1) == th_tolower(*s2))
+    {
         s1++;
         s2++;
         n--;
@@ -171,7 +177,8 @@
 
     i = str;
     j = str;
-    while (*i) {
+    while (*i)
+    {
         if (!th_iscntrl(*i))
             *(j++) = *i;
         i++;
@@ -208,13 +215,16 @@
     if (str == NULL)
         return -1;
 
-    if (*result != NULL) {
+    if (*result != NULL)
+    {
         *result = th_realloc(*result, strlen(*result) + strlen(str) + 1);
         if (*result == NULL)
             return -1;
 
         strcat(*result, str);
-    } else {
+    }
+    else
+    {
         *result = th_malloc(strlen(str) + 1);
         if (*result == NULL)
             return -1;
@@ -237,6 +247,7 @@
     /* Terminating NULL-character is not whitespace! */
     while (th_isspace(str[*pos]))
         (*pos)++;
+
     return &str[*pos];
 }
 
@@ -284,10 +295,12 @@
     /* Start comparision */
     do {
         didMatch = FALSE;
-        switch (*pattern) {
+        switch (*pattern)
+        {
         case '?':
             /* Any single character matches */
-            if (*str) {
+            if (*str)
+            {
                 didMatch = TRUE;
                 pattern++;
                 str++;
@@ -304,42 +317,59 @@
             break;
 
         case 0:
-            if (isAnyMode) {
+            if (isAnyMode)
+            {
                 if (*str)
                     str++;
                 else
                     isEnd = TRUE;
-            } else {
-                if (*str) {
-                    if (tmpPattern) {
+            }
+            else
+            {
+                if (*str)
+                {
+                    if (tmpPattern)
+                    {
                         isAnyMode = TRUE;
                         pattern = tmpPattern;
-                    } else
+                    }
+                    else
                         didMatch = FALSE;
                 } else
                     isEnd = TRUE;
             }
             break;
         default:
-            if (isAnyMode) {
-                if (*pattern == *str) {
+            if (isAnyMode)
+            {
+                if (*pattern == *str)
+                {
                     isAnyMode = FALSE;
                     didMatch = TRUE;
-                } else {
-                    if (*str) {
+                }
+                else
+                {
+                    if (*str)
+                    {
                         didMatch = TRUE;
                         str++;
                     }
                 }
-            } else {
-                if (*pattern == *str) {
+            }
+            else
+            {
+                if (*pattern == *str)
+                {
                     didMatch = TRUE;
                     if (*pattern)
                         pattern++;
                     if (*str)
                         str++;
-                } else {
-                    if (tmpPattern) {
+                }
+                else
+                {
+                    if (tmpPattern)
+                    {
                         didMatch = TRUE;
                         isAnyMode = TRUE;
                         pattern = tmpPattern;
@@ -375,7 +405,8 @@
         switch (*pattern) {
         case '?':
             /* Any single character matches */
-            if (*str) {
+            if (*str)
+            {
                 pattern++;
                 str++;
             } else
@@ -391,17 +422,23 @@
             break;
 
         case 0:
-            if (isAnyMode) {
+            if (isAnyMode)
+            {
                 if (*str)
                     str++;
                 else
                     isEnd = TRUE;
-            } else {
-                if (*str) {
-                    if (tmpPattern) {
+            }
+            else
+            {
+                if (*str)
+                {
+                    if (tmpPattern)
+                    {
                         isAnyMode = TRUE;
                         pattern = tmpPattern;
-                    } else
+                    }
+                    else
                         didMatch = FALSE;
                 } else
                     isEnd = TRUE;
@@ -409,32 +446,44 @@
             break;
 
         default:
-            if (isAnyMode) {
-                if (th_tolower(*pattern) == th_tolower(*str)) {
+            if (isAnyMode)
+            {
+                if (th_tolower(*pattern) == th_tolower(*str))
+                {
                     isAnyMode = FALSE;
-                } else {
+                }
+                else
+                {
                     if (*str)
                         str++;
                     else
                         didMatch = FALSE;
                 }
-            } else {
-                if (th_tolower(*pattern) == th_tolower(*str)) {
+            }
+            else
+            {
+                if (th_tolower(*pattern) == th_tolower(*str))
+                {
                     if (*pattern)
                         pattern++;
                     if (*str)
                         str++;
-                } else {
-                    if (tmpPattern) {
+                }
+                else
+                {
+                    if (tmpPattern)
+                    {
                         isAnyMode = TRUE;
                         pattern = tmpPattern;
-                    } else
+                    }
+                    else
                         didMatch = FALSE;
                 }
             }
 
             if (!*str && !*pattern)
                 isEnd = TRUE;
+
             break;
 
         }        /* switch */
@@ -450,14 +499,24 @@
     const char *p = str;
     int len, val = 0;
     
-    for (len = 0; *p && len < 6; p++, len++) {
-        if (*p >= '0' && *p <= '9') {
-            val *= 16; val += (*p - '0');
-        } else if (*p >= 'A' && *p <= 'F') {
-            val *= 16; val += (*p - 'A') + 10;
-        } else if (*p >= 'a' && *p <= 'f') {
-            val *= 16; val += (*p - 'a') + 10;
-        } else
+    for (len = 0; *p && len < 6; p++, len++)
+    {
+        if (*p >= '0' && *p <= '9')
+        {
+            val *= 16;
+            val += (*p - '0');
+        }
+        else if (*p >= 'A' && *p <= 'F')
+        {
+            val *= 16;
+            val += (*p - 'A') + 10;
+        }
+        else if (*p >= 'a' && *p <= 'f')
+        {
+            val *= 16;
+            val += (*p - 'a') + 10;
+        }
+        else
             return -1;
     }
     
@@ -470,7 +529,8 @@
     if (*buf == NULL)
         *bufsize = *len = 0;
     
-    if (*buf == NULL || *len + grow >= *bufsize) {
+    if (*buf == NULL || *len + grow >= *bufsize)
+    {
         *bufsize += grow + TH_BUFGROW;
         *buf = (char *) th_realloc(*buf, *bufsize);
         if (*buf == NULL)