author | llornkcor <llornkcor> | 2003-04-06 17:45:00 (UTC) |
---|---|---|
committer | llornkcor <llornkcor> | 2003-04-06 17:45:00 (UTC) |
commit | 70b1ff1a3f134d75d1539f269d52b34e6762684d (patch) (unidiff) | |
tree | 28c1b3d0c03e8c0188f014dfe094645d1ddfef5f /noncore/net/mailit/textparser.cpp | |
parent | 75e8f29020e267d1013a79839831035073b4eeae (diff) | |
download | opie-70b1ff1a3f134d75d1539f269d52b34e6762684d.zip opie-70b1ff1a3f134d75d1539f269d52b34e6762684d.tar.gz opie-70b1ff1a3f134d75d1539f269d52b34e6762684d.tar.bz2 |
move malit to head again, since it is working
Diffstat (limited to 'noncore/net/mailit/textparser.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/net/mailit/textparser.cpp | 294 |
1 files changed, 294 insertions, 0 deletions
diff --git a/noncore/net/mailit/textparser.cpp b/noncore/net/mailit/textparser.cpp new file mode 100644 index 0000000..f082417 --- a/dev/null +++ b/noncore/net/mailit/textparser.cpp | |||
@@ -0,0 +1,294 @@ | |||
1 | /********************************************************************** | ||
2 | ** Copyright (C) 2001 Trolltech AS. All rights reserved. | ||
3 | ** | ||
4 | ** This file is part of Qt Palmtop 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 "textparser.h" | ||
21 | |||
22 | TextParser::TextParser(QString in, QString lineBreak) | ||
23 | { | ||
24 | data = in; | ||
25 | lineSep = lineBreak; | ||
26 | |||
27 | init(); | ||
28 | createSeparators(); | ||
29 | split(); | ||
30 | } | ||
31 | |||
32 | TextParser::TextParser(QString in, QString lineBreak, QString sep) | ||
33 | { | ||
34 | data = in; | ||
35 | lineSep = lineBreak; | ||
36 | |||
37 | init(); | ||
38 | separators = sep; | ||
39 | split(); | ||
40 | } | ||
41 | |||
42 | void TextParser::init() | ||
43 | { | ||
44 | lineCount = 0; | ||
45 | linePos = 0; | ||
46 | totalElmCount = 0; | ||
47 | separatorPos = -1; //not initialized | ||
48 | wordPos = -1; //not initialized | ||
49 | sepAtLine = 0; | ||
50 | sepAtPosElm = -1; //such that nextSep equals 0 | ||
51 | wordAtLine = 0; | ||
52 | wordAtPosElm = -1; //such that nextWord equals 0 | ||
53 | atLine = 0; | ||
54 | atPosElm = 0; | ||
55 | } | ||
56 | |||
57 | void TextParser::createSeparators() | ||
58 | { | ||
59 | separators = " @#,.:;<>*/(){}|'?-+=_"; | ||
60 | } | ||
61 | |||
62 | /*Returns pos of given search criteria, -1 if not found */ | ||
63 | int TextParser::find(QString target, QChar sep, int pos, bool upperCase) | ||
64 | { | ||
65 | int atLine = 0, atPosElm = 0; | ||
66 | |||
67 | for (int x = 0; x < totalElmCount; x++) { | ||
68 | if (x >= pos) { | ||
69 | if (upperCase) { | ||
70 | if ((splitDone[atLine].elm[atPosElm].str.upper() == target) && | ||
71 | (splitDone[atLine].elm[atPosElm].separator == sep)) | ||
72 | return x; | ||
73 | } else { | ||
74 | if ((splitDone[atLine].elm[atPosElm].str == target) && | ||
75 | (splitDone[atLine].elm[atPosElm].separator == sep)) | ||
76 | return x; | ||
77 | } | ||
78 | } | ||
79 | atPosElm++; | ||
80 | if (atPosElm >= splitDone[atLine].elmCount) { //new Line | ||
81 | atLine++; | ||
82 | atPosElm = 0; | ||
83 | } | ||
84 | } | ||
85 | |||
86 | return -1; | ||
87 | } | ||
88 | |||
89 | int TextParser::elmCount() | ||
90 | { | ||
91 | return totalElmCount; | ||
92 | } | ||
93 | |||
94 | QChar TextParser::separatorAt(int pos) | ||
95 | { | ||
96 | if (getLineReference(pos, &sepAtLine, &sepAtPosElm) == -1) | ||
97 | return QChar::null; | ||
98 | |||
99 | separatorPos = pos; | ||
100 | return splitDone[sepAtLine].elm[sepAtPosElm].separator; | ||
101 | } | ||
102 | |||
103 | QChar TextParser::nextSeparator() | ||
104 | { | ||
105 | sepAtPosElm++; | ||
106 | if (splitDone[sepAtLine].elmCount <= sepAtPosElm) { | ||
107 | sepAtLine++; | ||
108 | sepAtPosElm = 0; | ||
109 | } | ||
110 | |||
111 | separatorPos++; | ||
112 | return splitDone[sepAtLine].elm[sepAtPosElm].separator; | ||
113 | } | ||
114 | |||
115 | bool TextParser::hasNextSeparator() | ||
116 | { | ||
117 | return ((separatorPos+1) < totalElmCount); | ||
118 | } | ||
119 | |||
120 | QString TextParser::wordAt(int pos) | ||
121 | { | ||
122 | if (getLineReference(pos, &wordAtLine, &wordAtPosElm) == -1) | ||
123 | return NULL; | ||
124 | |||
125 | wordPos = pos; | ||
126 | return splitDone[wordAtLine].elm[wordAtPosElm].str; | ||
127 | } | ||
128 | |||
129 | QString TextParser::nextWord() | ||
130 | { | ||
131 | wordAtPosElm++; | ||
132 | if (splitDone[wordAtLine].elmCount <= wordAtPosElm) { | ||
133 | wordAtLine++; | ||
134 | wordAtPosElm = 0; | ||
135 | } | ||
136 | |||
137 | wordPos++; | ||
138 | return splitDone[wordAtLine].elm[wordAtPosElm].str; | ||
139 | } | ||
140 | |||
141 | bool TextParser::hasNextWord() | ||
142 | { | ||
143 | return ((wordPos + 1) < totalElmCount); | ||
144 | } | ||
145 | |||
146 | QString TextParser::getString(int *pos, QChar stop, bool lineEnd = false) | ||
147 | { | ||
148 | QString returnStr = wordAt(*pos); | ||
149 | QChar chr = separatorAt(*pos); | ||
150 | QString s; | ||
151 | |||
152 | if (returnStr == "") | ||
153 | return ""; | ||
154 | if (chr == stop) | ||
155 | return returnStr; | ||
156 | |||
157 | if (!lineEnd) { | ||
158 | while ((chr != stop) && hasNextWord()) { | ||
159 | returnStr.append(chr); | ||
160 | returnStr += nextWord(); | ||
161 | chr = nextSeparator(); | ||
162 | } | ||
163 | } else { //copy from pos to end of line | ||
164 | getLineReference(*pos, &atLine, &atPosElm); | ||
165 | returnStr = ""; | ||
166 | while (atPosElm < splitDone[atLine].elmCount) { | ||
167 | if (splitDone[atLine].elm[atPosElm].str != "") { | ||
168 | returnStr += splitDone[atLine].elm[atPosElm].str; | ||
169 | } | ||
170 | chr = splitDone[atLine].elm[atPosElm].separator; | ||
171 | if (!chr.isNull() && (int) chr != 0) { | ||
172 | returnStr.append(splitDone[atLine].elm[atPosElm].separator); | ||
173 | } | ||
174 | atPosElm++; | ||
175 | } | ||
176 | } | ||
177 | |||
178 | *pos = wordPos; | ||
179 | return returnStr; | ||
180 | } | ||
181 | |||
182 | QString TextParser::getNextLine() | ||
183 | { | ||
184 | atLine++; | ||
185 | atPosElm = 0; | ||
186 | if (atLine < lineCount) | ||
187 | return splitDone[atLine].str; | ||
188 | return ""; | ||
189 | } | ||
190 | |||
191 | bool TextParser::hasNextLine() | ||
192 | { | ||
193 | if (atLine+1 < lineCount) | ||
194 | return TRUE;; | ||
195 | return FALSE; | ||
196 | } | ||
197 | |||
198 | int TextParser::endLinePos(int pos) | ||
199 | { | ||
200 | if ( (getLineReference(pos, &atLine, &atPosElm)) == -1) | ||
201 | return -1; | ||
202 | |||
203 | return (pos + (splitDone[atLine].elmCount - atPosElm) + 1); | ||
204 | } | ||
205 | |||
206 | int TextParser::getLineReference(int pos, int *line, int *inLinePos) | ||
207 | { | ||
208 | int currentPos = 0; | ||
209 | |||
210 | for (int x = 0; x < lineCount; x++) { | ||
211 | if ( currentPos + splitDone[x].elmCount > pos) { | ||
212 | *line = x; | ||
213 | *inLinePos = pos - currentPos; | ||
214 | return 0; //pos found okay | ||
215 | } | ||
216 | currentPos += splitDone[x].elmCount; | ||
217 | } | ||
218 | return -1; //no reference found | ||
219 | } | ||
220 | |||
221 | void TextParser::split() | ||
222 | { | ||
223 | t_splitLine newLine; | ||
224 | |||
225 | while ((uint) linePos < data.length()) { | ||
226 | newLine = nextLine(); | ||
227 | splitDone[lineCount] = splitLine(newLine); | ||
228 | totalElmCount += splitDone[lineCount].elmCount; | ||
229 | lineCount++; | ||
230 | } | ||
231 | } | ||
232 | |||
233 | t_splitLine TextParser::splitLine(t_splitLine line) | ||
234 | { | ||
235 | uint pos = 0; | ||
236 | uint elmCount = 0; | ||
237 | t_splitLine tempLine = line; | ||
238 | |||
239 | tempLine.str = line.str.simplifyWhiteSpace(); | ||
240 | tempLine.elm[0].str = ""; | ||
241 | while ( pos < line.str.length() ) { | ||
242 | if ( isSeparator(tempLine.str[pos]) ) { | ||
243 | tempLine.elm[elmCount].separator = tempLine.str[pos]; | ||
244 | elmCount++; | ||
245 | pos++; | ||
246 | while (tempLine.str[pos] == ' ') | ||
247 | pos++; | ||
248 | if (pos > line.str.length()) | ||
249 | elmCount--; | ||
250 | tempLine.elm[elmCount].str = ""; | ||
251 | } else { | ||
252 | if (!tempLine.str[pos].isNull()) | ||
253 | tempLine.elm[elmCount].str += tempLine.str[pos]; | ||
254 | pos++; | ||
255 | } | ||
256 | } | ||
257 | |||
258 | tempLine.elmCount = elmCount + 1; | ||
259 | return tempLine; | ||
260 | } | ||
261 | |||
262 | bool TextParser::isSeparator(QChar chr) | ||
263 | { | ||
264 | for (uint x = 0; x < separators.length(); x++) { | ||
265 | if (chr == separators[x]) | ||
266 | return true; | ||
267 | } | ||
268 | return false; | ||
269 | } | ||
270 | |||
271 | t_splitLine TextParser::nextLine() | ||
272 | { | ||
273 | int newLinePos; | ||
274 | t_splitLine lineType; | ||
275 | |||
276 | newLinePos = data.find(lineSep, linePos); | ||
277 | |||
278 | lineType.lineType = NewLine; | ||
279 | lineType.str = ""; | ||
280 | |||
281 | if (newLinePos == -1) { | ||
282 | newLinePos = data.length(); | ||
283 | lineType.lineType = LastLine; | ||
284 | } | ||
285 | |||
286 | for (int x = linePos; x < newLinePos; x++) | ||
287 | lineType.str += data[x]; | ||
288 | |||
289 | linePos = newLinePos; | ||
290 | if ((uint) linePos < data.length()) //if not EOF, add length of lineSep | ||
291 | linePos += lineSep.length(); | ||
292 | |||
293 | return lineType; | ||
294 | } | ||