From 0d2485056d0025aa4e73a79e5bdc536cb9b9ea7e Mon Sep 17 00:00:00 2001 From: AlysonStahl-NOAA <166434581+AlysonStahl-NOAA@users.noreply.github.com> Date: Wed, 7 Aug 2024 16:55:03 -0700 Subject: [PATCH] complete function + add test --- src/g2ccsv.c | 48 +++++++++++++++++++++++++++++++++++++++++++----- tests/tst_csv.c | 18 ++++++++++++++++++ 2 files changed, 61 insertions(+), 5 deletions(-) diff --git a/src/g2ccsv.c b/src/g2ccsv.c index 72976d83..a8d6e3cf 100644 --- a/src/g2ccsv.c +++ b/src/g2ccsv.c @@ -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; @@ -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; @@ -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); diff --git a/tests/tst_csv.c b/tests/tst_csv.c index 292b761c..4c0f9b57 100644 --- a/tests/tst_csv.c +++ b/tests/tst_csv.c @@ -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; }