Item file parsing :)
[tpg/opendispense2.git] / server / src / itemdb.c
1 /*
2  * OpenDispense 2 
3  * UCC (University [of WA] Computer Club) Electronic Accounting System
4  *
5  * itemdb.c - Dispense Item Databse
6  *
7  * This file is licenced under the 3-clause BSD Licence. See the file COPYING
8  * for full details.
9  */
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <string.h>
13 #include <ctype.h>
14 #include "common.h"
15 #include <regex.h>
16
17 // === GLOBALS ===
18  int    giNumItems = 0;
19 tItem   *gaItems = NULL;
20 tHandler        *gaHandlers = NULL;
21 char    *gsItemListFile = DEFAULT_ITEM_FILE;
22
23 // === PROTOTYPES ===
24 void    Load_Itemlist(void);
25 char    *trim(char *__str);
26
27 // === CODE ===
28 /**
29  * \brief Read the item list from disk
30  */
31 void Load_Itemlist(void)
32 {
33         FILE    *fp = fopen(gsItemListFile, "r");
34         char    buffer[BUFSIZ];
35         char    *line;
36          int    lineNum = 0;
37          int    i;
38         regex_t regex;
39         regmatch_t      matches[5];
40         
41         i = regcomp(&regex, "^([a-zA-Z][a-zA-Z0-9]*)\\s+([0-9]+)\\s+([0-9]+)\\s+(.*)", REG_EXTENDED);
42         //i = regcomp(&regex, "\\(\\d+\\)", 0);//\\s+([0-9]+)\\s+([0-9]+)\\s+(.*)", 0);
43         if( i )
44         {
45                 size_t  len = regerror(i, &regex, NULL, 0);
46                 char    *errorStr = malloc(len);
47                 regerror(i, &regex, errorStr, len);
48                 fprintf(stderr, "Rexex compilation failed - %s\n", errorStr);
49                 free(errorStr);
50                 exit(-1);
51         }
52
53         // Error check
54         if(!fp) {
55                 fprintf(stderr, "Unable to open item file '%s'\n", gsItemListFile);
56                 perror("Unable to open item file");
57         }
58         
59         while( fgets(buffer, BUFSIZ, fp) )
60         {
61                 char    *tmp;
62                 char    *type, *desc;
63                  int    num, price;
64
65                 lineNum ++;
66
67                 // Remove comments
68                 tmp = strchr(buffer, '#');
69                 if(tmp) *tmp = '\0';
70                 tmp = strchr(buffer, ';');
71                 if(tmp) *tmp = '\0';
72                 
73                 // Trim whitespace
74                 line = trim(buffer);
75                 
76                 if(strlen(line) == 0)   continue;
77                 
78                 // Pass regex over line
79                 if( (i = regexec(&regex, line, 5, matches, 0)) ) {
80                         size_t  len = regerror(i, &regex, NULL, 0);
81                         char    *errorStr = malloc(len);
82                         regerror(i, &regex, errorStr, len);
83                         fprintf(stderr, "Syntax error on line %i of item file '%s'\n%s", lineNum, gsItemListFile, errorStr);
84                         free(errorStr);
85                         exit(-1);
86                 }
87
88                 // Read line data
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;
93                 
94
95                 printf("Item '%s' - %i cents, %s:%i\n", desc, price, type, num);
96         }       
97 }
98
99 char *trim(char *__str)
100 {
101         char    *ret;
102          int    i;
103         
104         while( isspace(*__str) )
105                 __str++;
106         ret = __str;
107
108         i = strlen(ret);
109         while( i-- && isspace(__str[i]) ) {
110                 __str[i] = '\0';
111         }
112
113         return ret;
114 }

UCC git Repository :: git.ucc.asn.au