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 return i;
00059 }
00060
00061 int mimeIO::outputMimeLine (const QByteArray & inLine)
00062 {
00063 int retVal = 0;
00064 QByteArray aLine = inLine;
00065 int len = inLine.length();
00066
00067 int theLF = aLine.lastIndexOf ('\n');
00068 if (theLF == len - 1 && theLF != -1)
00069 {
00070
00071 if (aLine[theLF - 1] == '\r')
00072 theLF--;
00073
00074 aLine.truncate(theLF);
00075 len = theLF;
00076 theLF = -1;
00077 }
00078
00079 {
00080 int start, end, offset;
00081 start = 0;
00082 end = aLine.indexOf ('\n', start);
00083 while (end >= 0)
00084 {
00085 offset = 1;
00086 if (end && aLine[end - 1] == '\r')
00087 {
00088 offset++;
00089 end--;
00090 }
00091 outputLine (aLine.mid (start, end - start) + theCRLF, end - start + crlfLen);
00092 start = end + offset;
00093 end = aLine.indexOf ('\n', start);
00094 }
00095 outputLine (aLine.mid (start, len - start) + theCRLF, len - start + crlfLen);
00096 }
00097 return retVal;
00098 }
00099
00100 int mimeIO::inputChar (char &aChar)
00101 {
00102 if (cin.eof ())
00103 {
00104
00105 return 0;
00106 }
00107 cin.get (aChar);
00108 return 1;
00109 }
00110
00111 int mimeIO::outputChar (char aChar)
00112 {
00113 cout << aChar;
00114 return 1;
00115 }
00116
00117
00118
00119
00120
00121
00122
00123 mimeIOQFile::mimeIOQFile (const QString & aName):
00124 mimeIO (),
00125 myFile (aName)
00126 {
00127 myFile.open (QIODevice::ReadOnly);
00128 }
00129
00130 mimeIOQFile::~mimeIOQFile ()
00131 {
00132 myFile.close ();
00133 }
00134
00135 int mimeIOQFile::outputLine (const QByteArray &, int)
00136 {
00137 return 0;
00138 }
00139
00140 int mimeIOQFile::inputLine (QByteArray & data)
00141 {
00142 data.resize( 1024 );
00143 myFile.readLine (data.data(), 1024);
00144
00145 return data.length ();
00146 }
00147
00148 mimeIOQString::mimeIOQString ()
00149 {
00150 }
00151
00152 mimeIOQString::~mimeIOQString ()
00153 {
00154 }
00155
00156 int mimeIOQString::outputLine (const QByteArray & _str, int len)
00157 {
00158 if (len == -1) {
00159 len = _str.length();
00160 }
00161 theString += _str;
00162 return len;
00163 }
00164
00165 int mimeIOQString::inputLine (QByteArray & _str)
00166 {
00167 if (theString.isEmpty ())
00168 return 0;
00169
00170 int i = theString.indexOf ('\n');
00171
00172 if (i == -1)
00173 return 0;
00174 _str = theString.left (i + 1).toLatin1 ();
00175 theString = theString.right (theString.length () - i - 1);
00176 return _str.length ();
00177 }