# HG changeset patch # User Matti Hamalainen # Date 1317591106 -10800 # Node ID afbc3bfd3e0375165ce8fb0dc13456c0fa052ca6 # Parent 9ad157feb99aa2ad3bb058f15bff77514f8bdac8 Sync th-libs. diff -r 9ad157feb99a -r afbc3bfd3e03 th_args.c --- a/th_args.c Mon Oct 03 00:04:33 2011 +0300 +++ b/th_args.c Mon Oct 03 00:31:46 2011 +0300 @@ -154,9 +154,9 @@ /* 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; @@ -211,9 +211,9 @@ /* 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; @@ -365,7 +365,7 @@ /* Print help for commandline arguments/options */ -void th_args_help(FILE * outFile, +void th_args_help(FILE *outFile, optarg_t optList[], int optListN, char *progName, char *progUsage) { diff -r 9ad157feb99a -r afbc3bfd3e03 th_config.c --- a/th_config.c Mon Oct 03 00:04:33 2011 +0300 +++ b/th_config.c Mon Oct 03 00:31:46 2011 +0300 @@ -23,12 +23,13 @@ { cfgitem_t *curr = cfg; - while (curr != NULL) { + while (curr != NULL) + { cfgitem_t *next = curr->next; - + if (curr->type == ITEM_SECTION) th_cfg_free((cfgitem_t *) curr->v.data); - + th_free(curr->name); th_free(curr); curr = next; @@ -38,7 +39,8 @@ /* Allocate and add new item to configuration */ -static cfgitem_t *th_cfg_add(cfgitem_t **cfg, const char *name, const int type, void *data) +static cfgitem_t *th_cfg_add(cfgitem_t **cfg, const char *name, + const int type, void *data) { cfgitem_t *node; @@ -54,13 +56,16 @@ node->type = type; node->v.data = data; node->name = th_strdup(name); - + /* Insert into linked list */ - if (*cfg != NULL) { + if (*cfg != NULL) + { node->prev = (*cfg)->prev; (*cfg)->prev->next = node; (*cfg)->prev = node; - } else { + } + else + { *cfg = node; node->prev = node; } @@ -72,8 +77,7 @@ /* Add integer type setting into give configuration */ -int th_cfg_add_int(cfgitem_t ** cfg, char * name, - int *itemData, int itemDef) +int th_cfg_add_int(cfgitem_t **cfg, char *name, int *itemData, int itemDef) { cfgitem_t *node; @@ -87,8 +91,8 @@ } -int th_cfg_add_hexvalue(cfgitem_t ** cfg, char * name, - int *itemData, int itemDef) +int th_cfg_add_hexvalue(cfgitem_t **cfg, char *name, + int *itemData, int itemDef) { cfgitem_t *node; @@ -104,8 +108,8 @@ /* Add unsigned integer type setting into give configuration */ -int th_cfg_add_uint(cfgitem_t ** cfg, char * name, - unsigned int * itemData, unsigned int itemDef) +int th_cfg_add_uint(cfgitem_t **cfg, char *name, + unsigned int *itemData, unsigned int itemDef) { cfgitem_t *node; @@ -121,8 +125,8 @@ /* Add strint type setting into given configuration */ -int th_cfg_add_string(cfgitem_t ** cfg, char * name, - char ** itemData, char * itemDef) +int th_cfg_add_string(cfgitem_t **cfg, char *name, + char **itemData, char *itemDef) { cfgitem_t *node; @@ -138,8 +142,8 @@ /* Add boolean type setting into given configuration */ -int th_cfg_add_bool(cfgitem_t ** cfg, char * name, - BOOL * itemData, BOOL itemDef) +int th_cfg_add_bool(cfgitem_t **cfg, char *name, + BOOL *itemData, BOOL itemDef) { cfgitem_t *node; @@ -155,7 +159,7 @@ /* Add implicit comment */ -int th_cfg_add_comment(cfgitem_t ** cfg, char * comment) +int th_cfg_add_comment(cfgitem_t **cfg, char *comment) { cfgitem_t *node; @@ -169,10 +173,10 @@ /* Add new section */ -int th_cfg_add_section(cfgitem_t ** cfg, char * name, cfgitem_t *data) +int th_cfg_add_section(cfgitem_t **cfg, char *name, cfgitem_t *data) { cfgitem_t *node; - + node = th_cfg_add(cfg, name, ITEM_SECTION, (void *) data); if (node == NULL) return -1; @@ -181,13 +185,13 @@ } -int th_cfg_add_string_list(cfgitem_t ** cfg, char * name, qlist_t **data) +int th_cfg_add_string_list(cfgitem_t **cfg, char *name, qlist_t **data) { cfgitem_t *node; - + if (data == NULL) return -5; - + node = th_cfg_add(cfg, name, ITEM_STRING_LIST, (void *) data); if (node == NULL) return -1; @@ -198,7 +202,8 @@ /* Read a given file into configuration structure and variables */ -enum { +enum +{ PM_EOF, PM_ERROR, PM_NORMAL, @@ -216,7 +221,8 @@ #define VADDCH(ch) if (strPos < SET_MAX_BUF) { tmpStr[strPos++] = ch; } #define VISEND(ch) (ch == '\r' || ch == '\n' || ch == ';' || th_isspace(c) || ch == '#') -typedef struct { +typedef struct +{ FILE *file; char *filename; size_t line; @@ -227,13 +233,14 @@ { va_list ap; va_start(ap, fmt); - fprintf(stderr, "%s: '%s', line #%d: ", th_prog_name, f->filename, (unsigned int) f->line); + fprintf(stderr, "%s: '%s', line #%d: ", th_prog_name, f->filename, + (unsigned int) f->line); vfprintf(stderr, fmt, ap); va_end(ap); } -static int th_cfg_read_sect(conffile_t *f, cfgitem_t * cfg, int nesting) +static int th_cfg_read_sect(conffile_t *f, cfgitem_t *cfg, int nesting) { cfgitem_t *item = NULL; char tmpStr[SET_MAX_BUF + 1]; @@ -249,16 +256,20 @@ nextMode = prevMode = parseMode = PM_NORMAL; /* Parse the configuration */ - while (parseMode != PM_EOF && parseMode != PM_ERROR) { - if (c == -1) { + while (parseMode != PM_EOF && parseMode != PM_ERROR) + { + if (c == -1) + { /* Get next character */ - switch (c = fgetc(f->file)) { + switch (c = fgetc(f->file)) + { case EOF: - if (parseMode != PM_NORMAL) { - th_cfg_error(f, - "Unexpected end of file.\n"); + if (parseMode != PM_NORMAL) + { + th_cfg_error(f, "Unexpected end of file.\n"); parseMode = PM_ERROR; - } else + } + else parseMode = PM_EOF; break; @@ -267,10 +278,12 @@ } } - switch (parseMode) { + switch (parseMode) + { case PM_COMMENT: /* Comment parsing mode */ - if (c == '\n') { + if (c == '\n') + { /* End of line, end of comment */ parseMode = prevMode; prevMode = PM_COMMENT; @@ -280,72 +293,93 @@ case PM_NORMAL: /* Normal parsing mode */ - if (c == '#') { + if (c == '#') + { prevMode = parseMode; parseMode = PM_COMMENT; c = -1; - } else if (VISEND(c)) { + } + else if (VISEND(c)) + { c = -1; - } else if (c == '}') { - if (nesting > 0) { + } + else if (c == '}') + { + if (nesting > 0) + { /* Check for validation errors */ return (validError) ? 1 : 0; - } else { - th_cfg_error(f, "Invalid nesting sequence encountered.\n"); + } + else + { + th_cfg_error(f, + "Invalid nesting sequence encountered.\n"); parseMode = PM_ERROR; } - } else if (th_isalpha(c)) { + } + else if (th_isalpha(c)) + { /* Start of key name found */ prevMode = parseMode; parseMode = PM_KEYNAME; strPos = 0; - } else { + } + else + { /* Error! Invalid character found */ - th_cfg_error(f, - "Unexpected character '%c'.\n", c); + th_cfg_error(f, "Unexpected character '%c'.\n", c); parseMode = PM_ERROR; } break; case PM_KEYNAME: /* Configuration KEY name parsing mode */ - if (c == '#') { + if (c == '#') + { /* Start of comment */ prevMode = parseMode; parseMode = PM_COMMENT; c = -1; - } else if (th_iscrlf(c) || th_isspace(c) || c == '=') { + } + else if (th_iscrlf(c) || th_isspace(c) || c == '=') + { /* End of key name */ prevMode = parseMode; parseMode = PM_NEXT; nextMode = PM_KEYSET; - } else if (th_isalnum(c) || c == '_') { + } + else if (th_isalnum(c) || c == '_') + { /* Add to key name string */ VADDCH(c) else { /* Error! Key name string too long! */ - th_cfg_error(f, - "Config key name too long!"); + th_cfg_error(f, "Config key name too long!"); parseMode = PM_ERROR; } c = -1; - } else { + } + else + { /* Error! Invalid character found */ tmpStr[strPos] = 0; th_cfg_error(f, - "Unexpected character '%c' in key name '%s'.\n", c, tmpStr); + "Unexpected character '%c' in key name '%s'.\n", + c, tmpStr); parseMode = PM_ERROR; } break; case PM_KEYSET: - if (c == '=') { + if (c == '=') + { /* Find key from configuration */ tmpStr[strPos] = 0; isFound = FALSE; item = cfg; - while (item != NULL && !isFound) { + while (item != NULL && !isFound) + { if (item->name != NULL && strcmp(item->name, tmpStr) == 0) isFound = TRUE; else @@ -353,9 +387,11 @@ } /* Check if key was found */ - if (isFound) { + if (isFound) + { /* Okay, set next mode */ - switch (item->type) { + switch (item->type) + { case ITEM_HEX_TRIPLET: case ITEM_STRING: nextMode = PM_STRING; @@ -373,7 +409,7 @@ case ITEM_BOOL: nextMode = PM_BOOL; break; - + case ITEM_SECTION: nextMode = PM_SECTION; break; @@ -383,32 +419,43 @@ parseMode = PM_NEXT; isStart = TRUE; strPos = 0; - } else { + } + else + { /* Error! No configuration key by this name found */ th_cfg_error(f, - "No such configuration setting ('%s')\n", tmpStr); + "No such configuration setting ('%s')\n", + tmpStr); parseMode = PM_ERROR; } c = -1; - } else { + } + else + { /* Error! '=' expected! */ th_cfg_error(f, - "Unexpected character '%c', assignation '=' was expected.\n", c); + "Unexpected character '%c', assignation '=' was expected.\n", + c); parseMode = PM_ERROR; } break; case PM_NEXT: /* Search next item parsing mode */ - if (c == '#') { + if (c == '#') + { /* Start of comment */ prevMode = parseMode; parseMode = PM_COMMENT; - } else if (th_isspace(c) || th_iscrlf(c)) { + } + else if (th_isspace(c) || th_iscrlf(c)) + { /* Ignore whitespaces and linechanges */ c = -1; - } else { + } + else + { /* Next item found */ prevMode = parseMode; parseMode = nextMode; @@ -416,24 +463,31 @@ break; case PM_ARRAY: - if (isStart) { - switch (item->type) { - case ITEM_STRING_LIST: - prevMode = parseMode; - parseMode = PM_STRING; - break; + if (isStart) + { + switch (item->type) + { + case ITEM_STRING_LIST: + prevMode = parseMode; + parseMode = PM_STRING; + break; } - } else if (c == ',') { - switch (item->type) { - case ITEM_STRING_LIST: - c = -1; - isStart = TRUE; - prevMode = parseMode; - parseMode = PM_NEXT; - nextMode = PM_STRING; - break; + } + else if (c == ',') + { + switch (item->type) + { + case ITEM_STRING_LIST: + c = -1; + isStart = TRUE; + prevMode = parseMode; + parseMode = PM_NEXT; + nextMode = PM_STRING; + break; } - } else { + } + else + { prevMode = parseMode; parseMode = PM_NORMAL; } @@ -441,64 +495,75 @@ case PM_SECTION: /* Section parsing mode */ - if (c != '{') { + if (c != '{') + { /* Error! Section start '{' expected! */ th_cfg_error(f, - "Unexpected character '%c', section start '{' was expected.\n", c); + "Unexpected character '%c', section start '{' was expected.\n", + c); parseMode = PM_ERROR; - } else { + } + else + { int res = th_cfg_read_sect(f, item->v.section, nesting + 1); c = -1; if (res > 0) validError = TRUE; else if (res < 0) parseMode = PM_ERROR; - else { + else + { prevMode = parseMode; parseMode = PM_NORMAL; } } break; - + case PM_STRING: /* String parsing mode */ - if (isStart) { + if (isStart) + { /* Start of string, get delimiter */ tmpCh = c; isStart = FALSE; strPos = 0; - } else if (c == tmpCh) { + } + else if (c == tmpCh) + { /* End of string, set the value */ tmpStr[strPos] = 0; - - switch (item->type) { - case ITEM_HEX_TRIPLET: - *(item->v.val_int) = th_get_hex_triplet(tmpStr); - prevMode = parseMode; - parseMode = PM_NORMAL; - break; - case ITEM_STRING: - th_pstrcpy(item->v.val_str, tmpStr); - prevMode = parseMode; - parseMode = PM_NORMAL; - break; - case ITEM_STRING_LIST: - th_llist_append(item->v.list, th_strdup(tmpStr)); - prevMode = parseMode; - parseMode = PM_NEXT; - nextMode = PM_ARRAY; - break; + + switch (item->type) + { + case ITEM_HEX_TRIPLET: + *(item->v.val_int) = th_get_hex_triplet(tmpStr); + prevMode = parseMode; + parseMode = PM_NORMAL; + break; + case ITEM_STRING: + th_pstrcpy(item->v.val_str, tmpStr); + prevMode = parseMode; + parseMode = PM_NORMAL; + break; + case ITEM_STRING_LIST: + th_llist_append(item->v.list, th_strdup(tmpStr)); + prevMode = parseMode; + parseMode = PM_NEXT; + nextMode = PM_ARRAY; + break; } - - } else { + + } + else + { /* Add character to string */ VADDCH(c) else { /* Error! String too long! */ th_cfg_error(f, - "String too long! Maximum is %d characters.", - SET_MAX_BUF); + "String too long! Maximum is %d characters.", + SET_MAX_BUF); parseMode = PM_ERROR; } } @@ -508,24 +573,32 @@ case PM_INT: /* Integer parsing mode */ - if (isStart && item->type == ITEM_UINT && c == '-') { + if (isStart && item->type == ITEM_UINT && c == '-') + { /* Error! Negative values not allowed for unsigned ints */ th_cfg_error(f, - "Negative value specified for %s, unsigned value expected.", - item->name); + "Negative value specified for %s, unsigned value expected.", + item->name); parseMode = PM_ERROR; - } else if (isStart && (c == '-' || c == '+')) { + } + else if (isStart && (c == '-' || c == '+')) + { VADDCH(c) else isError = TRUE; - } else if (th_isdigit(c)) { + } + else if (th_isdigit(c)) + { VADDCH(c) else isError = TRUE; - } else if (VISEND(c)) { + } + else if (VISEND(c)) + { /* End of integer parsing mode */ tmpStr[strPos] = 0; - switch (item->type) { + switch (item->type) + { case ITEM_INT: *(item->v.val_int) = atoi(tmpStr); break; @@ -537,18 +610,21 @@ prevMode = parseMode; parseMode = PM_NORMAL; - } else { + } + else + { /* Error! Unexpected character. */ th_cfg_error(f, - "Unexpected character '%c' for integer setting '%s'.", - c, item->name); + "Unexpected character '%c' for integer setting '%s'.", + c, item->name); parseMode = PM_ERROR; } - if (isError) { + if (isError) + { /* Error! String too long! */ th_cfg_error(f, "String too long! Maximum is %d characters.", - SET_MAX_BUF); + SET_MAX_BUF); parseMode = PM_ERROR; } @@ -558,34 +634,46 @@ case PM_BOOL: /* Boolean parsing mode */ - if (isStart) { + if (isStart) + { tmpCh = c; isStart = FALSE; - } else if (VISEND(c)) { + } + else if (VISEND(c)) + { BOOL tmpBool = FALSE; - + /* End of boolean parsing */ - switch (tmpCh) { - case 'Y': case 'y': - case 'T': case 't': + switch (tmpCh) + { + case 'Y': + case 'y': + case 'T': + case 't': case '1': tmpBool = TRUE; break; - - case 'N': case 'n': - case 'F': case 'f': + + case 'N': + case 'n': + case 'F': + case 'f': case '0': tmpBool = FALSE; break; - + default: isError = TRUE; } - - if (isError) { - th_cfg_error(f, "Invalid boolean value for '%s'.\n", item->name); + + if (isError) + { + th_cfg_error(f, "Invalid boolean value for '%s'.\n", + item->name); parseMode = PM_ERROR; - } else { + } + else + { *(item->v.val_bool) = tmpBool; prevMode = parseMode; @@ -609,14 +697,14 @@ } -int th_cfg_read(FILE *inFile, char *filename, cfgitem_t * cfg) +int th_cfg_read(FILE *inFile, char *filename, cfgitem_t *cfg) { conffile_t f; - + f.file = inFile; f.filename = filename; f.line = 1; - + return th_cfg_read_sect(&f, cfg, 0); } @@ -633,42 +721,57 @@ static int th_cfg_write_sect(conffile_t *f, cfgitem_t *item, int nesting) { - while (item != NULL) { - if (item->type == ITEM_COMMENT) { + while (item != NULL) + { + if (item->type == ITEM_COMMENT) + { th_print_indent(f, nesting); - if (fprintf(f->file, "# %s\n", (item->name != NULL) ? item->name : "" ) < 0) + if (fprintf + (f->file, "# %s\n", + (item->name != NULL) ? item->name : "") < 0) return -1; - } else - if (item->name != NULL) { + } + else if (item->name != NULL) + { th_print_indent(f, nesting); - - switch (item->type) { + + switch (item->type) + { case ITEM_STRING: - if (*(item->v.val_str) == NULL) { + if (*(item->v.val_str) == NULL) + { if (fprintf(f->file, "#%s = \"\"\n", item->name) < 0) return -3; - } else { + } + else + { if (fprintf(f->file, "%s = \"%s\"\n", - item->name, *(item->v.val_str)) < 0) + item->name, *(item->v.val_str)) < 0) return -3; } break; case ITEM_STRING_LIST: - if (*(item->v.list) == NULL) { - if (fprintf(f->file, "#%s = \"\", \"\"\n", item->name) < 0) + if (*(item->v.list) == NULL) + { + if (fprintf(f->file, "#%s = \"\", \"\"\n", item->name) < + 0) return -3; - } else { + } + else + { qlist_t *node = *(item->v.list); size_t n = th_llist_length(node); if (fprintf(f->file, "%s = ", item->name) < 0) return -3; - - while (node != NULL) { + + while (node != NULL) + { if (node->data != NULL) fprintf(f->file, "\"%s\"", (char *) node->data); - if (--n > 0) { + if (--n > 0) + { fprintf(f->file, ",\n"); th_print_indent(f, nesting); } @@ -682,44 +785,46 @@ case ITEM_INT: if (fprintf(f->file, "%s = %i\n", - item->name, *(item->v.val_int)) < 0) + item->name, *(item->v.val_int)) < 0) return -4; break; case ITEM_UINT: if (fprintf(f->file, "%s = %d\n", - item->name, *(item->v.val_uint)) < 0) + item->name, *(item->v.val_uint)) < 0) return -5; break; case ITEM_BOOL: if (fprintf(f->file, "%s = %s\n", - item->name, *(item->v.val_bool) ? "yes" : "no") < 0) + item->name, + *(item->v.val_bool) ? "yes" : "no") < 0) return -6; break; - + case ITEM_SECTION: { - int res; - if (fprintf(f->file, "%s = {\n", item->name) < 0) - return -7; - res = th_cfg_write_sect(f, item->v.section, nesting + 1); - if (res != 0) return res; - if (fprintf(f->file, "}\n\n") < 0) - return -8; + int res; + if (fprintf(f->file, "%s = {\n", item->name) < 0) + return -7; + res = th_cfg_write_sect(f, item->v.section, nesting + 1); + if (res != 0) + return res; + if (fprintf(f->file, "}\n\n") < 0) + return -8; } break; - + case ITEM_HEX_TRIPLET: if (fprintf(f->file, "%s = \"%06x\"\n", - item->name, *(item->v.val_int)) < 0) + item->name, *(item->v.val_int)) < 0) return -6; break; } } item = item->next; } - + return 0; } @@ -727,18 +832,16 @@ int th_cfg_write(FILE *outFile, char *filename, cfgitem_t *cfg) { conffile_t f; - + if (cfg == NULL) return -1; f.file = outFile; f.filename = filename; f.line = 1; - - fprintf(outFile, "# Configuration written by %s %s\n\n", - th_prog_fullname, th_prog_version); - + + fprintf(outFile, "# Configuration written by %s %s\n\n", + th_prog_fullname, th_prog_version); + return th_cfg_write_sect(&f, cfg, 0); } - - diff -r 9ad157feb99a -r afbc3bfd3e03 th_string.c --- a/th_string.c Mon Oct 03 00:04:33 2011 +0300 +++ b/th_string.c Mon Oct 03 00:31:46 2011 +0300 @@ -17,16 +17,16 @@ char *res; if (s == NULL) return NULL; - + if ((res = th_malloc(strlen(s) + 1)) == NULL) return NULL; - + strcpy(res, s); return res; } -char *th_strncpy(char * dst, const char * src, const size_t n) +char *th_strncpy(char *dst, const char *src, const size_t n) { const char *s = src; char *d = dst; @@ -58,14 +58,14 @@ /* Simulate a sprintf() that allocates memory */ -char * th_strdup_vprintf(const char *fmt, va_list args) +char *th_strdup_vprintf(const char *fmt, va_list args) { int size = 64; char *buf, *nbuf = NULL; if ((buf = th_malloc(size)) == NULL) return NULL; - + while (1) { int n; @@ -86,13 +86,13 @@ th_free(buf); return NULL; } - + buf = nbuf; } } -char * th_strdup_printf(const char *fmt, ...) +char *th_strdup_printf(const char *fmt, ...) { char *res; va_list ap; @@ -128,7 +128,7 @@ /* Compare two strings ignoring case [strcasecmp, strncasecmp] */ -int th_strcasecmp(const char * str1, const char * str2) +int th_strcasecmp(const char *str1, const char *str2) { const char *s1 = str1, *s2 = str2; assert(str1 != NULL); @@ -147,7 +147,7 @@ } -int th_strncasecmp(const char * str1, const char * str2, size_t n) +int th_strncasecmp(const char *str1, const char *str2, size_t n) { const char *s1 = str1, *s2 = str2; assert(str1 != NULL); @@ -170,7 +170,7 @@ /* Remove all occurences of control characters, in-place. * Resulting string is always shorter or same length than original. */ -void th_strip_ctrlchars(char * str) +void th_strip_ctrlchars(char *str) { char *i, *j; assert(str != NULL); @@ -190,7 +190,7 @@ /* Copy a given string over in *result. */ -int th_pstrcpy(char ** result, const char * str) +int th_pstrcpy(char **result, const char *str) { assert(result != NULL); @@ -208,7 +208,7 @@ /* Concatenates a given string into string pointed by *result. */ -int th_pstrcat(char ** result, const char * str) +int th_pstrcat(char **result, const char *str) { assert(result != NULL); @@ -240,7 +240,7 @@ * Updates iPos into the position of such character and * returns pointer to the string. */ -const char *th_findnext(const char * str, size_t * pos) +const char *th_findnext(const char *str, size_t *pos) { assert(str != NULL); @@ -254,7 +254,7 @@ /* Find next sep-character from string */ -const char *th_findsep(const char * str, size_t * pos, char sep) +const char *th_findsep(const char *str, size_t *pos, char sep) { assert(str != NULL); @@ -267,7 +267,7 @@ /* Find next sep- or whitespace from string */ -const char *th_findseporspace(const char * str, size_t * pos, char sep) +const char *th_findseporspace(const char *str, size_t *pos, char sep) { assert(str != NULL); @@ -283,7 +283,7 @@ * wildcards ? and *. "?" matches any character and "*" matches * any number of characters. */ -BOOL th_strmatch(const char * str, const char * pattern) +BOOL th_strmatch(const char *str, const char *pattern) { BOOL didMatch = TRUE, isAnyMode = FALSE, isEnd = FALSE; const char *tmpPattern = NULL; @@ -293,7 +293,8 @@ return FALSE; /* Start comparision */ - do { + do + { didMatch = FALSE; switch (*pattern) { @@ -335,7 +336,8 @@ } else didMatch = FALSE; - } else + } + else isEnd = TRUE; } break; @@ -381,9 +383,10 @@ isEnd = TRUE; break; - } /* switch */ + } /* switch */ - } while (didMatch && !isEnd); + } + while (didMatch && !isEnd); return didMatch; } @@ -391,7 +394,7 @@ /* Compare a string to a pattern. Case-INSENSITIVE version. */ -BOOL th_strcasematch(const char * str, const char * pattern) +BOOL th_strcasematch(const char *str, const char *pattern) { BOOL didMatch = TRUE, isAnyMode = FALSE, isEnd = FALSE; const char *tmpPattern = NULL; @@ -401,15 +404,18 @@ return FALSE; /* Start comparision */ - do { - switch (*pattern) { + do + { + switch (*pattern) + { case '?': /* Any single character matches */ if (*str) { pattern++; str++; - } else + } + else didMatch = FALSE; break; @@ -440,7 +446,8 @@ } else didMatch = FALSE; - } else + } + else isEnd = TRUE; } break; @@ -486,9 +493,10 @@ break; - } /* switch */ + } /* switch */ - } while (didMatch && !isEnd); + } + while (didMatch && !isEnd); return didMatch; } @@ -498,7 +506,7 @@ { const char *p = str; int len, val = 0; - + for (len = 0; *p && len < 6; p++, len++) { if (*p >= '0' && *p <= '9') @@ -519,7 +527,7 @@ else return -1; } - + return (len == 6) ? val : -1; } @@ -528,7 +536,7 @@ { if (*buf == NULL) *bufsize = *len = 0; - + if (*buf == NULL || *len + grow >= *bufsize) { *bufsize += grow + TH_BUFGROW; @@ -544,7 +552,7 @@ { if (!th_growbuf(buf, bufsize, len, 1)) return FALSE; - + (*buf)[*len] = ch; (*len)++; @@ -557,14 +565,13 @@ size_t slen; if (str == NULL) return FALSE; - + slen = strlen(str); if (!th_growbuf(buf, bufsize, len, slen)) return FALSE; strcpy(*buf + *len, str); (*len) += slen; - + return TRUE; } -