1 | /* |
---|
2 | Copyright (c) 2013, Ronie P. Martinez <ronmarti18@gmail.com> |
---|
3 | All rights reserved. |
---|
4 | |
---|
5 | Permission to use, copy, modify, and/or distribute this software for any purpose |
---|
6 | with or without fee is hereby granted, provided that the above copyright notice |
---|
7 | and this permission notice appear in all copies. |
---|
8 | |
---|
9 | THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH |
---|
10 | REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
---|
11 | FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, |
---|
12 | OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA |
---|
13 | OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, |
---|
14 | ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
---|
15 | */ |
---|
16 | |
---|
17 | #include "csvreader.H" |
---|
18 | |
---|
19 | CsvReader::CsvReader(QWidget *parent) : |
---|
20 | QMainWindow(parent), |
---|
21 | ui(new Ui::CsvReader) |
---|
22 | { |
---|
23 | ui->setupUi(this); |
---|
24 | } |
---|
25 | |
---|
26 | CsvReader::~CsvReader() |
---|
27 | { |
---|
28 | delete ui; |
---|
29 | } |
---|
30 | |
---|
31 | void CsvReader::on_action_Open_triggered() |
---|
32 | { |
---|
33 | model = new QStandardItemModel(this); |
---|
34 | ui->tableView->setModel(model); |
---|
35 | QString fileName = QFileDialog::getOpenFileName (this, "Open CSV file", |
---|
36 | QDir::currentPath(), "CSV (*.csv)"); |
---|
37 | QFile file (fileName); |
---|
38 | if (file.open(QIODevice::ReadOnly)) { |
---|
39 | QString data = file.readAll(); |
---|
40 | data.remove( QRegExp("\r") ); //remove all ocurrences of CR (Carriage Return) |
---|
41 | QString temp; |
---|
42 | QChar character; |
---|
43 | QTextStream textStream(&data); |
---|
44 | while (!textStream.atEnd()) { |
---|
45 | textStream >> character; |
---|
46 | if (character == ',') { |
---|
47 | checkString(temp, character); |
---|
48 | } else if (character == '\n') { |
---|
49 | checkString(temp, character); |
---|
50 | } else if (textStream.atEnd()) { |
---|
51 | temp.append(character); |
---|
52 | checkString(temp); |
---|
53 | } else { |
---|
54 | temp.append(character); |
---|
55 | } |
---|
56 | } |
---|
57 | } |
---|
58 | } |
---|
59 | |
---|
60 | void CsvReader::checkString(QString &temp, QChar character) |
---|
61 | { |
---|
62 | if(temp.count("\"")%2 == 0) { |
---|
63 | //if (temp.size() == 0 && character != ',') //problem with line endings |
---|
64 | // return; |
---|
65 | if (temp.startsWith( QChar('\"')) && temp.endsWith( QChar('\"') ) ) { |
---|
66 | temp.remove( QRegExp("^\"") ); |
---|
67 | temp.remove( QRegExp("\"$") ); |
---|
68 | } |
---|
69 | //FIXME: will possibly fail if there are 4 or more reapeating double quotes |
---|
70 | temp.replace("\"\"", "\""); |
---|
71 | QStandardItem *item = new QStandardItem(temp); |
---|
72 | standardItemList.append(item); |
---|
73 | if (character != QChar(',')) { |
---|
74 | model->appendRow(standardItemList); |
---|
75 | standardItemList.clear(); |
---|
76 | } |
---|
77 | temp.clear(); |
---|
78 | } else { |
---|
79 | temp.append(character); |
---|
80 | } |
---|
81 | } |
---|