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 /**
35  @file ColumnTextParser.hpp
36 
37  @author Jean-Christophe FABRE <jean-christophe.fabre@supagro.inra.fr>
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 namespace openfluid { namespace tools {
50 
51 
52 /**
53  Class for column file management and handling
54 */
56 {
57 
58  protected:
59 
60  private:
61 
62  std::string m_Delimiter;
63  std::string m_CommentSymbol;
64 
65  unsigned int m_LinesCount;
66  unsigned int m_ColsCount;
67 
68  std::vector<std::vector<std::string> > m_Contents;
69 
70  std::vector<std::string> tokenizeLine(const std::string& Line);
71 
72  std::vector<std::string> tokenizeString(const std::string& String);
73 
74  bool checkContents();
75 
76  bool isCommentLineStr(const std::string& LineStr);
77 
78  bool isEmptyLineStr(const std::string& LineStr);
79 
80 
81  public:
82 
83  /**
84  Constructor
85  */
86  ColumnTextParser(const std::string& CommentLineSymbol = "", const std::string& Delimiter = " \t\r\n");
87 
88  /**
89  Destructor
90  */
92 
93 
94  /**
95  Loads a column text file
96  @param[in] Filename the full path of the file to load
97  @return true if everything went fine
98  */
99  bool loadFromFile(const std::string& Filename);
100 
101  /**
102  Parses a one-line delimiter-separated string to set contents
103  ex: "147.2 18 15 25 36 51.3 25.1 15", for 4 columns
104  @param[in] Contents the string to parse
105  @param[in] ColumnsNbr the number of columns (for splitting the sting into lines)
106  @return true if everything went fine
107  */
108  bool setFromString(const std::string& Contents, unsigned int ColumnsNbr);
109 
110  /**
111  Returns the value at a specified row-column, as a string
112  @param[in] Line the line number of the value (first line is 0)
113  @param[in] Column the column number of the value (first column is 0)
114  @return the requested value
115  */
116  std::string getValue(unsigned int Line, unsigned int Column) const;
117 
118  /**
119  Gets the value at a specified row-column, as a string
120  @param[in] Line the line number of the value (first line is 0)
121  @param[in] Column the column number of the value (first column is 0)
122  @param[out] Value the requested value if exists
123  @return true if the value has been found, false otherwise
124  */
125  bool getStringValue(unsigned int Line, unsigned int Column, std::string *Value) const;
126 
127  /**
128  Gets the value at a specified row-column, as a long int
129  @param[in] Line the line number of the value (first line is 0)
130  @param[in] Column the column number of the value (first column is 0)
131  @param[out] Value the requested value if exists
132  @return true if the value has been found, false otherwise
133  */
134  bool getLongValue(unsigned int Line, unsigned int Column, long* Value) const;
135 
136 
137  /**
138  Gets the value at a specified row-column, as a double precision float
139  @param[in] Line the line number of the value (first line is 0)
140  @param[in] Column the column number of the value (first column is 0)
141  @param[out] Value the requested value if exists
142  @return true if the value has been found, false otherwise
143  */
144  bool getDoubleValue(unsigned int Line, unsigned int Column, double* Value) const;
145 
146  /**
147  Returns the values at a specified line
148  @param[in] Line the line number of the value (first line is 0)
149  @return the requested values
150  */
151  std::vector<std::string> getValues(unsigned int Line) const;
152 
153  /**
154  Returns the number of lines
155  @return the number of lines
156  */
157  inline unsigned int getLinesCount() const { return m_LinesCount;};
158 
159  /**
160  Returns the number of columns
161  @return the number of columns
162  */
163  inline unsigned int getColsCount() const { return m_ColsCount;};
164 
165  void streamContents(std::ostream& OStream) const;
166 
167 };
168 
169 } }
170 
171 
172 #endif
173 
174 
Definition: ApplicationException.hpp:47
unsigned int getColsCount() const
Definition: ColumnTextParser.hpp:163
#define OPENFLUID_API
Definition: dllexport.hpp:87
unsigned int getLinesCount() const
Definition: ColumnTextParser.hpp:157
void OPENFLUID_API tokenizeString(const std::string &StrToTokenize, std::vector< std::string > &Tokens, const std::string &Delimiters)
Definition: ColumnTextParser.hpp:55