libdballe 9.6
csv.h
Go to the documentation of this file.
1/*
2 * dballe/csv - CSV utility functions
3 *
4 * Copyright (C) 2005--2014 ARPA-SIM <urpsim@smr.arpa.emr.it>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Author: Enrico Zini <enrico@enricozini.com>
20 */
21
22#ifndef DBA_CSV_H
23#define DBA_CSV_H
24
30#include <wreport/var.h>
31#include <vector>
32#include <string>
33#include <iosfwd>
34#include <stdio.h>
35
36namespace dballe
37{
38
49bool csv_read_next(FILE* in, std::vector<std::string>& cols);
50
52{
53protected:
54 std::istream* in;
55
56 int next_char();
57
58public:
65 std::string line;
66
68 std::vector<std::string> cols;
69
70 CSVReader();
71 CSVReader(std::istream& in);
72 CSVReader(const std::string& pathname);
73 ~CSVReader();
74
78 void open(const std::string& pathname);
83 void close();
84
90 int as_int(unsigned col) const;
91
97 int as_int_withmissing(unsigned col) const;
98
104 wreport::Varcode as_varcode(unsigned col) const;
105
116 bool move_to_data(unsigned number_col=0);
117
119 bool next();
120
121 static std::string unescape(const std::string& csvstr);
122};
123
124// TODO: CSV readers allowing to peek on the next line without consuming it, to
125// allow the Msg parser to stop at msg boundary after peeking at a line
126// also, stripping newlines at end of lines
127// also, reading from istream
128// also, de-escaping strings (if they start with quote)
129
133void csv_output_quoted_string(std::ostream& out, const std::string& str);
134
138void csv_output_quoted_string(FILE* out, const std::string& str);
139
141{
142protected:
143 std::string row;
144
145public:
146 virtual ~CSVWriter();
147
150
152 void add_value_raw(const char* str);
153
155 void add_value_raw(const std::string& str);
156
158 void add_value(int val);
159
162
164 void add_value(unsigned val);
165
167 void add_value(uint64_t val);
168
171
174
177
179 void add_value(const char* val);
180
182 void add_value(const std::string& val);
183
185 virtual void flush_row() = 0;
186};
187
188
189}
190#endif
Definition: csv.h:52
void close()
Sets in to 0.
wreport::Varcode as_varcode(unsigned col) const
Return the given column, as a Varcode.
bool next()
Read the next CSV line, returning false if EOF is reached.
bool close_on_exit
If true, the input stream will be deleted upon destruction.
Definition: csv.h:63
bool move_to_data(unsigned number_col=0)
Find the first line where the given column exists and starts with a number.
std::string line
Last line read.
Definition: csv.h:65
std::vector< std::string > cols
Parsed CSV columns for the last line read.
Definition: csv.h:68
void open(const std::string &pathname)
Open the given file and sets close_on_exit to true.
int as_int_withmissing(unsigned col) const
Return the given column, as an integer.
int as_int(unsigned col) const
Return the given column, as an integer.
Definition: csv.h:141
virtual void flush_row()=0
Write the current line to the output file, and start a new one.
void add_value(const std::string &val)
Add a string to the current row.
void add_var_value_raw(const wreport::Var &val)
Add a variable value, in its raw integer form.
void add_value(unsigned val)
Add an int value to the current row.
void add_value(const char *val)
Add a string to the current row.
void add_var_value_formatted(const wreport::Var &val)
Add a variable value, formatted.
void add_value_raw(const char *str)
Add a value to the current row, without any escaping.
void add_value_withmissing(int val)
Add an int value that can potentially be missing.
void add_value(uint64_t val)
Add an int value to the current row.
void add_value_raw(const std::string &str)
Add a value to the current row, without any escaping.
void add_value(wreport::Varcode val)
Add a formatted varcode to the current row.
void add_value(int val)
Add an int value to the current row.
void add_value_empty()
Add an empty value to the current row.
bool csv_read_next(FILE *in, std::vector< std::string > &cols)
Parse a CSV line.
void csv_output_quoted_string(std::ostream &out, const std::string &str)
Output a string value, quoted if needed according to CSV rules.
uint16_t Varcode