.TH "FBB::CSV4180" "3bobcat" "2005\-2020" "libbobcat\-dev_5\&.07\&.00" "CSV4180 convertor" .PP .SH "NAME" FBB::CSV4180 \- Converter for comma separated values .PP .SH "SYNOPSIS" \fB#include \fP .br .PP Linking option: \fI\-lbobcat\fP .PP .SH "DESCRIPTION" .PP Objects of the class \fBCSV4180\fP can be used to convert series of comma separated values to the individual separated values (also called `fields\(cq\& below)\&. The class implements RFC 4180 (cf\&. https://www\&.ietf\&.org/rfc/rfc4180\&.txt, section 2)\&. .PP According to RFC 4180 lines contain comma separated values: comma separated values on one line are processed together, as a series of values\&. The final comma separated value on a line is not ended by a comma\&. .PP Comma separated values may be surrounded by double quotes\&. However, they \fImust\fP be surrounded by double quotes in these cases: .IP o if the values contain commas; .IP o if the values contain double quotes (in which case the double quote is `escaped\(cq\& by doubling it, e\&.g\&., \fI\(dq\&a \(dq\&\(dq\& double quote\(dq\&\fP); .IP o if the values extend over multiple lines\&. E\&.g\&., .nf \(dq\&First line second line\(dq\& .fi .PP Comma separated values may be empty: the following line defines three empty comma separated values: .nf ,, .fi The first empty value starts at the beginning of the line, and continues up to the first comma; the second empty value starts beyond the first comma and continues up to the second comma; the third empty value starts beyond the second comma, and continues up to the end of the line\&. If the line ends in blank space characters then the third value isn\(cq\&t empty, but contains those blank space characters\&. .PP By default, values are interpreted as strings\&. The \fBCSV4180\fP class also offers facilities to ignore specific fields, or to ensure that they can be converted to integral or floating point values\&. The second constructor (below) expects a \fIstd::string\fP argument defining how to interpret fields\&. Options are: .IP o \fII\fP: the field must be convertible to an integral value; .IP o \fID\fP: the field must be convertible to a floating point value; .IP o \fIS\fP: the field is a string: it is used as\-is; .IP o \fIX\fP: the field is omitted from the final set of comma separated values\&. I\&.e\&., if a line contains three comma separated values, and the specification \fI\(dq\&SXS\(dq\&\fP is used then this results in two comma separated values: the first and third value of three comma separated values encountered on lines\&. .IP o \fI\-\fP: synonym of \fIX\fP\&. .PP In addition, field specifications may contain blank spaces, which are ignored\&. .PP When processing comma separated values the first line may be considered a \fIheader\fP line\&. \fIX\fP specifications also apply to header lines, but otherwise they merely consist of \fIS\fP\-type fields\&. In addition, when processing multiple input lines all non\-header lines are made available in a vector of vectors of fields, whereas the header line itself can be accessed via a dedicated member (\fIheader()\fP)\&. .PP .SH "NAMESPACE" \fBFBB\fP .br All constructors, members, operators and manipulators, mentioned in this man\-page, are defined in the namespace \fBFBB\fP\&. .PP .SH "INHERITS FROM" \- .PP .SH "CONSTRUCTORS" .IP o \fBexplicit CSV4180(size_t nFields = 0, bool header = false, char fieldSep = \(cq\&,\(cq\&)\fP: .br The first parameter specifies the number of fields that must be present on input lines\&. When using the default value the number of fields encountered on the first line determines the number of fields that must be present on subsequent lines\&. If the second parameter is \fItrue\fP then the first line is interpreted as the header line\&. The third parameter specifies the character separating the fields\&. By default it\(cq\&s a comma, but sometimes (not part of the RFC) a semicolon is used\&. By specifying \fIfieldSep\fP any character other than a comma can be used as field separator\&. .IP .IP o \fBexplicit CSV4180(std::string const &specs, bool header = false, char fieldSep = \(cq\&,\(cq\&)\fP: .br The first parameter defines the number and types of the comma separated values on input lines\&. Specifications can be .RS .IP o \fID\fP: the field must be convertible to a floating point value; .IP o \fII\fP: the field must be convertible to an integral value; .IP o \fIS\fP: the field is left as\-is, and can be retrieved as a \fIstd::string\fP\&. .IP o \fIX\fP or \fI\-\fP: the field is ignored and is not stored inside the \fBCSV4180\fP object\&. .IP o blank space characters are ignored\&. .RE An exception is thrown when encountering other than the abovementioned characters are encountered\&. .IP If \fII\fP or \fID\fP fields cannot be properly converted, or if a line contains too few or too many comma separated values the input stream\(cq\&s fail status is set\&. .IP The last two parameters are interpreted as the last two parameters of the previous constructor\&. .IP Copy and move constructors (and assignment operators) are available\&. .PP .SH "OVERLOADED OPERATORS" .IP o \fBstd::istream &operator>>(std::istream &in, CSV4180 &csv)\fP: .br One line of text is extracted from \fIin\fP and processed by the \fIcsv\fP object\&. The \fIcsv\fP object may or may not already contain converted comma separated values\&. When empty, the first line is processed according to the specifications provided to the \fIcsv\fP object at construction time\&. Otherwise, the comma separated values on extracted lines must match the number and types of the fields, as specified by the \fIcsv\fP object\&. When input lines do not match these specifications \fIin\(cq\&s\fP fail status is set\&. .PP .SH "MEMBER FUNCTIONS" .IP o \fBvoid clear(size_t nFields = 0)\fP: .br The internally stored data (referred to by the \fIdata, header,\fP and \fIlastLine\fP members) are erased\&. By default, the required number of CSV fields is reset to 0, but can be set to a specific value by specifying a value for its \fInFields\fP parameter\&. .IP .IP o \fBstd::vector> const &data() const\fP: .br A reference to the vector of vectors of fields stored inside the \fBCSV4180\fP object is returned\&. The vector returned by \fIdata\fP does not contain the header line\&. If a header line was requested it can be retrieved from the \fIheader()\fP member\&. .IP .IP o \fBstd::vector const &header() const\fP: .br If the constructor\(cq\&s \fIheader\fP parameter was specified as \fItrue\fP then this member returns the fields encountered on the first line that was processed by the \fIread1\fP member\&. Otherwise, \fIheader\fP returns a reference to an empty vector\&. .IP .IP o \fBstd::string const &lastLine() const\fP: .br A reference to the last line that was successfully extracted from the input stream by the \fIread1\fP member is returned\&. So once the lines containing the comma separated values have been processed, the next line on the input stream can be obtained from this member\&. .IP .IP o \fBsize_t nValues() const\fP: .br After successfully calling \fIread1\fP for the first time this member returns the required number of comma separated values that must be encountered on subsequent input lines\&. .IP .IP o \fBsize_t read(std::istream &in, size_t nLines = 0)\fP: .br By default, all lines of \fIin\fP are read and are processed by the \fIread1\fP member\&. By specifying a non\-zero value for the \fInLines\fP parameter the specified number of lines is read from \fIin\fP\&. Reading stops once \fIin\(cq\&s\fP status is not \fIgood\fP\&. When \fInLines\fP is specified as zero, then \fIin\(cq\&s\fP status flags are cleared\&. The number of successfully processed lines is returned\&. .IP .IP o \fBstd::istream &read1(std::istream &in)\fP: .br One line is read from \fIin\fP and is parsed for its comma separated values\&. If parsing fails, \fIin\(cq\&s fail\fP status is set\&. After successfully calling \fIread1\fP for the first time all subsequent lines read by \fIread1\fP must have the same number of comma separated values as encountered when calling \fIread1\fP for the first time\&. The parsed fields are stored in a vector of \fIstd::string\fP objects, and that vector is added to the vector of vectors of strings that is returned by the \fIdata\fP member\&. .IP .IP o \fBstd::vector> release()\fP: .br The vector of vectors of fields stored inside the \fBCSV4180\fP object is returned\&. After calling \fIrelease\fP the internally stored vector of fields is empty\&. The vector returned by \fIdata\fP does not contain the header line\&. If a header line was requested it can be retrieved from the \fIheader()\fP member\&. Note that this member does not reset the number of expected fields for subsequently processed CSV\-lines\&. If that\(cq\&s what you want, call \fIclear\fP after calling \fIrelease\fP\&. .IP .SH "EXAMPLE" .nf #include #include using namespace std; using namespace FBB; int main(int argc, char **argv) { .fi .PP .nf CSV4180 csv; // this processes \(cq\&input\(cq\& .fi .PP .nf size_t nLines = csv\&.read(cin); cerr << nLines << \(dq\& lines were read\en\(dq\&; if (not csv\&.header()\&.empty()) { cerr << \(dq\&header: \(dq\& << \(cq\&\en\(cq\&; for (auto const &field: csv\&.header()) cerr << \(dq\& `\(dq\& << field << \(dq\&\(cq\&\en\(dq\&; } cerr << \(dq\&# CSV values: \(dq\& << csv\&.nValues() << \(cq\&\en\(cq\&; for (auto const &line: csv\&.data()) { cerr << \(dq\&Line:\en\(dq\&; for (auto const &entry: line) cerr << \(dq\& `\(dq\& << entry << \(dq\&\(cq\&\en\(dq\&; } } .fi .PP .SH "FILES" \fIbobcat/csv\fP \- defines the class interface .PP .SH "SEE ALSO" \fBbobcat\fP(7) .PP .SH "BUGS" None Reported\&. .PP .SH "BOBCAT PROJECT FILES" .PP .IP o \fIhttps://fbb\-git\&.gitlab\&.io/bobcat/\fP: gitlab project page; .IP o \fIbobcat_5\&.07\&.00\-x\&.dsc\fP: detached signature; .IP o \fIbobcat_5\&.07\&.00\-x\&.tar\&.gz\fP: source archive; .IP o \fIbobcat_5\&.07\&.00\-x_i386\&.changes\fP: change log; .IP o \fIlibbobcat1_5\&.07\&.00\-x_*\&.deb\fP: debian package containing the libraries; .IP o \fIlibbobcat1\-dev_5\&.07\&.00\-x_*\&.deb\fP: debian package containing the libraries, headers and manual pages; .PP .SH "BOBCAT" Bobcat is an acronym of `Brokken\(cq\&s Own Base Classes And Templates\(cq\&\&. .PP .SH "COPYRIGHT" This is free software, distributed under the terms of the GNU General Public License (GPL)\&. .PP .SH "AUTHOR" Frank B\&. Brokken (\fBf\&.b\&.brokken@rug\&.nl\fP)\&. .PP