tdeioslave/imap4

mimehdrline.cpp
1 /***************************************************************************
2  mimehdrline.cpp - 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 <config.h>
19 #include <iostream>
20 #include <ctype.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 
24 #include "mimehdrline.h"
25 #include "rfcdecoder.h"
26 
27 using namespace std;
28 
29 const char *wdays[] = {
30  "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
31 };
32 
33 const char *months[] = {
34  "Jan", "Feb", "Mar", "Apr", "May", "Jun",
35  "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
36 };
37 
38 mimeHdrLine::mimeHdrLine ():
39 mimeValue ((const char *) NULL), mimeLabel ((const char *) NULL)
40 {
41 }
42 
43 mimeHdrLine::mimeHdrLine (const TQCString & aLabel, const TQCString & aValue):
44 mimeValue (aValue),
45 mimeLabel (aLabel)
46 {
47 }
48 
49 mimeHdrLine::mimeHdrLine (mimeHdrLine * aHdrLine):
50 mimeValue (aHdrLine->mimeValue), mimeLabel (aHdrLine->mimeLabel)
51 {
52 }
53 
54 mimeHdrLine::~mimeHdrLine ()
55 {
56 }
57 
58 int
59 mimeHdrLine::appendStr (const char *inCStr)
60 {
61  int retVal = 0;
62  int skip;
63  char *aCStr = (char *) inCStr;
64 
65  if (aCStr)
66  {
67  skip = skipWS (aCStr);
68  if (skip && !mimeLabel.isEmpty ())
69  {
70  if (skip > 0)
71  {
72  mimeValue += TQCString (aCStr, skip + 1);
73  aCStr += skip;
74  retVal += skip;
75  skip = parseFullLine (aCStr);
76  mimeValue += TQCString (aCStr, skip + 1);
77  retVal += skip;
78  aCStr += skip;
79  }
80  }
81  else
82  {
83  if (mimeLabel.isEmpty ())
84  return setStr (aCStr);
85  }
86  }
87  return retVal;
88 }
89 
93 int
94 mimeHdrLine::setStr (const char *inCStr)
95 {
96  int retVal = 0;
97  char *aCStr = (char *) inCStr;
98 // char *begin = aCStr;
99  mimeLabel = TQCString ((const char *) NULL);
100  mimeValue = TQCString ((const char *) NULL);
101 
102  if (aCStr)
103  {
104  // can't have spaces on normal lines
105  if (!skipWS (aCStr))
106  {
107  int label = 0, advance;
108  while ((advance = parseWord (&aCStr[label])))
109  {
110  label += advance;
111  }
112  if (label && aCStr[label - 1] != ':')
113  retVal = 0;
114  else
115  {
116  mimeLabel = TQCString (aCStr, label); //length including zero
117  retVal += label;
118  aCStr += label;
119  }
120  }
121  if (retVal)
122  {
123  int skip;
124  skip = skipWS (aCStr);
125  if (skip < 0)
126  skip *= -1;
127  aCStr += skip;
128  retVal += skip;
129  skip = parseFullLine (aCStr);
130  mimeValue = TQCString (aCStr, skip + 1);
131  retVal += skip;
132  aCStr += skip;
133  }
134  else
135  {
136  //Skip malformed line
137  while (*aCStr && *aCStr != '\r' && *aCStr != '\n')
138  {
139  retVal--;
140  aCStr++;
141  }
142  if (*aCStr == '\r')
143  {
144  retVal--;
145  aCStr++;
146  }
147  if (*aCStr == '\n')
148  {
149  retVal--;
150  aCStr++;
151  }
152  }
153  }
154  else
155  {
156  //debug
157  }
158  return retVal;
159 }
160 
162 int
163 mimeHdrLine::parseWord (const char *inCStr)
164 {
165  int retVal = 0;
166  char *aCStr = (char *) inCStr;
167 
168  if (aCStr && *aCStr)
169  {
170  if (*aCStr == '"')
171  return mimeHdrLine::parseQuoted ('"', '"', aCStr);
172  else
173  return mimeHdrLine::parseHalfWord (aCStr);
174  }
175  else
176  {
177  //debug();
178  }
179  return retVal;
180 }
181 
183 int
184 mimeHdrLine::parseQuoted (char startQuote, char endQuote, const char *inCStr)
185 {
186  char *aCStr = (char *) inCStr;
187  int retVal = 0;
188 
189  if (aCStr && *aCStr)
190  {
191  if (*aCStr == startQuote)
192  {
193  aCStr++;
194  retVal++;
195  }
196  else
197  return 0;
198  while (*aCStr && *aCStr != endQuote)
199  {
200  //skip over backticks
201  if (*aCStr == '\\')
202  {
203  aCStr++;
204  retVal++;
205  }
206  //eat this
207  aCStr++;
208  retVal++;
209  }
210  if (*aCStr == endQuote)
211  {
212  aCStr++;
213  retVal++;
214  }
215  }
216  else
217  {
218  //debug();
219  }
220  return retVal;
221 }
222 
224 int
225 mimeHdrLine::parseAlphaNum (const char *inCStr)
226 {
227  int retVal = 0;
228  char *aCStr = (char *) inCStr;
229 
230  if (aCStr)
231  {
232  while (*aCStr && isalnum (*aCStr))
233  {
234  //skip over backticks
235  if (*aCStr == '\\')
236  {
237  aCStr++;
238  retVal++;
239  }
240  //eat this
241  aCStr++;
242  retVal++;
243  }
244  }
245  else
246  {
247  //debug();
248  }
249  return retVal;
250 }
251 
252 int
253 mimeHdrLine::parseHalfWord (const char *inCStr)
254 {
255  int retVal = 0;
256  char *aCStr = (char *) inCStr;
257 
258  if (aCStr && *aCStr)
259  {
260  if (isalnum (*aCStr))
261  return mimeHdrLine::parseAlphaNum (aCStr);
262  //skip over backticks
263  if (*aCStr == '\\')
264  {
265  aCStr++;
266  retVal++;
267  }
268  else if (!isspace (*aCStr))
269  {
270  //eat this
271  aCStr++;
272  retVal++;
273  }
274  }
275  else
276  {
277  //debug();
278  }
279  return retVal;
280 }
281 
283 int
284 mimeHdrLine::parseHalfLine (const char *inCStr)
285 {
286  int retVal = 0;
287  char *aCStr = (char *) inCStr;
288 
289  if (aCStr)
290  {
291  while (*aCStr && *aCStr != '\n')
292  {
293  //skip over backticks
294  if (*aCStr == '\\')
295  {
296  aCStr++;
297  retVal++;
298  }
299  //eat this
300  aCStr++;
301  retVal++;
302  }
303  if (*aCStr == '\n')
304  {
305  aCStr++;
306  retVal++;
307  }
308  }
309  else
310  {
311  //debug();
312  }
313  return retVal;
314 }
315 
317 int
318 mimeHdrLine::skipWS (const char *inCStr)
319 {
320  int retVal = 0;
321  char *aCStr = (char *) inCStr;
322 
323  if (aCStr && *aCStr)
324  {
325  while (*aCStr == ' ' || *aCStr == '\t')
326  {
327  aCStr++;
328  retVal++;
329  }
330  //check out for continuation lines
331  if (*aCStr == '\r')
332  {
333  aCStr++;
334  retVal++;
335  }
336  if (*aCStr++ == '\n')
337  if (*aCStr == '\t' || *aCStr == ' ')
338  {
339  int skip = mimeHdrLine::skipWS (aCStr);
340  if (skip < 0)
341  skip *= -1;
342  retVal += 1 + skip;
343  }
344  else
345  {
346  retVal = -retVal - 1;
347  }
348  }
349  else
350  {
351  //debug();
352  }
353  return retVal;
354 }
355 
357 int
358 mimeHdrLine::parseFullLine (const char *inCStr)
359 {
360  int retVal = 0;
361  char *aCStr = (char *) inCStr;
362  int skip;
363 
364  if (aCStr)
365  {
366  //skip leading white space
367  skip = skipWS (aCStr);
368  if (skip > 0)
369  {
370  aCStr += skip;
371  retVal += skip;
372  }
373  while (*aCStr)
374  {
375  int advance;
376 
377  if ((advance = parseHalfLine (aCStr)))
378  {
379  retVal += advance;
380  aCStr += advance;
381  }
382  else if ((advance = skipWS (aCStr)))
383  {
384  if (advance > 0)
385  {
386  retVal += advance;
387  aCStr += advance;
388  }
389  else
390  {
391  retVal -= advance;
392  break;
393  }
394  }
395  else
396  break;
397  }
398  }
399  else
400  {
401  //debug();
402  }
403  return retVal;
404 }
405 
407 int
408 mimeHdrLine::parseSeparator (char separator, const char *inCStr)
409 {
410  char *aCStr = (char *) inCStr;
411  int retVal = 0;
412  int skip;
413 
414  if (aCStr)
415  {
416  //skip leading white space
417  skip = skipWS (aCStr);
418  if (skip > 0)
419  {
420  aCStr += skip;
421  retVal += skip;
422  }
423  while (*aCStr)
424  {
425  int advance;
426 
427  if (*aCStr != separator)
428  {
429  if ((advance = mimeHdrLine::parseWord (aCStr)))
430  {
431  retVal += advance;
432  aCStr += advance;
433  }
434  else if ((advance = mimeHdrLine::skipWS (aCStr)))
435  {
436  if (advance > 0)
437  {
438  retVal += advance;
439  aCStr += advance;
440  }
441  else
442  {
443  retVal -= advance;
444  break;
445  }
446  }
447  else
448  break;
449  }
450  else
451  {
452  //include separator in result
453  retVal++;
454  aCStr++;
455  break;
456  }
457  }
458  }
459  else
460  {
461  //debug();
462  }
463  return retVal;
464 }
465 
468 const TQCString&
470 {
471  return mimeLabel;
472 }
473 
475 const TQCString&
477 {
478  return mimeValue;
479 }
480 
481 
482 // FIXME: very inefficient still
483 TQCString
484 mimeHdrLine::truncateLine(TQCString aLine, unsigned int truncate)
485 {
486  int cutHere;
487  TQCString retVal;
488  uint len = aLine.length();
489 
490  // see if we have a line of the form "key: value" (like "Subject: bla")
491  // then we do not want to truncate between key and value
492  int validStart = aLine.find(": ");
493  if (validStart > -1) {
494  validStart += 2;
495  }
496  while (len > truncate) {
497  cutHere = aLine.findRev(' ', truncate);
498  if (cutHere < 1 || cutHere < validStart) {
499  cutHere = aLine.findRev('\t', truncate);
500  if (cutHere < 1) {
501  cutHere = aLine.find(' ', 1);
502  if (cutHere < 1) {
503  cutHere = aLine.find('\t', 1);
504  if (cutHere < 1) {
505  // simply truncate
506  return aLine.left(truncate);
507  }
508  }
509  }
510  }
511 
512  retVal += aLine.left(cutHere) + '\n';
513  int chop = len - cutHere;
514  aLine = aLine.right(chop);
515  len -= chop;
516  }
517  retVal += aLine;
518 
519  return retVal;
520 }
521 
static int parseHalfWord(const char *)
slurp one word respecting backticks
static int parseWord(const char *)
slurp one word
TQCString mimeValue
contains the Value
Definition: mimehdrline.h:57
static int parseSeparator(char, const char *)
parses continuated lines
TQCString mimeLabel
contains the Label of the line
Definition: mimehdrline.h:60
static int skipWS(const char *)
skip all white space characters
const TQCString & getLabel()
return the label
int setStr(const char *)
parse a Line into the class and report characters slurped
Definition: mimehdrline.cpp:94
int parseHalfLine(const char *)
slurp one line without continuation
static int parseAlphaNum(const char *)
slurp one alphanumerical word without continuation
int parseFullLine(const char *)
parses a continuated line
static int parseQuoted(char, char, const char *)
slurp one word
const TQCString & getValue()
return the value