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 189 190 191
|
/*
** Copyright 2002, Double Precision Inc.
**
** See COPYING for distribution information.
*/
#include "libmail_config.h"
#include "file.H"
#include "unicode/unicode.h"
#include <ctype.h>
using namespace std;
/////////////////////////////////////////////////////////////////////////////
mail::file::file(int fdArg, const char *mode)
: fd(dup(fdArg)), fp(NULL), pos(-1)
{
if (fd >= 0)
fp=fdopen(fd, mode);
if (fp && fseek(fp, 0L, SEEK_SET) < 0)
{
fclose(fp);
fp=NULL;
}
}
mail::file::~file()
{
if (fp != NULL)
fclose(fp);
if (fd >= 0)
close(fd);
}
void mail::file::seeked()
{
if (fp)
pos=ftell(fp);
}
string mail::file::getline()
{
// First time through, allocate a 1000 bye buffer.
if (buffer.size() != 1000)
{
buffer.clear();
buffer.insert(buffer.end(), 1000, 0);
}
string l;
for (;;)
{
vector<char>::iterator b=buffer.begin(), e=buffer.end();
while (b != e)
{
int ch=getc(fp);
if (ch == EOF)
break;
if (pos != -1)
pos++;
*b++ = ch;
if (ch == '\n')
break;
}
if (b == buffer.begin())
break; // EOF
if (b[-1] != '\n')
{
l.insert(l.end(), buffer.begin(), b);
continue;
}
--b;
l.insert(l.end(), buffer.begin(), b);
break;
}
size_t n=l.size();
if (n > 0 && l[n-1] == '\r')
l=l.substr(0, n-1);
return l;
}
void mail::file::genericMessageRead(mail::account *account,
size_t messageNumber,
mail::readMode readType,
mail::callback::message &callback)
{
ptr<mail::account> ptrAccount(account);
if (readType == mail::readHeadersFolded
|| readType == mail::readHeaders)
{
string hdr="";
string l;
while (!ptrAccount.isDestroyed() && !feof(fp) && !ferror(fp))
{
l=getline();
if (l.size() == 0)
break;
if (unicode_isspace((unsigned char)l[0]))
{
const char *p=l.c_str();
if (readType == mail::readHeaders)
hdr += "\n";
else // Fold headers
{
hdr += " ";
while (*p &&
unicode_isspace((unsigned char)
*p))
p++;
}
hdr += p;
continue;
}
if (hdr.size() > 0) // Prev header.
{
hdr += "\n";
callback.messageTextCallback(messageNumber,
hdr);
}
hdr=l;
}
if (hdr.size() > 0 && !ptrAccount.isDestroyed())
{
hdr += "\n";
callback.messageTextCallback(messageNumber, hdr);
}
return;
}
if (readType == mail::readContents)
// Skip over the header portion.
{
while (!feof(fp) && !ferror(fp))
{
if (getline().size() == 0)
break;
}
}
vector<char> buffer;
buffer.insert(buffer.end(), BUFSIZ, 0);
size_t i=0;
int ch;
while (!ptrAccount.isDestroyed() && (ch=getc(fp)) != EOF)
{
if (ch == '\r')
continue;
if (i >= buffer.size())
{
callback.messageTextCallback(messageNumber,
string(buffer.begin(),
buffer.end()));
i=0;
}
buffer[i++]=ch;
}
if (i > 0 && !ptrAccount.isDestroyed())
callback.messageTextCallback(messageNumber,
string(buffer.begin(),
buffer.begin()+i));
}
|