Tulip  4.1.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 1 and Inria Bordeaux - Sud Ouest
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 
22 #ifndef CSVDATALOADER_H_
23 #define CSVDATALOADER_H_
24 #include <tulip/tulipconf.h>
25 #include <string>
26 #include <vector>
27 #include <climits>
28 
29 #include <QtCore/QTextCodec>
30 #include <QtCore/QString>
31 #include "tulip/CSVContentHandler.h"
32 namespace tlp {
33 
34 class PluginProgress;
35 
36 
37 /*
38 * @brief Interface for CSV data parser.
39 *
40 * Send the found tokens to the CSVContentHandler interface.
41 */
42 class TLP_QT_SCOPE CSVParser {
43 public:
44  virtual ~CSVParser() {}
45 
46  /**
47  * @brief Parse the data and send the tokens found to the CSVContentHandler.
48  *
49  * Notify the progression of the parsing with the progress object.
50  **/
51  virtual bool parse(CSVContentHandler * handler,tlp::PluginProgress* progress=NULL) = 0;
52 };
53 
54 /**
55 * @brief Parse a csv data and send each tokens to the given CSVContentHandler object.
56 *
57 * 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.
58 * Send the found tokens to the CSVContentHandler interface.
59 * \code
60 * CSVParser parser(fileName,";","\"","UTF-8",true);
61 * \/\/Automatically remove quotes.
62 * CSVContentHandler * handler ;
63 * parser.parse(handler);
64 * \endcode
65 **/
66 class TLP_QT_SCOPE CSVSimpleParser : public CSVParser {
67 public:
68  /**
69  * @brief Construct a csv simple file parser.
70  * @param filename The path to the file to import.
71  * @param separator The separator to use.
72  * @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.
73  * @param firstLine The number of the first line to read. The first line is 0.
74  * @param lastLine The number of the last line to read.
75  **/
76  CSVSimpleParser(const std::string& fileName,const std::string& separator=std::string(";"),char textDelimiter='"',const std::string& fileEncoding=std::string("UTF-8"),unsigned int firstLine = 0,unsigned int lastLine = UINT_MAX);
77 
78  virtual ~CSVSimpleParser();
79 
80  std::string fileName()const {
81  return _fileName;
82  }
83  void setFileName(const std::string& fileName) {
84  _fileName = fileName;
85  }
86  std::string separator()const {
87  return _separator;
88  }
89 
90  void setSeparator(const std::string& separator) {
91  _separator = separator;
92  }
93 
94  char textDelimiter()const {
95  return _textDelimiter;
96  }
97 
98  void setTextDelimiter(char delimiter) {
99  _textDelimiter = delimiter;
100  }
101 
102  std::string fileEncoding()const {
103  return _fileEncoding;
104  }
105 
106  void setFileEncoding(const std::string& fileEncoding) {
107  _fileEncoding = fileEncoding;
108  }
109 
110  bool parse(CSVContentHandler * handler,tlp::PluginProgress* progress=NULL);
111 
112 protected:
113  virtual std::string treatToken(const std::string& token, int row, int column);
114 
115 private:
116  void tokenize(const std::string& str, std::vector<std::string>& tokens,
117  const std::string& delimiters,char _textDelimiter, unsigned int numberOfCol);
118  inline std::string convertStringEncoding(const std::string& toConvert,QTextCodec* encoder) {
119  return std::string(encoder->toUnicode ( toConvert.c_str() ).toUtf8().data());
120  }
121 
122  /**
123  * @brief Function to extract a line from a istream. Can handle Linux,Mac and Windows end of line pattern.
124  **/
125  bool multiplatformgetline ( std::istream& is, std::string& str );
126 
127  std::string removeQuotesIfAny(const std::string &s,const std::string& rejectedChars);
128  std::string _fileName;
129  std::string _separator;
130  char _textDelimiter;
131  std::string _fileEncoding;
132  unsigned int _firstLine;
133  unsigned int _lastLine;
134 
135 };
136 
137 /**
138  *@brief CSV parser used to invert the token matrix in order to treat rows as columns.
139  **/
140 class TLP_QT_SCOPE CSVInvertMatrixParser : public tlp::CSVParser , public tlp::CSVContentHandler {
141 public:
142  CSVInvertMatrixParser(CSVParser* parser);
143  virtual ~CSVInvertMatrixParser();
144 
145  bool parse(CSVContentHandler *handler, tlp::PluginProgress *progress=NULL);
146 
147  void begin();
148  void line(unsigned int row,const std::vector<std::string>& lineTokens);
149  void end(unsigned int rowNumber, unsigned int columnNumber);
150 private:
151  CSVParser *parser;
152  CSVContentHandler *handler;
153  std::vector<std::vector<std::string> > columns;
154  unsigned int maxLineSize;
155 };
156 }
157 #endif /* CSVDATALOADER_H_ */
158 ///@endcond