Skip to content

Commit

Permalink
feature 1922 iodas2nc - Removed trailing spaces based on the string l…
Browse files Browse the repository at this point in the history
…ength (#1923)

* #1922 Removed trailing spaces based on the string lenth than buffer length

* #1922 Renamed cleanup_hdr_bfr to m_rstrip and moved to str_wrappers.cc

Co-authored-by: Howard Soh <hsoh@kiowa.rap.ucar.edu>
  • Loading branch information
hsoh-u and Howard Soh authored Sep 18, 2021
1 parent a96c9df commit 9811d5e
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 25 deletions.
29 changes: 29 additions & 0 deletions met/src/basic/vx_log/str_wrappers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ void m_strncpy(char *to_str, const char *from_str, const int buf_len,
if (str_len > buf_len) str_len = buf_len;

memset(to_str, 0, str_len);
// Kludge: there were cases that sizeof returns 8 instead of the real size.
// Use sizeof only if it's not 8.
int to_buf_size = sizeof(to_str);
if (to_buf_size != 8) {
if (str_len < to_buf_size) memset(to_str, str_len, to_buf_size);
if (str_len > to_buf_size) str_len = to_buf_size; // truncate
}

string temp_str = from_str;
temp_str.copy(to_str, str_len);
to_str[str_len] = 0;
Expand All @@ -99,4 +107,25 @@ void m_strncpy(char *to_str, const char *from_str, const int buf_len,

}

////////////////////////////////////////////////////////////////////////

void m_rstrip(char *str_buf, int buf_len) {
// Make sure it's NULL terminated
if (buf_len >= 0) str_buf[buf_len] = '\0';
// Change the trailing blank space to a null
int str_len = m_strlen(str_buf);
for(int idx=str_len-1; idx>=0; idx--) {
if(is_whitespaces(str_buf[idx])) {
str_buf[idx] = '\0';
if((idx > 0) && !is_whitespaces(str_buf[idx-1])) break;
}
}
}

////////////////////////////////////////////////////////////////////////

bool is_whitespaces(char cur_char) {
return (' ' == cur_char || '\t' == cur_char || '\n' == cur_char || '\r' == cur_char);
}

////////////////////////////////////////////////////////////////////////
4 changes: 4 additions & 0 deletions met/src/basic/vx_log/str_wrappers.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ extern void m_strncpy(char *to_str, const char *from_str, const int buf_len,
const char *method_name, const char *extra_msg=(char *)0,
bool truncate=false);

extern void m_rstrip(char *str_buf, const int buf_len=-1);

extern bool is_whitespaces(char cur_char);

////////////////////////////////////////////////////////////////////////


Expand Down
47 changes: 22 additions & 25 deletions met/src/tools/other/ioda2nc/ioda2nc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ static const char * DEF_CONFIG_NAME = "MET_BASE/config/IODA2NCConfig_default";

static const char *program_name = "ioda2nc";

static const int REJECT_DEBUG_LEVEL = 9;

////////////////////////////////////////////////////////////////////////

//
Expand Down Expand Up @@ -150,7 +152,6 @@ static void set_valid_beg_time(const StringArray &);
static void set_valid_end_time(const StringArray &);
static void set_verbosity(const StringArray &);

static void cleanup_hdr_buf(char *hdr_buf, int buf_len);
static bool check_core_data(const bool, const bool, StringArray &, StringArray &);
static bool check_missing_thresh(float value);
static ConcatString find_meta_name(StringArray, StringArray);
Expand Down Expand Up @@ -662,9 +663,7 @@ void process_ioda_file(int i_pb) {
if(has_msg_type) {
int buf_len = sizeof(modified_hdr_typ);
m_strncpy(hdr_typ, hdr_msg_types+(i_read*nstring), nstring, method_name, "hdr_typ");
hdr_typ[nstring] = 0;
// Null terminate the message type string
cleanup_hdr_buf(hdr_typ, nstring);
m_rstrip(hdr_typ, nstring);

// If the message type is not listed in the configuration
// file and it is not the case that all message types should be
Expand Down Expand Up @@ -692,8 +691,7 @@ void process_ioda_file(int i_pb) {
if(has_station_id) {
char tmp_sid[nstring+1];
m_strncpy(tmp_sid, hdr_station_ids+(i_read*nstring), nstring, method_name, "tmp_sid");
tmp_sid[nstring] = 0;
cleanup_hdr_buf(tmp_sid, nstring);
m_rstrip(tmp_sid, nstring);
hdr_sid = tmp_sid;
}
else hdr_sid.clear();
Expand Down Expand Up @@ -986,25 +984,13 @@ void clean_up() {

////////////////////////////////////////////////////////////////////////

static void cleanup_hdr_buf(char *hdr_buf, int buf_len) {
int i;
hdr_buf[buf_len] = '\0';
// Change the trailing blank space to a null
for(i=buf_len-1; i>=0; i--) {
if(' ' == hdr_buf[i]) {
hdr_buf[i] = '\0';
if(i > 0 && ' ' != hdr_buf[i-1]) break;
}
}
}

////////////////////////////////////////////////////////////////////////

bool keep_message_type(const char *mt_str) {
bool keep = false;
bool keep = conf_info.message_type.n_elements() == 0 ||
conf_info.message_type.has(mt_str, false);

keep = conf_info.message_type.n_elements() == 0 ||
conf_info.message_type.has(mt_str, false);
if(!keep && mlog.verbosity_level() >= REJECT_DEBUG_LEVEL) {
mlog << Debug(REJECT_DEBUG_LEVEL) << "The message type [" << mt_str << "] is rejected\n";
}

return(keep);
}
Expand All @@ -1013,8 +999,14 @@ bool keep_message_type(const char *mt_str) {

bool keep_station_id(const char *sid_str) {

return(conf_info.station_id.n_elements() == 0 ||
conf_info.station_id.has(sid_str, false));
bool keep = (conf_info.station_id.n_elements() == 0 ||
conf_info.station_id.has(sid_str, false));

if(!keep && mlog.verbosity_level() >= REJECT_DEBUG_LEVEL) {
mlog << Debug(REJECT_DEBUG_LEVEL) << "The station ID [" << sid_str << "] is rejected\n";
}

return(keep);
}

////////////////////////////////////////////////////////////////////////
Expand All @@ -1036,6 +1028,11 @@ bool keep_valid_time(const unixtime ut,
if(ut > max_ut) keep = false;
}

if(!keep && mlog.verbosity_level() >= REJECT_DEBUG_LEVEL) {
mlog << Debug(REJECT_DEBUG_LEVEL) << "The valid_time [" << ut << ", "
<< unix_to_yyyymmdd_hhmmss(ut) << "] is rejected\n";
}

return(keep);
}

Expand Down

0 comments on commit 9811d5e

Please sign in to comment.