tools/ColTextParser.hpp
Go to the documentation of this file.
00001 /*
00002 
00003   This file is part of OpenFLUID software
00004   Copyright(c) 2007, INRA - Montpellier SupAgro
00005 
00006 
00007  == GNU General Public License Usage ==
00008 
00009   OpenFLUID is free software: you can redistribute it and/or modify
00010   it under the terms of the GNU General Public License as published by
00011   the Free Software Foundation, either version 3 of the License, or
00012   (at your option) any later version.
00013 
00014   OpenFLUID is distributed in the hope that it will be useful,
00015   but WITHOUT ANY WARRANTY; without even the implied warranty of
00016   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017   GNU General Public License for more details.
00018 
00019   You should have received a copy of the GNU General Public License
00020   along with OpenFLUID. If not, see <http://www.gnu.org/licenses/>.
00021 
00022 
00023  == Other Usage ==
00024 
00025   Other Usage means a use of OpenFLUID that is inconsistent with the GPL
00026   license, and requires a written agreement between You and INRA.
00027   Licensees for Other Usage of OpenFLUID may use this file in accordance
00028   with the terms contained in the written agreement between You and INRA.
00029   
00030 */
00031 
00032 
00033 
00034 /**
00035   @file
00036 
00037   @author Jean-Christophe FABRE <fabrejc@supagro.inra.fr>
00038 */
00039 
00040 #ifndef __COLFILEPARSER_HPP__
00041 #define __COLFILEPARSER_HPP__
00042 
00043 
00044 #include <vector>
00045 #include <string>
00046 
00047 #include <openfluid/dllexport.hpp>
00048 
00049 namespace openfluid { namespace tools {
00050 
00051 
00052 /**
00053   Class for column file management and handling
00054 */
00055 class DLLEXPORT ColumnTextParser
00056 {
00057 
00058   protected:
00059 
00060   private:
00061 
00062     std::string m_Delimiter;
00063     std::string m_CommentSymbol;
00064 
00065     unsigned int m_LinesCount;
00066     unsigned int m_ColsCount;
00067 
00068     std::vector<std::vector<std::string> > m_Contents;
00069 
00070     std::vector<std::string> tokenizeLine(const std::string& Line);
00071 
00072     bool checkContents();
00073 
00074     bool isCommentLineStr(const std::string& LineStr);
00075 
00076     bool isEmptyLineStr(const std::string& LineStr);
00077 
00078 
00079   public:
00080 
00081     /**
00082       Constructor
00083     */
00084     ColumnTextParser(const std::string& CommentLineSymbol = "", const std::string& Delimiter = " \t\r\n");
00085 
00086     /**
00087       Destructor
00088     */
00089     ~ColumnTextParser();
00090 
00091 
00092     /**
00093       Loads a column text file
00094       @param[in] Filename the full path of the file to load
00095       @return true if everything went fine
00096     */
00097     bool loadFromFile(const std::string& Filename);
00098 
00099     /**
00100       Parses a one-line delimiter-separated string to set contents
00101       ex: "147.2 18 15 25 36 51.3 25.1 15", for 4 columns
00102       @param[in] Contents the string to parse
00103       @param[in] ColumnsNbr the number of columns (for splitting the sting into lines)
00104       @return true if everything went fine
00105     */
00106     bool setFromString(const std::string& Contents, unsigned int ColumnsNbr);
00107 
00108     /**
00109       Returns the value at a specified row-column, as a string
00110       @param[in] Line the line number of the value (first line is 0)
00111       @param[in] Column the column number of the value (first column is 0)
00112       @return the requested value
00113     */
00114     std::string getValue(unsigned int Line, unsigned int Column);
00115 
00116     /**
00117       Gets the value at a specified row-column, as a string
00118       @param[in] Line the line number of the value (first line is 0)
00119       @param[in] Column the column number of the value (first column is 0)
00120       @param[out] Value the requested value if exists
00121       @return true if the value has been found, false otherwise
00122     */
00123     bool getStringValue(unsigned int Line, unsigned int Column, std::string *Value);
00124 
00125     /**
00126       Gets the value at a specified row-column, as a long int
00127       @param[in] Line the line number of the value (first line is 0)
00128       @param[in] Column the column number of the value (first column is 0)
00129       @param[out] Value the requested value if exists
00130       @return true if the value has been found, false otherwise
00131     */
00132     bool getLongValue(unsigned int Line, unsigned int Column, long* Value);
00133 
00134 
00135     /**
00136       Gets the value at a specified row-column, as a double precision float
00137       @param[in] Line the line number of the value (first line is 0)
00138       @param[in] Column the column number of the value (first column is 0)
00139       @param[out] Value the requested value if exists
00140       @return true if the value has been found, false otherwise
00141     */
00142     bool getDoubleValue(unsigned int Line, unsigned int Column, double* Value);
00143 
00144     /**
00145       Returns the values at a specified line
00146       @param[in] Line the line number of the value (first line is 0)
00147       @return the requested values
00148     */
00149     std::vector<std::string> getValues(unsigned int Line);
00150 
00151     /**
00152       Returns the number of lines
00153       @return the number of lines
00154     */
00155     inline unsigned int getLinesCount() const { return m_LinesCount;};
00156 
00157     /**
00158       Returns the number of columns
00159       @return the number of columns
00160     */
00161     inline unsigned int getColsCount() const { return m_ColsCount;};
00162 
00163     void streamContents(std::ostream& OStream);
00164 
00165 };
00166 
00167 } }
00168 
00169 
00170 #endif
00171 
00172 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines