Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
Aleph::CsvReader Class Reference

Lazy CSV reader for large files. More...

#include <parse-csv.H>

Collaboration diagram for Aleph::CsvReader:
[legend]

Classes

class  Iterator
 

Public Member Functions

 CsvReader (const std::string &filename, char delimiter=',')
 Construct reader from file path.
 
 CsvReader (std::istream &stream, char delimiter=',')
 Construct reader from stream.
 
 CsvReader (CsvReader &&other)
 Take the stream and the header of another reader, which is left reading nothing.
 
CsvReader & operator= (CsvReader &&other)
 Exchange this reader with another, which is left with the previous stream and header of this one.
 
void swap (CsvReader &other)
 Exchange the streams, the headers and the counters of this reader and another.
 
Array< std::string > read_header ()
 Read and store the header row.
 
bool has_next () const
 Check if more rows are available.
 
Array< std::string > next ()
 Read the next row as Array.
 
CsvRow next_row ()
 Read the next row with header access.
 
size_t rows_read () const
 Get number of rows read so far.
 
const Array< std::string > & header () const
 Get the header (if read).
 
bool has_header () const
 Check if header was read.
 
void skip (size_t n)
 Skip N rows.
 
void reset ()
 Reset to beginning (only works for file-based readers).
 
Iterator begin ()
 
Iterator end ()
 

Private Attributes

std::ifstream file_
 
std::istream * stream_
 
bool owns_stream_
 
char delimiter_
 
Array< std::string > header_
 
bool has_header_
 
size_t row_count_
 

Detailed Description

Lazy CSV reader for large files.

CsvReader provides an iterator-based interface for reading CSV files row by row without loading the entire file into memory.

// Read large CSV file efficiently
CsvReader reader("large_file.csv");
// Skip header and use it for column names
reader.read_header();
while (reader.has_next())
{
CsvRow row = reader.next_row();
process(row["id"], row["value"]);
}
// Or use range-based for (without header)
CsvReader reader2("data.csv");
for (const auto & row : reader2)
std::cout << row[0] << "\n";
size_t row
Definition ca-c-api.h:115
Lazy CSV reader for large files.
Definition parse-csv.H:712
A CSV row with header-based field access.
Definition parse-csv.H:602
STL namespace.

Definition at line 711 of file parse-csv.H.

Constructor & Destructor Documentation

◆ CsvReader() [1/3]

Aleph::CsvReader::CsvReader ( const std::string &  filename,
char  delimiter = ',' 
)
inlineexplicit

Construct reader from file path.

Exceptions
std::runtime_errorIf file cannot be opened.

Definition at line 724 of file parse-csv.H.

References file_.

◆ CsvReader() [2/3]

Aleph::CsvReader::CsvReader ( std::istream &  stream,
char  delimiter = ',' 
)
inlineexplicit

Construct reader from stream.

Definition at line 733 of file parse-csv.H.

◆ CsvReader() [3/3]

Aleph::CsvReader::CsvReader ( CsvReader &&  other)
inline

Take the stream and the header of another reader, which is left reading nothing.

The implicit move took the file but copied the pointer to it: the reader read through the file member of the moved-from reader, also after that reader was destroyed.

Parameters
[in,out]otherreader to move from.

Definition at line 746 of file parse-csv.H.

Member Function Documentation

◆ begin()

Iterator Aleph::CsvReader::begin ( )
inline

Definition at line 878 of file parse-csv.H.

◆ end()

Iterator Aleph::CsvReader::end ( )
inline

Definition at line 879 of file parse-csv.H.

◆ has_header()

bool Aleph::CsvReader::has_header ( ) const
inline

Check if header was read.

Definition at line 822 of file parse-csv.H.

References has_header_.

Referenced by TEST_F().

◆ has_next()

bool Aleph::CsvReader::has_next ( ) const
inline

Check if more rows are available.

Definition at line 793 of file parse-csv.H.

References Aleph::and, and stream_.

Referenced by Aleph::CsvReader::Iterator::Iterator(), main(), Aleph::CsvReader::Iterator::operator++(), skip(), TEST(), TEST(), and TEST_F().

◆ header()

const Array< std::string > & Aleph::CsvReader::header ( ) const
inline

Get the header (if read).

Definition at line 819 of file parse-csv.H.

References header_.

Referenced by TEST_F().

◆ next()

Array< std::string > Aleph::CsvReader::next ( )
inline

◆ next_row()

CsvRow Aleph::CsvReader::next_row ( )
inline

Read the next row with header access.

Definition at line 806 of file parse-csv.H.

References Aleph::csv_read_row(), delimiter_, has_header_, header_, row_count_, and stream_.

Referenced by main(), TEST_F(), and TEST_F().

◆ operator=()

CsvReader & Aleph::CsvReader::operator= ( CsvReader &&  other)
inline

Exchange this reader with another, which is left with the previous stream and header of this one.

Parameters
[in,out]otherreader to move from.
Returns
this reader.

Definition at line 758 of file parse-csv.H.

References swap().

◆ read_header()

Array< std::string > Aleph::CsvReader::read_header ( )
inline

Read and store the header row.

Returns
The header row.

Definition at line 785 of file parse-csv.H.

References Aleph::csv_read_row(), delimiter_, has_header_, header_, and stream_.

Referenced by main(), reset(), TEST(), TEST(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), and TEST_F().

◆ reset()

void Aleph::CsvReader::reset ( )
inline

Reset to beginning (only works for file-based readers).

Definition at line 832 of file parse-csv.H.

References file_, has_header_, owns_stream_, read_header(), and row_count_.

Referenced by TEST().

◆ rows_read()

size_t Aleph::CsvReader::rows_read ( ) const
inline

Get number of rows read so far.

Definition at line 816 of file parse-csv.H.

References row_count_.

Referenced by TEST_F().

◆ skip()

void Aleph::CsvReader::skip ( size_t  n)
inline

Skip N rows.

Definition at line 825 of file parse-csv.H.

References Aleph::and, has_next(), and next().

Referenced by TEST_F().

◆ swap()

void Aleph::CsvReader::swap ( CsvReader &  other)
inline

Exchange the streams, the headers and the counters of this reader and another.

Parameters
[in,out]otherreader to exchange with.

Definition at line 768 of file parse-csv.H.

References delimiter_, file_, has_header_, header_, owns_stream_, row_count_, stream_, and Aleph::Array< T >::swap().

Referenced by operator=().

Member Data Documentation

◆ delimiter_

char Aleph::CsvReader::delimiter_
private

Definition at line 716 of file parse-csv.H.

Referenced by next(), next_row(), read_header(), and swap().

◆ file_

std::ifstream Aleph::CsvReader::file_
private

Definition at line 713 of file parse-csv.H.

Referenced by CsvReader(), reset(), and swap().

◆ has_header_

bool Aleph::CsvReader::has_header_
private

Definition at line 718 of file parse-csv.H.

Referenced by has_header(), next_row(), read_header(), reset(), and swap().

◆ header_

Array<std::string> Aleph::CsvReader::header_
private

Definition at line 717 of file parse-csv.H.

Referenced by header(), next_row(), read_header(), and swap().

◆ owns_stream_

bool Aleph::CsvReader::owns_stream_
private

Definition at line 715 of file parse-csv.H.

Referenced by reset(), and swap().

◆ row_count_

size_t Aleph::CsvReader::row_count_
private

Definition at line 719 of file parse-csv.H.

Referenced by next(), next_row(), reset(), rows_read(), and swap().

◆ stream_

std::istream* Aleph::CsvReader::stream_
private

Definition at line 714 of file parse-csv.H.

Referenced by has_next(), next(), next_row(), read_header(), and swap().


The documentation for this class was generated from the following file: