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