# HG changeset patch # User Matti Hamalainen # Date 1240254157 -10800 # Node ID 69aed051f84d52c7e84372c4575ad213c651cd05 # Parent 335b5a74c22e4a70fcf33e4bd38b4bf734dc542e Synced th-libs. diff -r 335b5a74c22e -r 69aed051f84d th_args.c --- a/th_args.c Fri Jan 30 01:54:51 2009 +0200 +++ b/th_args.c Mon Apr 20 22:02:37 2009 +0300 @@ -128,19 +128,19 @@ */ 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) { - THERR("Option '-%c ARG' (--%s=ARG) requires an argument!\n", - o->optShort, o->optLong); - } else if (o->optShort != 0) { - THERR("Option '-%c ARG' requires an argument!\n", o->optShort); - } else if (o->optLong != NULL) { - THERR("Option --%s=ARG requires an argument!\n", o->optLong); - } - - return FALSE; - } else - return TRUE; + 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) { + THERR("Option '-%c ARG' requires an argument!\n", o->optShort); + } else if (o->optLong != NULL) { + THERR("Option --%s=ARG requires an argument!\n", o->optLong); + } + + return FALSE; + } else + return TRUE; } @@ -148,101 +148,101 @@ /* Handle short options */ static BOOL th_args_process_short(char *currArg, int *newArgIndex, - BOOL *wasGiven, int argc, char *argv[], - optarg_t optList[], int optListN, - BOOL (*handleOpt)(int, char *, char *)) + BOOL *wasGiven, int argc, char *argv[], + optarg_t optList[], int optListN, + BOOL (*handleOpt)(int, char *, char *)) { - char *tmpArg = currArg, *optArg; - int optN; - BOOL isFound; - - /* Short options can be combined: -a -b -c == -abc */ - 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 }; + char *tmpArg = currArg, *optArg; + int optN; + BOOL isFound; + + /* Short options can be combined: -a -b -c == -abc */ + 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 }; - /* 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; - } - - if (!isFound) { - THERR("Unknown short option '%c' in argument '-%s'\n", - *tmpArg, currArg); - 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; + } + + if (!isFound) { + THERR("Unknown short option '%c' in argument '-%s'\n", + *tmpArg, currArg); + return FALSE; + } - tmpArg++; - } + tmpArg++; + } - return TRUE; + return TRUE; } /* Handle long options */ static BOOL th_args_process_long(char *currArg, int *newArgIndex, - BOOL *wasGiven, int argc, char *argv[], - optarg_t optList[], int optListN, - BOOL (*handleOpt)(int, char *, char *)) + BOOL *wasGiven, int argc, char *argv[], + optarg_t optList[], int optListN, + BOOL (*handleOpt)(int, char *, char *)) { - int optN, optLen, i; - char *optArg; - - (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; - } - - /* Get possible option argument, if needed */ - if (optN >= 0) { - if ((optList[optN].optFlags & OPT_ARGMASK) != 0) { - if (currArg[optLen] == '=') - optArg = &currArg[optLen + 1]; - else - optArg = NULL; - } else - optArg = NULL; + int optN, optLen, i; + char *optArg; + + (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; + } + + /* Get possible option argument, if needed */ + if (optN >= 0) { + if ((optList[optN].optFlags & OPT_ARGMASK) != 0) { + if (currArg[optLen] == '=') + optArg = &currArg[optLen + 1]; + else + optArg = NULL; + } else + optArg = NULL; - /* Check if option argument is required */ - if (!th_args_check_arg(&optList[optN], optArg)) - return FALSE; - else { - /* Option was given succesfully, try to handle it */ - wasGiven[optN] = TRUE; - if (!handleOpt(optList[optN].optID, optArg, currArg)) - return FALSE; - } - } else { - THERR("Unknown long option '--%s'\n", currArg); - return FALSE; - } + /* Check if option argument is required */ + if (!th_args_check_arg(&optList[optN], optArg)) + return FALSE; + else { + /* Option was given succesfully, try to handle it */ + wasGiven[optN] = TRUE; + if (!handleOpt(optList[optN].optID, optArg, currArg)) + return FALSE; + } + } else { + THERR("Unknown long option '--%s'\n", currArg); + return FALSE; + } - return TRUE; + return TRUE; } @@ -250,138 +250,138 @@ * 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; - char *currArg; - BOOL *wasGiven; + BOOL endOptions, optionsOK; + int argIndex, newArgIndex, i; + char *currArg; + BOOL *wasGiven; - /* Allocate wasGiven */ - wasGiven = (BOOL *) th_calloc(optListN, sizeof(BOOL)); - if (!wasGiven) { - THERR("FATAL ERROR! Could not allocate wasGiven in th_args_process()!\n"); - exit(128); - } + /* Allocate wasGiven */ + wasGiven = (BOOL *) th_calloc(optListN, sizeof(BOOL)); + if (!wasGiven) { + THERR("FATAL ERROR! Could not allocate wasGiven in th_args_process()!\n"); + exit(128); + } - /* Parse arguments */ - argIndex = 1; - optionsOK = TRUE; - endOptions = FALSE; - while (argIndex < argc) { - currArg = argv[argIndex]; - if ((currArg[0] == '-') && !endOptions) { - newArgIndex = argIndex; - currArg++; - if (*currArg == '-') { - /* Check for "--", which ends the options-list */ - currArg++; - if (*currArg == 0) { - endOptions = TRUE; - continue; - } - - /* Long options */ - if (!th_args_process_long(currArg, &newArgIndex, - wasGiven, argc, argv, optList, optListN, - handleOpt)) - optionsOK = FALSE; - } else { - /* Short options */ - if (!th_args_process_short(currArg, &newArgIndex, - wasGiven, argc, argv, optList, optListN, - handleOpt)) - optionsOK = FALSE; - } + /* Parse arguments */ + argIndex = 1; + optionsOK = TRUE; + endOptions = FALSE; + while (argIndex < argc) { + currArg = argv[argIndex]; + if ((currArg[0] == '-') && !endOptions) { + newArgIndex = argIndex; + currArg++; + if (*currArg == '-') { + /* Check for "--", which ends the options-list */ + currArg++; + if (*currArg == 0) { + endOptions = TRUE; + continue; + } + + /* Long options */ + if (!th_args_process_long(currArg, &newArgIndex, + wasGiven, argc, argv, optList, optListN, + handleOpt)) + optionsOK = FALSE; + } else { + /* Short options */ + if (!th_args_process_short(currArg, &newArgIndex, + wasGiven, argc, argv, optList, optListN, + handleOpt)) + optionsOK = FALSE; + } - argIndex = newArgIndex; - } else { - /* Was not option argument */ - if (!handleNonOption || (handleNonOption && !handleNonOption(currArg))) { - THERR("Invalid argument '%s'\n", currArg); - optionsOK = FALSE; - } - } - - /* Check if we bail out on invalid argument */ - if (!optionsOK && bailOut) { - th_free(wasGiven); - return FALSE; - } - - argIndex++; - } + argIndex = newArgIndex; + } else { + /* Was not option argument */ + 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) { + 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); + /* 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); - optionsOK = FALSE; - if (bailOut) break; - } - - th_free(wasGiven); - return optionsOK; + optionsOK = FALSE; + if (bailOut) break; + } + + th_free(wasGiven); + return optionsOK; } /* 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; + 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); + 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); - 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) { - char tmpStr[64], *p; - - 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); - p = tmpStr; - } else - p = o->optLong; - - fprintf(outFile, "--%-15s", p); - } else - fprintf(outFile, " "); + 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) { + char tmpStr[64], *p; + + 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); + p = tmpStr; + } else + p = o->optLong; + + fprintf(outFile, "--%-15s", p); + } else + fprintf(outFile, " "); - fprintf(outFile, " %s.", optList[i].optDesc); - - if (o->optFlags & OPT_REQUIRED) { - fprintf(outFile, " [*]\n"); - nrequired++; - } else - fprintf(outFile, "\n"); - } - - if (nrequired > 0) - fprintf(outFile, "(Options marked with [*] are required)\n"); + fprintf(outFile, " %s.", optList[i].optDesc); + + if (o->optFlags & OPT_REQUIRED) { + fprintf(outFile, " [*]\n"); + nrequired++; + } else + fprintf(outFile, "\n"); + } + + if (nrequired > 0) + fprintf(outFile, "(Options marked with [*] are required)\n"); } diff -r 335b5a74c22e -r 69aed051f84d th_args.h --- a/th_args.h Fri Jan 30 01:54:51 2009 +0200 +++ b/th_args.h Mon Apr 20 22:02:37 2009 +0300 @@ -18,31 +18,31 @@ /* Option flags */ -#define OPT_NONE (0) /* Simple option with no arguments */ +#define OPT_NONE (0) /* Simple option with no arguments */ -#define OPT_ARGREQ (1) /* Option's argument is required */ -#define OPT_ARGOPT (3) /* Option's argument is optional */ -#define OPT_ARGMASK (3) /* Mask for option argument flags */ +#define OPT_ARGREQ (1) /* Option's argument is required */ +#define OPT_ARGOPT (3) /* Option's argument is optional */ +#define OPT_ARGMASK (3) /* Mask for option argument flags */ -#define OPT_REQUIRED (4) /* This option is required to be given */ +#define OPT_REQUIRED (4) /* This option is required to be given */ typedef struct { - int optID; - char optShort; - char *optLong; - char *optDesc; - int optFlags; + int optID; + char optShort; + char *optLong; + char *optDesc; + int optFlags; } optarg_t; BOOL th_args_process(int argc, char *argv[], - optarg_t argList[], int argListN, - BOOL (*handleOpt)(int, char *, char *), - BOOL (*handleFile)(char *), BOOL); + optarg_t argList[], int argListN, + BOOL (*handleOpt)(int, char *, char *), + BOOL (*handleFile)(char *), BOOL); void th_args_help(FILE *, optarg_t optList[], int optListN, - char *, char *); + char *, char *); #ifdef __cplusplus } diff -r 335b5a74c22e -r 69aed051f84d th_string.c --- a/th_string.c Fri Jan 30 01:54:51 2009 +0200 +++ b/th_string.c Mon Apr 20 22:02:37 2009 +0300 @@ -18,15 +18,15 @@ */ char *th_strdup(const char *s) { - char *res; - if (s == NULL) - return NULL; - - if ((res = th_malloc(strlen(s) + 1)) == NULL) - return NULL; - - strcpy(res, s); - return res; + char *res; + if (s == NULL) + return NULL; + + if ((res = th_malloc(strlen(s) + 1)) == NULL) + return NULL; + + strcpy(res, s); + return res; } @@ -34,69 +34,69 @@ */ char *th_stralloc(const size_t l) { - assert(l > 0); - return th_malloc(sizeof(char) * l); + assert(l > 0); + return th_malloc(sizeof(char) * l); } char *th_strrealloc(char * s, const size_t l) { - assert(l > 0); - return th_realloc(s, sizeof(char) * l); + assert(l > 0); + return th_realloc(s, sizeof(char) * l); } char *th_strncpy(char * dst, const char * src, size_t n) { - const char *s = src; - char *d = dst; - size_t i; - assert(src != NULL); - assert(dst != NULL); + const char *s = src; + char *d = dst; + size_t i; + assert(src != NULL); + assert(dst != NULL); - /* Copy to the destination */ - i = n; - while (*s && (i > 0)) { - *(d++) = *(s++); - i--; - } + /* Copy to the destination */ + i = n; + while (*s && (i > 0)) { + *(d++) = *(s++); + i--; + } - /* Fill rest of space with zeros */ - while (i > 0) { - *(d++) = 0; - i--; - } + /* Fill rest of space with zeros */ + while (i > 0) { + *(d++) = 0; + i--; + } - /* Ensure that last is always zero */ - dst[n - 1] = 0; + /* Ensure that last is always zero */ + dst[n - 1] = 0; - return dst; + return dst; } int th_strncmp(char * str1, char * str2, size_t n) { - char *s1, *s2; - assert(str1 != NULL); - assert(str2 != NULL); + char *s1, *s2; + assert(str1 != NULL); + assert(str2 != NULL); - /* Check the string pointers */ - if (str1 == str2) - return 0; + /* Check the string pointers */ + if (str1 == str2) + return 0; - /* Go through the string */ - s1 = str1; - s2 = str2; - while ((n > 0) && *s1 && *s2 && (*s1 == *s2)) { - s1++; - s2++; - n--; - } + /* Go through the string */ + s1 = str1; + s2 = str2; + while ((n > 0) && *s1 && *s2 && (*s1 == *s2)) { + s1++; + s2++; + n--; + } - if (n > 0) - return ((*s1) - (*s2)); - else - return 0; + if (n > 0) + return ((*s1) - (*s2)); + else + return 0; } @@ -104,45 +104,45 @@ */ int th_strcasecmp(char * str1, char * str2) { - char *s1 = str1, *s2 = str2; - assert(str1 != NULL); - assert(str2 != NULL); + char *s1 = str1, *s2 = str2; + assert(str1 != NULL); + assert(str2 != NULL); - /* Check the string pointers */ - if (str1 == str2) - return 0; + /* Check the string pointers */ + if (str1 == str2) + return 0; - /* Go through the string */ - while (*s1 && *s2 && (th_tolower(*s1) == th_tolower(*s2))) { - s1++; - s2++; - } + /* Go through the string */ + while (*s1 && *s2 && (th_tolower(*s1) == th_tolower(*s2))) { + s1++; + s2++; + } - return (th_tolower(*s1) - th_tolower(*s2)); + return (th_tolower(*s1) - th_tolower(*s2)); } int th_strncasecmp(char * str1, char * str2, size_t n) { - char *s1 = str1, *s2 = str2; - assert(str1 != NULL); - assert(str2 != NULL); + char *s1 = str1, *s2 = str2; + assert(str1 != NULL); + assert(str2 != NULL); - /* Check the string pointers */ - if (str1 == str2) - return 0; + /* Check the string pointers */ + if (str1 == str2) + return 0; - /* Go through the string */ - while ((n > 0) && *s1 && *s2 && (th_tolower(*s1) == th_tolower(*s2))) { - s1++; - s2++; - n--; - } + /* Go through the string */ + while ((n > 0) && *s1 && *s2 && (th_tolower(*s1) == th_tolower(*s2))) { + s1++; + s2++; + n--; + } - if (n > 0) - return (th_tolower(*s1) - th_tolower(*s2)); - else - return 0; + if (n > 0) + return (th_tolower(*s1) - th_tolower(*s2)); + else + return 0; } @@ -151,18 +151,18 @@ */ void th_strip_ctrlchars(char * str) { - char *i, *j; - assert(str != NULL); + char *i, *j; + assert(str != NULL); - i = str; - j = str; - while (*i) { - if (!th_iscntrl(*i)) - *(j++) = *i; - i++; - } + i = str; + j = str; + while (*i) { + if (!th_iscntrl(*i)) + *(j++) = *i; + i++; + } - *j = 0; + *j = 0; } @@ -170,22 +170,22 @@ */ int th_pstrcpy(char ** result, char * str) { - assert(result != NULL); + assert(result != NULL); - /* Check the string pointers */ - if (str == NULL) - return -1; + /* Check the string pointers */ + if (str == NULL) + return -1; - /* Allocate memory for destination */ - th_free(*result); - *result = th_stralloc(strlen(str) + 1); - if (!*result) - return -2; + /* Allocate memory for destination */ + th_free(*result); + *result = th_stralloc(strlen(str) + 1); + if (!*result) + return -2; - /* Copy to the destination */ - strcpy(*result, str); + /* Copy to the destination */ + strcpy(*result, str); - return 0; + return 0; } @@ -193,27 +193,27 @@ */ int th_pstrcat(char ** result, char * str) { - assert(result != NULL); + assert(result != NULL); - /* Check the string pointers */ - if (str == NULL) - return -1; + /* Check the string pointers */ + if (str == NULL) + return -1; - if (*result != NULL) { - *result = th_strrealloc(*result, strlen(*result) + strlen(str) + 1); - if (*result == NULL) - return -1; + if (*result != NULL) { + *result = th_strrealloc(*result, strlen(*result) + strlen(str) + 1); + if (*result == NULL) + return -1; - strcat(*result, str); - } else { - *result = th_stralloc(strlen(str) + 1); - if (*result == NULL) - return -1; + strcat(*result, str); + } else { + *result = th_stralloc(strlen(str) + 1); + if (*result == NULL) + return -1; - strcpy(*result, str); - } + strcpy(*result, str); + } - return 0; + return 0; } @@ -223,12 +223,12 @@ */ char *th_findnext(char * str, size_t * iPos) { - assert(str != NULL); + assert(str != NULL); - /* Terminating NULL-character is not whitespace! */ - while (th_isspace(str[*iPos])) - (*iPos)++; - return &str[*iPos]; + /* Terminating NULL-character is not whitespace! */ + while (th_isspace(str[*iPos])) + (*iPos)++; + return &str[*iPos]; } @@ -236,12 +236,12 @@ */ char *th_findsep(char * str, size_t * iPos, char chSep) { - assert(str != NULL); + assert(str != NULL); - /* Terminating NULL-character is not digit! */ - while (str[*iPos] && (str[*iPos] != chSep)) - (*iPos)++; - return &str[*iPos]; + /* Terminating NULL-character is not digit! */ + while (str[*iPos] && (str[*iPos] != chSep)) + (*iPos)++; + return &str[*iPos]; } @@ -249,12 +249,12 @@ */ char *th_findseporspace(char * str, size_t * iPos, char chSep) { - assert(str != NULL); + assert(str != NULL); - /* Terminating NULL-character is not digit! */ - while (!th_isspace(str[*iPos]) && (str[*iPos] != chSep)) - (*iPos)++; - return &str[*iPos]; + /* Terminating NULL-character is not digit! */ + while (!th_isspace(str[*iPos]) && (str[*iPos] != chSep)) + (*iPos)++; + return &str[*iPos]; } @@ -265,88 +265,88 @@ */ BOOL th_strmatch(char * str, char * pattern) { - BOOL didMatch = TRUE, isAnyMode = FALSE, isEnd = FALSE; - char *tmpPattern = NULL; + BOOL didMatch = TRUE, isAnyMode = FALSE, isEnd = FALSE; + char *tmpPattern = NULL; - /* Check given pattern and string */ - if (str == NULL || pattern == NULL) - return FALSE; + /* Check given pattern and string */ + if (str == NULL || pattern == NULL) + return FALSE; - /* Start comparision */ - do { - didMatch = FALSE; - switch (*pattern) { - case '?': - /* Any single character matches */ - if (*str) { - didMatch = TRUE; - pattern++; - str++; - } - break; + /* Start comparision */ + do { + didMatch = FALSE; + switch (*pattern) { + case '?': + /* Any single character matches */ + if (*str) { + didMatch = TRUE; + pattern++; + str++; + } + break; - case '*': - didMatch = TRUE; - pattern++; - if (!*pattern) - isEnd = TRUE; - isAnyMode = TRUE; - tmpPattern = pattern; - break; + case '*': + didMatch = TRUE; + pattern++; + if (!*pattern) + isEnd = TRUE; + isAnyMode = TRUE; + tmpPattern = pattern; + break; - case 0: - if (isAnyMode) { - if (*str) - str++; - else - isEnd = TRUE; - } else { - if (*str) { - if (tmpPattern) { - isAnyMode = TRUE; - pattern = tmpPattern; - } else - didMatch = FALSE; - } else - isEnd = TRUE; - } - break; - default: - if (isAnyMode) { - if (*pattern == *str) { - isAnyMode = FALSE; - didMatch = TRUE; - } else { - if (*str) { - didMatch = TRUE; - str++; - } - } - } else { - if (*pattern == *str) { - didMatch = TRUE; - if (*pattern) - pattern++; - if (*str) - str++; - } else { - if (tmpPattern) { - didMatch = TRUE; - isAnyMode = TRUE; - pattern = tmpPattern; - } - } - } + case 0: + if (isAnyMode) { + if (*str) + str++; + else + isEnd = TRUE; + } else { + if (*str) { + if (tmpPattern) { + isAnyMode = TRUE; + pattern = tmpPattern; + } else + didMatch = FALSE; + } else + isEnd = TRUE; + } + break; + default: + if (isAnyMode) { + if (*pattern == *str) { + isAnyMode = FALSE; + didMatch = TRUE; + } else { + if (*str) { + didMatch = TRUE; + str++; + } + } + } else { + if (*pattern == *str) { + didMatch = TRUE; + if (*pattern) + pattern++; + if (*str) + str++; + } else { + if (tmpPattern) { + didMatch = TRUE; + isAnyMode = TRUE; + pattern = tmpPattern; + } + } + } - if (!*str && !*pattern) - isEnd = TRUE; - break; + if (!*str && !*pattern) + isEnd = TRUE; + break; - } /* switch */ + } /* switch */ - } while (didMatch && !isEnd); + } while (didMatch && !isEnd); - return didMatch; + return didMatch; } @@ -354,84 +354,84 @@ */ BOOL th_strcasematch(char * str, char * pattern) { - BOOL didMatch = TRUE, isAnyMode = FALSE, isEnd = FALSE; - char *tmpPattern = NULL; + BOOL didMatch = TRUE, isAnyMode = FALSE, isEnd = FALSE; + char *tmpPattern = NULL; - /* Check given pattern and string */ - if (str == NULL || pattern == NULL) - return FALSE; + /* Check given pattern and string */ + if (str == NULL || pattern == NULL) + return FALSE; - /* Start comparision */ - do { - switch (*pattern) { - case '?': - /* Any single character matches */ - if (*str) { - pattern++; - str++; - } else - didMatch = FALSE; - break; + /* Start comparision */ + do { + switch (*pattern) { + case '?': + /* Any single character matches */ + if (*str) { + pattern++; + str++; + } else + didMatch = FALSE; + break; - case '*': - pattern++; - if (!*pattern || *pattern == '?') - isEnd = TRUE; - isAnyMode = TRUE; - tmpPattern = pattern; - break; + case '*': + pattern++; + if (!*pattern || *pattern == '?') + isEnd = TRUE; + isAnyMode = TRUE; + tmpPattern = pattern; + break; - case 0: - if (isAnyMode) { - if (*str) - str++; - else - isEnd = TRUE; - } else { - if (*str) { - if (tmpPattern) { - isAnyMode = TRUE; - pattern = tmpPattern; - } else - didMatch = FALSE; - } else - isEnd = TRUE; - } - break; + case 0: + if (isAnyMode) { + if (*str) + str++; + else + isEnd = TRUE; + } else { + if (*str) { + if (tmpPattern) { + isAnyMode = TRUE; + pattern = tmpPattern; + } else + didMatch = FALSE; + } else + isEnd = TRUE; + } + break; - default: - if (isAnyMode) { - if (th_tolower(*pattern) == th_tolower(*str)) { - isAnyMode = FALSE; - } else { - if (*str) - str++; - else - didMatch = FALSE; - } - } else { - if (th_tolower(*pattern) == th_tolower(*str)) { - if (*pattern) - pattern++; - if (*str) - str++; - } else { - if (tmpPattern) { - isAnyMode = TRUE; - pattern = tmpPattern; - } else - didMatch = FALSE; - } - } + default: + if (isAnyMode) { + if (th_tolower(*pattern) == th_tolower(*str)) { + isAnyMode = FALSE; + } else { + if (*str) + str++; + else + didMatch = FALSE; + } + } else { + if (th_tolower(*pattern) == th_tolower(*str)) { + if (*pattern) + pattern++; + if (*str) + str++; + } else { + if (tmpPattern) { + isAnyMode = TRUE; + pattern = tmpPattern; + } else + didMatch = FALSE; + } + } - if (!*str && !*pattern) - isEnd = TRUE; - break; + if (!*str && !*pattern) + isEnd = TRUE; + break; - } /* switch */ + } /* switch */ - } while (didMatch && !isEnd); + } while (didMatch && !isEnd); - return didMatch; + return didMatch; } diff -r 335b5a74c22e -r 69aed051f84d th_string.h --- a/th_string.h Fri Jan 30 01:54:51 2009 +0200 +++ b/th_string.h Mon Apr 20 22:02:37 2009 +0300 @@ -18,46 +18,46 @@ /* Macros */ -#define th_isalnum(c) isalnum((int)(unsigned char) c) -#define th_isalpha(c) isalpha((int)(unsigned char) c) -#define th_isascii(c) isascii((int)(unsigned char) c) -#define th_isblank(c) isblank((int)(unsigned char) c) -#define th_iscntrl(c) iscntrl((int)(unsigned char) c) -#define th_isdigit(c) isdigit((int)(unsigned char) c) -#define th_isgraph(c) isgraph((int)(unsigned char) c) -#define th_islower(c) islower((int)(unsigned char) c) -#define th_isprint(c) isprint((int)(unsigned char) c) -#define th_ispunct(c) ispunct((int)(unsigned char) c) -#define th_isspace(c) isspace((int)(unsigned char) c) -#define th_isupper(c) isupper((int)(unsigned char) c) -#define th_isxdigit(c) isxdigit((int)(unsigned char) c) -#define th_iscrlf(c) ((c=='\r')||(c=='\n')) +#define th_isalnum(c) isalnum((int)(unsigned char) c) +#define th_isalpha(c) isalpha((int)(unsigned char) c) +#define th_isascii(c) isascii((int)(unsigned char) c) +#define th_isblank(c) isblank((int)(unsigned char) c) +#define th_iscntrl(c) iscntrl((int)(unsigned char) c) +#define th_isdigit(c) isdigit((int)(unsigned char) c) +#define th_isgraph(c) isgraph((int)(unsigned char) c) +#define th_islower(c) islower((int)(unsigned char) c) +#define th_isprint(c) isprint((int)(unsigned char) c) +#define th_ispunct(c) ispunct((int)(unsigned char) c) +#define th_isspace(c) isspace((int)(unsigned char) c) +#define th_isupper(c) isupper((int)(unsigned char) c) +#define th_isxdigit(c) isxdigit((int)(unsigned char) c) +#define th_iscrlf(c) ((c=='\r')||(c=='\n')) -#define th_isspecial(q) (((q >= 0x5b) && (q <= 0x60)) || ((q >= 0x7b) && (q <= 0x7d))) +#define th_isspecial(q) (((q >= 0x5b) && (q <= 0x60)) || ((q >= 0x7b) && (q <= 0x7d))) -#define th_tolower(c) tolower((int)(unsigned char) c) -#define th_toupper(c) toupper((int)(unsigned char) c) +#define th_tolower(c) tolower((int)(unsigned char) c) +#define th_toupper(c) toupper((int)(unsigned char) c) /* Normal NUL-terminated string functions */ -char *th_stralloc(size_t); -char *th_strrealloc(char *, size_t); -char *th_strncpy(char *, const char *, size_t); -int th_strcasecmp(char *, char *); -int th_strncasecmp(char *, char *, size_t); -void th_strip_ctrlchars(char *); +char *th_stralloc(size_t); +char *th_strrealloc(char *, size_t); +char *th_strncpy(char *, const char *, size_t); +int th_strcasecmp(char *, char *); +int th_strncasecmp(char *, char *, size_t); +void th_strip_ctrlchars(char *); char *th_strdup(const char *); int th_pstrcpy(char **, char *); int th_pstrcat(char **, char *); -char *th_findnext(char *, size_t *); -char *th_findsep(char *, size_t *, char); -char *th_findseporspace(char *, size_t *, char); +char *th_findnext(char *, size_t *); +char *th_findsep(char *, size_t *, char); +char *th_findseporspace(char *, size_t *, char); -BOOL th_strmatch(char *, char *); -BOOL th_strcasematch(char *, char *); +BOOL th_strmatch(char *, char *); +BOOL th_strcasematch(char *, char *); #ifdef __cplusplus diff -r 335b5a74c22e -r 69aed051f84d th_types.h --- a/th_types.h Fri Jan 30 01:54:51 2009 +0200 +++ b/th_types.h Mon Apr 20 22:02:37 2009 +0300 @@ -36,8 +36,8 @@ * feel free to define TH_TYPE_* if necessary to remedy */ #ifdef TH_TYPE_I8 -typedef unsigned TH_TYPE_I8 uint8_t; /* 8 bits, unsigned */ -typedef signed TH_TYPE_I8 int8_t; /* 8 bits, signed */ +typedef unsigned TH_TYPE_I8 uint8_t; /* 8 bits, unsigned */ +typedef signed TH_TYPE_I8 int8_t; /* 8 bits, signed */ #else #ifndef HAVE_INT_TYPES typedef unsigned char uint8_t; @@ -47,8 +47,8 @@ #ifdef TH_TYPE_I16 -typedef unsigned TH_TYPE_I16 uint16_t; /* 16 bits, unsigned == 2 BYTEs */ -typedef signed TH_TYPE_I16 int16_t; /* 16 bits, signed */ +typedef unsigned TH_TYPE_I16 uint16_t; /* 16 bits, unsigned == 2 BYTEs */ +typedef signed TH_TYPE_I16 int16_t; /* 16 bits, signed */ #else #ifndef HAVE_INT_TYPES typedef unsigned short int uint16_t; @@ -57,8 +57,8 @@ #endif #ifdef TH_TYPE_I32 -typedef unsigned TH_TYPE_I32 uint32_t; /* 32 bits, unsigned == 4 BYTES == 2 WORDs */ -typedef signed TH_TYPE_I32 int32_t; /* 32 bits, signed */ +typedef unsigned TH_TYPE_I32 uint32_t; /* 32 bits, unsigned == 4 BYTES == 2 WORDs */ +typedef signed TH_TYPE_I32 int32_t; /* 32 bits, signed */ #else #ifndef HAVE_INT_TYPES typedef unsigned int uint32_t; @@ -67,8 +67,8 @@ #endif #ifdef TH_TYPE_I64 -typedef unsigned TH_TYPE_I64 uint64_t; /* 64 bits, unsigned == 8 BYTES == 2 DWORDs */ -typedef signed TH_TYPE_I64 int64_t; /* 64 bits, signed */ +typedef unsigned TH_TYPE_I64 uint64_t; /* 64 bits, unsigned == 8 BYTES == 2 DWORDs */ +typedef signed TH_TYPE_I64 int64_t; /* 64 bits, signed */ #else #ifndef HAVE_INT_TYPES typedef unsigned long long uint64_t; diff -r 335b5a74c22e -r 69aed051f84d th_util.c --- a/th_util.c Fri Jan 30 01:54:51 2009 +0200 +++ b/th_util.c Mon Apr 20 22:02:37 2009 +0300 @@ -13,35 +13,35 @@ /* Default settings */ -static BOOL th_initialized = FALSE; -int th_verbosityLevel = 2; -char *th_prog_name = NULL, - *th_prog_fullname = NULL, - *th_prog_version = NULL, - *th_prog_author = NULL, - *th_prog_license = NULL; +static BOOL th_initialized = FALSE; +int th_verbosityLevel = 2; +char *th_prog_name = NULL, + *th_prog_fullname = NULL, + *th_prog_version = NULL, + *th_prog_author = NULL, + *th_prog_license = NULL; /* Initialize th_util-library and global variables */ void th_init(char *progName, char *progFullName, char *progVersion, - char *progAuthor, char *progLicense) + char *progAuthor, char *progLicense) { - th_prog_name = progName; - th_prog_fullname = progFullName; - th_prog_version = progVersion; + th_prog_name = progName; + th_prog_fullname = progFullName; + th_prog_version = progVersion; - if (progAuthor) - th_prog_author = progAuthor; - else - th_prog_author = TH_PROG_AUTHOR; + if (progAuthor) + th_prog_author = progAuthor; + else + th_prog_author = TH_PROG_AUTHOR; - if (progLicense) - th_prog_license = progLicense; - else - th_prog_license = TH_PROG_LICENSE; + if (progLicense) + th_prog_license = progLicense; + else + th_prog_license = TH_PROG_LICENSE; - th_initialized = TRUE; + th_initialized = TRUE; } @@ -50,42 +50,42 @@ */ void THERR(const char *pcFormat, ...) { - va_list ap; - assert(th_initialized); + va_list ap; + assert(th_initialized); - va_start(ap, pcFormat); - fprintf(stderr, "%s: ", th_prog_name); - vfprintf(stderr, pcFormat, ap); - va_end(ap); + va_start(ap, pcFormat); + fprintf(stderr, "%s: ", th_prog_name); + vfprintf(stderr, pcFormat, ap); + va_end(ap); } void THMSG(int verbLevel, const char *pcFormat, ...) { - va_list ap; - assert(th_initialized); + va_list ap; + assert(th_initialized); - /* Check if the current verbosity level is enough */ - if (th_verbosityLevel >= verbLevel) { - va_start(ap, pcFormat); - fprintf(stderr, "%s: ", th_prog_name); - vfprintf(stderr, pcFormat, ap); - va_end(ap); - } + /* Check if the current verbosity level is enough */ + if (th_verbosityLevel >= verbLevel) { + va_start(ap, pcFormat); + fprintf(stderr, "%s: ", th_prog_name); + vfprintf(stderr, pcFormat, ap); + va_end(ap); + } } void THPRINT(int verbLevel, const char *pcFormat, ...) { - va_list ap; - assert(th_initialized); + va_list ap; + assert(th_initialized); - /* Check if the current verbosity level is enough */ - if (th_verbosityLevel >= verbLevel) { - va_start(ap, pcFormat); - vfprintf(stderr, pcFormat, ap); - va_end(ap); - } + /* Check if the current verbosity level is enough */ + if (th_verbosityLevel >= verbLevel) { + va_start(ap, pcFormat); + vfprintf(stderr, pcFormat, ap); + va_end(ap); + } } @@ -93,39 +93,39 @@ */ void *th_malloc(size_t l) { - return malloc(l); + return malloc(l); } void *th_calloc(size_t n, size_t l) { - return calloc(n, l); + return calloc(n, l); } void *th_realloc(void *p, size_t l) { - return realloc(p, l); + return realloc(p, l); } void th_free(void *p) { - /* Check for NULL pointers for portability due to some libc - * implementations not handling free(NULL) too well. - */ - if (p) free(p); + /* Check for NULL pointers for portability due to some libc + * implementations not handling free(NULL) too well. + */ + if (p) free(p); } #ifndef HAVE_MEMSET void *th_memset(void *p, int c, size_t n) { - unsigned char *dp = (unsigned char *) p; - - while (n--) - *(dp++) = c; + unsigned char *dp = (unsigned char *) p; + + while (n--) + *(dp++) = c; - return p; + return p; } #endif diff -r 335b5a74c22e -r 69aed051f84d th_util.h --- a/th_util.h Fri Jan 30 01:54:51 2009 +0200 +++ b/th_util.h Mon Apr 20 22:02:37 2009 +0300 @@ -53,7 +53,7 @@ /* Global variables */ -extern int th_verbosityLevel; +extern int th_verbosityLevel; extern char *th_prog_name, *th_prog_fullname, *th_prog_version,