GammaLib 2.0.0
Loading...
Searching...
No Matches
GCsv.hpp
Go to the documentation of this file.
1/***************************************************************************
2 * GCsv.hpp - Comma-separated values table class *
3 * ----------------------------------------------------------------------- *
4 * copyright (C) 2010-2016 by Juergen Knoedlseder *
5 * ----------------------------------------------------------------------- *
6 * *
7 * This program is free software: you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation, either version 3 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU General Public License *
18 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
19 * *
20 ***************************************************************************/
21/**
22 * @file GCsv.hpp
23 * @brief Comma-separated values table class definition
24 * @author Juergen Knoedlseder
25 */
26
27#ifndef GCSV_HPP
28#define GCSV_HPP
29
30/* __ Includes ___________________________________________________________ */
31#include <vector>
32#include <string>
33#include "GBase.hpp"
34
35/* __ Forward declarations _______________________________________________ */
36class GFilename;
37
38
39/***********************************************************************//**
40 * @class GCsv
41 *
42 * @brief Comma-separated values table class
43 *
44 * This class implements a table of std::string elements that is loaded
45 * from a comma-separated value ASCII file. The comma-separation string
46 * can be specified upon loading of the file (by default the class assumes
47 * that elements are separated by a white space).
48 *
49 * The class provides operators for string element access, and methods for
50 * conversion of the string values:
51 *
52 * double real = csv.real(row,col);
53 * int integer = csv.integer(row,col);
54 * std::string string = csv.string(row,col);
55 *
56 ***************************************************************************/
57class GCsv : public GBase {
58
59public:
60 // Constructors and destructors
61 GCsv(void);
62 GCsv(const int& nrows, const int& ncols);
63 GCsv(const GFilename& filename, const std::string& sep = " ");
64 GCsv(const GCsv& csv);
65 virtual ~GCsv(void);
66
67 // Operators
68 GCsv& operator=(const GCsv& csv);
69 std::string& operator()(const int& row, const int& col);
70 const std::string& operator()(const int& row, const int& col) const;
71
72 // Methods
73 void clear(void);
74 GCsv* clone(void) const;
75 std::string classname(void) const;
76 int size(void) const;
77 const int& ncols(void) const;
78 const int& nrows(void) const;
79 const int& precision(void) const;
80 void precision(const int& precision);
81 void append(const std::vector<std::string>& list);
82 std::string string(const int& row, const int& col) const;
83 double real(const int& row, const int& col) const;
84 int integer(const int& row, const int& col) const;
85 void string(const int& row, const int& col, const std::string& value);
86 void real(const int& row, const int& col, const double& value);
87 void integer(const int& row, const int& col, const int& value);
88 void load(const GFilename& filename, const std::string& sep = " ");
89 void save(const GFilename& filename, const std::string& sep = " ",
90 const bool& clobber = false) const;
91 std::string print(const GChatter& chatter = NORMAL) const;
92
93protected:
94 // Protected methods
95 void init_members(void);
96 void copy_members(const GCsv& csv);
97 void free_members(void);
98
99 // Protected data members
100 int m_cols; //!< Number of columns
101 int m_rows; //!< Number of rows
102 std::vector<std::vector<std::string> > m_data; //!< CSV table data
103 int m_precision; //!< Precision for floats
104};
105
106
107/***********************************************************************//**
108 * @brief Return class name
109 *
110 * @return String containing the class name ("GCsv").
111 ***************************************************************************/
112inline
113std::string GCsv::classname(void) const
114{
115 return ("GCsv");
116}
117
118
119/***********************************************************************//**
120 * @brief Return table size (columns times rows)
121 *
122 * @return Table size.
123 ***************************************************************************/
124inline
125int GCsv::size(void) const
126{
127 return m_rows*m_cols;
128}
129
130
131/***********************************************************************//**
132 * @brief Return number of columns
133 *
134 * @return Number of columns.
135 ***************************************************************************/
136inline
137const int& GCsv::ncols(void) const
138{
139 return m_cols;
140}
141
142
143/***********************************************************************//**
144 * @brief Return number of rows
145 *
146 * @return Number of rows.
147 ***************************************************************************/
148inline
149const int& GCsv::nrows(void) const
150{
151 return m_rows;
152}
153
154
155/***********************************************************************//**
156 * @brief Return fixed field floating point precision
157 *
158 * @return Fixed field floating point precision.
159 *
160 * Returns the precision for floating point values when setting values using
161 * the real() method. Any value >0 indicates the number of decimal places
162 * that the floating point value will have.
163 ***************************************************************************/
164inline
165const int& GCsv::precision(void) const
166{
167 return m_precision;
168}
169
170
171/***********************************************************************//**
172 * @brief Set fixed field floating point precision
173 *
174 * @param[in] precision Fixed field floating point precision.
175 *
176 * Set the precision for floating point values when setting values using
177 * the real() method. Any value >0 indicates the number of decimal places
178 * that the floating point value will have.
179 ***************************************************************************/
180inline
181void GCsv::precision(const int& precision)
182{
184 return;
185}
186
187#endif /* GCSV_HPP */
Definition of interface for all GammaLib classes.
GChatter
Definition GTypemaps.hpp:33
@ NORMAL
Definition GTypemaps.hpp:36
Interface class for all GammaLib classes.
Definition GBase.hpp:52
Comma-separated values table class.
Definition GCsv.hpp:57
void copy_members(const GCsv &csv)
Copy class members.
Definition GCsv.cpp:627
std::string string(const int &row, const int &col) const
Get string value.
Definition GCsv.cpp:309
const int & nrows(void) const
Return number of rows.
Definition GCsv.hpp:149
std::vector< std::vector< std::string > > m_data
CSV table data.
Definition GCsv.hpp:102
virtual ~GCsv(void)
Destructor.
Definition GCsv.cpp:135
void load(const GFilename &filename, const std::string &sep=" ")
Load CSV table.
Definition GCsv.cpp:423
void init_members(void)
Initialise class members.
Definition GCsv.cpp:609
int integer(const int &row, const int &col) const
Get integer value.
Definition GCsv.cpp:342
std::string print(const GChatter &chatter=NORMAL) const
Print column separated values information.
Definition GCsv.cpp:569
void clear(void)
Clear CSV table.
Definition GCsv.cpp:235
int m_rows
Number of rows.
Definition GCsv.hpp:101
double real(const int &row, const int &col) const
Get double precision value.
Definition GCsv.cpp:324
void free_members(void)
Delete class members.
Definition GCsv.cpp:643
int m_precision
Precision for floats.
Definition GCsv.hpp:103
void save(const GFilename &filename, const std::string &sep=" ", const bool &clobber=false) const
Save CSV table.
Definition GCsv.cpp:519
std::string classname(void) const
Return class name.
Definition GCsv.hpp:113
std::string & operator()(const int &row, const int &col)
Table element access operator.
Definition GCsv.cpp:184
int m_cols
Number of columns.
Definition GCsv.hpp:100
void append(const std::vector< std::string > &list)
Append list of strings.
Definition GCsv.cpp:273
int size(void) const
Return table size (columns times rows)
Definition GCsv.hpp:125
GCsv * clone(void) const
Clone CSV table.
Definition GCsv.cpp:253
const int & ncols(void) const
Return number of columns.
Definition GCsv.hpp:137
const int & precision(void) const
Return fixed field floating point precision.
Definition GCsv.hpp:165
GCsv & operator=(const GCsv &csv)
Assignment operator.
Definition GCsv.cpp:157
GCsv(void)
Void constructor.
Definition GCsv.cpp:59
Filename class.
Definition GFilename.hpp:62