• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.14.10 API Reference
  • KDE Home
  • Contact Us
 

kioslave/imap4

  • kioslave
  • imap4
mimehdrline.cpp
1/***************************************************************************
2 mimehdrline.cc - description
3 -------------------
4 begin : Wed Oct 11 2000
5 copyright : (C) 2000 by Sven Carstens
6 email : s.carstens@gmx.de
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
18#include "mimehdrline.h"
19#include <iostream>
20#include <ctype.h>
21#include <stdlib.h>
22#include <stdio.h>
23
24using namespace std;
25
26mimeHdrLine::mimeHdrLine ():
27mimeValue (), mimeLabel ()
28{
29}
30
31mimeHdrLine::mimeHdrLine (const QByteArray & aLabel, const QByteArray & aValue):
32mimeValue (aValue),
33mimeLabel (aLabel)
34{
35}
36
37mimeHdrLine::mimeHdrLine (mimeHdrLine * aHdrLine):
38mimeValue (aHdrLine->mimeValue), mimeLabel (aHdrLine->mimeLabel)
39{
40}
41
42mimeHdrLine::~mimeHdrLine ()
43{
44}
45
46int mimeHdrLine::appendStr (const char *aCStr)
47{
48 int retVal = 0;
49 int skip;
50
51 if ( aCStr ) {
52 skip = skipWS( aCStr );
53 if ( skip && !mimeLabel.isEmpty() ) {
54 if ( skip > 0 ) {
55 mimeValue += QByteArray( aCStr, skip );
56 aCStr += skip;
57 retVal += skip;
58 skip = parseFullLine( aCStr );
59 mimeValue += QByteArray( aCStr, skip );
60 retVal += skip;
61 aCStr += skip;
62 }
63 } else {
64 if ( mimeLabel.isEmpty() ) {
65 return setStr( aCStr );
66 }
67 }
68 }
69 return retVal;
70}
71
75int
76mimeHdrLine::setStr (const char *aCStr)
77{
78 int retVal = 0;
79// char *begin = aCStr;
80 mimeLabel = QByteArray();
81 mimeValue = QByteArray();
82
83 if ( aCStr ) {
84 // can't have spaces on normal lines
85 if ( !skipWS( aCStr ) ) {
86 int label = 0, advance;
87 while ( ( advance = parseWord( &aCStr[label] ) ) ) {
88 label += advance;
89 }
90 if ( label && aCStr[label - 1] != ':' ) {
91 retVal = 0;
92 } else {
93 mimeLabel = QByteArray( aCStr, label-1 );
94 retVal += label;
95 aCStr += label;
96 }
97 }
98 if ( retVal ) {
99 int skip;
100 skip = skipWS( aCStr );
101 if ( skip < 0 ) {
102 skip *= -1;
103 }
104 aCStr += skip;
105 retVal += skip;
106 skip = parseFullLine( aCStr );
107 mimeValue = QByteArray( aCStr, skip );
108 retVal += skip;
109 aCStr += skip;
110 } else {
111 //Skip malformed line
112 while ( *aCStr && *aCStr != '\r' && *aCStr != '\n' ) {
113 retVal--;
114 aCStr++;
115 }
116 if ( *aCStr == '\r' ) {
117 retVal--;
118 aCStr++;
119 }
120 if ( *aCStr == '\n' ) {
121 retVal--;
122 aCStr++;
123 }
124 }
125 } else {
126 //debug
127 }
128 return retVal;
129}
130
132int mimeHdrLine::parseWord (const char *aCStr)
133{
134 int retVal = 0;
135
136 if ( aCStr && *aCStr ) {
137 if ( *aCStr == '"' ) {
138 return mimeHdrLine::parseQuoted( '"', '"', aCStr );
139 } else {
140 return mimeHdrLine::parseHalfWord( aCStr );
141 }
142 } else {
143 //debug();
144 }
145 return retVal;
146}
147
149int mimeHdrLine::parseQuoted (char startQuote, char endQuote, const char *aCStr)
150{
151 int retVal = 0;
152
153 if ( aCStr && *aCStr ) {
154 if ( *aCStr == startQuote ) {
155 aCStr++;
156 retVal++;
157 } else {
158 return 0;
159 }
160 while ( *aCStr && *aCStr != endQuote ) {
161 //skip over backticks
162 if ( *aCStr == '\\' ) {
163 aCStr++;
164 retVal++;
165 }
166 //eat this
167 aCStr++;
168 retVal++;
169 }
170 if ( *aCStr == endQuote ) {
171 aCStr++;
172 retVal++;
173 }
174 } else {
175 //debug();
176 }
177 return retVal;
178}
179
181int mimeHdrLine::parseAlphaNum (const char *aCStr)
182{
183 int retVal = 0;
184
185 if ( aCStr ) {
186 while ( *aCStr && isalnum( *aCStr ) ) {
187 //skip over backticks
188 if ( *aCStr == '\\' ) {
189 aCStr++;
190 retVal++;
191 }
192 //eat this
193 aCStr++;
194 retVal++;
195 }
196 } else {
197 //debug();
198 }
199 return retVal;
200}
201
202int mimeHdrLine::parseHalfWord (const char *aCStr)
203{
204 int retVal = 0;
205
206 if ( aCStr && *aCStr ) {
207 if ( isalnum( *aCStr ) ) {
208 return mimeHdrLine::parseAlphaNum( aCStr );
209 }
210 //skip over backticks
211 if ( *aCStr == '\\' ) {
212 aCStr++;
213 retVal++;
214 } else if ( !isspace( *aCStr ) ) {
215 //eat this
216 aCStr++;
217 retVal++;
218 }
219 } else {
220 //debug();
221 }
222 return retVal;
223}
224
226int mimeHdrLine::parseHalfLine (const char *aCStr)
227{
228 int retVal = 0;
229
230 if ( aCStr ) {
231 while ( *aCStr && *aCStr != '\n' ) {
232 //skip over backticks
233 if ( *aCStr == '\\' ) {
234 aCStr++;
235 retVal++;
236 }
237 //eat this
238 aCStr++;
239 retVal++;
240 }
241 if ( *aCStr == '\n' ) {
242 aCStr++;
243 retVal++;
244 }
245 } else {
246 //debug();
247 }
248 return retVal;
249}
250
252int mimeHdrLine::skipWS (const char *aCStr)
253{
254 int retVal = 0;
255
256 if ( aCStr && *aCStr ) {
257 while ( *aCStr == ' ' || *aCStr == '\t' ) {
258 aCStr++;
259 retVal++;
260 }
261 //check out for continuation lines
262 if ( *aCStr == '\r' ) {
263 aCStr++;
264 retVal++;
265 }
266 if ( *aCStr++ == '\n' ) {
267 if ( *aCStr == '\t' || *aCStr == ' ' ) {
268 int skip = mimeHdrLine::skipWS( aCStr );
269 if ( skip < 0 ) {
270 skip *= -1;
271 }
272 retVal += 1 + skip;
273 } else {
274 retVal = -retVal - 1;
275 }
276 }
277 } else {
278 //debug();
279 }
280 return retVal;
281}
282
284int mimeHdrLine::parseFullLine (const char *aCStr)
285{
286 int retVal = 0;
287 int skip;
288
289 if ( aCStr ) {
290 //skip leading white space
291 skip = skipWS( aCStr );
292 if ( skip > 0 ) {
293 aCStr += skip;
294 retVal += skip;
295 }
296 while ( *aCStr ) {
297 int advance;
298
299 if ( ( advance = parseHalfLine( aCStr ) ) ) {
300 retVal += advance;
301 aCStr += advance;
302 } else if ( ( advance = skipWS( aCStr ) ) ) {
303 if ( advance > 0 ) {
304 retVal += advance;
305 aCStr += advance;
306 } else {
307 retVal -= advance;
308 break;
309 }
310 } else {
311 break;
312 }
313 }
314 } else {
315 //debug();
316 }
317 return retVal;
318}
319
321int mimeHdrLine::parseSeparator (char separator, const char *aCStr)
322{
323 int retVal = 0;
324 int skip;
325
326 if ( aCStr ) {
327 //skip leading white space
328 skip = skipWS( aCStr );
329 if ( skip > 0 ) {
330 aCStr += skip;
331 retVal += skip;
332 }
333 while ( *aCStr ) {
334 int advance;
335
336 if ( *aCStr != separator ) {
337 if ( ( advance = mimeHdrLine::parseWord( aCStr ) ) ) {
338 retVal += advance;
339 aCStr += advance;
340 } else if ( ( advance = mimeHdrLine::skipWS( aCStr ) ) ) {
341 if ( advance > 0 ) {
342 retVal += advance;
343 aCStr += advance;
344 } else {
345 retVal -= advance;
346 break;
347 }
348 } else {
349 break;
350 }
351 } else {
352 //include separator in result
353 retVal++;
354 aCStr++;
355 break;
356 }
357 }
358 } else {
359 //debug();
360 }
361 return retVal;
362}
363
366const QByteArray& mimeHdrLine::getLabel ()
367{
368 return mimeLabel;
369}
370
372const QByteArray& mimeHdrLine::getValue ()
373{
374 return mimeValue;
375}
376
377
378// FIXME: very inefficient still
379QByteArray mimeHdrLine::truncateLine(QByteArray aLine, unsigned int truncate)
380{
381 int cutHere;
382 QByteArray retVal;
383 uint len = aLine.length();
384
385 // see if we have a line of the form "key: value" (like "Subject: bla")
386 // then we do not want to truncate between key and value
387 int validStart = aLine.indexOf(": ");
388 if ( validStart > -1 ) {
389 validStart += 2;
390 }
391 while ( len > truncate ) {
392 cutHere = aLine.lastIndexOf( ' ', truncate );
393 if ( cutHere < 1 || cutHere < validStart ) {
394 cutHere = aLine.lastIndexOf( '\t', truncate );
395 if ( cutHere < 1 ) {
396 cutHere = aLine.indexOf( ' ', 1 );
397 if ( cutHere < 1 ) {
398 cutHere = aLine.indexOf( '\t', 1 );
399 if ( cutHere < 1 ) {
400 // simply truncate
401 return aLine.left( truncate );
402 }
403 }
404 }
405 }
406
407 retVal += aLine.left( cutHere ) + '\n';
408 int chop = len - cutHere;
409 aLine = aLine.right( chop );
410 len -= chop;
411 }
412 retVal += aLine;
413
414 return retVal;
415}
mimeHdrLine
Definition: mimehdrline.h:29
mimeHdrLine::parseHalfWord
static int parseHalfWord(const char *)
slurp one word respecting backticks
Definition: mimehdrline.cpp:202
mimeHdrLine::parseWord
static int parseWord(const char *)
slurp one word
Definition: mimehdrline.cpp:132
mimeHdrLine::parseSeparator
static int parseSeparator(char, const char *)
parses continuated lines
Definition: mimehdrline.cpp:321
mimeHdrLine::skipWS
static int skipWS(const char *)
skip all white space characters
Definition: mimehdrline.cpp:252
mimeHdrLine::setStr
int setStr(const char *)
parse a Line into the class and report characters slurped
Definition: mimehdrline.cpp:76
mimeHdrLine::mimeValue
QByteArray mimeValue
contains the Value
Definition: mimehdrline.h:56
mimeHdrLine::parseHalfLine
int parseHalfLine(const char *)
slurp one line without continuation
Definition: mimehdrline.cpp:226
mimeHdrLine::mimeLabel
QByteArray mimeLabel
contains the Label of the line
Definition: mimehdrline.h:59
mimeHdrLine::getValue
const QByteArray & getValue()
return the value
Definition: mimehdrline.cpp:372
mimeHdrLine::parseAlphaNum
static int parseAlphaNum(const char *)
slurp one alphanumerical word without continuation
Definition: mimehdrline.cpp:181
mimeHdrLine::parseFullLine
int parseFullLine(const char *)
parses a continuated line
Definition: mimehdrline.cpp:284
mimeHdrLine::parseQuoted
static int parseQuoted(char, char, const char *)
slurp one word
Definition: mimehdrline.cpp:149
mimeHdrLine::getLabel
const QByteArray & getLabel()
return the label
Definition: mimehdrline.cpp:366
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Thu Jul 21 2022 00:00:00 by doxygen 1.9.5 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kioslave/imap4

Skip menu "kioslave/imap4"
  • Main Page
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • Related Pages

kdepimlibs-4.14.10 API Reference

Skip menu "kdepimlibs-4.14.10 API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal