NCEPLIBS-g2c 2.0.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
188int
190{
191 const int max_line_size = 500;
192 const int num_columns = 9;
193 int i;
194 char *buf, *tmp, *key;
195 char line[max_line_size];
196 G2C_CODE_TABLE_T *my_table = NULL;
197 G2C_CODE_ENTRY_T *new_entry = NULL;
198
199 /* If g2c_table is not NULL, then tables have already been
200 * initialized. */
201 if (g2c_table)
202 return G2C_NOERROR;
203
204 /* Ingest the CSV document. */
205 if (!(doc = fopen("CodeFlag.txt", "r")))
206 return G2C_ECSV;
207
208 /* Skip header line */
209 buf = fgets(line, max_line_size, doc);
210
211 /* Go through the document and save table data.
212 * Each line is a table of codes. */
213 while ((buf = fgets(line, max_line_size, doc)) != NULL)
214 {
215 i = 0;
216 while (buf != NULL && i < num_columns)
217 {
218 G2C_CODE_TABLE_T *new_table = NULL;
219
220 if (*buf == '\"')
221 {
222 tmp = strsep(&buf, "\"");
223 tmp = strsep(&buf, "\"");
224 key = strdup((const char *)tmp);
225 tmp = strsep(&buf, ",");
226 }
227 else
228 {
229 tmp = strsep(&buf, ",");
230 key = strdup((const char *)tmp);
231 }
232
233 /* Title_en */
234 if (i == 0)
235 {
236 if (strlen(key) > G2C_MAX_GRIB_TITLE_LEN)
237 return G2C_ENAMETOOLONG;
238 if (!(my_table = g2c_find_table(key)))
239 {
240 if (!(new_table = calloc(1, sizeof(G2C_CODE_TABLE_T))))
241 return G2C_ENOMEM;
242 strncpy(new_table->title, key, G2C_MAX_GRIB_TITLE_LEN);
243 my_table = new_table;
244 }
245 }
246
247 if (my_table)
248 {
249 /* CodeFlag */
250 if (i == 2)
251 {
253
254 if (!(new_entry = calloc(1, sizeof(G2C_CODE_ENTRY_T))))
255 return G2C_ENOMEM;
256 if (strlen(key) > G2C_MAX_GRIB_CODE_LEN)
257 return G2C_ENAMETOOLONG;
258 strncpy(new_entry->code, key, G2C_MAX_GRIB_CODE_LEN);
259
260 /* Add entry at end of list. */
261 if (my_table->entry)
262 {
263 for (e = my_table->entry; e->next; e = e->next)
264 ;
265 e->next = new_entry;
266 }
267 else
268 my_table->entry = new_entry;
269 }
270 /* MeaningParameterDescription */
271 if (i == 4)
272 {
273 if (strlen(key) > G2C_MAX_GRIB_DESC_LEN)
274 return G2C_ENAMETOOLONG;
275 if (!new_entry)
276 return G2C_ECSV;
277 strncpy(new_entry->desc, key, G2C_MAX_GRIB_LEVEL_DESC_LEN);
278 }
279 /* Status */
280 if (i == 8)
281 {
282 if (strlen(key) > G2C_MAX_GRIB_STATUS_LEN)
283 return G2C_ENAMETOOLONG;
284 if (!new_entry)
285 return G2C_ECSV;
286 strncpy(new_entry->status, key, G2C_MAX_GRIB_STATUS_LEN);
287 }
288 }
289
290 /* Add this table to our list of GRIB tables. */
291 if (new_table)
292 {
293 if (!g2c_table)
294 g2c_table = new_table;
295 else
296 {
298
299 /* Go to end of list and add the table. */
300 if (g)
301 {
302 for (; g->next; g = g->next)
303 ;
304 g->next = new_table;
305 }
306 else
307 {
308 g2c_table = new_table;
309 }
310 }
311 new_table = NULL;
312 }
313
314 free(key);
315 i++;
316 }
317 }
318
319 fclose(doc);
320
321 return G2C_NOERROR;
322}
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:189
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:413
#define G2C_MAX_GRIB_DESC_LEN
Maximum length of code description.
Definition grib2.h:409
#define G2C_ECSV
CSV error.
Definition grib2.h:503
#define G2C_ENAMETOOLONG
Name too long.
Definition grib2.h:480
#define G2C_ENOMEM
Out of memory.
Definition grib2.h:485
#define G2C_ENOTFOUND
Table or entry not found.
Definition grib2.h:489
#define G2C_MAX_GRIB_STATUS_LEN
Maximum length of code status.
Definition grib2.h:410
#define G2C_EINVAL
Invalid input.
Definition grib2.h:481
#define G2C_MAX_GRIB_CODE_LEN
Maximum length of code.
Definition grib2.h:412
#define G2C_MAX_GRIB_LEVEL_DESC_LEN
Maximum length of level description.
Definition grib2.h:411
#define G2C_NOERROR
No error.
Definition grib2.h:476
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