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