#include <string.h>
#include <ctype.h>
#include "common.h"
+#include <regex.h>
// === GLOBALS ===
int giNumItems = 0;
char buffer[BUFSIZ];
char *line;
int lineNum = 0;
+ int i;
+ regex_t regex;
+ regmatch_t matches[5];
+ i = regcomp(®ex, "^([a-zA-Z][a-zA-Z0-9]*)\\s+([0-9]+)\\s+([0-9]+)\\s+(.*)", REG_EXTENDED);
+ //i = regcomp(®ex, "\\(\\d+\\)", 0);//\\s+([0-9]+)\\s+([0-9]+)\\s+(.*)", 0);
+ if( i )
+ {
+ size_t len = regerror(i, ®ex, NULL, 0);
+ char *errorStr = malloc(len);
+ regerror(i, ®ex, errorStr, len);
+ fprintf(stderr, "Rexex compilation failed - %s\n", errorStr);
+ free(errorStr);
+ exit(-1);
+ }
+
// Error check
if(!fp) {
fprintf(stderr, "Unable to open item file '%s'\n", gsItemListFile);
while( fgets(buffer, BUFSIZ, fp) )
{
char *tmp;
- char *type, *num, *price, *desc;
-
+ char *type, *desc;
+ int num, price;
+
lineNum ++;
// Remove comments
// 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;
+ if(strlen(line) == 0) continue;
+
+ // Pass regex over line
+ if( (i = regexec(®ex, line, 5, matches, 0)) ) {
+ size_t len = regerror(i, ®ex, NULL, 0);
+ char *errorStr = malloc(len);
+ regerror(i, ®ex, errorStr, len);
+ fprintf(stderr, "Syntax error on line %i of item file '%s'\n%s", lineNum, gsItemListFile, errorStr);
+ free(errorStr);
+ exit(-1);
}
- // - Name/Description
- desc = strchr(price, ' ');
- }
-
+
+ // Read line data
+ type = line + matches[1].rm_so; line[ matches[1].rm_eo ] = '\0';
+ num = atoi( line + matches[2].rm_so );
+ price = atoi( line + matches[3].rm_so );
+ desc = line + matches[4].rm_so;
+
+
+ printf("Item '%s' - %i cents, %s:%i\n", desc, price, type, num);
+ }
}
char *trim(char *__str)
*/
#include <stdlib.h>
#include <stdio.h>
+#include <string.h>
#include "common.h"
// === IMPORTS ===
-extern void Init_Cokebank(void);
+extern void Init_Cokebank(void); // cokebank.c
extern void Load_Itemlist(void);
extern void Server_Start(void);
extern int giServer_Port;
+extern char *gsItemListFile;
// === GLOBALS ===
int giDebugLevel = 0;
for( i = 1; i < argc; i++ )
{
char *arg = argv[i];
- if( arg[0] == '-' )
+ if( arg[0] == '-' && arg[1] != '-')
{
switch(arg[1])
{
break;
}
}
+ else if( arg[0] == '-' && arg[1] == '-' ) {
+ if( strcmp(arg, "--itemsfile") == 0 ) {
+ gsItemListFile = argv[++i];
+ }
+ else {
+ // Usage error?
+ }
+ }
else {
// Usage Error?
}