MyBooks
csvwriter.hpp
Go to the documentation of this file.
1 #ifndef CSVWRITER_HPP
2 #define CSVWRITER_HPP
3 #include <QObject>
4 #include <QString>
5 #define DEFAULT_ESCAPE_CHARACTER = '"'
6 #define DEFAULT_SEPARATOR = ','
7 #define DEFAULT_QUOTE_CHARACTER = '"'
8 #define NO_QUOTE_CHARACTER = '\u0000';
9 #define NO_ESCAPE_CHARACTER = '\u0000';
10 #define DEFAULT_LINE_END = "\n";
16 public class CSVWriter
17 {
18 
19 public:
20  CSVWriter(Writer writer)
21  {
24  }
25 
35  CSVWriter(Writer writer, char separator, char quotechar, char escapechar, String lineEnd)
36  {
37  this.pw = new PrintWriter(writer);
38  this.separator = separator;
39  this.quotechar = quotechar;
40  this.escapechar = escapechar;
41  this.lineEnd = lineEnd;
42  }
43 
44 
51  void writeNext(QString nextLine)
52  {
53  if ( nextLine == NULL ) return;
54 
55  QString sb;
56  for ( int i = 0; i < nextLine.length(); ++i )
57  {
58  if ( i != 0 ) sb.append(separator);
59 
60  QString nextElement = nextLine[i];
61  if ( nextElement == NULL ) continue;
62  if ( quotechar != NO_QUOTE_CHARACTER ) sb.append(quotechar);
63 
64  for ( int j = 0; j < nextElement.length(); ++j )
65  {
66  char nextChar = nextElement.mid(j,1);
67  if ( (escapechar != NO_ESCAPE_CHARACTER) &&
68  (nextChar == quotechar)
69  )
70  {
71  sb.append(escapechar).append(nextChar);
72  }
73  else if ( ( escapechar != NO_ESCAPE_CHARACTER ) &&
74  ( nextChar == escapechar )
75  )
76  {
77  sb.append(escapechar).append(nextChar);
78  }
79  else
80  {
81  sb.append(nextChar);
82  }
83  }
84  if ( quotechar != NO_QUOTE_CHARACTER ) sb.append(quotechar);
85  }
86 
87  sb.append(lineEnd);
88  pw.write(sb.toString());
89 
90  }
91 
96  void flush() throws IOException
97  {
98  pw.flush();
99  }
100 
108  void close() throws IOException
109  {
110  pw.flush();
111  pw.close();
112  }
113 
114 
115 private:
116  PrintWriter pw;
117  char separator;
118  char quotechar;
120  QString lineEnd;
121 }
122 #endif // CSVWRITER_HPP
char escapechar
Definition: csvwriter.hpp:119
#define DEFAULT_SEPARATOR
Definition: csvwriter.hpp:6
QString lineEnd
Definition: csvwriter.hpp:120
CSVWriter(Writer writer, char separator, char quotechar, char escapechar, String lineEnd)
Constructs CSVWriter with supplied separator, quote char, escape char and line ending.
Definition: csvwriter.hpp:35
#define DEFAULT_QUOTE_CHARACTER
Definition: csvwriter.hpp:7
void close()
Close the underlying stream writer.
Definition: csvwriter.hpp:108
#define DEFAULT_ESCAPE_CHARACTER
Definition: csvwriter.hpp:5
char quotechar
Definition: csvwriter.hpp:118
CSVWriter(Writer writer)
Definition: csvwriter.hpp:20
#define NO_QUOTE_CHARACTER
Definition: csvwriter.hpp:8
void flush()
Flush underlying stream to writer.
Definition: csvwriter.hpp:96
PrintWriter pw
Definition: csvwriter.hpp:116
char separator
Definition: csvwriter.hpp:117
void writeNext(QString nextLine)
Writes the next line to the file.
Definition: csvwriter.hpp:51
#define DEFAULT_LINE_END
Definition: csvwriter.hpp:10
#define NO_ESCAPE_CHARACTER
Definition: csvwriter.hpp:9