Working on item database
[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
16 // === GLOBALS ===
17  int    giNumItems = 0;
18 tItem   *gaItems = NULL;
19 tHandler        *gaHandlers = NULL;
20 char    *gsItemListFile = DEFAULT_ITEM_FILE;
21
22 // === PROTOTYPES ===
23 void    Load_Itemlist(void);
24 char    *trim(char *__str);
25
26 // === CODE ===
27 /**
28  * \brief Read the item list from disk
29  */
30 void Load_Itemlist(void)
31 {
32         FILE    *fp = fopen(gsItemListFile, "r");
33         char    buffer[BUFSIZ];
34         char    *line;
35          int    lineNum = 0;
36         
37         // Error check
38         if(!fp) {
39                 fprintf(stderr, "Unable to open item file '%s'\n", gsItemListFile);
40                 perror("Unable to open item file");
41         }
42         
43         while( fgets(buffer, BUFSIZ, fp) )
44         {
45                 char    *tmp;
46                 char    *type, *num, *price, *desc;
47                 
48                 lineNum ++;
49
50                 // Remove comments
51                 tmp = strchr(buffer, '#');
52                 if(tmp) *tmp = '\0';
53                 tmp = strchr(buffer, ';');
54                 if(tmp) *tmp = '\0';
55                 
56                 // Trim whitespace
57                 line = trim(buffer);
58                 
59                 // Parse Line
60                 // - Type
61                 type = line;
62                 // - Number
63                 num = strchr(type, ' ');
64                 if(num) {
65                         while(*num == ' ' || *num == '\t')      num ++;
66                 }
67                 else {
68                         fprintf(stderr, "Syntax error on line %i of item file\n", lineNum);
69                         continue;
70                 }
71                 // - Price
72                 price = strchr(num, ' ');
73                 if( price ) {
74                         while(*num == ' ' || *num == '\t')      num ++;
75                 }
76                 else {
77                         fprintf(stderr, "Syntax error on line %i of item file\n", lineNum);
78                         continue;
79                 }
80                 // - Name/Description
81                 desc = strchr(price, ' ');
82         }
83         
84 }
85
86 char *trim(char *__str)
87 {
88         char    *ret;
89          int    i;
90         
91         while( isspace(*__str) )
92                 __str++;
93         ret = __str;
94
95         i = strlen(ret);
96         while( i-- && isspace(__str[i]) ) {
97                 __str[i] = '\0';
98         }
99
100         return ret;
101 }

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