2009-01-08 16:17:32 +01:00
|
|
|
#include "config.hpp"
|
|
|
|
#include <iostream>
|
|
|
|
#include <fstream>
|
|
|
|
#include <algorithm>
|
|
|
|
#include "fortran.hpp"
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace CosmoTool;
|
|
|
|
|
|
|
|
UnformattedRead::UnformattedRead(const string& fname)
|
2009-01-10 02:11:05 +01:00
|
|
|
throw(NoSuchFileException)
|
2009-01-08 16:17:32 +01:00
|
|
|
{
|
|
|
|
f = new ifstream(fname.c_str());
|
2009-01-10 02:11:05 +01:00
|
|
|
if (!*f)
|
|
|
|
throw NoSuchFileException();
|
|
|
|
|
2009-01-08 16:17:32 +01:00
|
|
|
swapOrdering = false;
|
|
|
|
cSize = Check_32bits;
|
|
|
|
checkPointRef = checkPointAccum = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
UnformattedRead::UnformattedRead(const char *fname)
|
2009-01-10 02:11:05 +01:00
|
|
|
throw(NoSuchFileException)
|
2009-01-08 16:17:32 +01:00
|
|
|
{
|
|
|
|
f = new ifstream(fname);
|
2009-01-10 02:11:05 +01:00
|
|
|
if (!*f)
|
|
|
|
throw NoSuchFileException();
|
2009-01-08 16:17:32 +01:00
|
|
|
swapOrdering = false;
|
|
|
|
cSize = Check_32bits;
|
|
|
|
checkPointRef = checkPointAccum = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
UnformattedRead::~UnformattedRead()
|
|
|
|
{
|
|
|
|
delete f;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Todo implement primitive description
|
|
|
|
void UnformattedRead::setOrdering(Ordering o)
|
|
|
|
{
|
|
|
|
if (o == LittleEndian)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void UnformattedRead::setCheckpointSize(CheckpointSize cs)
|
|
|
|
{
|
|
|
|
cSize = cs;
|
|
|
|
}
|
|
|
|
|
|
|
|
void UnformattedRead::beginCheckpoint()
|
|
|
|
throw (InvalidUnformattedAccess,EndOfFileException)
|
|
|
|
{
|
|
|
|
if (checkPointAccum != 0)
|
|
|
|
throw InvalidUnformattedAccess();
|
|
|
|
|
|
|
|
checkPointRef = (cSize == Check_32bits) ? 4 : 8;
|
|
|
|
checkPointAccum = 0;
|
|
|
|
|
|
|
|
checkPointRef = (cSize == Check_32bits) ? readInt32() : readInt64();
|
|
|
|
checkPointAccum = 0;
|
|
|
|
|
|
|
|
if (f->eof())
|
|
|
|
throw EndOfFileException();
|
|
|
|
}
|
|
|
|
|
|
|
|
void UnformattedRead::endCheckpoint()
|
|
|
|
throw (InvalidUnformattedAccess)
|
|
|
|
{
|
|
|
|
if (checkPointRef != checkPointAccum)
|
|
|
|
throw InvalidUnformattedAccess();
|
|
|
|
|
|
|
|
int64_t oldCheckPoint = checkPointRef;
|
|
|
|
|
|
|
|
checkPointRef = (cSize == Check_32bits) ? 4 : 8;
|
|
|
|
checkPointAccum = 0;
|
|
|
|
checkPointRef = (cSize == Check_32bits) ? readInt32() : readInt64();
|
|
|
|
|
|
|
|
if (oldCheckPoint != checkPointRef)
|
|
|
|
throw InvalidUnformattedAccess();
|
|
|
|
|
|
|
|
checkPointAccum = checkPointRef = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void UnformattedRead::readOrderedBuffer(void *buffer, int size)
|
|
|
|
throw (InvalidUnformattedAccess)
|
|
|
|
{
|
|
|
|
if ((checkPointAccum+size) > checkPointRef)
|
|
|
|
throw InvalidUnformattedAccess();
|
|
|
|
|
|
|
|
f->read((char *)buffer, size);
|
|
|
|
|
|
|
|
if (swapOrdering)
|
|
|
|
{
|
|
|
|
char *cb = (char *)buffer;
|
|
|
|
for (int i = 0; i < size/2; i++)
|
|
|
|
swap(cb[i], cb[size-i-1]);
|
|
|
|
}
|
|
|
|
checkPointAccum += size;
|
|
|
|
}
|
|
|
|
|
|
|
|
double UnformattedRead::readReal64()
|
|
|
|
throw (InvalidUnformattedAccess)
|
|
|
|
{
|
|
|
|
union
|
|
|
|
{
|
|
|
|
char b[8];
|
|
|
|
double d;
|
|
|
|
} a;
|
|
|
|
|
|
|
|
readOrderedBuffer(&a, 8);
|
|
|
|
|
|
|
|
return a.d;
|
|
|
|
}
|
|
|
|
|
|
|
|
float UnformattedRead::readReal32()
|
|
|
|
throw (InvalidUnformattedAccess)
|
|
|
|
{
|
|
|
|
union
|
|
|
|
{
|
|
|
|
char b[4];
|
|
|
|
float f;
|
|
|
|
} a;
|
|
|
|
|
|
|
|
readOrderedBuffer(&a, 4);
|
|
|
|
|
|
|
|
return a.f;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t UnformattedRead::readInt32()
|
|
|
|
throw (InvalidUnformattedAccess)
|
|
|
|
{
|
|
|
|
union
|
|
|
|
{
|
|
|
|
char b[4];
|
|
|
|
int32_t i;
|
|
|
|
} a;
|
|
|
|
|
|
|
|
readOrderedBuffer(&a, 4);
|
|
|
|
|
|
|
|
return a.i;
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t UnformattedRead::readInt64()
|
|
|
|
throw (InvalidUnformattedAccess)
|
|
|
|
{
|
|
|
|
union
|
|
|
|
{
|
|
|
|
char b[8];
|
|
|
|
int64_t i;
|
|
|
|
} a;
|
|
|
|
|
|
|
|
readOrderedBuffer(&a, 8);
|
|
|
|
|
|
|
|
return a.i;
|
|
|
|
}
|