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>
19using namespace std;
20
21#include "mimeio.h"
22
23mimeIO::mimeIO ()
24{
25 theCRLF = "\r\n";
26 crlfLen = 2;
27}
28
29mimeIO::~mimeIO ()
30{
31}
32
33int
34mimeIO::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
49int
50mimeIO::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
64int
65mimeIO::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
104int
105mimeIO::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
116int
117mimeIO::outputChar (char aChar)
118{
119 cout << aChar;
120 return 1;
121}
122
123void
124mimeIO::setCRLF (const char *aCRLF)
125{
126 theCRLF = aCRLF;
127 crlfLen = strlen(aCRLF);
128}
129
130mimeIOTQFile::mimeIOTQFile (const TQString & aName):
131mimeIO (),
132myFile (aName)
133{
134 myFile.open (IO_ReadOnly);
135}
136
137mimeIOTQFile::~mimeIOTQFile ()
138{
139 myFile.close ();
140}
141
142int
143mimeIOTQFile::outputLine (const TQCString &, int)
144{
145 return 0;
146}
147
148int
149mimeIOTQFile::inputLine (TQCString & data)
150{
151 data.resize( 1024 );
152 myFile.readLine (data.data(), 1024);
153
154 return data.length ();
155}
156
157mimeIOTQString::mimeIOTQString ()
158{
159}
160
161mimeIOTQString::~mimeIOTQString ()
162{
163}
164
165int
166mimeIOTQString::outputLine (const TQCString & _str, int len)
167{
168 if (len == -1) {
169 len = _str.length();
170 }
171 theString += _str;
172 return len;
173}
174
175int
176mimeIOTQString::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