diff --git a/cwCsv.cpp b/cwCsv.cpp index dff0ae8..c6aa024 100644 --- a/cwCsv.cpp +++ b/cwCsv.cpp @@ -310,7 +310,37 @@ namespace cw return rc; } + rc_t _parse_bool_field( csv_t* p, unsigned colIdx, bool& valRef ) + { + rc_t rc = kOkRC; + const char* fieldStr = nullptr; + if((rc = _get_field_str(p,colIdx,fieldStr)) != kOkRC ) + goto errLabel; + else + { + if( textIsEqualI(fieldStr,"true") ) + valRef = true; + else + if( textIsEqualI(fieldStr,"false") ) + valRef = false; + else + rc = cwLogError(kSyntaxErrorRC,"The value of a boolean must be either 'true' or 'false'."); + } + + errLabel: + return rc; + } + + rc_t _parse_bool_field( csv_t* p, const char* colLabel, bool& valRef ) + { + unsigned colIdx; + if((colIdx = _title_to_col_index(p, colLabel)) == kInvalidIdx ) + return cwLogError(kInvalidArgRC,"The column label '%s' is not valid.",cwStringNullGuard(colLabel)); + + return _parse_bool_field(p,colIdx,valRef); + } + } } @@ -496,6 +526,12 @@ cw::rc_t cw::csv::field_char_count( handle_t h, unsigned colIdx, unsigned& charC return rc; } +cw::rc_t cw::csv::parse_field( handle_t h, unsigned colIdx, bool& valRef ) +{ + csv_t* p = _handleToPtr(h); + return _parse_bool_field( p, colIdx, valRef ) ; +} + cw::rc_t cw::csv::parse_field( handle_t h, unsigned colIdx, uint8_t& valRef ) { csv_t* p = _handleToPtr(h); @@ -526,6 +562,12 @@ cw::rc_t cw::csv::parse_field( handle_t h, unsigned colIdx, const char*& valRef return _parse_string_field( p, colIdx, valRef ); } +cw::rc_t cw::csv::parse_field( handle_t h, const char* colLabel, bool& valRef ) +{ + csv_t* p = _handleToPtr(h); + return _parse_bool_field(p, colLabel, valRef ); +} + cw::rc_t cw::csv::parse_field( handle_t h, const char* colLabel, uint8_t& valRef ) { csv_t* p = _handleToPtr(h); diff --git a/cwCsv.h b/cwCsv.h index ec4c2b6..36c3c3f 100644 --- a/cwCsv.h +++ b/cwCsv.h @@ -43,6 +43,8 @@ namespace cw rc_t parse_field( handle_t h, unsigned colIdx, unsigned& valRef ); rc_t parse_field( handle_t h, unsigned colIdx, int& valRef ); rc_t parse_field( handle_t h, unsigned colIdx, double& valRef ); + rc_t parse_field( handle_t h, unsigned colIdx, bool& valRef ); + // The returned pointer is a pointer into an internal 'line' buffer. // The reference is therefore only valid until the next call to next_line(). @@ -52,6 +54,7 @@ namespace cw rc_t parse_field( handle_t h, const char* colLabel, unsigned& valRef ); rc_t parse_field( handle_t h, const char* colLabel, int& valRef ); rc_t parse_field( handle_t h, const char* colLabel, double& valRef ); + rc_t parse_field( handle_t h, const char* colLabel, bool& valRef ); // The returned pointer is a pointer into an internal 'line' buffer. // The reference is therefore only valid until the next call to next_line().