diff --git a/src/grib_accessor_class.cc b/src/grib_accessor_class.cc index 524e40ecf..9de104328 100644 --- a/src/grib_accessor_class.cc +++ b/src/grib_accessor_class.cc @@ -71,8 +71,6 @@ static struct table_entry table[] = { }; #endif /* ACCESSOR_FACTORY_USE_TRIE */ -#define NUMBER(x) (sizeof(x) / sizeof(x[0])) - grib_section* grib_create_root_section(const grib_context* context, grib_handle* h) { char* fpath = 0; @@ -113,7 +111,7 @@ static GRIB_INLINE grib_accessor_class* get_class(grib_context* c, char* type) if ((the_class = (grib_accessor_class**)grib_trie_get(c->classes, type)) != NULL) return *(the_class); - table_count = NUMBER(table); + const int table_count = sizeof(table) / sizeof(table[0]); for (i = 0; i < table_count; i++) { if (grib_inline_strcmp(type, table[i].type) == 0) { grib_trie_insert(c->classes, type, table[i].cclass); diff --git a/src/grib_accessor_class_proj_string.cc b/src/grib_accessor_class_proj_string.cc index 00b25c6a3..219ff09d5 100644 --- a/src/grib_accessor_class_proj_string.cc +++ b/src/grib_accessor_class_proj_string.cc @@ -275,7 +275,6 @@ static int proj_mercator(grib_handle* h, char* result) return err; } -#define NUMBER(a) (sizeof(a) / sizeof(a[0])) static proj_mapping proj_mappings[] = { { "regular_ll", &proj_unprojected }, { "regular_gg", &proj_unprojected }, @@ -308,7 +307,8 @@ static int unpack_string(grib_accessor* a, char* v, size_t* len) err = grib_get_string(h, self->grid_type, grid_type, &size); if (err) return err; - for (i = 0; !found && i < NUMBER(proj_mappings); ++i) { + const size_t num_proj_mappings = sizeof(proj_mappings) / sizeof(proj_mappings[0]); + for (i = 0; !found && i < num_proj_mappings; ++i) { proj_mapping pm = proj_mappings[i]; if (strcmp(grid_type, pm.gridType) == 0) { found = 1; diff --git a/src/grib_dumper_class.cc b/src/grib_dumper_class.cc index 3f204957c..abd1cbf02 100644 --- a/src/grib_dumper_class.cc +++ b/src/grib_dumper_class.cc @@ -26,12 +26,11 @@ static struct table_entry table[] = { #include "grib_dumper_factory.h" }; -#define NUMBER(x) (sizeof(x) / sizeof(x[0])) - grib_dumper* grib_dumper_factory(const char* op, const grib_handle* h, FILE* out, unsigned long option_flags, void* arg) { - int i; - for (i = 0; i < NUMBER(table); i++) + size_t i = 0; + const size_t num_table_entries = sizeof(table) / sizeof(table[0]); + for (i = 0; i < num_table_entries; i++) if (strcmp(op, table[i].type) == 0) { grib_dumper_class* c = *(table[i].cclass); grib_dumper* d = (grib_dumper*)grib_context_malloc_clear(h->context, c->size); @@ -83,7 +82,8 @@ void grib_dump_content(const grib_handle* h, FILE* f, const char* mode, unsigned dumper = grib_dumper_factory(mode ? mode : "serialize", h, f, flags, data); if (!dumper) { fprintf(stderr, "Here are some possible values for the dumper mode:\n"); - for (size_t i = 0; i < NUMBER(table); i++) { + const size_t num_table_entries = sizeof(table) / sizeof(table[0]); + for (size_t i = 0; i < num_table_entries; i++) { const char* t = table[i].type; if (strstr(t, "bufr") == NULL && strstr(t, "grib") == NULL) { fprintf(stderr, "\t%s\n", t); diff --git a/src/grib_errors.cc b/src/grib_errors.cc index d94b5f845..90dc45e79 100644 --- a/src/grib_errors.cc +++ b/src/grib_errors.cc @@ -94,12 +94,11 @@ static const char *errors[] = { "Assertion failure", /* -79 GRIB_ASSERTION_FAILURE */ }; -#define NUMBER(a) sizeof(a)/sizeof(a[0]) - const char* grib_get_error_message(int code) { code = -code; - if (code < 0 || code >= NUMBER(errors)) { + const int num_errors = int( sizeof(errors)/sizeof(errors[0]) ); + if (code < 0 || code >= num_errors) { static char mess[64]; snprintf(mess, sizeof(mess), "Unknown error %d", code); return mess; diff --git a/src/grib_errors.cc.in b/src/grib_errors.cc.in index 4e826bb8b..dfc1a1db1 100644 --- a/src/grib_errors.cc.in +++ b/src/grib_errors.cc.in @@ -15,12 +15,11 @@ static const char *errors[] = { !ERRORS go in here }; -#define NUMBER(a) sizeof(a)/sizeof(a[0]) - const char* grib_get_error_message(int code) { code = -code; - if (code < 0 || code >= NUMBER(errors)) { + const int num_errors = int( sizeof(errors)/sizeof(errors[0]) ); + if (code < 0 || code >= num_errors) { static char mess[64]; snprintf(mess, sizeof(mess), "Unknown error %d", code); return mess; diff --git a/src/grib_geography.cc b/src/grib_geography.cc index d70945d32..844a38abb 100644 --- a/src/grib_geography.cc +++ b/src/grib_geography.cc @@ -16,7 +16,6 @@ #include #include -#define NUMBER(x) (sizeof(x) / sizeof(x[0])) #define MAXITER 10 #define RAD2DEG 57.29577951308232087684 /* 180 over pi */ @@ -28,7 +27,7 @@ static void gauss_first_guess(long trunc, double* vals) { - long i = 0, numVals; + long i = 0, numVals; static double gvals[] = { 2.4048255577E0, 5.5200781103E0, @@ -82,7 +81,7 @@ static void gauss_first_guess(long trunc, double* vals) 156.2950342685E0, }; - numVals = NUMBER(gvals); + numVals = sizeof(gvals) / sizeof(gvals[0]); for (i = 0; i < trunc; i++) { if (i < numVals) vals[i] = gvals[i]; diff --git a/src/grib_iterator_class.cc b/src/grib_iterator_class.cc index 571221836..6e1ec78b7 100644 --- a/src/grib_iterator_class.cc +++ b/src/grib_iterator_class.cc @@ -15,8 +15,6 @@ #include "grib_api_internal.h" -#define NUMBER(x) (sizeof(x) / sizeof(x[0])) - /* This file is generated by ./make_class.pl */ #include "grib_iterator_class.h" @@ -33,11 +31,12 @@ static const struct table_entry table[] = { grib_iterator* grib_iterator_factory(grib_handle* h, grib_arguments* args, unsigned long flags, int* error) { - size_t i = 0; + size_t i = 0, num_table_entries = 0; const char* type = (char*)grib_arguments_get_name(h, args, 0); *error = GRIB_NOT_IMPLEMENTED; - for (i = 0; i < NUMBER(table); i++) { + num_table_entries = sizeof(table) / sizeof(table[0]); + for (i = 0; i < num_table_entries; i++) { if (strcmp(type, table[i].type) == 0) { grib_iterator_class* c = *(table[i].cclass); grib_iterator* it = (grib_iterator*)grib_context_malloc_clear(h->context, c->size); diff --git a/src/grib_nearest_class.cc b/src/grib_nearest_class.cc index 42e618ff8..8128e844e 100644 --- a/src/grib_nearest_class.cc +++ b/src/grib_nearest_class.cc @@ -10,8 +10,6 @@ #include "grib_api_internal.h" -#define NUMBER(x) (sizeof(x) / sizeof(x[0])) - /* This file is generated by ./make_class.pl */ #include "grib_nearest_class.h" @@ -28,11 +26,12 @@ static const struct table_entry table[] = { grib_nearest* grib_nearest_factory(grib_handle* h, grib_arguments* args, int* error) { - size_t i = 0; - *error = GRIB_NOT_IMPLEMENTED; + size_t i = 0, num_table_entries = 0; + *error = GRIB_NOT_IMPLEMENTED; char* type = (char*)grib_arguments_get_name(h, args, 0); - for (i = 0; i < NUMBER(table); i++) { + num_table_entries = sizeof(table) / sizeof(table[0]); + for (i = 0; i < num_table_entries; i++) { if (strcmp(type, table[i].type) == 0) { grib_nearest_class* c = *(table[i].cclass); grib_nearest* it = (grib_nearest*)grib_context_malloc_clear(h->context, c->size); diff --git a/src/grib_templates.cc b/src/grib_templates.cc index 01e0bc39f..3e357382c 100644 --- a/src/grib_templates.cc +++ b/src/grib_templates.cc @@ -25,12 +25,10 @@ // size_t size; // } grib_templates; -// #define NUMBER(x) (sizeof(x) / sizeof(x[0])) // grib_handle* grib_internal_sample(grib_context* c,const char* name) // { // size_t i; -// const size_t num_samples = NUMBER(templates); -// Assert(0); +// const size_t num_samples = sizeof(templates) / sizeof(templates[0]); // for(i = 0; i < num_samples; i++) // if(strcmp(name,templates[i].name) == 0) // return grib_handle_new_from_message_copy(c,templates[i].data,templates[i].size); diff --git a/src/grib_util.cc b/src/grib_util.cc index e09bc7a27..ab6283987 100644 --- a/src/grib_util.cc +++ b/src/grib_util.cc @@ -1862,13 +1862,11 @@ int parse_keyval_string(const char* grib_tool, // Return 1 if the productDefinitionTemplateNumber (GRIB2) is for EPS (ensemble) products int grib2_is_PDTN_EPS(long pdtn) { -#define NUMBER(x) (sizeof(x) / sizeof(x[0])) - static int eps_pdtns[] = { 1, 11, 33, 34, 41, 43, 45, 47, 49, 54, 56, 58, 59, 60, 61, 63, 68, 71, 73, 77, 79, 81, 83, 84, 85, 92, 94, 96, 98 }; - size_t i; - for (i = 0; i < NUMBER(eps_pdtns); ++i) { + size_t i = 0, num_epss = (sizeof(eps_pdtns) / sizeof(eps_pdtns[0])); + for (i = 0; i < num_epss; ++i) { if (eps_pdtns[i] == pdtn) return 1; } return 0;