3 * UCC (University [of WA] Computer Club) Electronic Accounting System
5 * itemdb.c - Dispense Item Databse
7 * This file is licenced under the 3-clause BSD Licence. See the file COPYING
19 tItem *gaItems = NULL;
20 tHandler *gaHandlers = NULL;
21 char *gsItemListFile = DEFAULT_ITEM_FILE;
24 void Load_Itemlist(void);
25 char *trim(char *__str);
29 * \brief Read the item list from disk
31 void Load_Itemlist(void)
33 FILE *fp = fopen(gsItemListFile, "r");
39 regmatch_t matches[5];
41 i = regcomp(®ex, "^([a-zA-Z][a-zA-Z0-9]*)\\s+([0-9]+)\\s+([0-9]+)\\s+(.*)", REG_EXTENDED);
42 //i = regcomp(®ex, "\\(\\d+\\)", 0);//\\s+([0-9]+)\\s+([0-9]+)\\s+(.*)", 0);
45 size_t len = regerror(i, ®ex, NULL, 0);
46 char *errorStr = malloc(len);
47 regerror(i, ®ex, errorStr, len);
48 fprintf(stderr, "Rexex compilation failed - %s\n", errorStr);
55 fprintf(stderr, "Unable to open item file '%s'\n", gsItemListFile);
56 perror("Unable to open item file");
59 while( fgets(buffer, BUFSIZ, fp) )
68 tmp = strchr(buffer, '#');
70 tmp = strchr(buffer, ';');
76 if(strlen(line) == 0) continue;
78 // Pass regex over line
79 if( (i = regexec(®ex, line, 5, matches, 0)) ) {
80 size_t len = regerror(i, ®ex, NULL, 0);
81 char *errorStr = malloc(len);
82 regerror(i, ®ex, errorStr, len);
83 fprintf(stderr, "Syntax error on line %i of item file '%s'\n%s", lineNum, gsItemListFile, errorStr);
89 type = line + matches[1].rm_so; line[ matches[1].rm_eo ] = '\0';
90 num = atoi( line + matches[2].rm_so );
91 price = atoi( line + matches[3].rm_so );
92 desc = line + matches[4].rm_so;
95 printf("Item '%s' - %i cents, %s:%i\n", desc, price, type, num);
99 char *trim(char *__str)
104 while( isspace(*__str) )
109 while( i-- && isspace(__str[i]) ) {