Added fortran unformatted file support

This commit is contained in:
Guilhem Lavaux 2009-01-08 09:17:32 -06:00
parent 4862035315
commit 453f5b0a2d
2 changed files with 216 additions and 0 deletions

152
src/fortran.cpp Normal file
View File

@ -0,0 +1,152 @@
#include "config.hpp"
#include <iostream>
#include <fstream>
#include <algorithm>
#include "fortran.hpp"
using namespace std;
using namespace CosmoTool;
UnformattedRead::UnformattedRead(const string& fname)
throw()
{
f = new ifstream(fname.c_str());
swapOrdering = false;
cSize = Check_32bits;
checkPointRef = checkPointAccum = 0;
}
UnformattedRead::UnformattedRead(const char *fname)
throw()
{
f = new ifstream(fname);
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;
}

64
src/fortran.hpp Normal file
View File

@ -0,0 +1,64 @@
#ifndef __COSMO_FORTRAN_HPP
#define __COSMO_FORTRAN_HPP
#include <string>
#include <stdint.h>
#include <inttypes.h>
#include <iostream>
#include "config.hpp"
namespace CosmoTool
{
class InvalidUnformattedAccess : public Exception
{
};
class UnformattedRead
{
public:
enum Ordering {
LittleEndian, BigEndian
};
enum CheckpointSize {
Check_32bits, Check_64bits
};
UnformattedRead(const std::string& fname)
throw ();
UnformattedRead(const char *fname)
throw ();
~UnformattedRead();
// Todo implement primitive description
void setOrdering(Ordering o);
void setCheckpointSize(CheckpointSize cs);
void beginCheckpoint()
throw (InvalidUnformattedAccess,EndOfFileException);
void endCheckpoint()
throw (InvalidUnformattedAccess);
double readReal64()
throw (InvalidUnformattedAccess);
float readReal32()
throw (InvalidUnformattedAccess);
int32_t readInt32()
throw (InvalidUnformattedAccess);
int64_t readInt64()
throw (InvalidUnformattedAccess);
protected:
bool swapOrdering;
CheckpointSize cSize;
uint64_t checkPointRef;
uint64_t checkPointAccum;
std::ifstream *f;
void readOrderedBuffer(void *buffer, int size)
throw (InvalidUnformattedAccess);
};
};
#endif