From 4ffa2e7df7652d5262baf327ad188cfea5f66eef Mon Sep 17 00:00:00 2001 From: kevin Date: Wed, 3 Jul 2024 14:24:18 -0400 Subject: [PATCH] cwCsv.cpp : _parse_number_field() now skips over empty fields. --- cwCsv.cpp | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/cwCsv.cpp b/cwCsv.cpp index e42d9e1..616c8f7 100644 --- a/cwCsv.cpp +++ b/cwCsv.cpp @@ -8,6 +8,7 @@ #include "cwObject.h" #include "cwCsv.h" #include "cwNumericConvert.h" +#include namespace cw { @@ -263,6 +264,8 @@ namespace cw goto errLabel; } + + fieldStr_Ref = p->lineBuf + p->colA[colIdx].char_idx; errLabel: @@ -275,16 +278,28 @@ namespace cw { rc_t rc = kOkRC; const char* fieldStr = nullptr; - + if((rc = _get_field_str(p,colIdx,fieldStr)) != kOkRC ) goto errLabel; - if((rc = string_to_number(fieldStr,valueRef)) != kOkRC ) + if( fieldStr != nullptr ) { - rc = cwLogError(rc,"Numeric parse failed on column '%s' on line index:%i",cwStringNullGuard(p->colA[colIdx].title),p->curLineIdx); - goto errLabel; - } + // 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()) ) + { + + 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); + goto errLabel; + } + } + } + errLabel: return rc; }