-
Notifications
You must be signed in to change notification settings - Fork 74
/
commodities.c
178 lines (154 loc) · 3.78 KB
/
commodities.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
Copyright (C) 2014 Stephen M. Cameron
Author: Stephen M. Cameron
This file is part of Spacenerds In Space.
Spacenerds in Space is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Spacenerds in Space is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Spacenerds in Space; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include "commodities.h"
static void clean_spaces(char *line)
{
char *s, *d;
int skip_spaces = 1;
s = line;
d = line;
while (*s) {
if ((*s == ' ' || *s == '\t' || *s == '\n' || *s == '\r') && skip_spaces) {
s++;
continue;
}
skip_spaces = 0;
if (*s == '\t' || *s == '\n' || *s == '\r')
*s = ' ';
if (*s == ' ')
skip_spaces = 1;
*d = *s;
s++;
d++;
}
*d = '\0';
}
static int parse_error(char *filename, char *line, int ln, char *bad_word)
{
if (bad_word)
printf("Error at line %s:%d at %s\n", filename, ln, bad_word);
else
printf("Error at line %s:%d\n", filename, ln);
return -1;
}
static int parse_float_field(char *filename, char *line, int ln, float *value, char **saveptr)
{
char *c;
char word[100];
int rc;
c = strtok_r(NULL, ",", saveptr);
if (!c)
return parse_error(filename, line, ln, NULL);
strcpy(word, c);
clean_spaces(word);
rc = sscanf(word, "%f", value);
if (rc != 1)
return parse_error(filename, line, ln, word);
return 0;
}
static void uppercase(char *w)
{
char *i;
for (i = w; *i; i++)
*i = toupper(*i);
}
static int parse_line(char *filename, char *line, int ln, struct commodity *c)
{
char *x, *saveptr;
char word[100];
int rc;
if (line[0] == '#')
return 1;
clean_spaces(line);
if (strcmp(line, "") == 0)
return 1;
/* Name */
x = strtok_r(line, ",", &saveptr);
if (!x)
return parse_error(filename, line, ln, NULL);
strcpy(word, x);
clean_spaces(word);
uppercase(word);
strcpy(c->name, word);
/* unit */
x = strtok_r(NULL, ",", &saveptr);
if (!x)
return parse_error(filename, line, ln, NULL);
strcpy(word, x);
clean_spaces(word);
uppercase(word);
strcpy(c->unit, word);
rc = parse_float_field(filename, line, ln, &c->base_price, &saveptr);
if (rc)
return rc;
rc = parse_float_field(filename, line, ln, &c->volatility, &saveptr);
if (rc)
return rc;
rc = parse_float_field(filename, line, ln, &c->legality, &saveptr);
if (rc)
return rc;
rc = parse_float_field(filename, line, ln, &c->econ_sensitivity, &saveptr);
if (rc)
return rc;
rc = parse_float_field(filename, line, ln, &c->govt_sensitivity, &saveptr);
if (rc)
return rc;
rc = parse_float_field(filename, line, ln, &c->tech_sensitivity, &saveptr);
if (rc)
return rc;
return 0;
}
#define MAX_COMMODITIES 100
struct commodity *read_commodities(char *filename, int *ncommodities)
{
FILE *f;
char line[1000];
char *l;
struct commodity c, *clist;
int rc, ln = 0;
int n = 0;
n = 0;
clist = malloc(sizeof(*clist) * MAX_COMMODITIES);
memset(clist, 0, sizeof(*clist) * MAX_COMMODITIES);
f = fopen(filename, "r");
if (!f)
return NULL;
while (!feof(f)) {
l = fgets(line, 1000, f);
if (!l)
break;
line[strlen(line) - 1] = '\0';
ln++;
rc = parse_line(filename, line, ln, &c);
switch (rc) {
case 0:
clist[n] = c;
n++;
continue;
case 1: /* comment */
continue;
case -1:
return NULL;
}
}
*ncommodities = n;
return clist;
}