Tulip 6.1.0
Large graphs analysis and drawing
Loading...
Searching...
No Matches
CSVParser.h
1/*
2 *
3 * This file is part of Tulip (https://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
32namespace tlp {
33
34class PluginProgress;
35
36/*
37 * @brief Interface for CSV data parser.
38 *
39 * Send the found tokens to the CSVContentHandler interface.
40 */
41class TLP_QT_SCOPE CSVParser {
42public:
43 virtual ~CSVParser() {}
44
45 virtual char decimalMark() const {
46 return '.';
47 }
48 /**
49 * @brief Parse the data and send the tokens found to the CSVContentHandler.
50 *
51 * Notify the progression of the parsing with the progress object.
52 **/
53 virtual bool parse(CSVContentHandler *handler, tlp::PluginProgress *progress = nullptr,
54 bool firstLineOnly = false) = 0;
55};
56
57/**
58 * @brief Parse a csv data and send each tokens to the given CSVContentHandler object.
59 *
60 * Parse a csv data and send each tokens to the given CSVContentHandler object. Get each line of the
61 *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 **/
70class TLP_QT_SCOPE CSVSimpleParser : public CSVParser {
71public:
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 character we ignore all the separators
77 *found in this token. Useful if a token contains the separator.
78 * @param firstLine The number of the first line to read. The first line is 0.
79 * @param lastLine The number of the last line to read.
80 **/
81 CSVSimpleParser(const std::string &fileName, const QString &separator = ";",
82 bool mergesep = false, char textDelimiter = '"', char delimiterMark = '.',
83 bool considerAsString = false,
84 const std::string &fileEncoding = std::string("UTF-8"),
85 unsigned int firstLine = 0, unsigned int lastLine = UINT_MAX);
86
87 ~CSVSimpleParser() override;
88
89 inline std::string fileName() const {
90 return _fileName;
91 }
92 inline void setFileName(const std::string &fileName) {
93 _fileName = fileName;
94 }
95
96 inline char textDelimiter() const {
97 return _textDelimiter;
98 }
99
100 char decimalMark() const override {
101 return _decimalMark;
102 }
103
104 inline void setTextDelimiter(char delimiter) {
105 _textDelimiter = delimiter;
106 }
107
108 inline std::string fileEncoding() const {
109 return _fileEncoding;
110 }
111
112 inline void setFileEncoding(const std::string &fileEncoding) {
113 _fileEncoding = fileEncoding;
114 }
115
116 bool parse(CSVContentHandler *handler, tlp::PluginProgress *progress = nullptr,
117 bool firstLineOnly = false) override;
118
119protected:
120 virtual std::string treatToken(const std::string &token, int row, int column);
121
122private:
123 void tokenize(const std::string &str, std::vector<CSVToken> &tokens, const QString &delimiters,
124 const bool mergedelim, char _textDelimiter, unsigned int numberOfCol);
125 std::string convertStringEncoding(const std::string &toConvert, const std::string& encoder);
126
127 /**
128 * @brief Function to extract a line from a istream. Can handle Linux,Mac and Windows end of line
129 *pattern.
130 **/
131 bool multiplatformgetline(std::istream &is, std::string &str);
132
133 bool checkForContiguousTdlm(std::istream &is, std::string &str, char sep, bool tdlm);
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, _considerAsString;
143};
144
145/**
146 *@brief CSV parser used to invert the token matrix in order to treat rows as columns.
147 **/
148class TLP_QT_SCOPE CSVInvertMatrixParser : public tlp::CSVParser, public tlp::CSVContentHandler {
149public:
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<CSVToken> &lineTokens) override;
162 bool end(unsigned int rowNumber, unsigned int columnNumber) override;
163
164private:
165 CSVParser *parser;
166 CSVContentHandler *handler;
167 std::vector<std::vector<CSVToken>> 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...