tdeioslave/imap4

mimeio.cpp
1 /***************************************************************************
2  mimeio.cpp - description
3  -------------------
4  begin : Wed Oct 25 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 <iostream>
19 using namespace std;
20 
21 #include "mimeio.h"
22 
23 mimeIO::mimeIO ()
24 {
25  theCRLF = "\r\n";
26  crlfLen = 2;
27 }
28 
29 mimeIO::~mimeIO ()
30 {
31 }
32 
33 int
34 mimeIO::inputLine (TQCString & aLine)
35 {
36  char input;
37 
38  aLine = (const char *) NULL;
39  while (inputChar (input))
40  {
41  aLine += input;
42  if (input == '\n')
43  break;
44  }
45 // cout << aLine.length() << " - " << aLine;
46  return aLine.length ();
47 }
48 
49 int
50 mimeIO::outputLine (const TQCString & aLine, int len)
51 {
52  int i;
53 
54  if (len == -1) {
55  len = aLine.length();
56  }
57  int start = len;
58  for (i = 0; i < start; i++)
59  if (!outputChar (aLine[i]))
60  break;
61  return i;
62 }
63 
64 int
65 mimeIO::outputMimeLine (const TQCString & inLine)
66 {
67  int retVal = 0;
68  TQCString aLine = inLine;
69  int len = inLine.length();
70 
71  int theLF = aLine.findRev ('\n');
72  if (theLF == len - 1 && theLF != -1)
73  {
74  //we have a trailing LF, now check for CR
75  if (aLine[theLF - 1] == '\r')
76  theLF--;
77  //truncate the line
78  aLine.truncate(theLF);
79  len = theLF;
80  theLF = -1;
81  }
82  //now truncate the line
83  {
84  int start, end, offset;
85  start = 0;
86  end = aLine.find ('\n', start);
87  while (end >= 0)
88  {
89  offset = 1;
90  if (end && aLine[end - 1] == '\r')
91  {
92  offset++;
93  end--;
94  }
95  outputLine (aLine.mid (start, end - start) + theCRLF, end - start + crlfLen);
96  start = end + offset;
97  end = aLine.find ('\n', start);
98  }
99  outputLine (aLine.mid (start, len - start) + theCRLF, len - start + crlfLen);
100  }
101  return retVal;
102 }
103 
104 int
105 mimeIO::inputChar (char &aChar)
106 {
107  if (cin.eof ())
108  {
109 // cout << "EOF" << endl;
110  return 0;
111  }
112  cin.get (aChar);
113  return 1;
114 }
115 
116 int
117 mimeIO::outputChar (char aChar)
118 {
119  cout << aChar;
120  return 1;
121 }
122 
123 void
124 mimeIO::setCRLF (const char *aCRLF)
125 {
126  theCRLF = aCRLF;
127  crlfLen = strlen(aCRLF);
128 }
129 
130 mimeIOTQFile::mimeIOTQFile (const TQString & aName):
131 mimeIO (),
132 myFile (aName)
133 {
134  myFile.open (IO_ReadOnly);
135 }
136 
137 mimeIOTQFile::~mimeIOTQFile ()
138 {
139  myFile.close ();
140 }
141 
142 int
143 mimeIOTQFile::outputLine (const TQCString &, int)
144 {
145  return 0;
146 }
147 
148 int
149 mimeIOTQFile::inputLine (TQCString & data)
150 {
151  data.resize( 1024 );
152  myFile.readLine (data.data(), 1024);
153 
154  return data.length ();
155 }
156 
157 mimeIOTQString::mimeIOTQString ()
158 {
159 }
160 
161 mimeIOTQString::~mimeIOTQString ()
162 {
163 }
164 
165 int
166 mimeIOTQString::outputLine (const TQCString & _str, int len)
167 {
168  if (len == -1) {
169  len = _str.length();
170  }
171  theString += _str;
172  return len;
173 }
174 
175 int
176 mimeIOTQString::inputLine (TQCString & _str)
177 {
178  if (theString.isEmpty ())
179  return 0;
180 
181  int i = theString.find ('\n');
182 
183  if (i == -1)
184  return 0;
185  _str = theString.left (i + 1).latin1 ();
186  theString = theString.right (theString.length () - i - 1);
187  return _str.length ();
188 }
Definition: mimeio.h:29