/usr/include/dballe/core/csv.h is in libdballe-dev 7.21-1build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 | /*
* dballe/csv - CSV utility functions
*
* Copyright (C) 2005--2014 ARPA-SIM <urpsim@smr.arpa.emr.it>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* Author: Enrico Zini <enrico@enricozini.com>
*/
#ifndef DBA_CSV_H
#define DBA_CSV_H
/** @file
* @ingroup core
* Routines to parse data in CSV format
*/
#include <wreport/var.h>
#include <vector>
#include <string>
#include <iosfwd>
#include <stdio.h>
namespace dballe
{
/**
* Parse a CSV line.
*
* @param in
* The file where to read from
* @param cols
* The parsed columns.
* @return
* true if a new line was found, else false
*/
bool csv_read_next(FILE* in, std::vector<std::string>& cols);
class CSVReader
{
protected:
std::istream* in;
int next_char();
public:
/**
* If true, the input stream will be deleted upon destruction.
* If false, it will be left alone.
*/
bool close_on_exit;
/// Last line read
std::string line;
/// Parsed CSV columns for the last line read
std::vector<std::string> cols;
CSVReader();
CSVReader(std::istream& in);
CSVReader(const std::string& pathname);
~CSVReader();
/**
* Open the given file and sets close_on_exit to true
*/
void open(const std::string& pathname);
/**
* Sets in to 0.
* If close_on_exit is true, close the currently opened file.
*/
void close();
/**
* Return the given column, as an integer.
*
* A missing value raises an exception.
*/
int as_int(unsigned col) const;
/**
* Return the given column, as an integer.
*
* A missing value is returned as MISSING_INT.
*/
int as_int_withmissing(unsigned col) const;
/**
* Return the given column, as a Varcode.
*
* A missing value raises an exception.
*/
wreport::Varcode as_varcode(unsigned col) const;
/**
* Find the first line where the given column exists and starts with a
* number.
*
* This can be used to skip titles and empty lines, moving to the start of
* the real data. Real data is identified by using a column that starts
* with text in the headers and number in the data.
*
* @returns true if a data line has been found, false if we reached EOF
*/
bool move_to_data(unsigned number_col=0);
/// Read the next CSV line, returning false if EOF is reached
bool next();
static std::string unescape(const std::string& csvstr);
};
// TODO: CSV readers allowing to peek on the next line without consuming it, to
// allow the Msg parser to stop at msg boundary after peeking at a line
// also, stripping newlines at end of lines
// also, reading from istream
// also, de-escaping strings (if they start with quote)
/**
* Output a string value, quoted if needed according to CSV rules
*/
void csv_output_quoted_string(std::ostream& out, const std::string& str);
/**
* Output a string value, quoted if needed according to CSV rules
*/
void csv_output_quoted_string(FILE* out, const std::string& str);
class CSVWriter
{
protected:
std::string row;
public:
virtual ~CSVWriter();
/// Add an empty value to the current row
void add_value_empty();
/// Add a value to the current row, without any escaping
void add_value_raw(const char* str);
/// Add a value to the current row, without any escaping
void add_value_raw(const std::string& str);
/// Add an int value to the current row
void add_value(int val);
/// Add an int value that can potentially be missing
void add_value_withmissing(int val);
/// Add an int value to the current row
void add_value(unsigned val);
/// Add an int value to the current row
void add_value(uint64_t val);
/// Add a formatted varcode to the current row
void add_value(wreport::Varcode val);
/// Add a variable value, in its raw integer form
void add_var_value_raw(const wreport::Var& val);
/// Add a variable value, formatted
void add_var_value_formatted(const wreport::Var& val);
/// Add a string to the current row
void add_value(const char* val);
/// Add a string to the current row
void add_value(const std::string& val);
/// Write the current line to the output file, and start a new one
virtual void flush_row() = 0;
};
}
#endif
|