Skip to content

Commit

Permalink
complete function + add test
Browse files Browse the repository at this point in the history
  • Loading branch information
AlysonStahl-NOAA committed Aug 7, 2024
1 parent cca3294 commit 0d24850
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 5 deletions.
48 changes: 43 additions & 5 deletions src/g2ccsv.c
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ g2c_csv_init()
{
const int max_line_size = 500;
const int num_columns = 9;
int i,j;
int i;
char *buf, *tmp, *key;
char line[max_line_size];
G2C_CODE_TABLE_T *my_table = NULL;
Expand All @@ -209,8 +209,8 @@ g2c_csv_init()

/* Skip header line */
fgets(line,max_line_size,doc);
j = 1;
while((fgets(line,max_line_size,doc)) != NULL && j < 2)

while((fgets(line,max_line_size,doc)) != NULL)
{
buf = strdup(line);
i = 0;
Expand Down Expand Up @@ -265,13 +265,51 @@ g2c_csv_init()
else
my_table->entry = new_entry;
}
if (i==4)
{
if (strlen(key) > G2C_MAX_GRIB_DESC_LEN)
return G2C_ENAMETOOLONG;
if (!new_entry)
return G2C_ECSV;
strncpy(new_entry->desc,key,G2C_MAX_GRIB_LEVEL_DESC_LEN);
}
if (i==8)
{
if (strlen(key) > G2C_MAX_GRIB_STATUS_LEN)
return G2C_ENAMETOOLONG;
if (!new_entry)
return G2C_ECSV;
strncpy(new_entry->status,key,G2C_MAX_GRIB_STATUS_LEN);
}
}

/* Add this table to our list of GRIB tables. */
if (new_table)
{
if (!g2c_table)
g2c_table = new_table;
else
{
G2C_CODE_TABLE_T *g = g2c_table;

/* Go to end of list and add the table. */
if (g)
{
for (; g->next; g = g->next)
;
g->next = new_table;
}
else
{
g2c_table = new_table;
}
}
new_table = NULL;
}

free(key);
i++;
}

j++;
}

fclose(doc);
Expand Down
18 changes: 18 additions & 0 deletions tests/tst_csv.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,25 @@ main()
printf("Testing CSV ingestion...\n");
if (g2c_csv_init())
return G2C_ERROR;
if ((ret = g2c_find_desc("Code table 0.0", 0, desc)))
return ret;
if (strcmp("Meteorological products", desc))
return G2C_ERROR;
if ((ret = g2c_find_desc_str("Code table 0.0", "0", desc)))
return ret;
if (strcmp("Meteorological products", desc))
return G2C_ERROR;

/* Calling init again is harmless. */
if (g2c_xml_init())
return G2C_ERROR;

g2c_free_tables();

/* Calling free again is harmless. */
g2c_free_tables();

printf("desc %s\n", desc);
printf("SUCCESS!!!\n");
return G2C_NOERROR;
}
Expand Down

0 comments on commit 0d24850

Please sign in to comment.