1
0
mirror of https://github.com/aportelli/LatAnalyze.git synced 2025-04-10 19:20:44 +01: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 //////////////////////////////////////////////////////////////// // constructors ////////////////////////////////////////////////////////////////
File::File(void) File::File(void)
: name_("") : name_("")
, mode_(FileMode::null) , mode_(Mode::null)
, data_() , data_()
{} {}
@ -68,10 +68,18 @@ void File::deleteData(void)
data_.clear(); 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, AsciiFile::AsciiParserState::AsciiParserState(istream* stream, string* name,
IoDataTable* data) IoDataTable* data)
: ParserState<IoDataTable>(stream, name, data) : ParserState<IoDataTable>(stream, name, data)
@ -79,15 +87,12 @@ AsciiFile::AsciiParserState::AsciiParserState(istream* stream, string* name,
initScanner(); initScanner();
} }
// destructor ////////////////////////////////////////////////////////////////// // AsciiParserState destructor /////////////////////////////////////////////////
AsciiFile::AsciiParserState::~AsciiParserState(void) AsciiFile::AsciiParserState::~AsciiParserState(void)
{ {
destroyScanner(); destroyScanner();
} }
/******************************************************************************
* AsciiFile implementation *
******************************************************************************/
// constructor ///////////////////////////////////////////////////////////////// // constructor /////////////////////////////////////////////////////////////////
AsciiFile::AsciiFile(void) AsciiFile::AsciiFile(void)
: File(), fileStream_() : File(), fileStream_()
@ -97,13 +102,23 @@ AsciiFile::AsciiFile(void)
AsciiFile::AsciiFile(const string &name, const unsigned int mode) AsciiFile::AsciiFile(const string &name, const unsigned int mode)
{ {
openAscii(name, mode); open(name, mode);
} }
// destructor ////////////////////////////////////////////////////////////////// // destructor //////////////////////////////////////////////////////////////////
AsciiFile::~AsciiFile(void) 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 /////////////////////////////////////////////////////////////////////// // tests ///////////////////////////////////////////////////////////////////////
@ -115,7 +130,15 @@ bool AsciiFile::isOpen() const
// IO ////////////////////////////////////////////////////////////////////////// // IO //////////////////////////////////////////////////////////////////////////
void AsciiFile::close(void) void AsciiFile::close(void)
{ {
closeAscii(); delete state_;
state_ = NULL;
if (isOpen())
{
fileStream_.close();
}
name_ = "";
mode_ = Mode::null;
isParsed_ = false;
deleteData(); deleteData();
} }
@ -125,28 +148,27 @@ void AsciiFile::open(const string &name, const unsigned int mode)
{ {
LATAN_ERROR(Io, "file already opened with name '" + name_ + "'"); LATAN_ERROR(Io, "file already opened with name '" + name_ + "'");
} }
openAscii(name, mode); else
}
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())
{ {
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; name_ = name;
mode_ = mode; mode_ = mode;
isParsed_ = false; isParsed_ = false;
fileStream_.open(name_.c_str()); fileStream_.open(name_.c_str(), stdMode);
if (mode_ & FileMode::read) if (mode_ & Mode::read)
{ {
state_ = new AsciiParserState(&fileStream_, &name_, &data_); 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) void AsciiFile::load(const string &name __dumb)
{ {
if ((mode_ & FileMode::read)&&(isOpen())) if ((mode_ & Mode::read)&&(isOpen()))
{ {
if (!isParsed_) if (!isParsed_)
{ {

View File

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