Tulip  4.7.0
Better Visualization Through Research
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties Groups Pages
CSVParser.h
1 /*
2  *
3  * This file is part of Tulip (www.tulip-software.org)
4  *
5  * Authors: David Auber and the Tulip development Team
6  * from LaBRI, University of Bordeaux
7  *
8  * Tulip is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation, either version 3
11  * of the License, or (at your option) any later version.
12  *
13  * Tulip is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU General Public License for more details.
17  *
18  */
19 ///@cond DOXYGEN_HIDDEN
20 
21 #ifndef CSVDATALOADER_H_
22 #define CSVDATALOADER_H_
23 
24 #include <tulip/tulipconf.h>
25 #include <tulip/CSVContentHandler.h>
26 
27 #include <vector>
28 #include <climits>
29 
30 #include <QString>
31 
32 class QTextCodec;
33 
34 namespace tlp {
35 
36 class PluginProgress;
37 
38 /*
39 * @brief Interface for CSV data parser.
40 *
41 * Send the found tokens to the CSVContentHandler interface.
42 */
43 class TLP_QT_SCOPE CSVParser {
44 public:
45  virtual ~CSVParser() {}
46 
47  /**
48  * @brief Parse the data and send the tokens found to the CSVContentHandler.
49  *
50  * Notify the progression of the parsing with the progress object.
51  **/
52  virtual bool parse(CSVContentHandler * handler,tlp::PluginProgress* progress=NULL) = 0;
53 };
54 
55 /**
56 * @brief Parse a csv data and send each tokens to the given CSVContentHandler object.
57 *
58 * 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.
59 * Send the found tokens to the CSVContentHandler interface.
60 * \code
61 * CSVParser parser(fileName,";","\"","UTF-8",true);
62 * \/\/Automatically remove quotes.
63 * CSVContentHandler * handler ;
64 * parser.parse(handler);
65 * \endcode
66 **/
67 class TLP_QT_SCOPE CSVSimpleParser : public CSVParser {
68 public:
69  /**
70  * @brief Construct a csv simple file parser.
71  * @param filename The path to the file to import.
72  * @param separator The separator to use.
73  * @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.
74  * @param firstLine The number of the first line to read. The first line is 0.
75  * @param lastLine The number of the last line to read.
76  **/
77  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);
78 
79  virtual ~CSVSimpleParser();
80 
81  inline std::string fileName()const {
82  return _fileName;
83  }
84  inline void setFileName(const std::string& fileName) {
85  _fileName = fileName;
86  }
87 
88  inline char textDelimiter()const {
89  return _textDelimiter;
90  }
91 
92  inline void setTextDelimiter(char delimiter) {
93  _textDelimiter = delimiter;
94  }
95 
96  inline std::string fileEncoding()const {
97  return _fileEncoding;
98  }
99 
100  inline void setFileEncoding(const std::string& fileEncoding) {
101  _fileEncoding = fileEncoding;
102  }
103 
104  bool parse(CSVContentHandler * handler,tlp::PluginProgress* progress=NULL);
105 
106 protected:
107  virtual std::string treatToken(const std::string& token, int row, int column);
108 
109 private:
110  void tokenize(const std::string& str, std::vector<std::string>& tokens,
111  const QString &delimiters, const bool mergedelim, char _textDelimiter, unsigned int numberOfCol);
112  std::string convertStringEncoding(const std::string& toConvert,QTextCodec* encoder);
113 
114  /**
115  * @brief Function to extract a line from a istream. Can handle Linux,Mac and Windows end of line pattern.
116  **/
117  bool multiplatformgetline ( std::istream& is, std::string& str );
118 
119  std::string removeQuotesIfAny(const std::string &s,const std::string& rejectedChars);
120  std::string _fileName;
121  QString _separator;
122  char _textDelimiter;
123  std::string _fileEncoding;
124  unsigned int _firstLine;
125  unsigned int _lastLine;
126  bool _mergesep;
127 
128 };
129 
130 /**
131  *@brief CSV parser used to invert the token matrix in order to treat rows as columns.
132  **/
133 class TLP_QT_SCOPE CSVInvertMatrixParser : public tlp::CSVParser , public tlp::CSVContentHandler {
134 public:
135  CSVInvertMatrixParser(CSVParser* parser);
136  virtual ~CSVInvertMatrixParser();
137 
138  bool parse(CSVContentHandler *handler, tlp::PluginProgress *progress=NULL);
139 
140  void begin();
141  void line(unsigned int row,const std::vector<std::string>& lineTokens);
142  void end(unsigned int rowNumber, unsigned int columnNumber);
143 private:
144  CSVParser *parser;
145  CSVContentHandler *handler;
146  std::vector<std::vector<std::string> > columns;
147  unsigned int maxLineSize;
148 };
149 }
150 #endif /* CSVDATALOADER_H_ */
151 ///@endcond
PluginProcess subclasses are meant to notify about the progress state of some process (typically a pl...