X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=server%2Fsrc%2Fitemdb.c;h=0ddcbf78fc758ae6f4255101210630e1005df84e;hb=249b8fbd6e839ed85868b60a0e5ef0d65a57fdf3;hp=cf8e7f1cbdcac91a5c197854c26f311f9013b32f;hpb=e5b57935abf86799d97140f2cfa36a10ae8e0526;p=tpg%2Fopendispense2.git diff --git a/server/src/itemdb.c b/server/src/itemdb.c index cf8e7f1..0ddcbf7 100644 --- a/server/src/itemdb.c +++ b/server/src/itemdb.c @@ -9,7 +9,10 @@ */ #include #include +#include +#include #include "common.h" +#include // === GLOBALS === int giNumItems = 0; @@ -17,6 +20,10 @@ tItem *gaItems = NULL; tHandler *gaHandlers = NULL; char *gsItemListFile = DEFAULT_ITEM_FILE; +// === PROTOTYPES === +void Load_Itemlist(void); +char *trim(char *__str); + // === CODE === /** * \brief Read the item list from disk @@ -26,7 +33,23 @@ void Load_Itemlist(void) FILE *fp = fopen(gsItemListFile, "r"); char buffer[BUFSIZ]; char *line; + int lineNum = 0; + int i; + regex_t regex; + regmatch_t matches[5]; + i = regcomp(®ex, "^([a-zA-Z][a-zA-Z0-9]*)\\s+([0-9]+)\\s+([0-9]+)\\s+(.*)", REG_EXTENDED); + //i = regcomp(®ex, "\\(\\d+\\)", 0);//\\s+([0-9]+)\\s+([0-9]+)\\s+(.*)", 0); + if( i ) + { + size_t len = regerror(i, ®ex, NULL, 0); + char *errorStr = malloc(len); + regerror(i, ®ex, errorStr, len); + fprintf(stderr, "Rexex compilation failed - %s\n", errorStr); + free(errorStr); + exit(-1); + } + // Error check if(!fp) { fprintf(stderr, "Unable to open item file '%s'\n", gsItemListFile); @@ -36,7 +59,11 @@ void Load_Itemlist(void) while( fgets(buffer, BUFSIZ, fp) ) { char *tmp; - char *type, *num, *price, *desc; + char *type, *desc; + int num, price; + + lineNum ++; + // Remove comments tmp = strchr(buffer, '#'); if(tmp) *tmp = '\0'; @@ -46,18 +73,42 @@ void Load_Itemlist(void) // Trim whitespace line = trim(buffer); - // Parse Line - // - Type - type = line; - // - Number - num = strchr(type, ' '); - if(num) while(*num == ' ' || *num == '\t'); - if(!num) { - fprintf(stderr, "Syntax error on line %i of item file\n", lineNum); - continue; + if(strlen(line) == 0) continue; + + // Pass regex over line + if( (i = regexec(®ex, line, 5, matches, 0)) ) { + size_t len = regerror(i, ®ex, NULL, 0); + char *errorStr = malloc(len); + regerror(i, ®ex, errorStr, len); + fprintf(stderr, "Syntax error on line %i of item file '%s'\n%s", lineNum, gsItemListFile, errorStr); + free(errorStr); + exit(-1); } - // - Price - price = strchr(num, ' '); - } + + // Read line data + type = line + matches[1].rm_so; line[ matches[1].rm_eo ] = '\0'; + num = atoi( line + matches[2].rm_so ); + price = atoi( line + matches[3].rm_so ); + desc = line + matches[4].rm_so; + + + printf("Item '%s' - %i cents, %s:%i\n", desc, price, type, num); + } +} + +char *trim(char *__str) +{ + char *ret; + int i; + while( isspace(*__str) ) + __str++; + ret = __str; + + i = strlen(ret); + while( i-- && isspace(__str[i]) ) { + __str[i] = '\0'; + } + + return ret; }