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 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
|
// Copyright (c) 1999 Peter Karlsson
//
// $Id: mypointread.cpp,v 1.7 1999/07/18 13:08:14 peter Exp $
//
// 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, version 2
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#include <string>
#include <iostream.h>
#include <time.h>
#include <stdio.h>
#include "mypointread.h"
#if defined(UNIX)
# define DIRSEP '/'
#else
# define DIRSEP '\\'
#endif
MyPointRead::MyPointRead(const char *path, unsigned areanum)
{
areapath = strdup(path);
areanumber = areanum;
}
MyPointRead::~MyPointRead()
{
if (areapath) delete areapath;
}
bool MyPointRead::Transfer(time_t starttime, StatEngine &destination)
{
// Check that we got the path correctly in initialization
if (!areapath)
{
cerr << "Internal error: Area path was not allocated properly" << endl;
return false;
}
// Calculate area number ending
char ending[4] = { 0, 0, 0, 0};
ending[0] = 'A';
ending[1] = '0' + areanumber / 26;
ending[2] = 'A' + areanumber % 26;
// Sanity check
if (ending[1] > '9' || ending[2] > 'Z')
{
cerr << "Illegal area number" << endl;
return false;
}
// Open the message area files
string basepath = areapath;
if (DIRSEP != basepath[basepath.length() - 1])
basepath += DIRSEP;
basepath += "MYPOINT.";
string filepath = basepath + ending;
FILE *areaf = fopen(filepath.c_str(), "rb");
if (!areaf)
{
cerr << "Error: Cannot open " << areapath << endl;
return false;
}
ending[0] = 'F';
filepath = basepath + ending;
FILE *flagf = fopen(filepath.c_str(), "rb");
if (!flagf)
{
cerr << "Error: Cannot open " << areapath << endl;
return false;
}
header_s hdr;
fread(&hdr, sizeof (hdr), 1, areaf);
// Check for valid message base versions
UINT8 msgbaserevision = hdr.areaprf[0];
if (Mypoint_msgbaseversion2 != msgbaserevision &&
Mypoint_msgbaseversion3 != msgbaserevision)
{
cerr << "Error: Illegal MyPoint message base version: "
<< msgbaserevision << endl;
return false;
}
fseek(flagf, sizeof (flags_s), SEEK_SET);
// Check size of ltrhdr structure;
size_t sizeof_ltrhdr, sizeof_ltrtrl;
ltrhdr_u ltrhdr;
ltrtrl_u ltrtrl;
if (Mypoint_msgbaseversion2 == msgbaserevision)
{
sizeof_ltrhdr = sizeof (ltrhdr.version2);
sizeof_ltrtrl = sizeof (ltrtrl.version2);
}
else //if (Mypoint_msgbaseversion3 == msgbaserevision)
{
sizeof_ltrhdr = sizeof (ltrhdr.version3);
sizeof_ltrtrl = sizeof (ltrtrl.version3);
}
// Seek past dummy message trailer
fseek(areaf, sizeof_ltrtrl, SEEK_CUR);
// Read messages one by one
bool stay = true;
flags_s flags;
unsigned msgnum = 0;
bool iskludge, wascr;
unsigned tosize, fromsize, subjsize, bodysize;
char *ctrlbuf, *buf, *to_p, *from_p, *sub_p,
*ctrl_p, *body_p, *newbody_p;
UINT16 fromp, subjp, textp, ltrsiz, ltrtrl_ltrsiz;
UINT32 delim, arrtim, ltrtim;
while (stay)
{
if (1 != fread(<rhdr, sizeof_ltrhdr, 1, areaf))
{
// Nothing more to read, we are done
stay = false;
break;
}
fread(&flags, sizeof (flags), 1, flagf);
cout << msgnum << " messages done\r";
msgnum ++;
if (Mypoint_msgbaseversion2 == msgbaserevision)
{
delim = ltrhdr.version2.delim;
arrtim= ltrhdr.version2.arrtim;
ltrtim= ltrhdr.version2.ltrtim;
fromp = ltrhdr.version2.fromp;
subjp = ltrhdr.version2.subjp;
textp = ltrhdr.version2.textp;
ltrsiz= ltrhdr.version2.ltrsiz;
}
else //if (Mypoint_msgbaseversion3 == msgbaserevision)
{
delim = ltrhdr.version3.delim;
arrtim= ltrhdr.version3.arrtim;
ltrtim= ltrhdr.version3.ltrtim;
fromp = ltrhdr.version3.fromp;
subjp = ltrhdr.version3.subjp;
textp = ltrhdr.version3.textp;
ltrsiz= ltrhdr.version3.ltrsiz;
}
if (delim != Mypoint_delimeter)
{
// Something went wrong
cerr << "Message area garbled (illegal delimeter)!" << endl;
fclose(areaf);
return false;
}
// Only proceed if message is inside interval and not deleted
if ((time_t) arrtim >= starttime &&
0 == (flags.bits & Mypoint_delete))
{
// Retrieve sender, recipient, subject
tosize = fromp - sizeof_ltrhdr;
fromsize = subjp - fromp;
subjsize = textp - subjp;
bodysize = ltrsiz- textp - sizeof_ltrtrl;
to_p = new char[tosize ];
from_p = new char[fromsize];
sub_p = new char[subjsize];
buf = new char[bodysize];
ctrlbuf = new char[bodysize];
if (!buf || !ctrlbuf || !to_p || !from_p || !sub_p)
{
cerr << "Unable to allocate memory for message body (msg #"
<< msgnum << ')' << endl;
if (to_p) delete to_p;
if (from_p) delete from_p;
if (sub_p) delete sub_p;
if (buf) delete buf;
if (ctrlbuf) delete ctrlbuf;
goto out;
}
fread(to_p, tosize, 1, areaf);
fread(from_p, fromsize, 1, areaf);
fread(sub_p, subjsize, 1, areaf);
fread(buf, bodysize, 1, areaf); // Text + kludges
fread(<rtrl, sizeof_ltrtrl, 1, areaf);
if (Mypoint_msgbaseversion2 == msgbaserevision)
{
ltrtrl_ltrsiz = ltrtrl.version2.ltrsiz;
}
else //if (Mypoint_msgbaseversion3 == msgbaserevision)
{
ltrtrl_ltrsiz = ltrtrl.version3.ltrsiz;
}
// Check for inconsistencies
if (ltrtrl_ltrsiz != ltrsiz)
{
cerr << "Message area garbled (footer does not match header)!"
<< endl;
fclose(areaf);
return false;
}
// Copy out kludges and body to separate buffers
// kludges go into ctrlbuf, body stays in buf, but is
// relocated.
ctrl_p = ctrlbuf;
body_p = buf;
newbody_p = buf;
iskludge = false;
wascr = true;
while (*body_p)
{
if (wascr && 1 == *body_p)
iskludge = true;
if (iskludge)
{
// We don't CR/LF in the kludge buffer
if ('\r' != *body_p && '\n' != *body_p)
*(ctrl_p ++) = *body_p;
}
else
*(newbody_p ++) = *body_p;
if ('\r' == *body_p || '\n' == *body_p)
{
iskludge = false;
wascr = true;
}
else
wascr = false;
body_p ++;
}
// Zero terminate what we got
*ctrl_p = 0;
*newbody_p = 0;
destination.AddData(string(from_p),
string(to_p),
string(sub_p),
string(ctrlbuf), string(buf),
ltrtim,
arrtim);
// Deallocate
delete buf;
delete ctrlbuf;
delete to_p;
delete sub_p;
delete from_p;
}
else
{
// Seek past letter structure
fseek(areaf, ltrsiz - sizeof_ltrhdr, SEEK_CUR);
}
out:;
}
fclose(areaf);
fclose(flagf);
return true;
}
|