libdballe  9.11
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 
36 namespace dballe
37 {
38 
49 bool csv_read_next(FILE* in, std::vector<std::string>& cols);
50 
51 class CSVReader
52 {
53 protected:
54  std::istream* in;
55 
56  int next_char();
57 
58 public:
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 
133 void csv_output_quoted_string(std::ostream& out, const std::string& str);
134 
138 void csv_output_quoted_string(FILE* out, const std::string& str);
139 
141 {
142 protected:
143  std::string row;
144 
145 public:
146  virtual ~CSVWriter();
147 
149  void add_value_empty();
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 
161  void add_value_withmissing(int val);
162 
164  void add_value(unsigned val);
165 
167  void add_value(uint64_t val);
168 
170  void add_value(wreport::Varcode val);
171 
173  void add_var_value_raw(const wreport::Var& val);
174 
176  void add_var_value_formatted(const wreport::Var& val);
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:140
wreport::Varcode as_varcode(unsigned col) const
Return the given column, as a Varcode.
virtual void flush_row()=0
Write the current line to the output file, and start a new one.
std::string line
Last line read.
Definition: csv.h:65
void add_value_raw(const char *str)
Add a value to the current row, without any escaping.
void csv_output_quoted_string(std::ostream &out, const std::string &str)
Output a string value, quoted if needed according to CSV rules.
bool close_on_exit
If true, the input stream will be deleted upon destruction.
Definition: csv.h:63
void add_var_value_raw(const wreport::Var &val)
Add a variable value, in its raw integer form.
Definition: cmdline.h:18
void add_var_value_formatted(const wreport::Var &val)
Add a variable value, formatted.
bool next()
Read the next CSV line, returning false if EOF is reached.
uint16_t Varcode
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.
void close()
Sets in to 0.
void add_value_empty()
Add an empty value to the current row.
std::vector< std::string > cols
Parsed CSV columns for the last line read.
Definition: csv.h:68
Definition: csv.h:51
int as_int(unsigned col) const
Return the given column, as an integer.
void add_value_withmissing(int val)
Add an int value that can potentially be missing.
bool csv_read_next(FILE *in, std::vector< std::string > &cols)
Parse a CSV line.
bool move_to_data(unsigned number_col=0)
Find the first line where the given column exists and starts with a number.
void add_value(int val)
Add an int value to the current row.