cwCsv.h/cpp : Add minor functions and update API comments.

This commit is contained in:
kevin 2023-05-12 09:00:50 -04:00
parent b178e1994a
commit 2a51daaa90
2 changed files with 32 additions and 1 deletions

View File

@ -380,6 +380,23 @@ cw::rc_t cw::csv::line_count( handle_t h, unsigned& lineCntRef )
return rc; return rc;
} }
unsigned cw::csv::col_count( handle_t h )
{
csv_t* p = _handleToPtr(h);
return p->colN;
}
const char* cw::csv::col_title( handle_t h, unsigned idx )
{
csv_t* p = _handleToPtr(h);
if( idx >= p->colN )
return nullptr;
return p->colA[idx].title;
}
unsigned cw::csv::title_col_index( handle_t h, const char* title ) unsigned cw::csv::title_col_index( handle_t h, const char* title )
{ {
csv_t* p = _handleToPtr(h); csv_t* p = _handleToPtr(h);

14
cwCsv.h
View File

@ -7,21 +7,35 @@ namespace cw
{ {
typedef handle<struct csv_str> handle_t; typedef handle<struct csv_str> handle_t;
// The first line of the CSV is expected to hold the column titles.
// 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 ); rc_t create( handle_t& hRef, const char* fname, const char** titleA=nullptr, unsigned titleN=0 );
rc_t destroy(handle_t& hRef ); rc_t destroy(handle_t& hRef );
// Count of lines in the CSV including the title line.
// Subtract 1 to get the count of data lines.
rc_t line_count( handle_t h, unsigned& lineCntRef ); rc_t line_count( handle_t h, unsigned& lineCntRef );
// Count of columns in the first row (title row).
unsigned col_count( handle_t h );
const char* col_title( handle_t h, unsigned idx );
unsigned title_col_index( handle_t h, const char* title ); unsigned title_col_index( handle_t h, const char* title );
// Reset the CSV to make the title line current.
// The next call to 'next_line()' will make the first data row current.
rc_t rewind( handle_t h ); rc_t rewind( handle_t h );
// Make the next row current. The 'getv()' and parse_???()' functions
// operate on the current row.
// This function return kEofRC when it increments past the last line in the file.
rc_t next_line( handle_t h ); rc_t next_line( handle_t h );
// line index (first line==0) of the line currently being parsed. // line index (first line==0) of the line currently being parsed.
unsigned cur_line_index( handle_t h ); unsigned cur_line_index( handle_t h );
// Return the count of characters in the field identified by 'colIdx'.
rc_t field_char_count( handle_t h, unsigned colIdx, unsigned& charCntRef ); rc_t field_char_count( handle_t h, unsigned colIdx, unsigned& charCntRef );
rc_t parse_field( handle_t h, unsigned colIdx, unsigned& valRef ); rc_t parse_field( handle_t h, unsigned colIdx, unsigned& valRef );