From 35c186d32fbeb1f4e75e1af9c6dc6fa6272a9400 Mon Sep 17 00:00:00 2001 From: John Halley Gotway Date: Thu, 16 Feb 2023 11:51:00 -0700 Subject: [PATCH] Per #2402, reimplement the TableFlatFile class using std::vector instead of dynamically allocating memory to eliminate SonarQube findings. --- src/libcode/vx_data2d/table_lookup.cc | 253 +++++++++----------------- src/libcode/vx_data2d/table_lookup.h | 6 +- 2 files changed, 88 insertions(+), 171 deletions(-) diff --git a/src/libcode/vx_data2d/table_lookup.cc b/src/libcode/vx_data2d/table_lookup.cc index dbc920ae21..d9c4605d9e 100644 --- a/src/libcode/vx_data2d/table_lookup.cc +++ b/src/libcode/vx_data2d/table_lookup.cc @@ -617,9 +617,6 @@ void TableFlatFile::init_from_scratch() { -g1e = (Grib1TableEntry **) 0; -g2e = (Grib2TableEntry **) 0; - clear(); } @@ -634,30 +631,8 @@ void TableFlatFile::clear() int j; -if ( g1e ) { - - for (j=0; jdump(out, depth + 1); + g1e[j].dump(out, depth + 1); } @@ -696,7 +671,7 @@ for (j=0; jdump(out, depth + 1); + g2e[j].dump(out, depth + 1); } @@ -722,15 +697,7 @@ if ( f.N_grib1_elements != 0 ) { N_grib1_elements = N_grib1_alloc = f.N_grib1_elements; - g1e = new Grib1TableEntry * [N_grib1_elements]; - - for (j=0; j 0 ) { - - for (j=0; j 0 ) { - - for (j=0; jparse_line(line.c_str()); + status = e.parse_line(line.c_str()); if ( ! status ) { @@ -957,9 +873,11 @@ while ( line.read_line(in) ) { } // - // increment counter + // store entry and increment counter // + g1e.push_back(e); + j++; } // while @@ -988,6 +906,7 @@ bool TableFlatFile::read_grib2(istream & in, const char * filename, const int n) int j; ConcatString line; +Grib2TableEntry e; bool status = false; // @@ -1016,9 +935,7 @@ while ( line.read_line(in) ) { line << "\n"; - g2e[N_grib2_elements + j] = new Grib2TableEntry; - - status = g2e[N_grib2_elements + j]->parse_line(line.c_str()); + status = e.parse_line(line.c_str()); if ( ! status ) { @@ -1031,9 +948,11 @@ while ( line.read_line(in) ) { } // - // increment counter + // store entry and increment counter // + g2e.push_back(e); + j++; } // while @@ -1062,9 +981,9 @@ e.clear(); for (j=0; jcode == code) && (g1e[j]->table_number == table_number) ) { + if ( (g1e[j].code == code) && (g1e[j].table_number == table_number) ) { - e = *(g1e[j]); + e = g1e[j]; return ( true ); @@ -1091,14 +1010,14 @@ bool TableFlatFile::lookup_grib1(int code, int table_number, int center, int sub for (j=0; jsubcenter == -1){ + if( g1e[j].subcenter == -1){ matching_subsenter = -1; } - if ( (g1e[j]->code == code) && (g1e[j]->table_number == table_number) - && (g1e[j]->center == center) && (g1e[j]->subcenter == matching_subsenter)) { + if ( (g1e[j].code == code) && (g1e[j].table_number == table_number) && + (g1e[j].center == center) && (g1e[j].subcenter == matching_subsenter)) { - e = *(g1e[j]); + e = g1e[j]; return ( true ); @@ -1138,15 +1057,15 @@ bool TableFlatFile::lookup_grib1(const char * parm_name, int table_number, int c n_matches = 0; // build a list of matches - vector matches; + vector matches; for(int j=0; j < N_grib1_elements; j++){ - if( g1e[j]->parm_name != parm_name || - (bad_data_int != table_number && g1e[j]->table_number != table_number) || - (bad_data_int != code && g1e[j]->code != code ) ) + if( g1e[j].parm_name != parm_name || + (bad_data_int != table_number && g1e[j].table_number != table_number) || + (bad_data_int != code && g1e[j].code != code) ) continue; - if( n_matches++ == 0 ) e = *(g1e[j]); + if( n_matches++ == 0 ) e = g1e[j]; matches.push_back( g1e[j] ); } @@ -1162,11 +1081,11 @@ bool TableFlatFile::lookup_grib1(const char * parm_name, int table_number, int c msg << "):\n"; mlog << Debug(3) << "\n" << msg; - for(vector::iterator it = matches.begin(); + for(vector::iterator it = matches.begin(); it < matches.end(); it++) - mlog << Debug(3) << " parm_name: " << (*it)->parm_name - << ", table_number = " << (*it)->table_number - << ", code = " << (*it)->code << "\n"; + mlog << Debug(3) << " parm_name: " << (it)->parm_name + << ", table_number = " << (it)->table_number + << ", code = " << (it)->code << "\n"; mlog << Debug(3) << "Using the first match found: " << " parm_name: " << e.parm_name @@ -1193,22 +1112,22 @@ bool TableFlatFile::lookup_grib1(const char * parm_name, int table_number, int c n_matches = 0; // build a list of matches - vector matches; + vector matches; int matching_subsenter; for(int j=0; j < N_grib1_elements; j++){ matching_subsenter = subcenter; - if( g1e[j]->subcenter == -1){ + if( g1e[j].subcenter == -1){ matching_subsenter = -1; } - if( g1e[j]->parm_name != parm_name || - (bad_data_int != table_number && g1e[j]->table_number != table_number) || - (bad_data_int != code && g1e[j]->code != code ) || - (bad_data_int != center && g1e[j]->center != center ) || - (bad_data_int != matching_subsenter && g1e[j]->subcenter != matching_subsenter) ) + if( g1e[j].parm_name != parm_name || + (bad_data_int != table_number && g1e[j].table_number != table_number) || + (bad_data_int != code && g1e[j].code != code) || + (bad_data_int != center && g1e[j].center != center) || + (bad_data_int != matching_subsenter && g1e[j].subcenter != matching_subsenter) ) continue; - if( n_matches++ == 0 ) e = *(g1e[j]); + if( n_matches++ == 0 ) e = g1e[j]; matches.push_back( g1e[j] ); } @@ -1224,14 +1143,14 @@ bool TableFlatFile::lookup_grib1(const char * parm_name, int table_number, int c msg << "):\n"; mlog << Debug(3) << "\n" << msg; - for(vector::iterator it = matches.begin(); + for(vector::iterator it = matches.begin(); it < matches.end(); it++) { - mlog << Debug(3) << " parm_name: " << (*it)->parm_name - << ", table_number = " << (*it)->table_number - << ", code = " << (*it)->code - << ", center = " << (*it)->center - << ", subcenter = " << (*it)->subcenter << "\n"; + mlog << Debug(3) << " parm_name: " << (it)->parm_name + << ", table_number = " << (it)->table_number + << ", code = " << (it)->code + << ", center = " << (it)->center + << ", subcenter = " << (it)->subcenter << "\n"; } mlog << Debug(3) << "Using the first match found: " @@ -1272,9 +1191,9 @@ e.clear(); for (j=0; jindex_a == a) && (g2e[j]->index_b == b) && (g2e[j]->index_c == c) ) { + if ( (g2e[j].index_a == a) && (g2e[j].index_b == b) && (g2e[j].index_c == c) ) { - e = *(g2e[j]); + e = g2e[j]; return ( true ); @@ -1302,17 +1221,17 @@ bool TableFlatFile::lookup_grib2(int a, int b, int c, for (j=0; jindex_a != a || - g2e[j]->index_b != b || - g2e[j]->index_c != c ) continue; + if ( g2e[j].index_a != a || + g2e[j].index_b != b || + g2e[j].index_c != c ) continue; // Check master table, center, and local table - if ( (bad_data_int != mtab && g2e[j]->mtab_low > mtab) || - (bad_data_int != mtab && g2e[j]->mtab_high < mtab) || - (bad_data_int != cntr && g2e[j]->cntr > 0 && g2e[j]->cntr != cntr) || - (bad_data_int != ltab && g2e[j]->ltab > 0 && g2e[j]->ltab != ltab) ) continue; + if ( (bad_data_int != mtab && g2e[j].mtab_low > mtab) || + (bad_data_int != mtab && g2e[j].mtab_high < mtab) || + (bad_data_int != cntr && g2e[j].cntr > 0 && g2e[j].cntr != cntr) || + (bad_data_int != ltab && g2e[j].ltab > 0 && g2e[j].ltab != ltab) ) continue; - e = *(g2e[j]); + e = g2e[j]; return ( true ); @@ -1335,16 +1254,16 @@ bool TableFlatFile::lookup_grib2(const char * parm_name, int a, int b, int c, n_matches = 0; // build a list of matches - vector matches; + vector matches; for(int j=0; jparm_name != parm_name || - (bad_data_int != a && g2e[j]->index_a != a) || - (bad_data_int != b && g2e[j]->index_b != b) || - (bad_data_int != c && g2e[j]->index_c != c) ) + if( g2e[j].parm_name != parm_name || + (bad_data_int != a && g2e[j].index_a != a) || + (bad_data_int != b && g2e[j].index_b != b) || + (bad_data_int != c && g2e[j].index_c != c) ) continue; - if( n_matches++ == 0 ) e = *(g2e[j]); + if( n_matches++ == 0 ) e = g2e[j]; matches.push_back( g2e[j] ); } @@ -1361,12 +1280,12 @@ bool TableFlatFile::lookup_grib2(const char * parm_name, int a, int b, int c, msg << "):\n"; mlog << Debug(3) << "\n" << msg; - for(vector::iterator it = matches.begin(); + for(vector::iterator it = matches.begin(); it < matches.end(); it++) - mlog << Debug(3) << " parm_name: " << (*it)->parm_name - << ", index_a = " << (*it)->index_a - << ", index_b = " << (*it)->index_b - << ", index_c = " << (*it)->index_c << "\n"; + mlog << Debug(3) << " parm_name: " << (it)->parm_name + << ", index_a = " << (it)->index_a + << ", index_b = " << (it)->index_b + << ", index_c = " << (it)->index_c << "\n"; mlog << Debug(3) << "Using the first match found: " << " parm_name: " << e.parm_name @@ -1395,20 +1314,20 @@ bool TableFlatFile::lookup_grib2(const char * parm_name, n_matches = 0; // build a list of matches - vector matches; + vector matches; for(int j=0; jparm_name != parm_name || - (bad_data_int != a && g2e[j]->index_a != a) || - (bad_data_int != b && g2e[j]->index_b != b) || - (bad_data_int != c && g2e[j]->index_c != c) || - (bad_data_int != mtab && g2e[j]->mtab_low > mtab) || - (bad_data_int != mtab && g2e[j]->mtab_high < mtab) || - (bad_data_int != cntr && g2e[j]->cntr > 0 && g2e[j]->cntr != cntr) || - (bad_data_int != ltab && g2e[j]->ltab > 0 && g2e[j]->ltab != ltab) ) + if( g2e[j].parm_name != parm_name || + (bad_data_int != a && g2e[j].index_a != a) || + (bad_data_int != b && g2e[j].index_b != b) || + (bad_data_int != c && g2e[j].index_c != c) || + (bad_data_int != mtab && g2e[j].mtab_low > mtab) || + (bad_data_int != mtab && g2e[j].mtab_high < mtab) || + (bad_data_int != cntr && g2e[j].cntr > 0 && g2e[j].cntr != cntr) || + (bad_data_int != ltab && g2e[j].ltab > 0 && g2e[j].ltab != ltab) ) continue; - if( n_matches++ == 0 ) e = *(g2e[j]); + if( n_matches++ == 0 ) e = g2e[j]; matches.push_back( g2e[j] ); } @@ -1428,15 +1347,15 @@ bool TableFlatFile::lookup_grib2(const char * parm_name, msg << "):\n"; mlog << Debug(3) << "\n" << msg; - for(vector::iterator it = matches.begin(); + for(vector::iterator it = matches.begin(); it < matches.end(); it++) - mlog << Debug(3) << " parm_name: " << (*it)->parm_name - << ", index_a = " << (*it)->index_a - << ", grib2_mtab = " << (*it)->mtab_set - << ", grib2_cntr = " << (*it)->cntr - << ", grib2_ltab = " << (*it)->ltab - << ", index_b = " << (*it)->index_b - << ", index_c = " << (*it)->index_c + mlog << Debug(3) << " parm_name: " << (it)->parm_name + << ", index_a = " << (it)->index_a + << ", grib2_mtab = " << (it)->mtab_set + << ", grib2_cntr = " << (it)->cntr + << ", grib2_ltab = " << (it)->ltab + << ", index_b = " << (it)->index_b + << ", index_c = " << (it)->index_c << "\n"; mlog << Debug(3) << "Using the first match found: " diff --git a/src/libcode/vx_data2d/table_lookup.h b/src/libcode/vx_data2d/table_lookup.h index a234bf72bb..e376293a4e 100644 --- a/src/libcode/vx_data2d/table_lookup.h +++ b/src/libcode/vx_data2d/table_lookup.h @@ -145,8 +145,8 @@ class TableFlatFile { void extend_grib1(int); void extend_grib2(int); - Grib1TableEntry ** g1e; // elements ... allocated - Grib2TableEntry ** g2e; // elements ... allocated + std::vector g1e; + std::vector g2e; int N_grib1_elements; int N_grib2_elements; @@ -229,5 +229,3 @@ extern TableFlatFile GribTable; //////////////////////////////////////////////////////////////////////// - -