summaryrefslogtreecommitdiffstats
path: root/kioslaves/imap4/mimeio.cc
blob: 9325df13085547adb11c039820f50814b45176a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/***************************************************************************
                          mimeio.cc  -  description
                             -------------------
    begin                : Wed Oct 25 2000
    copyright            : (C) 2000 by Sven Carstens
    email                : s.carstens@gmx.de
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include <iostream>
using namespace std;

#include "mimeio.h"

mimeIO::mimeIO ()
{
  theCRLF = "\r\n";
  crlfLen = 2;
}

mimeIO::~mimeIO ()
{
}

int
mimeIO::inputLine (TQCString & aLine)
{
  char input;

  aLine = (const char *) NULL;
  while (inputChar (input))
  {
    aLine += input;
    if (input == '\n')
      break;
  }
//  cout << aLine.length() << " - " << aLine;
  return aLine.length ();
}

int
mimeIO::outputLine (const TQCString & aLine, int len)
{
  int i;

  if (len == -1) {
    len = aLine.length();
  }
  int start = len;
  for (i = 0; i < start; i++)
    if (!outputChar (aLine[i]))
      break;
  return i;
}

int
mimeIO::outputMimeLine (const TQCString & inLine)
{
  int retVal = 0;
  TQCString aLine = inLine;
  int len = inLine.length();

  int theLF = aLine.tqfindRev ('\n');
  if (theLF == len - 1 && theLF != -1)
  {
    //we have a trailing LF, now check for CR
    if (aLine[theLF - 1] == '\r')
      theLF--;
    //truncate the line
    aLine.truncate(theLF);
    len = theLF;
    theLF = -1;
  }
  //now truncate the line
  {
    int start, end, offset;
    start = 0;
    end = aLine.tqfind ('\n', start);
    while (end >= 0)
    {
      offset = 1;
      if (end && aLine[end - 1] == '\r')
      {
        offset++;
        end--;
      }
      outputLine (aLine.mid (start, end - start) + theCRLF, end - start + crlfLen);
      start = end + offset;
      end = aLine.tqfind ('\n', start);
    }
    outputLine (aLine.mid (start, len - start) + theCRLF, len - start + crlfLen);
  }
  return retVal;
}

int
mimeIO::inputChar (char &aChar)
{
  if (cin.eof ())
  {
//    cout << "EOF" << endl;
    return 0;
  }
  cin.get (aChar);
  return 1;
}

int
mimeIO::outputChar (char aChar)
{
  cout << aChar;
  return 1;
}

void
mimeIO::setCRLF (const char *aCRLF)
{
  theCRLF = aCRLF;
  crlfLen = strlen(aCRLF);
}

mimeIOTQFile::mimeIOTQFile (const TQString & aName):
mimeIO (),
myFile (aName)
{
  myFile.open (IO_ReadOnly);
}

mimeIOTQFile::~mimeIOTQFile ()
{
  myFile.close ();
}

int
mimeIOTQFile::outputLine (const TQCString &, int)
{
  return 0;
}

int
mimeIOTQFile::inputLine (TQCString & data)
{
  data.resize( 1024 );
  myFile.readLine (data.data(), 1024);

  return data.length ();
}

mimeIOTQString::mimeIOTQString ()
{
}

mimeIOTQString::~mimeIOTQString ()
{
}

int
mimeIOTQString::outputLine (const TQCString & _str, int len)
{
  if (len == -1) {
    len = _str.length();
  }
  theString += _str;
  return len;
}

int
mimeIOTQString::inputLine (TQCString & _str)
{
  if (theString.isEmpty ())
    return 0;

  int i = theString.tqfind ('\n');

  if (i == -1)
    return 0;
  _str = theString.left (i + 1).latin1 ();
  theString = theString.right (theString.length () - i - 1);
  return _str.length ();
}