X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=server%2Fsrc%2Fitemdb.c;h=01f801e0c8ada66557443ab2d1e2b4e6ffc47a06;hb=5a97bf48f2507ac7e8c3765959c25a9d4f9b74c3;hp=116b047ee3eb96611fb9c9ba39056930dc987b69;hpb=bb42916dda07ba08b2cf3cba590fa0f74ec27333;p=tpg%2Fopendispense2.git diff --git a/server/src/itemdb.c b/server/src/itemdb.c index 116b047..01f801e 100644 --- a/server/src/itemdb.c +++ b/server/src/itemdb.c @@ -9,11 +9,93 @@ */ #include #include +#include +#include #include "common.h" // === GLOBALS === int giNumItems = 0; 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 + */ +void Load_Itemlist(void) +{ + FILE *fp = fopen(gsItemListFile, "r"); + char buffer[BUFSIZ]; + char *line; + int lineNum = 0; + + // Error check + if(!fp) { + fprintf(stderr, "Unable to open item file '%s'\n", gsItemListFile); + perror("Unable to open item file"); + } + + while( fgets(buffer, BUFSIZ, fp) ) + { + char *tmp; + char *type, *num, *price, *desc; + + lineNum ++; + + // Remove comments + tmp = strchr(buffer, '#'); + if(tmp) *tmp = '\0'; + tmp = strchr(buffer, ';'); + if(tmp) *tmp = '\0'; + + // Trim whitespace + line = trim(buffer); + + // Parse Line + // - Type + type = line; + // - Number + num = strchr(type, ' '); + if(num) { + while(*num == ' ' || *num == '\t') num ++; + } + else { + fprintf(stderr, "Syntax error on line %i of item file\n", lineNum); + continue; + } + // - Price + price = strchr(num, ' '); + if( price ) { + while(*num == ' ' || *num == '\t') num ++; + } + else { + fprintf(stderr, "Syntax error on line %i of item file\n", lineNum); + continue; + } + // - Name/Description + desc = strchr(price, ' '); + } + +} + +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; +}