libdballe 5.10
|
00001 /* 00002 * dballe/csv - CSV utility functions 00003 * 00004 * Copyright (C) 2005--2011 ARPA-SIM <urpsim@smr.arpa.emr.it> 00005 * 00006 * This program is free software; you can redistribute it and/or modify 00007 * it under the terms of the GNU General Public License as published by 00008 * the Free Software Foundation; either version 2 of the License. 00009 * 00010 * This program is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 * GNU General Public License for more details. 00014 * 00015 * You should have received a copy of the GNU General Public License 00016 * along with this program; if not, write to the Free Software 00017 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 00018 * 00019 * Author: Enrico Zini <enrico@enricozini.com> 00020 */ 00021 00022 #ifndef DBA_CSV_H 00023 #define DBA_CSV_H 00024 00030 #include <vector> 00031 #include <string> 00032 #include <iosfwd> 00033 #include <stdio.h> 00034 00035 namespace dballe 00036 { 00037 00048 bool csv_read_next(FILE* in, std::vector<std::string>& cols); 00049 00050 class CSVReader 00051 { 00052 protected: 00054 virtual bool nextline() = 0; 00055 00056 public: 00058 std::string line; 00059 00061 std::vector<std::string> cols; 00062 00063 virtual ~CSVReader(); 00064 00075 bool move_to_data(unsigned number_col=0); 00076 00078 bool next(); 00079 00080 static std::string unescape(const std::string& csvstr); 00081 }; 00082 00083 class IstreamCSVReader : public CSVReader 00084 { 00085 protected: 00086 virtual bool nextline(); 00087 00088 public: 00089 std::istream& in; 00090 00091 IstreamCSVReader(std::istream& in) : in(in) {} 00092 }; 00093 00094 // TODO: CSV readers allowing to peek on the next line without consuming it, to 00095 // allow the Msg parser to stop at msg boundary after peeking at a line 00096 // also, stripping newlines at end of lines 00097 // also, reading from istream 00098 // also, de-escaping strings (if they start with quote) 00099 00103 void csv_output_quoted_string(std::ostream& out, const std::string& str); 00104 00105 } 00106 00107 /* vim:set ts=4 sw=4: */ 00108 #endif