From 2a51daaa901d3658502caaa2114df15b84efb711 Mon Sep 17 00:00:00 2001 From: kevin Date: Fri, 12 May 2023 09:00:50 -0400 Subject: [PATCH] cwCsv.h/cpp : Add minor functions and update API comments. --- cwCsv.cpp | 17 +++++++++++++++++ cwCsv.h | 16 +++++++++++++++- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/cwCsv.cpp b/cwCsv.cpp index a69cf0d..45a5fcf 100644 --- a/cwCsv.cpp +++ b/cwCsv.cpp @@ -380,6 +380,23 @@ cw::rc_t cw::csv::line_count( handle_t h, unsigned& lineCntRef ) 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 ) { csv_t* p = _handleToPtr(h); diff --git a/cwCsv.h b/cwCsv.h index c2795ac..3da8978 100644 --- a/cwCsv.h +++ b/cwCsv.h @@ -7,21 +7,35 @@ namespace cw { typedef handle 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 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 ); + // 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 ); + // 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 ); - + + // 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 ); // line index (first line==0) of the line currently being parsed. 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 parse_field( handle_t h, unsigned colIdx, unsigned& valRef );