Skip to content

Commit

Permalink
Adding another convenience function for codes. (#291)
Browse files Browse the repository at this point in the history
* improved testing

* more XML testing

* fixed header changes
  • Loading branch information
edwardhartnett authored Sep 1, 2022
1 parent acee54a commit 56d0bc2
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
22 changes: 21 additions & 1 deletion src/g2cxml.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ g2c_free_tables()
* @return 0 for success, error code otherwise.
*/
int
g2c_find_desc(char *title, char *code, char *desc)
g2c_find_desc_str(char *title, char *code, char *desc)
{
G2C_CODE_TABLE_T *t = NULL;
int found = 0;
Expand Down Expand Up @@ -117,6 +117,26 @@ g2c_find_desc(char *title, char *code, char *desc)
return G2C_NOERROR;
}

/** Given a table title and an integer code, find a description.
*
* @param title Title of table.
* @param code Code to search for as an int.
* @param desc Pointer that gets a copy of the description. Must be
* allocated to ::G2C_MAX_GRIB_DESC_LEN + 1.
*
* @author Ed Hartnett @date 8/28/22
*
* @return 0 for success, error code otherwise.
*/
int
g2c_find_desc(char *title, int code, char *desc)
{
char str_code[G2C_MAX_GRIB_CODE_LEN + 1];

sprintf(str_code, "%d", code);
return g2c_find_desc_str(title, str_code, desc);
}

/** Find a table given a key.
*
* @param key The table title to find.
Expand Down
3 changes: 2 additions & 1 deletion src/grib2.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,8 @@ g2int jpcunpackd(unsigned char *cpack, g2int len, g2int *idrstmpl, g2int ndpts,

/* Internal functions. */
int g2c_xml_init();
int g2c_find_desc(char *title, char *code, char *desc);
int g2c_find_desc_str(char *title, char *code, char *desc);
int g2c_find_desc(char *title, int code, char *desc);
void g2c_free_tables();

/* Logging, for debugging purposes. */
Expand Down
6 changes: 5 additions & 1 deletion tests/tst_xml.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ main()
printf("Testing XML ingestion...\n");
if (g2c_xml_init())
return G2C_ERROR;
if ((ret = g2c_find_desc("Code table 0.0", "0", desc)))
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;
Expand Down

0 comments on commit 56d0bc2

Please sign in to comment.