Tulip  4.9.0
Better Visualization Through Research
 All Classes 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  char decimalMark() const {
48  return '.';
49  }
50  /**
51  * @brief Parse the data and send the tokens found to the CSVContentHandler.
52  *
53  * Notify the progression of the parsing with the progress object.
54  **/
55  virtual bool parse(CSVContentHandler * handler,tlp::PluginProgress* progress=NULL) = 0;
56 };
57 
58 /**
59 * @brief Parse a csv data and send each tokens to the given CSVContentHandler object.
60 *
61 * 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.
62 * Send the found tokens to the CSVContentHandler interface.
63 * \code
64 * CSVParser parser(fileName,";","\"","UTF-8",true);
65 * \/\/Automatically remove quotes.
66 * CSVContentHandler * handler ;
67 * parser.parse(handler);
68 * \endcode
69 **/
70 class TLP_QT_SCOPE CSVSimpleParser : public CSVParser {
71 public:
72  /**
73  * @brief Construct a csv simple file parser.
74  * @param filename The path to the file to import.
75  * @param separator The separator to use.
76  * @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.
77  * @param firstLine The number of the first line to read. The first line is 0.
78  * @param lastLine The number of the last line to read.
79  **/
80  CSVSimpleParser(const std::string& fileName,const QString &separator=";", const bool mergesep=false, char textDelimiter='"', char delimiterMark='.',const std::string& fileEncoding=std::string("UTF-8"),unsigned int firstLine = 0,unsigned int lastLine = UINT_MAX);
81 
82  virtual ~CSVSimpleParser();
83 
84  inline std::string fileName()const {
85  return _fileName;
86  }
87  inline void setFileName(const std::string& fileName) {
88  _fileName = fileName;
89  }
90 
91  inline char textDelimiter()const {
92  return _textDelimiter;
93  }
94 
95  char decimalMark()const {
96  return _decimalMark;
97  }
98 
99  inline void setTextDelimiter(char delimiter) {
100  _textDelimiter = delimiter;
101  }
102 
103  inline std::string fileEncoding()const {
104  return _fileEncoding;
105  }
106 
107  inline void setFileEncoding(const std::string& fileEncoding) {
108  _fileEncoding = fileEncoding;
109  }
110 
111  bool parse(CSVContentHandler * handler,tlp::PluginProgress* progress=NULL);
112 
113 protected:
114  virtual std::string treatToken(const std::string& token, int row, int column);
115 
116 private:
117  void tokenize(const std::string& str, std::vector<std::string>& tokens,
118  const QString &delimiters, const bool mergedelim, char _textDelimiter, unsigned int numberOfCol);
119  std::string convertStringEncoding(const std::string& toConvert,QTextCodec* encoder);
120 
121  /**
122  * @brief Function to extract a line from a istream. Can handle Linux,Mac and Windows end of line pattern.
123  **/
124  bool multiplatformgetline ( std::istream& is, std::string& str );
125 
126  std::string removeQuotesIfAny(const std::string &s,const std::string& rejectedChars);
127  std::string _fileName;
128  QString _separator;
129  char _textDelimiter;
130  char _decimalMark;
131  std::string _fileEncoding;
132  unsigned int _firstLine;
133  unsigned int _lastLine;
134  bool _mergesep;
135 
136 };
137 
138 /**
139  *@brief CSV parser used to invert the token matrix in order to treat rows as columns.
140  **/
141 class TLP_QT_SCOPE CSVInvertMatrixParser : public tlp::CSVParser , public tlp::CSVContentHandler {
142 public:
143  CSVInvertMatrixParser(CSVParser* parser);
144  virtual ~CSVInvertMatrixParser();
145 
146  char decimalMark()const {
147  return parser->decimalMark();
148  }
149 
150  bool parse(CSVContentHandler *handler, tlp::PluginProgress *progress=NULL);
151 
152  bool begin();
153  bool line(unsigned int row,const std::vector<std::string>& lineTokens);
154  bool end(unsigned int rowNumber, unsigned int columnNumber);
155 private:
156  CSVParser *parser;
157  CSVContentHandler *handler;
158  std::vector<std::vector<std::string> > columns;
159  unsigned int maxLineSize;
160 };
161 }
162 #endif /* CSVDATALOADER_H_ */
163 ///@endcond
PluginProcess subclasses are meant to notify about the progress state of some process (typically a pl...