From c789d70ce2a4c6c0933feae2aae0c7e1a261f7bd Mon Sep 17 00:00:00 2001 From: kevin Date: Fri, 18 Apr 2025 14:59:07 -0400 Subject: [PATCH] cwCsv.h/cpp : Fixed off-by-1 bug in _udpate_col_array(). Fixed bug in _get_field_str() where leading white space was not skipped. --- cwCsv.cpp | 10 ++++++---- cwCsv.h | 1 + 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/cwCsv.cpp b/cwCsv.cpp index 3e389f6..2d3a0c1 100644 --- a/cwCsv.cpp +++ b/cwCsv.cpp @@ -74,7 +74,7 @@ namespace cw { rc_t rc = kOkRC; - if( col_idx > p->colN ) + if( col_idx >= p->colN ) { rc = cwLogError(kSyntaxErrorRC,"Too many CSV columns on line index:%i",p->curLineIdx); goto errLabel; @@ -131,7 +131,7 @@ namespace cw } fieldN += 1; - bi = i+1; + bi = i+1; state = kBeforeField; } break; @@ -266,8 +266,10 @@ namespace cw goto errLabel; } - - + // skip leading white space + while( isspace(p->lineBuf[ p->colA[colIdx].char_idx ]) && p->lineBuf[ p->colA[colIdx].char_idx ] ) + p->colA[colIdx].char_idx++; + fieldStr_Ref = p->lineBuf + p->colA[colIdx].char_idx; errLabel: diff --git a/cwCsv.h b/cwCsv.h index b4c935a..22048ad 100644 --- a/cwCsv.h +++ b/cwCsv.h @@ -10,6 +10,7 @@ namespace cw typedef handle handle_t; // The first line of the CSV is expected to hold the column titles. + // Every column must have a title. // If titlesA and titleN are valid then these will be verified to exist when the CSV file is opened. rc_t create( handle_t& hRef, const char* fname, const char** titleA=nullptr, unsigned titleN=0 );