-
Notifications
You must be signed in to change notification settings - Fork 1
/
example_usage.c
329 lines (280 loc) · 11.1 KB
/
example_usage.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/*
* LICENSE available at the bottom
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
#define ITJ_CSV_IMPLEMENTATION_AVX2
#include "itj_csv.h"
#define KB(x) (x * 1024)
#define SIZE_OF_NULL_TERMINATOR 1
#define SIZE_OF_DIR_SEPERATOR 1
#ifdef IS_WINDOWS
#define DIR_SEPERATOR "\\"
#else
#define DIR_SEPERATOR "/"
#endif
#define DATE_START 1960
#define DATE_END 2022
struct string {
char *base;
itj_csv_umax len;
};
struct entry {
struct string country_name;
struct string country_code;
struct string indicator_name;
struct string indicator_code;
float dates[DATE_END - DATE_START + 1];
};
struct context {
itj_csv_umax entries_max, entries_used;
struct entry *entries;
} *g_ctx = NULL;
itj_csv_bool string_alloc(struct string *out, char *base, itj_csv_umax len) {
if (len == 0) {
out->len = 0;
out->base = NULL;
}
out->len = len;
out->base = (char *)calloc(1, len + 1);
if (!out->base) {
return ITJ_CSV_FALSE;
}
memcpy(out->base, base, len);
out->base[len] = '\0';
return ITJ_CSV_TRUE;
}
int main(int argc, char *argv[]) {
g_ctx = (struct context *)calloc(1, sizeof(*g_ctx));
if (!g_ctx) {
printf("Unable to allocate memory\n");
return EXIT_FAILURE;
}
g_ctx->entries_max = 1024;
g_ctx->entries_used = 0;
g_ctx->entries = (struct entry *)calloc(1, g_ctx->entries_max * sizeof(*g_ctx->entries));
const char *exe_path = argv[0];
if (!exe_path) {
printf("Unable to run because argv[0] does not point to the path to the executable\n");
return EXIT_FAILURE;
}
itj_csv_umax exe_dir_len = strlen(exe_path);
if (exe_dir_len == 0) {
printf("Unable to run because argv[0] points to a zero length string\n");
return EXIT_FAILURE;
}
{
itj_csv_smax i;
for (i = exe_dir_len - 1; i >= 0; i--) {
if (exe_path[i] == '\\' || exe_path[i] == '/') {
break;
}
}
if (i <= 0) {
exe_dir_len = 0;
} else {
exe_dir_len = i;
}
}
char *exe_dir = NULL;
if (exe_dir_len > 0) {
exe_dir = (char *)calloc(1, exe_dir_len + SIZE_OF_NULL_TERMINATOR);
if (!exe_dir) {
printf("Unable to allocate memory for the executable directory path. The executable string is '%s'\n", exe_path);
return EXIT_FAILURE;
}
memcpy(exe_dir, exe_path, exe_dir_len);
exe_dir[exe_dir_len] = '\0';
}
const char *csv_path = "API_EG.ELC.ACCS.ZS_DS2_en_csv_v2_5995100.csv";
itj_csv_umax csv_path_len = strlen(csv_path);
if (exe_dir_len > 0) {
char *path = (char *)calloc(1, exe_dir_len + SIZE_OF_DIR_SEPERATOR + csv_path_len + SIZE_OF_NULL_TERMINATOR);
if (!path) {
printf("Unable to allocate memory for the example csv path, '%s'\n", csv_path);
return EXIT_FAILURE;
}
csv_path_len = snprintf(path, exe_dir_len + SIZE_OF_DIR_SEPERATOR + csv_path_len + SIZE_OF_NULL_TERMINATOR, "%s" DIR_SEPERATOR "%s", exe_dir, csv_path);
csv_path = path;
}
itj_csv_umax buffer_max = KB(16);
void *buffer = calloc(1, buffer_max);
if (!buffer) {
printf("Unable to allocate memory for parsing buffer\n");
return EXIT_FAILURE;
}
struct itj_csv csv;
if (!itj_csv_open(&csv, csv_path, csv_path_len, buffer, buffer_max, ITJ_CSV_DELIM_COMMA, NULL)) {
printf("Failed to initalize itj_csv struct to file, '%s'\n", csv_path);
return EXIT_FAILURE;
}
itj_csv_umax pump_ret = itj_csv_pump_stdio(&csv);
if (pump_ret == 0) {
printf("Unable to read '%s'\n", csv_path);
return EXIT_FAILURE;
}
// The CSV file, downloaded from the World Bank website, is non-standard, so we move around in the file first
struct itj_csv_value value;
value = itj_csv_get_next_value(&csv); // Skip Data Source
value = itj_csv_get_next_value(&csv); // Skip Data Source Value
itj_csv_ignore_newlines(&csv);
value = itj_csv_get_next_value(&csv); // Skip Last Updated Date
value = itj_csv_get_next_value(&csv); // Skip Last Updated Date Value
itj_csv_ignore_newlines(&csv);
itj_csv_u32 column_offset = 4;
itj_csv_s32 num_columns = -1;
for (;;) {
value = itj_csv_get_next_value(&csv);
if (value.need_data) {
pump_ret = itj_csv_pump_stdio(&csv);
if (pump_ret == 0) {
return EXIT_FAILURE;
} else {
continue;
}
}
if (value.is_end_of_line) {
num_columns = (value.idx + 1) - column_offset;
break;
}
}
struct entry ent = {0};
for (;;) {
value = itj_csv_get_next_value_avx2(&csv);
if (value.need_data) {
pump_ret = itj_csv_pump_stdio(&csv);
if (pump_ret == 0) {
break;
} else {
continue;
}
}
itj_csv_umax column = (value.idx - column_offset) % num_columns;
if (column == 0) {
if (!string_alloc(&ent.country_name, value.data.base, value.data.len)) {
printf("Unable to allocate memory for string\n");
return EXIT_FAILURE;
}
} else if (column == 1) {
if (!string_alloc(&ent.country_code, value.data.base, value.data.len)) {
printf("Unable to allocate memory for string\n");
return EXIT_FAILURE;
}
} else if (column == 2) {
if (!string_alloc(&ent.indicator_name, value.data.base, value.data.len)) {
printf("Unable to allocate memory for string\n");
return EXIT_FAILURE;
}
} else if (column == 3) {
if (!string_alloc(&ent.indicator_code, value.data.base, value.data.len)) {
printf("Unable to allocate memory for string\n");
return EXIT_FAILURE;
}
} else if (column > 3 && column <= 4 + (DATE_END - DATE_START)) {
float val = NAN;
if (value.data.len > 0) {
char *start = (char *)value.data.base;
char *expected_end = start + value.data.len;
char *end;
val = strtof(start, &end);
}
itj_csv_umax idx = (column - 4);
ent.dates[idx] = val;
}
if (value.is_end_of_line) {
if (g_ctx->entries_used == g_ctx->entries_max) {
g_ctx->entries_max += 512;
void *new_base = realloc(g_ctx->entries, g_ctx->entries_max * sizeof(*g_ctx->entries));
if (!new_base) {
printf("Unable to allocate memory for entire csv file in csv parsing loop\n");
return EXIT_FAILURE;
}
g_ctx->entries = (struct entry *)new_base;
}
g_ctx->entries[g_ctx->entries_used] = ent;
++g_ctx->entries_used;
memset(&ent, 0, sizeof(ent));
}
}
itj_csv_close_fh(&csv);
char buf[128];
for (;;) {
printf("Type the Country Code to get its statistics: ");
scanf("%s", buf);
printf("\n");
for (itj_csv_umax i = 0; buf[i]; ++i) {
buf[i] = toupper(buf[i]);
}
if (strcmp(buf, "QUIT") == 0) {
return EXIT_SUCCESS;
}
itj_csv_umax len = strlen(buf);
itj_csv_bool is_first = ITJ_CSV_TRUE;
for (itj_csv_umax i = 0; i < g_ctx->entries_used; ++i) {
struct entry ent = g_ctx->entries[i];
struct string country_code = ent.country_code;
if (country_code.len == len) {
if (memcmp(country_code.base, buf, len) == 0) {
printf("Country: %s\n", ent.country_name.base);
for (itj_csv_umax j = 0; j < DATE_END - DATE_START; ++j) {
itj_csv_umax date = DATE_START + j;
float val = ent.dates[j];
if (!isnan(val) && is_first) {
is_first = ITJ_CSV_FALSE;
printf("No data available from 1960 to %lld\n", date);
}
if (!isnan(val)) {
printf("%lld: %f\n", date, val);
}
}
}
}
}
printf("\n");
}
return EXIT_SUCCESS;
}
/*
------------------------------------------------------------------------------
This software is available under 2 licenses -- choose whichever you prefer.
------------------------------------------------------------------------------
ALTERNATIVE A - MIT License
Copyright (c) 2017 Ian Thomassen Jacobsen
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
------------------------------------------------------------------------------
ALTERNATIVE B - Public Domain (www.unlicense.org)
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
software, either in source code form or as a compiled binary, for any purpose,
commercial or non-commercial, and by any means.
In jurisdictions that recognize copyright laws, the author or authors of this
software dedicate any and all copyright interest in the software to the public
domain. We make this dedication for the benefit of the public at large and to
the detriment of our heirs and successors. We intend this dedication to be an
overt act of relinquishment in perpetuity of all present and future rights to
this software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
------------------------------------------------------------------------------
*/