From 575c7d7316b49b74847ad189755e0ed25f823857 Mon Sep 17 00:00:00 2001 From: Shahram Najm Date: Mon, 30 Oct 2023 16:35:53 +0000 Subject: [PATCH] Tools: More readable string compare function --- tools/grib_to_netcdf.cc | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/tools/grib_to_netcdf.cc b/tools/grib_to_netcdf.cc index fec300970..e62b69a7d 100644 --- a/tools/grib_to_netcdf.cc +++ b/tools/grib_to_netcdf.cc @@ -1317,8 +1317,9 @@ static int axisindex(const char* name) { size_t i = 0; for (i = 0; i < NUMBER(global_axis); i++) { - if (strcmp(name, global_axis[i].name) == 0) - return i; + if (STR_EQUAL(name, global_axis[i].name)){ + return (int)i; + } } return -1; } @@ -1335,7 +1336,7 @@ static namecmp comparator(const char* name) } if (dontcompare != NULL) { - if (strcmp(dontcompare, name) == 0) + if (STR_EQUAL(dontcompare, name)) return eq_null; } @@ -1434,7 +1435,7 @@ static void unset_value(request* r, const char* parname) p = r->params; while (p) { - if (strcmp(parname, p->name) == 0) { + if (STR_EQUAL(parname, p->name)) { if (q) q->next = p->next; else @@ -1624,7 +1625,7 @@ static void cube_indexes( int k = 0; int count = count_values(cube, axis); int last = h->index_cache[i]; - const bool is_time_axis = (strcmp(axis, "time") == 0); + const bool is_time_axis = (STR_EQUAL(axis, "time")); if (is_time_axis) { Assert(times_array); Assert(times_array_size == count); @@ -1956,9 +1957,9 @@ static long monthnumber(const char* m) static int check_stepUnits(const char* step_units_str) { /* Only hours, minutes and seconds supported */ - if (strcmp(step_units_str, "h") == 0 || - strcmp(step_units_str, "m") == 0 || - strcmp(step_units_str, "s") == 0) { + if (STR_EQUAL(step_units_str, "h") || + STR_EQUAL(step_units_str, "m") || + STR_EQUAL(step_units_str, "s")) { return GRIB_SUCCESS; } return GRIB_WRONG_STEP_UNIT; @@ -2051,10 +2052,10 @@ static void validation_time(request* r) grib_context_log(ctx, GRIB_LOG_ERROR, "Cannot convert stepUnits of '%s'. Only hours, minutes and seconds supported.", step_units); } - if (strcmp("m", step_units) == 0) { + if (STR_EQUAL("m", step_units)) { step /= 60; } - else if (strcmp("s", step_units) == 0) { + else if (STR_EQUAL("s", step_units)) { step /= 3600; } } @@ -2121,19 +2122,19 @@ static nc_type translate_nctype(const char* name) if (!name) return NC_SHORT; - if (strcmp(name, "NC_BYTE") == 0) + if (STR_EQUAL(name, "NC_BYTE")) return NC_BYTE; - if (strcmp(name, "NC_SHORT") == 0) + if (STR_EQUAL(name, "NC_SHORT")) return NC_SHORT; - if (strcmp(name, "NC_INT") == 0) + if (STR_EQUAL(name, "NC_INT")) return NC_INT; - if (strcmp(name, "NC_FLOAT") == 0) + if (STR_EQUAL(name, "NC_FLOAT")) return NC_FLOAT; - if (strcmp(name, "NC_DOUBLE") == 0) + if (STR_EQUAL(name, "NC_DOUBLE")) return NC_DOUBLE; grib_context_log(ctx, GRIB_LOG_ERROR, "Unknown netCDF type '%s'. Using NC_SHORT", name); @@ -2163,7 +2164,7 @@ static int set_dimension(int ncid, const char* name, int n, int xtype, const cha int dim_id = DIM_ID; int dim_vec[DIM_ID]; - if (setup.unlimited && (strcmp(name, setup.unlimited) == 0)) + if ( setup.unlimited && (STR_EQUAL(name, setup.unlimited)) ) n = NC_UNLIMITED; stat = nc_def_dim(ncid, name, n, &dim_id); @@ -2197,7 +2198,7 @@ static int check_grid(field* f) return e; } - if (strcmp(grid_type, "regular_ll") != 0 && (strcmp(grid_type, "regular_gg") != 0)) { + if ( !STR_EQUAL(grid_type, "regular_ll") && !STR_EQUAL(grid_type, "regular_gg") ) { grib_context_log(ctx, GRIB_LOG_ERROR, "Grid type = %s", grid_type); grib_context_log(ctx, GRIB_LOG_ERROR, "First GRIB is not on a regular lat/lon grid or on a regular Gaussian grid. Exiting.\n"); return GRIB_GEOCALCULUS_PROBLEM;