2014-02-06 18:52:13 +00:00
|
|
|
/*
|
|
|
|
* Io.cpp, part of LatAnalyze 3
|
|
|
|
*
|
|
|
|
* Copyright (C) 2013 - 2014 Antonin Portelli
|
|
|
|
*
|
|
|
|
* LatAnalyze 3 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, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* LatAnalyze 3 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 LatAnalyze 3. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2014-01-22 15:57:47 +00:00
|
|
|
#include <latan/Io.hpp>
|
|
|
|
#include <latan/includes.hpp>
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace Latan;
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* File implementation *
|
|
|
|
******************************************************************************/
|
|
|
|
// constructors ////////////////////////////////////////////////////////////////
|
|
|
|
File::File(void)
|
|
|
|
: name_("")
|
2014-02-10 11:20:40 +00:00
|
|
|
, mode_(Mode::null)
|
2014-01-22 15:57:47 +00:00
|
|
|
, data_()
|
|
|
|
{}
|
|
|
|
|
2014-02-06 18:08:46 +00:00
|
|
|
File::File(const string &name, const unsigned int mode)
|
2014-01-22 15:57:47 +00:00
|
|
|
: name_(name)
|
|
|
|
, mode_(mode)
|
|
|
|
, data_()
|
|
|
|
{}
|
|
|
|
|
|
|
|
// destructor //////////////////////////////////////////////////////////////////
|
|
|
|
File::~File(void)
|
2014-02-06 18:08:46 +00:00
|
|
|
{
|
|
|
|
deleteData();
|
|
|
|
}
|
2014-01-22 15:57:47 +00:00
|
|
|
|
|
|
|
// access //////////////////////////////////////////////////////////////////////
|
|
|
|
string File::getName(void) const
|
|
|
|
{
|
|
|
|
return name_;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int File::getMode(void) const
|
|
|
|
{
|
|
|
|
return mode_;
|
|
|
|
}
|
|
|
|
|
|
|
|
// internal functions //////////////////////////////////////////////////////////
|
|
|
|
void File::deleteData(void)
|
|
|
|
{
|
2014-02-13 19:23:39 +00:00
|
|
|
for (auto &i : data_)
|
2014-01-22 15:57:47 +00:00
|
|
|
{
|
2014-02-13 19:23:39 +00:00
|
|
|
i.second.reset();
|
2014-01-22 15:57:47 +00:00
|
|
|
}
|
|
|
|
data_.clear();
|
|
|
|
}
|
|
|
|
|
2014-02-10 11:20:40 +00:00
|
|
|
void File::checkWritability(void)
|
|
|
|
{
|
|
|
|
if (!((mode_ & Mode::write)||(mode_ & Mode::append))||!isOpen())
|
|
|
|
{
|
|
|
|
LATAN_ERROR(Io, "file '" + name_ + "' is not writable");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-22 15:57:47 +00:00
|
|
|
/******************************************************************************
|
2014-02-10 11:20:40 +00:00
|
|
|
* AsciiFile implementation *
|
2014-01-22 15:57:47 +00:00
|
|
|
******************************************************************************/
|
2014-02-10 11:20:40 +00:00
|
|
|
// AsciiParserState constructor ////////////////////////////////////////////////
|
2014-02-13 19:23:39 +00:00
|
|
|
AsciiFile::AsciiParserState::AsciiParserState(istream *stream, string *name,
|
|
|
|
IoDataTable *data)
|
2014-01-22 15:57:47 +00:00
|
|
|
: ParserState<IoDataTable>(stream, name, data)
|
|
|
|
{
|
|
|
|
initScanner();
|
|
|
|
}
|
|
|
|
|
2014-02-10 11:20:40 +00:00
|
|
|
// AsciiParserState destructor /////////////////////////////////////////////////
|
2014-02-06 18:08:46 +00:00
|
|
|
AsciiFile::AsciiParserState::~AsciiParserState(void)
|
2014-01-22 15:57:47 +00:00
|
|
|
{
|
|
|
|
destroyScanner();
|
|
|
|
}
|
|
|
|
|
|
|
|
// constructor /////////////////////////////////////////////////////////////////
|
|
|
|
AsciiFile::AsciiFile(void)
|
|
|
|
: File(), fileStream_()
|
|
|
|
, isParsed_(false)
|
2014-02-13 19:23:39 +00:00
|
|
|
, state_(nullptr)
|
2014-01-22 15:57:47 +00:00
|
|
|
{}
|
|
|
|
|
2014-02-06 18:08:46 +00:00
|
|
|
AsciiFile::AsciiFile(const string &name, const unsigned int mode)
|
2014-01-22 15:57:47 +00:00
|
|
|
{
|
2014-02-10 11:20:40 +00:00
|
|
|
open(name, mode);
|
2014-01-22 15:57:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// destructor //////////////////////////////////////////////////////////////////
|
|
|
|
AsciiFile::~AsciiFile(void)
|
|
|
|
{
|
2014-02-10 11:20:40 +00:00
|
|
|
close();
|
|
|
|
}
|
|
|
|
|
|
|
|
// access //////////////////////////////////////////////////////////////////////
|
|
|
|
void AsciiFile::save(const DMat &m, const std::string &name)
|
|
|
|
{
|
|
|
|
checkWritability();
|
2014-02-10 16:01:39 +00:00
|
|
|
isParsed_ = false;
|
2014-02-10 11:20:40 +00:00
|
|
|
fileStream_ << "#L latan_begin mat " << name << endl;
|
|
|
|
fileStream_ << m.cols() << endl;
|
|
|
|
fileStream_ << m << endl;
|
|
|
|
fileStream_ << "#L latan_end mat " << endl;
|
2014-01-22 15:57:47 +00:00
|
|
|
}
|
|
|
|
|
2014-02-10 16:01:39 +00:00
|
|
|
void AsciiFile::save(const RandGen::State &state, const std::string &name)
|
|
|
|
{
|
|
|
|
checkWritability();
|
|
|
|
isParsed_ = false;
|
|
|
|
fileStream_ << "#L latan_begin rg_state " << name << endl;
|
|
|
|
fileStream_ << state << endl;
|
|
|
|
fileStream_ << "#L latan_end rg_state " << endl;
|
|
|
|
}
|
|
|
|
|
2014-01-22 15:57:47 +00:00
|
|
|
// tests ///////////////////////////////////////////////////////////////////////
|
|
|
|
bool AsciiFile::isOpen() const
|
|
|
|
{
|
|
|
|
return fileStream_.is_open();
|
|
|
|
}
|
|
|
|
|
|
|
|
// IO //////////////////////////////////////////////////////////////////////////
|
|
|
|
void AsciiFile::close(void)
|
|
|
|
{
|
2014-02-13 19:23:39 +00:00
|
|
|
state_.reset(nullptr);
|
2014-02-10 11:20:40 +00:00
|
|
|
if (isOpen())
|
|
|
|
{
|
|
|
|
fileStream_.close();
|
|
|
|
}
|
|
|
|
name_ = "";
|
|
|
|
mode_ = Mode::null;
|
|
|
|
isParsed_ = false;
|
2014-02-06 18:08:46 +00:00
|
|
|
deleteData();
|
2014-01-22 15:57:47 +00:00
|
|
|
}
|
|
|
|
|
2014-02-06 18:08:46 +00:00
|
|
|
void AsciiFile::open(const string &name, const unsigned int mode)
|
2014-01-22 15:57:47 +00:00
|
|
|
{
|
|
|
|
if (isOpen())
|
|
|
|
{
|
|
|
|
LATAN_ERROR(Io, "file already opened with name '" + name_ + "'");
|
|
|
|
}
|
2014-02-10 11:20:40 +00:00
|
|
|
else
|
2014-01-22 15:57:47 +00:00
|
|
|
{
|
2014-02-13 19:23:39 +00:00
|
|
|
ios_base::openmode stdMode = static_cast<ios_base::openmode>(0);
|
2014-02-10 11:20:40 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2014-01-22 15:57:47 +00:00
|
|
|
name_ = name;
|
|
|
|
mode_ = mode;
|
|
|
|
isParsed_ = false;
|
2014-02-10 11:20:40 +00:00
|
|
|
fileStream_.open(name_.c_str(), stdMode);
|
|
|
|
if (mode_ & Mode::read)
|
2014-01-22 15:57:47 +00:00
|
|
|
{
|
2014-02-13 19:23:39 +00:00
|
|
|
state_.reset(new AsciiParserState(&fileStream_, &name_, &data_));
|
2014-01-22 15:57:47 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-02-13 19:23:39 +00:00
|
|
|
state_.reset(nullptr);
|
2014-01-22 15:57:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-06 18:08:46 +00:00
|
|
|
void AsciiFile::load(const string &name __dumb)
|
|
|
|
{
|
2014-02-10 11:20:40 +00:00
|
|
|
if ((mode_ & Mode::read)&&(isOpen()))
|
2014-02-06 18:08:46 +00:00
|
|
|
{
|
|
|
|
if (!isParsed_)
|
|
|
|
{
|
|
|
|
parse();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (isOpen())
|
|
|
|
{
|
|
|
|
LATAN_ERROR(Io, "file '" + name_ + "' is not opened in read mode");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LATAN_ERROR(Io, "file not opened");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// parser //////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// Bison/Flex parser declaration
|
2014-02-13 19:23:39 +00:00
|
|
|
int _ioAscii_parse(AsciiFile::AsciiParserState *state);
|
2014-02-06 18:08:46 +00:00
|
|
|
|
2014-01-22 15:57:47 +00:00
|
|
|
void AsciiFile::parse()
|
|
|
|
{
|
2014-02-10 16:01:39 +00:00
|
|
|
fileStream_.seekg(0);
|
2014-02-13 19:23:39 +00:00
|
|
|
_ioAscii_parse(state_.get());
|
2014-01-22 15:57:47 +00:00
|
|
|
isParsed_ = true;
|
|
|
|
}
|