Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding another convenience function for codes. #291

Merged
merged 4 commits into from
Sep 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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