kioslaves

mimeio.cc

Go to the documentation of this file.
00001 /***************************************************************************
00002                           mimeio.cc  -  description
00003                              -------------------
00004     begin                : Wed Oct 25 2000
00005     copyright            : (C) 2000 by Sven Carstens
00006     email                : s.carstens@gmx.de
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  ***************************************************************************/
00017 
00018 #include <iostream>
00019 using namespace std;
00020 
00021 #include "mimeio.h"
00022 
00023 mimeIO::mimeIO ()
00024 {
00025   theCRLF = "\r\n";
00026   crlfLen = 2;
00027 }
00028 
00029 mimeIO::~mimeIO ()
00030 {
00031 }
00032 
00033 int
00034 mimeIO::inputLine (QCString & aLine)
00035 {
00036   char input;
00037 
00038   aLine = (const char *) NULL;
00039   while (inputChar (input))
00040   {
00041     aLine += input;
00042     if (input == '\n')
00043       break;
00044   }
00045 //  cout << aLine.length() << " - " << aLine;
00046   return aLine.length ();
00047 }
00048 
00049 int
00050 mimeIO::outputLine (const QCString & aLine, int len)
00051 {
00052   int i;
00053 
00054   if (len == -1) {
00055     len = aLine.length();
00056   }
00057   int start = len;
00058   for (i = 0; i < start; i++)
00059     if (!outputChar (aLine[i]))
00060       break;
00061   return i;
00062 }
00063 
00064 int
00065 mimeIO::outputMimeLine (const QCString & inLine)
00066 {
00067   int retVal = 0;
00068   QCString aLine = inLine;
00069   int len = inLine.length();
00070 
00071   int theLF = aLine.findRev ('\n');
00072   if (theLF == len - 1 && theLF != -1)
00073   {
00074     //we have a trailing LF, now check for CR
00075     if (aLine[theLF - 1] == '\r')
00076       theLF--;
00077     //truncate the line
00078     aLine.truncate(theLF);
00079     len = theLF;
00080     theLF = -1;
00081   }
00082   //now truncate the line
00083   {
00084     int start, end, offset;
00085     start = 0;
00086     end = aLine.find ('\n', start);
00087     while (end >= 0)
00088     {
00089       offset = 1;
00090       if (end && aLine[end - 1] == '\r')
00091       {
00092         offset++;
00093         end--;
00094       }
00095       outputLine (aLine.mid (start, end - start) + theCRLF, end - start + crlfLen);
00096       start = end + offset;
00097       end = aLine.find ('\n', start);
00098     }
00099     outputLine (aLine.mid (start, len - start) + theCRLF, len - start + crlfLen);
00100   }
00101   return retVal;
00102 }
00103 
00104 int
00105 mimeIO::inputChar (char &aChar)
00106 {
00107   if (cin.eof ())
00108   {
00109 //    cout << "EOF" << endl;
00110     return 0;
00111   }
00112   cin.get (aChar);
00113   return 1;
00114 }
00115 
00116 int
00117 mimeIO::outputChar (char aChar)
00118 {
00119   cout << aChar;
00120   return 1;
00121 }
00122 
00123 void
00124 mimeIO::setCRLF (const char *aCRLF)
00125 {
00126   theCRLF = aCRLF;
00127   crlfLen = strlen(aCRLF);
00128 }
00129 
00130 mimeIOQFile::mimeIOQFile (const QString & aName):
00131 mimeIO (),
00132 myFile (aName)
00133 {
00134   myFile.open (IO_ReadOnly);
00135 }
00136 
00137 mimeIOQFile::~mimeIOQFile ()
00138 {
00139   myFile.close ();
00140 }
00141 
00142 int
00143 mimeIOQFile::outputLine (const QCString &, int)
00144 {
00145   return 0;
00146 }
00147 
00148 int
00149 mimeIOQFile::inputLine (QCString & data)
00150 {
00151   data.resize( 1024 );
00152   myFile.readLine (data.data(), 1024);
00153 
00154   return data.length ();
00155 }
00156 
00157 mimeIOQString::mimeIOQString ()
00158 {
00159 }
00160 
00161 mimeIOQString::~mimeIOQString ()
00162 {
00163 }
00164 
00165 int
00166 mimeIOQString::outputLine (const QCString & _str, int len)
00167 {
00168   if (len == -1) {
00169     len = _str.length();
00170   }
00171   theString += _str;
00172   return len;
00173 }
00174 
00175 int
00176 mimeIOQString::inputLine (QCString & _str)
00177 {
00178   if (theString.isEmpty ())
00179     return 0;
00180 
00181   int i = theString.find ('\n');
00182 
00183   if (i == -1)
00184     return 0;
00185   _str = theString.left (i + 1).latin1 ();
00186   theString = theString.right (theString.length () - i - 1);
00187   return _str.length ();
00188 }