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
|
// Copyright (c) 1999-2008 Peter Karlsson
//
// 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 <config.h>
#include <stdlib.h>
#include <string>
#ifdef HAVE_IOSTREAM
# include <iostream>
#else
# include <iostream.h>
#endif
#include <time.h>
#include <stdio.h>
#include <string.h>
#include "mypointread.h"
#include "utility.h"
#include "statengine.h"
#include "output.h"
MyPointRead::MyPointRead(const char *path, unsigned areanum)
{
areapath = strdup(path);
areanumber = areanum;
}
MyPointRead::~MyPointRead()
{
if (areapath) free(areapath);
}
bool MyPointRead::Transfer(time_t starttime, time_t endtime,
StatEngine &destination)
{
// Get the output object
TDisplay *display = TDisplay::GetOutputObject();
// Check that we got the path correctly in initialization
if (!areapath)
{
display->InternalErrorQuit(TDisplay::area_not_allocated, 1);
}
// 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')
{
display->ErrorMessage(TDisplay::illegal_area);
return false;
}
// Open the message area files
string basepath = areapath;
#ifdef BACKSLASH_PATHS
if (basepath[basepath.length() - 1] != '/' &&
basepath[basepath.length() - 1] != '\\')
basepath += '\\';
#else
if (basepath[basepath.length() - 1] != '/')
basepath += '/';
#endif
basepath += "MYPOINT.";
string filepath = basepath + ending;
FILE *areaf = fopen(filepath.c_str(), "rb");
if (!areaf)
{
display->ErrorMessage(TDisplay::cannot_open, filepath);
return false;
}
ending[0] = 'F';
filepath = basepath + ending;
FILE *flagf = fopen(filepath.c_str(), "rb");
if (!flagf)
{
display->ErrorMessage(TDisplay::cannot_open, filepath);
return false;
}
header_s hdr;
fread(&hdr, sizeof (hdr), 1, areaf);
// Check for valid message base versions
uint8_t msgbaserevision = hdr.areaprf[0];
if (Mypoint_msgbaseversion2 != msgbaserevision &&
Mypoint_msgbaseversion3 != msgbaserevision)
{
display->ErrorMessage(TDisplay::illegal_mypoint_version,
msgbaserevision);
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, static_cast<long>(sizeof_ltrtrl), SEEK_CUR);
// Read messages one by one
bool stay = true;
flags_s flags;
unsigned msgnum = 0;
size_t tosize, fromsize, subjsize, bodysize;
char *ctrlbuf, *buf, *to_p, *from_p, *sub_p;
uint16_t fromp, subjp, textp, ltrsiz, ltrtrl_ltrsiz;
uint32_t delim, arrtim, ltrtim;
display->SetMessagesTotal(-1);
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);
display->UpdateProgress(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
display->ErrorMessage(TDisplay::mypoint_area_garbled);
fclose(areaf);
return false;
}
// Only proceed if message is inside interval and not deleted
if (time_t(arrtim) >= starttime &&
time_t(arrtim) <= endtime &&
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)
{
display->WarningMessage(TDisplay::cannot_allocate_body,
msgnum);
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)
{
display->ErrorMessage(TDisplay::mypoint_area_garbled_2);
fclose(areaf);
return false;
}
// Separate kludges from text
fixupctrlbuffer(buf, ctrlbuf);
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, static_cast<long>(ltrsiz - sizeof_ltrhdr), SEEK_CUR);
}
out:;
}
fclose(areaf);
fclose(flagf);
return true;
}
|