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.
This commit is contained in:
kevin 2025-04-18 14:59:07 -04:00
parent 95f26398e8
commit c789d70ce2
2 changed files with 7 additions and 4 deletions

View File

@ -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:

View File

@ -10,6 +10,7 @@ namespace cw
typedef handle<struct csv_str> 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 );