Diffstat (limited to 'noncore/apps/tableviewer/db/csvsource.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/apps/tableviewer/db/csvsource.cpp | 207 |
1 files changed, 207 insertions, 0 deletions
diff --git a/noncore/apps/tableviewer/db/csvsource.cpp b/noncore/apps/tableviewer/db/csvsource.cpp new file mode 100644 index 0000000..2561b4b --- a/dev/null +++ b/noncore/apps/tableviewer/db/csvsource.cpp | |||
@@ -0,0 +1,207 @@ | |||
1 | /********************************************************************** | ||
2 | ** Copyright (C) 2000 Trolltech AS. All rights reserved. | ||
3 | ** | ||
4 | ** This file is part of Qtopia Environment. | ||
5 | ** | ||
6 | ** This file may be distributed and/or modified under the terms of the | ||
7 | ** GNU General Public License version 2 as published by the Free Software | ||
8 | ** Foundation and appearing in the file LICENSE.GPL included in the | ||
9 | ** packaging of this file. | ||
10 | ** | ||
11 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE | ||
12 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | ||
13 | ** | ||
14 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. | ||
15 | ** | ||
16 | ** Contact info@trolltech.com if any conditions of this licensing are | ||
17 | ** not clear to you. | ||
18 | ** | ||
19 | **********************************************************************/ | ||
20 | #include "csvsource.h" | ||
21 | #include "common.h" | ||
22 | #include "datacache.h" | ||
23 | #include <qtextstream.h> | ||
24 | #include <qstringlist.h> | ||
25 | #include <qmap.h> | ||
26 | #include <qregexp.h> | ||
27 | |||
28 | DBCsv::DBCsv(DBStore *d) | ||
29 | { | ||
30 | dstore = d; | ||
31 | } | ||
32 | |||
33 | DBCsv::~DBCsv() | ||
34 | { | ||
35 | } | ||
36 | |||
37 | QString DBCsv::type() | ||
38 | { | ||
39 | return "csv"; | ||
40 | } | ||
41 | |||
42 | QStringList readElem(QString in) | ||
43 | { | ||
44 | QStringList out; | ||
45 | |||
46 | if (in.isEmpty()) | ||
47 | return out; | ||
48 | |||
49 | bool firstChar = TRUE; | ||
50 | bool quotedElem = FALSE; | ||
51 | uint index = 0; | ||
52 | while(index < in.length()) { | ||
53 | if(firstChar) { | ||
54 | /* skip whitespace */ | ||
55 | while(index < in.length() && in[index] == ' ') | ||
56 | index++; | ||
57 | if(in[index] == '"') { | ||
58 | quotedElem = TRUE; | ||
59 | index++; | ||
60 | } | ||
61 | } | ||
62 | /* real first char */ | ||
63 | QString elem; | ||
64 | if(quotedElem) { | ||
65 | while(index < in.length() && in[index] != '"') { | ||
66 | /* check for escape character */ | ||
67 | if (in[index] == '\\') { | ||
68 | if (index++ < in.length()) { | ||
69 | elem.append(in[index]); | ||
70 | index++; | ||
71 | } | ||
72 | } else { | ||
73 | elem.append(in[index]); | ||
74 | index++; | ||
75 | } | ||
76 | } | ||
77 | } else { | ||
78 | while(index < in.length() && in[index] != ',') { | ||
79 | if (in[index] == '\\') { | ||
80 | if (index++ < in.length()) { | ||
81 | elem.append(in[index]); | ||
82 | index++; | ||
83 | } | ||
84 | } else { | ||
85 | elem.append(in[index]); | ||
86 | index++; | ||
87 | } | ||
88 | } | ||
89 | } | ||
90 | /* we have our current elem */ | ||
91 | out << elem.stripWhiteSpace(); | ||
92 | firstChar = TRUE; | ||
93 | quotedElem = FALSE; | ||
94 | /* skip till a , or end of line */ | ||
95 | while (index < in.length() && in[index] != ',') index++; | ||
96 | if(index == in.length()) | ||
97 | return out; | ||
98 | else | ||
99 | index++; | ||
100 | } | ||
101 | } | ||
102 | |||
103 | bool DBCsv::openSource(QIODevice *inDev) | ||
104 | { | ||
105 | QTextStream tsIn(inDev); | ||
106 | QString in = tsIn.readLine().stripWhiteSpace(); | ||
107 | QStringList keys; | ||
108 | |||
109 | keys = readElem(in); | ||
110 | |||
111 | QMap<int,int> keyIndexes; | ||
112 | |||
113 | KeyList *keyR = new KeyList(); | ||
114 | QStringList::Iterator i = keys.begin(); | ||
115 | |||
116 | uint fileIndex = 0; | ||
117 | while(i != keys.end()) { | ||
118 | if ((*i).isEmpty()) | ||
119 | keyIndexes.insert(fileIndex, keyR->addKey("Unamed", TVVariant::String)); | ||
120 | else | ||
121 | keyIndexes.insert(fileIndex, keyR->addKey(*i, TVVariant::String)); | ||
122 | i++; | ||
123 | fileIndex++; | ||
124 | } | ||
125 | dstore->setKeys(keyR); | ||
126 | |||
127 | in = tsIn.readLine().stripWhiteSpace(); | ||
128 | while(!in.isNull()) { | ||
129 | QStringList elems = readElem(in); | ||
130 | |||
131 | i = elems.begin(); | ||
132 | fileIndex = 0; | ||
133 | DataElem *current_data = new DataElem(dstore); | ||
134 | while(i != elems.end()) { | ||
135 | if(!(*i).isEmpty()) { | ||
136 | current_data->setField(keyIndexes[fileIndex], *i); | ||
137 | } | ||
138 | fileIndex++; | ||
139 | i++; | ||
140 | } | ||
141 | dstore->addItem(current_data); | ||
142 | in = tsIn.readLine().stripWhiteSpace(); | ||
143 | } | ||
144 | |||
145 | return TRUE; | ||
146 | } | ||
147 | |||
148 | bool DBCsv::saveSource(QIODevice *outDev) | ||
149 | { | ||
150 | /* try not to use the escape character when possible. */ | ||
151 | int i; | ||
152 | DataElem *elem; | ||
153 | KeyList *k; | ||
154 | QTextStream outstream(outDev); | ||
155 | |||
156 | k = dstore->getKeys(); | ||
157 | KeyListIterator it(*k); | ||
158 | while(it.current()) { | ||
159 | if(!it.current()->delFlag()) { | ||
160 | QString name = it.current()->name(); | ||
161 | |||
162 | name.replace(QRegExp("\\"), "\\\\"); | ||
163 | name.replace(QRegExp("\""), "\\\""); | ||
164 | if(name.find(',') != -1) { | ||
165 | name.prepend('\"'); | ||
166 | name.append('\"'); | ||
167 | } | ||
168 | |||
169 | outstream << name; | ||
170 | } | ||
171 | ++it; | ||
172 | if(it.current()) | ||
173 | outstream << ", "; | ||
174 | } | ||
175 | outstream << "\n"; | ||
176 | |||
177 | dstore->first(); | ||
178 | |||
179 | do { | ||
180 | elem = dstore->getCurrentData(); | ||
181 | if(!elem) | ||
182 | break; | ||
183 | it.toFirst(); | ||
184 | while(it.current()) { | ||
185 | i = it.currentKey(); | ||
186 | if (elem->hasValidValue(i)) { | ||
187 | QString name = elem->toQString(i); | ||
188 | |||
189 | name.replace(QRegExp("\\"), "\\\\"); | ||
190 | name.replace(QRegExp("\""), "\\\""); | ||
191 | if(name.find(',') != -1) { | ||
192 | name.prepend('\"'); | ||
193 | name.append('\"'); | ||
194 | } | ||
195 | |||
196 | outstream << name; | ||
197 | } | ||
198 | ++it; | ||
199 | if(it.current()) | ||
200 | outstream << ", "; | ||
201 | } | ||
202 | outstream << "\n"; | ||
203 | } while (dstore->next()); | ||
204 | |||
205 | return TRUE; | ||
206 | } | ||
207 | |||