cwCsv.h/cpp : Added parse_field(...,bool).

This commit is contained in:
kevin 2024-02-19 21:57:54 -05:00
parent 71bfa84e15
commit d32b7a891f
2 changed files with 45 additions and 0 deletions

View File

@ -310,6 +310,36 @@ namespace cw
return rc; 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; 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 ) cw::rc_t cw::csv::parse_field( handle_t h, unsigned colIdx, uint8_t& valRef )
{ {
csv_t* p = _handleToPtr(h); 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 ); 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 ) cw::rc_t cw::csv::parse_field( handle_t h, const char* colLabel, uint8_t& valRef )
{ {
csv_t* p = _handleToPtr(h); csv_t* p = _handleToPtr(h);

View File

@ -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, unsigned& valRef );
rc_t parse_field( handle_t h, unsigned colIdx, int& 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, 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 returned pointer is a pointer into an internal 'line' buffer.
// The reference is therefore only valid until the next call to next_line(). // 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, unsigned& valRef );
rc_t parse_field( handle_t h, const char* colLabel, int& 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, 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 returned pointer is a pointer into an internal 'line' buffer.
// The reference is therefore only valid until the next call to next_line(). // The reference is therefore only valid until the next call to next_line().