Skip to content
Snippets Groups Projects
Commit 5a97bf48 authored by John Hodge's avatar John Hodge
Browse files

Working on item database

parent e5b57935
No related merge requests found
...@@ -9,6 +9,8 @@ ...@@ -9,6 +9,8 @@
*/ */
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "common.h" #include "common.h"
// === GLOBALS === // === GLOBALS ===
...@@ -17,6 +19,10 @@ tItem *gaItems = NULL; ...@@ -17,6 +19,10 @@ tItem *gaItems = NULL;
tHandler *gaHandlers = NULL; tHandler *gaHandlers = NULL;
char *gsItemListFile = DEFAULT_ITEM_FILE; char *gsItemListFile = DEFAULT_ITEM_FILE;
// === PROTOTYPES ===
void Load_Itemlist(void);
char *trim(char *__str);
// === CODE === // === CODE ===
/** /**
* \brief Read the item list from disk * \brief Read the item list from disk
...@@ -26,6 +32,7 @@ void Load_Itemlist(void) ...@@ -26,6 +32,7 @@ void Load_Itemlist(void)
FILE *fp = fopen(gsItemListFile, "r"); FILE *fp = fopen(gsItemListFile, "r");
char buffer[BUFSIZ]; char buffer[BUFSIZ];
char *line; char *line;
int lineNum = 0;
// Error check // Error check
if(!fp) { if(!fp) {
...@@ -37,6 +44,9 @@ void Load_Itemlist(void) ...@@ -37,6 +44,9 @@ void Load_Itemlist(void)
{ {
char *tmp; char *tmp;
char *type, *num, *price, *desc; char *type, *num, *price, *desc;
lineNum ++;
// Remove comments // Remove comments
tmp = strchr(buffer, '#'); tmp = strchr(buffer, '#');
if(tmp) *tmp = '\0'; if(tmp) *tmp = '\0';
...@@ -51,13 +61,41 @@ void Load_Itemlist(void) ...@@ -51,13 +61,41 @@ void Load_Itemlist(void)
type = line; type = line;
// - Number // - Number
num = strchr(type, ' '); num = strchr(type, ' ');
if(num) while(*num == ' ' || *num == '\t'); if(num) {
if(!num) { while(*num == ' ' || *num == '\t') num ++;
}
else {
fprintf(stderr, "Syntax error on line %i of item file\n", lineNum); fprintf(stderr, "Syntax error on line %i of item file\n", lineNum);
continue; continue;
} }
// - Price // - Price
price = strchr(num, ' '); 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;
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment