Tulip  4.6.0
Better Visualization Through Research
library/tulip-gui/include/tulip/CSVParser.h
00001 /*
00002  *
00003  * This file is part of Tulip (www.tulip-software.org)
00004  *
00005  * Authors: David Auber and the Tulip development Team
00006  * from LaBRI, University of Bordeaux
00007  *
00008  * Tulip is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU Lesser General Public License
00010  * as published by the Free Software Foundation, either version 3
00011  * of the License, or (at your option) any later version.
00012  *
00013  * Tulip is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00016  * See the GNU General Public License for more details.
00017  *
00018  */
00019 ///@cond DOXYGEN_HIDDEN
00020 
00021 #ifndef CSVDATALOADER_H_
00022 #define CSVDATALOADER_H_
00023 
00024 #include <tulip/tulipconf.h>
00025 #include <tulip/CSVContentHandler.h>
00026 
00027 #include <vector>
00028 #include <climits>
00029 
00030 #include <QString>
00031 
00032 class QTextCodec;
00033 
00034 namespace tlp {
00035 
00036 class PluginProgress;
00037 
00038 /*
00039 * @brief Interface for CSV data parser.
00040 *
00041 * Send the found tokens to the CSVContentHandler interface.
00042 */
00043 class TLP_QT_SCOPE CSVParser {
00044 public:
00045   virtual ~CSVParser() {}
00046 
00047   /**
00048     * @brief Parse the data and send the tokens found to the CSVContentHandler.
00049     *
00050     * Notify the progression of the parsing with the progress object.
00051     **/
00052   virtual bool parse(CSVContentHandler * handler,tlp::PluginProgress* progress=NULL) = 0;
00053 };
00054 
00055 /**
00056 * @brief Parse a csv data and send each tokens to the given CSVContentHandler object.
00057 *
00058 * Parse a csv data and send each tokens to the given CSVContentHandler object. Get each line of the file in the given range and parse them. This object skip empty lines.
00059 * Send the found tokens to the CSVContentHandler interface.
00060 * \code
00061 * CSVParser parser(fileName,";","\"","UTF-8",true);
00062 * \/\/Automatically remove quotes.
00063 * CSVContentHandler * handler ;
00064 * parser.parse(handler);
00065 * \endcode
00066 **/
00067 class TLP_QT_SCOPE CSVSimpleParser : public CSVParser {
00068 public:
00069   /**
00070     * @brief Construct a csv simple file parser.
00071     * @param filename The path to the file to import.
00072     * @param separator The separator to use.
00073     * @param textDelimiter If a token is sourrounded by this charater we ignore all the separators found in this token. Useful if a token contains the separator.
00074     * @param firstLine The number of the first line to read. The first line is 0.
00075     * @param lastLine The number of the last line to read.
00076     **/
00077   CSVSimpleParser(const std::string& fileName,const QString &separator=";", const bool mergesep=false, char textDelimiter='"',const std::string& fileEncoding=std::string("UTF-8"),unsigned int firstLine = 0,unsigned int lastLine = UINT_MAX);
00078 
00079   virtual ~CSVSimpleParser();
00080 
00081   inline std::string fileName()const {
00082     return _fileName;
00083   }
00084   inline void setFileName(const std::string& fileName) {
00085     _fileName = fileName;
00086   }
00087 
00088   inline char textDelimiter()const {
00089     return _textDelimiter;
00090   }
00091 
00092   inline void setTextDelimiter(char delimiter) {
00093     _textDelimiter = delimiter;
00094   }
00095 
00096   inline std::string fileEncoding()const {
00097     return _fileEncoding;
00098   }
00099 
00100   inline void setFileEncoding(const std::string& fileEncoding) {
00101     _fileEncoding = fileEncoding;
00102   }
00103 
00104   bool parse(CSVContentHandler * handler,tlp::PluginProgress* progress=NULL);
00105 
00106 protected:
00107   virtual std::string treatToken(const std::string& token, int row, int column);
00108 
00109 private:
00110   void tokenize(const std::string& str, std::vector<std::string>& tokens,
00111                 const QString &delimiters, const bool mergedelim, char _textDelimiter, unsigned int numberOfCol);
00112   std::string convertStringEncoding(const std::string& toConvert,QTextCodec* encoder);
00113 
00114   /**
00115     * @brief Function to extract a line from a istream. Can handle Linux,Mac and Windows end of line pattern.
00116     **/
00117   bool multiplatformgetline ( std::istream& is, std::string& str );
00118 
00119   std::string removeQuotesIfAny(const std::string &s,const std::string& rejectedChars);
00120   std::string _fileName;
00121   QString _separator;
00122   char _textDelimiter;
00123   std::string _fileEncoding;
00124   unsigned int _firstLine;
00125   unsigned int _lastLine;
00126   bool _mergesep;
00127 
00128 };
00129 
00130 /**
00131   *@brief CSV parser used to invert the token matrix in order to treat rows as columns.
00132   **/
00133 class TLP_QT_SCOPE CSVInvertMatrixParser : public tlp::CSVParser , public tlp::CSVContentHandler {
00134 public:
00135   CSVInvertMatrixParser(CSVParser* parser);
00136   virtual ~CSVInvertMatrixParser();
00137 
00138   bool parse(CSVContentHandler *handler, tlp::PluginProgress *progress=NULL);
00139 
00140   void begin();
00141   void line(unsigned int row,const std::vector<std::string>& lineTokens);
00142   void end(unsigned int rowNumber, unsigned int columnNumber);
00143 private:
00144   CSVParser *parser;
00145   CSVContentHandler *handler;
00146   std::vector<std::vector<std::string> > columns;
00147   unsigned int maxLineSize;
00148 };
00149 }
00150 #endif /* CSVDATALOADER_H_ */
00151 ///@endcond
 All Classes Files Functions Variables Enumerations Enumerator Properties