NCEPLIBS-g2c 2.2.0
Loading...
Searching...
No Matches
g2ccsv.c
Go to the documentation of this file.
1
7#include "grib2_int.h"
8#include <stdio.h>
9#include <stdlib.h>
10#include <string.h>
11
13FILE *doc;
14
17
22void
24{
26
27 for (t = g2c_table; t; t = t->next)
28 {
30
31 printf("%s\n", t->title);
32 for (e = t->entry; e; e = e->next)
33 printf("code %s desc %s status %s\n", e->code, e->desc, e->status);
34 }
35}
36
41void
43{
44 G2C_CODE_TABLE_T *t, *the_next = NULL;
45
46 /* If g2c_table is NULL, then tables have already been
47 * freed. */
48 if (!g2c_table)
49 return;
50
51 /* Free each table. */
52 for (t = g2c_table; t; t = the_next)
53 {
55 G2C_CODE_ENTRY_T *e_next;
56
57 /* Free each entry in the table. */
58 the_next = t->next;
59 for (e = t->entry; e; e = e_next)
60 {
61 e_next = e->next;
62 free(e);
63 }
64
65 free(t);
66 }
67
68 /* Set to NULL so we all know g2c_table has been freed. */
69 g2c_table = NULL;
70}
71
83int
84g2c_find_desc_str(char *title, char *code, char *desc)
85{
86 G2C_CODE_TABLE_T *t = NULL;
87 int found = 0;
88
89 /* Check inputs. */
90 if (!title || strlen(title) > G2C_MAX_GRIB_TITLE_LEN || !code || strlen(code) > G2C_MAX_GRIB_CODE_LEN || !desc)
91 return G2C_EINVAL;
92
93 /* Find table. */
94 for (t = g2c_table; !found && t; t = t->next)
95 {
96 if (!strncmp(title, t->title, strlen(title)))
97 {
98 G2C_CODE_ENTRY_T *e = NULL;
99
100 /* Find entry. */
101 for (e = t->entry; e; e = e->next)
102 {
103 if (!strncmp(code, e->code, strlen(code)))
104 {
105 strcpy(desc, e->desc);
106 found++;
107 break;
108 }
109 }
110 }
111 }
112
113 if (!found)
114 return G2C_ENOTFOUND;
115
116 return G2C_NOERROR;
117}
118
130int
131g2c_find_desc(char *title, int code, char *desc)
132{
133 char str_code[G2C_MAX_GRIB_CODE_LEN + 1];
134
135 sprintf(str_code, "%d", code);
136 return g2c_find_desc_str(title, str_code, desc);
137}
138
149{
151
152 for (g = g2c_table; g; g = g->next)
153 if (!strncmp(key, g->title, G2C_MAX_GRIB_TITLE_LEN))
154 return g;
155
156 return NULL;
157}
158
170{
172
173 for (e = table->entry; e; e = e->next)
174 if (!strncmp(desc, e->desc, G2C_MAX_GRIB_DESC_LEN))
175 return e;
176
177 return NULL;
178}
179
191static char *
192g2c_csv_strsep(char **stringp, const char *delim)
193{
194 char *rv = *stringp;
195 if (rv)
196 {
197 *stringp += strcspn(*stringp, delim);
198 if (**stringp)
199 *(*stringp)++ = '\0';
200 else
201 *stringp = 0;
202 }
203 return rv;
204}
205
214int
216{
217 const int max_line_size = 500;
218 const int num_columns = 9;
219 int i;
220 char *buf, *tmp, *key;
221 char line[max_line_size];
222 G2C_CODE_TABLE_T *my_table = NULL;
223 G2C_CODE_ENTRY_T *new_entry = NULL;
224
225 /* If g2c_table is not NULL, then tables have already been
226 * initialized. */
227 if (g2c_table)
228 return G2C_NOERROR;
229
230 /* Ingest the CSV document. */
231 if (!(doc = fopen("CodeFlag.txt", "r")))
232 return G2C_ECSV;
233
234 /* Skip header line */
235 buf = fgets(line, max_line_size, doc);
236
237 /* Go through the document and save table data.
238 * Each line is a table of codes. */
239 while ((buf = fgets(line, max_line_size, doc)) != NULL)
240 {
241 i = 0;
242 while (buf != NULL && i < num_columns)
243 {
244 G2C_CODE_TABLE_T *new_table = NULL;
245
246 if (*buf == '\"')
247 {
248 tmp = g2c_csv_strsep(&buf, "\"");
249 tmp = g2c_csv_strsep(&buf, "\"");
250 key = strdup((const char *)tmp);
251 tmp = g2c_csv_strsep(&buf, ",");
252 }
253 else
254 {
255 tmp = g2c_csv_strsep(&buf, ",");
256 key = strdup((const char *)tmp);
257 }
258
259 /* Title_en */
260 if (i == 0)
261 {
262 if (strlen(key) > G2C_MAX_GRIB_TITLE_LEN)
263 return G2C_ENAMETOOLONG;
264 if (!(my_table = g2c_find_table(key)))
265 {
266 if (!(new_table = calloc(1, sizeof(G2C_CODE_TABLE_T))))
267 return G2C_ENOMEM;
268 strncpy(new_table->title, key, G2C_MAX_GRIB_TITLE_LEN);
269 my_table = new_table;
270 }
271 }
272
273 if (my_table)
274 {
275 /* CodeFlag */
276 if (i == 2)
277 {
279
280 if (!(new_entry = calloc(1, sizeof(G2C_CODE_ENTRY_T))))
281 return G2C_ENOMEM;
282 if (strlen(key) > G2C_MAX_GRIB_CODE_LEN)
283 return G2C_ENAMETOOLONG;
284 strncpy(new_entry->code, key, G2C_MAX_GRIB_CODE_LEN);
285
286 /* Add entry at end of list. */
287 if (my_table->entry)
288 {
289 for (e = my_table->entry; e->next; e = e->next)
290 ;
291 e->next = new_entry;
292 }
293 else
294 my_table->entry = new_entry;
295 }
296 /* MeaningParameterDescription */
297 if (i == 4)
298 {
299 if (strlen(key) > G2C_MAX_GRIB_DESC_LEN)
300 return G2C_ENAMETOOLONG;
301 if (!new_entry)
302 return G2C_ECSV;
303 strncpy(new_entry->desc, key, G2C_MAX_GRIB_LEVEL_DESC_LEN);
304 }
305 /* Status */
306 if (i == 8)
307 {
308 if (strlen(key) > G2C_MAX_GRIB_STATUS_LEN)
309 return G2C_ENAMETOOLONG;
310 if (!new_entry)
311 return G2C_ECSV;
312 strncpy(new_entry->status, key, G2C_MAX_GRIB_STATUS_LEN);
313 }
314 }
315
316 /* Add this table to our list of GRIB tables. */
317 if (new_table)
318 {
319 if (!g2c_table)
320 g2c_table = new_table;
321 else
322 {
324
325 /* Go to end of list and add the table. */
326 if (g)
327 {
328 for (; g->next; g = g->next)
329 ;
330 g->next = new_table;
331 }
332 else
333 {
334 g2c_table = new_table;
335 }
336 }
337 new_table = NULL;
338 }
339
340 free(key);
341 i++;
342 }
343 }
344
345 fclose(doc);
346
347 return G2C_NOERROR;
348}
FILE * doc
Contains the parsed CSV document.
Definition g2ccsv.c:13
int g2c_find_desc_str(char *title, char *code, char *desc)
Given a table title and a code, find a description.
Definition g2ccsv.c:84
int g2c_find_desc(char *title, int code, char *desc)
Given a table title and an integer code, find a description.
Definition g2ccsv.c:131
int g2c_csv_init()
Initialize tables from "CodeFlag.txt".
Definition g2ccsv.c:215
static char * g2c_csv_strsep(char **stringp, const char *delim)
Implementation of strsep for code portability.
Definition g2ccsv.c:192
G2C_CODE_TABLE_T * g2c_find_table(char *key)
Find a table given a key.
Definition g2ccsv.c:148
void g2c_free_tables()
Free table memory.
Definition g2ccsv.c:42
G2C_CODE_ENTRY_T * g2c_find_entry(char *desc, G2C_CODE_TABLE_T *table)
Find an entry in a table given a description.
Definition g2ccsv.c:169
G2C_CODE_TABLE_T * g2c_table
Pointer to the list of code tables.
Definition g2ccsv.c:16
void g2c_print_tables()
Print the table data.
Definition g2ccsv.c:23
#define G2C_MAX_GRIB_TITLE_LEN
Maximum length of code table title.
Definition grib2.h:421
#define G2C_MAX_GRIB_DESC_LEN
Maximum length of code description.
Definition grib2.h:417
#define G2C_ECSV
CSV error.
Definition grib2.h:513
#define G2C_ENAMETOOLONG
Name too long.
Definition grib2.h:490
#define G2C_ENOMEM
Out of memory.
Definition grib2.h:495
#define G2C_ENOTFOUND
Table or entry not found.
Definition grib2.h:499
#define G2C_MAX_GRIB_STATUS_LEN
Maximum length of code status.
Definition grib2.h:418
#define G2C_EINVAL
Invalid input.
Definition grib2.h:491
#define G2C_MAX_GRIB_CODE_LEN
Maximum length of code.
Definition grib2.h:420
#define G2C_MAX_GRIB_LEVEL_DESC_LEN
Maximum length of level description.
Definition grib2.h:419
#define G2C_NOERROR
No error.
Definition grib2.h:486
Header file with internal function prototypes NCEPLIBS-g2c library.
A GRIB2 code table.
Definition grib2_int.h:257
An entry in a GRIB2 code table.
Definition grib2_int.h:248