nlib
nn::nlib::msgpack::CsvReader Class Referencefinal

CSV parser. Reads and parses the CSV string from the stream. More...

#include "nn/nlib/msgpack/CsvReader.h"

Public Member Functions

errno_t Open (InputStream *stream) noexcept
 Sets an input stream. More...
 
bool Close () noexcept
 Closes CsvReader. More...
 
bool Read (MpObject *obj) noexcept
 
InputStreamGetStream () noexcept
 Gets the base stream specified in Init. More...
 
Constructor, Destructor, and Initialization
constexpr CsvReader () noexcept
 Instantiates the object with default parameters (default constructor). Requires initialization with Init() after execution.
 
 ~CsvReader () noexcept
 Destructor.
 
 CsvReader (CsvReader &&rhs)
 Instantiates the object (move constructor).
 
CsvReaderoperator= (CsvReader &&rhs)
 Move assignment operator.
 
void Reset () noexcept
 Resets this object to the state immediately after the default constructor was executed.
 
errno_t Init () noexcept
 Initializes CsvReader. More...
 
Error Checking
errno_t GetErrorValue () const noexcept
 Gets the error that occurred. More...
 
 operator bool () const
 Returns true if the object has been initialized and an error has not occurred inside, or returns false if an error has occurred inside.
 

Static Public Member Functions

static bool Read (MpObject *obj, const nlib_utf8_t *csvtext) noexcept
 Parses a CSV string and stores it in an object. More...
 
static std::pair< errno_t, std::unique_ptr< MpObject > > Read (const nlib_utf8_t *csvtext) noexcept
 Parses a CSV string to create and return MpObject. More...
 

Detailed Description

CSV parser. Reads and parses the CSV string from the stream.

Description
Basically complies with RFC 4180.
However, it is different in the following ways.
  • Both CR and LF alone, in addition to CRLF, are recognized as new line codes.
  • UTF-8 strings are passed. UTF-8 strings are checked for UTF-8 validity.
The read data is converted to MpObject. The data takes the form of an array of arrays, and the length is normalized to the longest item. The field string may be converted to a different type when parsing.
Specifics are shown below.
  • Integer and floating point values are converted to numerical values. Hex expressions are read as a string.
  • true and false are converted to a boolean value.
  • Empty strings are converted to nil.
This is illustrated by the following coding example.
const nlib_utf8_t* csv = R"(10,-10,1.2,0x100
true,false,
"aaa","b
bb","ccc"
zzz,yyy,xxx
"aaa","b""bb","ccc")";
auto r = CsvReader::Read(csv);
SUCCEED_IF(r.first == 0);
ToJson(buf, *r.second);
nlib_printf("%s\n", buf);
/*
Output:
[[10,-10,1.2,"0x100"],[true,false,null,null],["aaa","b\nbb","ccc",null],["zzz","yyy","xxx",null],["aaa","b\"bb","ccc",null]]
*/
See also
https://www.ietf.org/rfc/rfc4180.txt (RFC4180, CSV)
http://www.kasai.fm/wiki/rfc4180jp (RFC4180, CSV, in Japanese)

Definition at line 33 of file CsvReader.h.

Member Function Documentation

◆ Close()

nn::nlib::msgpack::CsvReader::Close ( )
noexcept

Closes CsvReader.

Returns
Returns true when successful.
Description
Closes CsvReader and detaches the base stream. The base stream will not be closed.
The object may be reused by calling the Open() member function again.

◆ GetErrorValue()

nn::nlib::msgpack::CsvReader::GetErrorValue ( ) const
inlinenoexcept

Gets the error that occurred.

Return values
0No error occurred.
EILSEQIncorrect CSV character found (parse error).
EINVALError in an argument.
EEXISTAttempted to initialize a CsvReader that has already been initialized.
EIOAn error occurred in the base stream.
EBADFAttempted to use an uninitialized CsvReader.
ENOMEMFailed to allocate memory.

Definition at line 46 of file CsvReader.h.

◆ GetStream()

nn::nlib::msgpack::CsvReader::GetStream ( )
noexcept

Gets the base stream specified in Init.

Returns
Returns the pointer to the stream.

◆ Init()

nn::nlib::msgpack::CsvReader::Init ( )
noexcept

Initializes CsvReader.

Returns
Returns 0 if successful.

◆ Open()

nn::nlib::msgpack::CsvReader::Open ( InputStream stream)
noexcept

Sets an input stream.

Parameters
[in]streamPointer to the input stream.
Returns
Returns 0 if successful.

◆ Read() [1/3]

nn::nlib::msgpack::CsvReader::Read ( MpObject obj,
const nlib_utf8_t csvtext 
)
staticnoexcept

Parses a CSV string and stores it in an object.

Parameters
[in]objObject used to store the converted CSV.
[in]csvtextCSV string.
Returns
Returns true on success.
Description
Internally creates CsvReader to read and parse the CSV string from the stream. Returns false if there is any error during this process. If this occurs, obj is not altered.

◆ Read() [2/3]

nn::nlib::msgpack::CsvReader::Read ( const nlib_utf8_t csvtext)
staticnoexcept

Parses a CSV string to create and return MpObject.

Parameters
[in]csvtextCSV string.
Returns
A pair of the error value and the created MpObject. Parses a CSV string and stores it in an object. Internally creates CsvReader to read and parse the CSV string. Successful if the error value is other than 0, and created MpObject is returned as the parsing result.

◆ Read() [3/3]

nn::nlib::msgpack::CsvReader::Read ( MpObject obj)
noexcept
Parameters
[in]objObject where the CSV is stored after being read.
Returns
Returns true when successful. Reads and parses the CSV string from the stream, and stores it in the obj parameter.
Description
The CSV string is converted to MpObject and stored in the obj parameter. Sets an error and returns false when an error occurs while reading.

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