kioslave/imap4
mimeio.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "mimeio.h"
00019 #include <iostream>
00020 using namespace std;
00021
00022 mimeIO::mimeIO ()
00023 {
00024 theCRLF = "\r\n";
00025 crlfLen = 2;
00026 }
00027
00028 mimeIO::~mimeIO ()
00029 {
00030 }
00031
00032 int mimeIO::inputLine (QByteArray & aLine)
00033 {
00034 char input;
00035
00036 aLine = QByteArray();
00037 while (inputChar (input))
00038 {
00039 aLine += input;
00040 if (input == '\n')
00041 break;
00042 }
00043
00044 return aLine.length ();
00045 }
00046
00047 int mimeIO::outputLine (const QByteArray & aLine, int len)
00048 {
00049 int i;
00050
00051 if (len == -1) {
00052 len = aLine.length();
00053 }
00054 int start = len;
00055 for (i = 0; i < start; ++i) {
00056 if (!outputChar (aLine[i])) {
00057 break;
00058 }
00059 }
00060 return i;
00061 }
00062
00063 int mimeIO::outputMimeLine (const QByteArray & inLine)
00064 {
00065 int retVal = 0;
00066 QByteArray aLine = inLine;
00067 int len = inLine.length();
00068
00069 int theLF = aLine.lastIndexOf ('\n');
00070 if (theLF == len - 1 && theLF != -1)
00071 {
00072
00073 if (aLine[theLF - 1] == '\r')
00074 theLF--;
00075
00076 aLine.truncate(theLF);
00077 len = theLF;
00078 theLF = -1;
00079 }
00080
00081 {
00082 int start, end, offset;
00083 start = 0;
00084 end = aLine.indexOf ('\n', start);
00085 while (end >= 0)
00086 {
00087 offset = 1;
00088 if (end && aLine[end - 1] == '\r')
00089 {
00090 offset++;
00091 end--;
00092 }
00093 outputLine (aLine.mid (start, end - start) + theCRLF, end - start + crlfLen);
00094 start = end + offset;
00095 end = aLine.indexOf ('\n', start);
00096 }
00097 outputLine (aLine.mid (start, len - start) + theCRLF, len - start + crlfLen);
00098 }
00099 return retVal;
00100 }
00101
00102 int mimeIO::inputChar (char &aChar)
00103 {
00104 if (cin.eof ())
00105 {
00106
00107 return 0;
00108 }
00109 cin.get (aChar);
00110 return 1;
00111 }
00112
00113 int mimeIO::outputChar (char aChar)
00114 {
00115 cout << aChar;
00116 return 1;
00117 }
00118
00119
00120
00121
00122
00123
00124
00125 mimeIOQFile::mimeIOQFile (const QString & aName):
00126 mimeIO (),
00127 myFile (aName)
00128 {
00129 myFile.open (QIODevice::ReadOnly);
00130 }
00131
00132 mimeIOQFile::~mimeIOQFile ()
00133 {
00134 myFile.close ();
00135 }
00136
00137 int mimeIOQFile::outputLine (const QByteArray &, int)
00138 {
00139 return 0;
00140 }
00141
00142 int mimeIOQFile::inputLine (QByteArray & data)
00143 {
00144 data.resize( 1024 );
00145 myFile.readLine (data.data(), 1024);
00146
00147 return data.length ();
00148 }
00149
00150 mimeIOQString::mimeIOQString ()
00151 {
00152 }
00153
00154 mimeIOQString::~mimeIOQString ()
00155 {
00156 }
00157
00158 int mimeIOQString::outputLine (const QByteArray & _str, int len)
00159 {
00160 if (len == -1) {
00161 len = _str.length();
00162 }
00163 theString += _str;
00164 return len;
00165 }
00166
00167 int mimeIOQString::inputLine (QByteArray & _str)
00168 {
00169 if (theString.isEmpty ())
00170 return 0;
00171
00172 int i = theString.indexOf ('\n');
00173
00174 if (i == -1)
00175 return 0;
00176 _str = theString.left (i + 1).toLatin1 ();
00177 theString = theString.right (theString.length () - i - 1);
00178 return _str.length ();
00179 }