Documentation for OpenFLUID 2.2.0
ColumnTextParser.hpp
Go to the documentation of this file.
1 /*
2 
3  This file is part of OpenFLUID software
4  Copyright(c) 2007, INRA - Montpellier SupAgro
5 
6 
7  == GNU General Public License Usage ==
8 
9  OpenFLUID is free software: you can redistribute it and/or modify
10  it under the terms of the GNU General Public License as published by
11  the Free Software Foundation, either version 3 of the License, or
12  (at your option) any later version.
13 
14  OpenFLUID is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with OpenFLUID. If not, see <http://www.gnu.org/licenses/>.
21 
22 
23  == Other Usage ==
24 
25  Other Usage means a use of OpenFLUID that is inconsistent with the GPL
26  license, and requires a written agreement between You and INRA.
27  Licensees for Other Usage of OpenFLUID may use this file in accordance
28  with the terms contained in the written agreement between You and INRA.
29 
30 */
31 
32 
33 /**
34  @file ColumnTextParser.hpp
35 
36  @author Jean-Christophe FABRE <jean-christophe.fabre@inra.fr>
37 */
38 
39 
40 #ifndef __OPENFLUID_TOOLS_COLUMNTEXTPARSER_HPP__
41 #define __OPENFLUID_TOOLS_COLUMNTEXTPARSER_HPP__
42 
43 
44 #include <vector>
45 #include <string>
46 
47 #include <openfluid/dllexport.hpp>
48 
49 
50 namespace openfluid { namespace tools {
51 
52 
53 /**
54  Class for column file management and handling
55 */
57 {
58 
59  protected:
60 
61  private:
62 
63  std::string m_Delimiter;
64 
65  std::string m_CommentSymbol;
66 
67  unsigned int m_LinesCount;
68 
69  unsigned int m_ColsCount;
70 
71  std::vector<std::vector<std::string> > m_Contents;
72 
73  std::vector<std::string> tokenizeLine(const std::string& Line);
74 
75  std::vector<std::string> tokenizeString(const std::string& String);
76 
77  bool checkContents();
78 
79  bool isCommentLineStr(const std::string& LineStr);
80 
81  bool isEmptyLineStr(const std::string& LineStr);
82 
83 
84  public:
85 
86  /**
87  Constructor
88  */
89  ColumnTextParser(const std::string& CommentLineSymbol = "", const std::string& Delimiter = " \t\r\n");
90 
91  /**
92  Destructor
93  */
95 
96 
97  /**
98  Loads a column text file
99  @param[in] Filename the full path of the file to load
100  @return true if everything went fine
101  */
102  bool loadFromFile(const std::string& Filename);
103 
104  /**
105  Parses a one-line delimiter-separated string to set contents
106  ex: "147.2 18 15 25 36 51.3 25.1 15", for 4 columns
107  @param[in] Contents the string to parse
108  @param[in] ColumnsNbr the number of columns (for splitting the sting into lines)
109  @return true if everything went fine
110  */
111  bool setFromString(const std::string& Contents, unsigned int ColumnsNbr);
112 
113  /**
114  Returns the value at a specified row-column, as a string
115  @param[in] Line the line number of the value (first line is 0)
116  @param[in] Column the column number of the value (first column is 0)
117  @return the requested value
118  */
119  std::string getValue(unsigned int Line, unsigned int Column) const;
120 
121  /**
122  Gets the value at a specified row-column, as a string
123  @param[in] Line the line number of the value (first line is 0)
124  @param[in] Column the column number of the value (first column is 0)
125  @param[out] Value the requested value if exists
126  @return true if the value has been found, false otherwise
127  */
128  bool getStringValue(unsigned int Line, unsigned int Column, std::string* Value) const;
129 
130  /**
131  Gets the value at a specified row-column, as a long int
132  @param[in] Line the line number of the value (first line is 0)
133  @param[in] Column the column number of the value (first column is 0)
134  @param[out] Value the requested value if exists
135  @return true if the value has been found, false otherwise
136  */
137  bool getLongValue(unsigned int Line, unsigned int Column, long* Value) const;
138 
139 
140  /**
141  Gets the value at a specified row-column, as a double precision float
142  @param[in] Line the line number of the value (first line is 0)
143  @param[in] Column the column number of the value (first column is 0)
144  @param[out] Value the requested value if exists
145  @return true if the value has been found, false otherwise
146  */
147  bool getDoubleValue(unsigned int Line, unsigned int Column, double* Value) const;
148 
149  /**
150  Returns the values at a specified line
151  @param[in] Line the line number of the value (first line is 0)
152  @return the requested values
153  */
154  std::vector<std::string> getValues(unsigned int Line) const;
155 
156  /**
157  Returns the number of lines
158  @return the number of lines
159  */
160  inline unsigned int getLinesCount() const { return m_LinesCount;};
161 
162  /**
163  Returns the number of columns
164  @return the number of columns
165  */
166  inline unsigned int getColsCount() const { return m_ColsCount;};
167 
168  void streamContents(std::ostream& OStream) const;
169 
170 };
171 
172 
173 } }
174 
175 
176 #endif /* __OPENFLUID_TOOLS_COLUMNTEXTPARSER_HPP__ */
177 
Definition: ColumnTextParser.hpp:57
bool loadFromFile(const std::string &Filename)
bool getStringValue(unsigned int Line, unsigned int Column, std::string *Value) const
bool setFromString(const std::string &Contents, unsigned int ColumnsNbr)
unsigned int getLinesCount() const
Definition: ColumnTextParser.hpp:160
std::vector< std::string > getValues(unsigned int Line) const
bool getDoubleValue(unsigned int Line, unsigned int Column, double *Value) const
bool getLongValue(unsigned int Line, unsigned int Column, long *Value) const
void streamContents(std::ostream &OStream) const
std::string getValue(unsigned int Line, unsigned int Column) const
ColumnTextParser(const std::string &CommentLineSymbol="", const std::string &Delimiter=" \t\r\n")
unsigned int getColsCount() const
Definition: ColumnTextParser.hpp:166
#define OPENFLUID_API
Definition: dllexport.hpp:86
void OPENFLUID_API tokenizeString(const std::string &StrToTokenize, std::vector< std::string > &Tokens, const std::string &Delimiters)
Definition: ApplicationException.hpp:47