libdballe  5.18
csv.h
Go to the documentation of this file.
1 /*
2  * dballe/csv - CSV utility functions
3  *
4  * Copyright (C) 2005--2011 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 <vector>
31 #include <string>
32 #include <iosfwd>
33 #include <stdio.h>
34 
35 namespace dballe
36 {
37 
48 bool csv_read_next(FILE* in, std::vector<std::string>& cols);
49 
50 class CSVReader
51 {
52 protected:
54  virtual bool nextline() = 0;
55 
56 public:
58  std::string line;
59 
61  std::vector<std::string> cols;
62 
63  virtual ~CSVReader();
64 
75  bool move_to_data(unsigned number_col=0);
76 
78  bool next();
79 
80  static std::string unescape(const std::string& csvstr);
81 };
82 
84 {
85 protected:
86  virtual bool nextline();
87 
88 public:
89  std::istream& in;
90 
91  IstreamCSVReader(std::istream& in) : in(in) {}
92 };
93 
94 // TODO: CSV readers allowing to peek on the next line without consuming it, to
95 // allow the Msg parser to stop at msg boundary after peeking at a line
96 // also, stripping newlines at end of lines
97 // also, reading from istream
98 // also, de-escaping strings (if they start with quote)
99 
103 void csv_output_quoted_string(std::ostream& out, const std::string& str);
104 
105 }
106 
107 /* vim:set ts=4 sw=4: */
108 #endif