cwCsv.cpp : _parse_number_field() now skips over empty fields.

This commit is contained in:
kevin 2024-07-03 14:24:18 -04:00
parent 85d647562f
commit 4ffa2e7df7

View File

@ -8,6 +8,7 @@
#include "cwObject.h" #include "cwObject.h"
#include "cwCsv.h" #include "cwCsv.h"
#include "cwNumericConvert.h" #include "cwNumericConvert.h"
#include <type_traits>
namespace cw namespace cw
{ {
@ -263,6 +264,8 @@ namespace cw
goto errLabel; goto errLabel;
} }
fieldStr_Ref = p->lineBuf + p->colA[colIdx].char_idx; fieldStr_Ref = p->lineBuf + p->colA[colIdx].char_idx;
errLabel: errLabel:
@ -279,11 +282,23 @@ namespace cw
if((rc = _get_field_str(p,colIdx,fieldStr)) != kOkRC ) if((rc = _get_field_str(p,colIdx,fieldStr)) != kOkRC )
goto errLabel; goto errLabel;
if( fieldStr != nullptr )
{
// advance past white space
while( *fieldStr && isspace(*fieldStr) )
++fieldStr;
// the first char must be a number or decimal point
if( isdigit(*fieldStr) || (*fieldStr=='.' && std::is_floating_point<T>()) )
{
if((rc = string_to_number(fieldStr,valueRef)) != kOkRC ) if((rc = string_to_number(fieldStr,valueRef)) != kOkRC )
{ {
rc = cwLogError(rc,"Numeric parse failed on column '%s' on line index:%i",cwStringNullGuard(p->colA[colIdx].title),p->curLineIdx); rc = cwLogError(rc,"Numeric parse failed on column '%s' on line index:%i",cwStringNullGuard(p->colA[colIdx].title),p->curLineIdx);
goto errLabel; goto errLabel;
} }
}
}
errLabel: errLabel:
return rc; return rc;