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