1
0
mirror of https://github.com/aportelli/LatAnalyze.git synced 2024-11-10 00:45:36 +00:00

AsciiFile: save DMat implemented

This commit is contained in:
Antonin Portelli 2014-02-10 11:20:40 +00:00
parent b2dea6a620
commit c62555aa09
2 changed files with 61 additions and 56 deletions

View File

@ -29,7 +29,7 @@ using namespace Latan;
// constructors ////////////////////////////////////////////////////////////////
File::File(void)
: name_("")
, mode_(FileMode::null)
, mode_(Mode::null)
, data_()
{}
@ -68,10 +68,18 @@ void File::deleteData(void)
data_.clear();
}
void File::checkWritability(void)
{
if (!((mode_ & Mode::write)||(mode_ & Mode::append))||!isOpen())
{
LATAN_ERROR(Io, "file '" + name_ + "' is not writable");
}
}
/******************************************************************************
* AsciiParserState implementation *
* AsciiFile implementation *
******************************************************************************/
// constructor /////////////////////////////////////////////////////////////////
// AsciiParserState constructor ////////////////////////////////////////////////
AsciiFile::AsciiParserState::AsciiParserState(istream* stream, string* name,
IoDataTable* data)
: ParserState<IoDataTable>(stream, name, data)
@ -79,15 +87,12 @@ AsciiFile::AsciiParserState::AsciiParserState(istream* stream, string* name,
initScanner();
}
// destructor //////////////////////////////////////////////////////////////////
// AsciiParserState destructor /////////////////////////////////////////////////
AsciiFile::AsciiParserState::~AsciiParserState(void)
{
destroyScanner();
}
/******************************************************************************
* AsciiFile implementation *
******************************************************************************/
// constructor /////////////////////////////////////////////////////////////////
AsciiFile::AsciiFile(void)
: File(), fileStream_()
@ -97,13 +102,23 @@ AsciiFile::AsciiFile(void)
AsciiFile::AsciiFile(const string &name, const unsigned int mode)
{
openAscii(name, mode);
open(name, mode);
}
// destructor //////////////////////////////////////////////////////////////////
AsciiFile::~AsciiFile(void)
{
closeAscii();
close();
}
// access //////////////////////////////////////////////////////////////////////
void AsciiFile::save(const DMat &m, const std::string &name)
{
checkWritability();
fileStream_ << "#L latan_begin mat " << name << endl;
fileStream_ << m.cols() << endl;
fileStream_ << m << endl;
fileStream_ << "#L latan_end mat " << endl;
}
// tests ///////////////////////////////////////////////////////////////////////
@ -115,7 +130,15 @@ bool AsciiFile::isOpen() const
// IO //////////////////////////////////////////////////////////////////////////
void AsciiFile::close(void)
{
closeAscii();
delete state_;
state_ = NULL;
if (isOpen())
{
fileStream_.close();
}
name_ = "";
mode_ = Mode::null;
isParsed_ = false;
deleteData();
}
@ -125,28 +148,27 @@ void AsciiFile::open(const string &name, const unsigned int mode)
{
LATAN_ERROR(Io, "file already opened with name '" + name_ + "'");
}
openAscii(name, mode);
}
void AsciiFile::save(void)
{
LATAN_ERROR(Implementation, "saving Ascii files not implemented yet");
}
void AsciiFile::saveAs(const string &name __dumb)
{
LATAN_ERROR(Implementation, "saving Ascii files not implemented yet");
}
void AsciiFile::openAscii(const string &name, const unsigned int mode)
{
if (!isOpen())
else
{
ios_base::openmode stdMode = 0;
if (mode & Mode::write)
{
stdMode |= ios::out|ios::trunc;
}
if (mode & Mode::read)
{
stdMode |= ios::in;
}
if (mode & Mode::append)
{
stdMode |= ios::out|ios::app;
}
name_ = name;
mode_ = mode;
isParsed_ = false;
fileStream_.open(name_.c_str());
if (mode_ & FileMode::read)
fileStream_.open(name_.c_str(), stdMode);
if (mode_ & Mode::read)
{
state_ = new AsciiParserState(&fileStream_, &name_, &data_);
}
@ -157,25 +179,9 @@ void AsciiFile::openAscii(const string &name, const unsigned int mode)
}
}
void AsciiFile::closeAscii(void)
{
if (state_)
{
delete state_;
state_ = NULL;
}
if (isOpen())
{
fileStream_.close();
}
name_ = "";
mode_ = FileMode::null;
isParsed_ = false;
}
void AsciiFile::load(const string &name __dumb)
{
if ((mode_ & FileMode::read)&&(isOpen()))
if ((mode_ & Mode::read)&&(isOpen()))
{
if (!isParsed_)
{

View File

@ -36,12 +36,12 @@ BEGIN_NAMESPACE
/******************************************************************************
* Generic datafile class *
******************************************************************************/
typedef std::map<std::string, IoObject*> IoDataTable;
typedef std::map<std::string, IoObject *> IoDataTable;
class File
{
public:
class FileMode
class Mode
{
public:
enum
@ -62,14 +62,13 @@ public:
std::string getName(void) const;
unsigned int getMode(void) const;
template <typename IoT>
const IoT& read(const std::string &name);
const IoT & read(const std::string &name);
virtual void save(const DMat &m, const std::string &name) = 0;
// tests
virtual bool isOpen(void) const = 0;
// IO
virtual void close(void) = 0;
virtual void open(const std::string &name, const unsigned int mode) = 0;
virtual void save(void) = 0;
virtual void saveAs(const std::string &name) = 0;
protected:
// data access
void deleteData(void);
@ -77,6 +76,8 @@ protected:
const IoT& getData(const std::string &name) const;
// IO
virtual void load(const std::string &name) = 0;
// error checking
void checkWritability(void);
protected:
std::string name_;
unsigned int mode_;
@ -117,8 +118,8 @@ public:
{
public:
// constructor
explicit AsciiParserState(std::istream* stream, std::string* name,\
IoDataTable* data);
explicit AsciiParserState(std::istream *stream, std::string *name,\
IoDataTable *data);
// destructor
virtual ~AsciiParserState(void);
// public members
@ -135,17 +136,15 @@ public:
AsciiFile(const std::string &name, const unsigned int mode);
// destructor
virtual ~AsciiFile(void);
// access
virtual void save(const DMat &m, const std::string &name);
// tests
virtual bool isOpen(void) const;
// IO
virtual void close(void);
virtual void open(const std::string &name, const unsigned int mode);
virtual void save(void);
virtual void saveAs(const std::string &name);
private:
// IO
void openAscii(const std::string &name, const unsigned int mode);
void closeAscii(void);
virtual void load(const std::string &name);
// parser
void parse(void);