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