libdballe 9.13
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
29
30#include <iosfwd>
31#include <stdio.h>
32#include <string>
33#include <vector>
34#include <wreport/var.h>
35
36namespace dballe {
37
48bool csv_read_next(FILE* in, std::vector<std::string>& cols);
49
50class CSVReader
51{
52protected:
53 std::istream* in;
54
55 int next_char();
56
57public:
64 std::string line;
65
67 std::vector<std::string> cols;
68
69 CSVReader();
70 CSVReader(std::istream& in);
71 CSVReader(const std::string& pathname);
72 ~CSVReader();
73
77 void open(const std::string& pathname);
82 void close();
83
89 int as_int(unsigned col) const;
90
96 int as_int_withmissing(unsigned col) const;
97
103 wreport::Varcode as_varcode(unsigned col) const;
104
115 bool move_to_data(unsigned number_col = 0);
116
118 bool next();
119
120 static std::string unescape(const std::string& csvstr);
121};
122
123// TODO: CSV readers allowing to peek on the next line without consuming it, to
124// allow the Msg parser to stop at msg boundary after peeking at a line
125// also, stripping newlines at end of lines
126// also, reading from istream
127// also, de-escaping strings (if they start with quote)
128
132void csv_output_quoted_string(std::ostream& out, const std::string& str);
133
137void csv_output_quoted_string(FILE* out, const std::string& str);
138
140{
141protected:
142 std::string row;
143
144public:
145 virtual ~CSVWriter();
146
149
151 void add_value_raw(const char* str);
152
154 void add_value_raw(const std::string& str);
155
157 void add_value(int val);
158
161
163 void add_value(unsigned val);
164
166 void add_value(uint64_t val);
167
169 void add_value(wreport::Varcode val);
170
173
176
178 void add_value(const char* val);
179
181 void add_value(const std::string& val);
182
184 virtual void flush_row() = 0;
185};
186
187} // namespace dballe
188#endif
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:62
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:64
std::vector< std::string > cols
Parsed CSV columns for the last line read.
Definition csv.h:67
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:140
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.