1
0
mirror of https://github.com/aportelli/LatAnalyze.git synced 2024-11-14 01:45:35 +00:00
LatAnalyze/lib/Io/AsciiFile.cpp

213 lines
5.6 KiB
C++
Raw Normal View History

2014-02-20 23:52:45 +00:00
/*
* AsciiFile.cpp, part of LatAnalyze 3
*
2020-01-13 09:57:06 +00:00
* Copyright (C) 2013 - 2020 Antonin Portelli
2014-02-20 23:52:45 +00:00
*
* 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/>.
*/
#include <LatAnalyze/Io/AsciiFile.hpp>
2014-03-13 18:51:01 +00:00
#include <LatAnalyze/includes.hpp>
2014-02-20 23:52:45 +00:00
using namespace std;
using namespace Latan;
/******************************************************************************
* AsciiFile implementation *
******************************************************************************/
// AsciiParserState constructor ////////////////////////////////////////////////
AsciiFile::AsciiParserState::AsciiParserState(istream *stream, string *name,
IoDataTable *data)
: ParserState<IoDataTable>(stream, name, data)
{
initScanner();
}
// AsciiParserState destructor /////////////////////////////////////////////////
AsciiFile::AsciiParserState::~AsciiParserState(void)
{
destroyScanner();
}
// constructor /////////////////////////////////////////////////////////////////
AsciiFile::AsciiFile(const string &name, const unsigned int mode)
{
open(name, mode);
}
// destructor //////////////////////////////////////////////////////////////////
AsciiFile::~AsciiFile(void)
{
close();
}
// access //////////////////////////////////////////////////////////////////////
void AsciiFile::save(const DMat &m, const std::string &name)
{
2015-10-07 19:03:19 +01:00
if (name.empty())
{
LATAN_ERROR(Io, "trying to save data with an empty name");
}
2015-01-28 17:20:51 +00:00
const auto defaultPrec = fileStream_.precision(defaultDoublePrec);
2014-02-20 23:52:45 +00:00
checkWritability();
isParsed_ = false;
fileStream_ << "#L latan_begin mat " << name << endl;
fileStream_ << m.cols() << endl;
fileStream_ << scientific << m << endl;
fileStream_ << "#L latan_end mat " << endl;
2015-01-28 17:20:51 +00:00
fileStream_.precision(defaultPrec);
2014-02-20 23:52:45 +00:00
}
2016-04-05 15:57:19 +01:00
void AsciiFile::save(const DSample &ds, const std::string &name)
{
if (name.empty())
{
LATAN_ERROR(Io, "trying to save data with an empty name");
}
checkWritability();
isParsed_ = false;
fileStream_ << "#L latan_begin rs_sample " << name << endl;
fileStream_ << ds.size() << endl;
save(ds.matrix(), name + "_data");
fileStream_ << "#L latan_end rs_sample " << endl;
}
void AsciiFile::save(const DMatSample &ms, const std::string &name)
2014-02-20 23:52:45 +00:00
{
2015-10-07 19:03:19 +01:00
if (name.empty())
{
LATAN_ERROR(Io, "trying to save data with an empty name");
}
2014-02-20 23:52:45 +00:00
checkWritability();
isParsed_ = false;
fileStream_ << "#L latan_begin rs_sample " << name << endl;
2016-04-05 15:57:19 +01:00
fileStream_ << ms.size() << endl;
save(ms[central], name + "_C");
for (Index i = 0; i < ms.size(); ++i)
2014-02-20 23:52:45 +00:00
{
2016-04-05 15:57:19 +01:00
save(ms[i], name + "_S_" + strFrom(i));
2014-02-20 23:52:45 +00:00
}
fileStream_ << "#L latan_end rs_sample " << endl;
}
2015-10-01 00:13:34 +01:00
// read first name ////////////////////////////////////////////////////////////
string AsciiFile::getFirstName(void)
{
return load();
}
2014-02-20 23:52:45 +00:00
// tests ///////////////////////////////////////////////////////////////////////
bool AsciiFile::isOpen() const
{
return fileStream_.is_open();
}
// IO //////////////////////////////////////////////////////////////////////////
void AsciiFile::close(void)
{
state_.reset();
if (isOpen())
{
fileStream_.close();
}
name_ = "";
mode_ = Mode::null;
isParsed_ = false;
deleteData();
}
void AsciiFile::open(const string &name, const unsigned int mode)
{
if (isOpen())
{
LATAN_ERROR(Io, "file already opened with name '" + name_ + "'");
}
else
{
ios_base::openmode stdMode = static_cast<ios_base::openmode>(0);
2015-10-01 00:13:34 +01:00
name_ = name;
mode_ = mode;
2014-02-20 23:52:45 +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;
}
isParsed_ = false;
fileStream_.open(name_.c_str(), stdMode);
if (mode_ & Mode::read)
{
state_.reset(new AsciiParserState(&fileStream_, &name_, &data_));
}
else
{
state_.reset();
}
}
}
std::string AsciiFile::load(const string &name)
{
2016-04-20 03:21:05 +01:00
if ((mode_ & Mode::read) and (isOpen()))
2014-02-20 23:52:45 +00:00
{
if (!isParsed_)
{
state_->isFirst = true;
parse();
}
if (name.empty())
{
return state_->first;
}
else
{
return name;
}
}
else
{
if (isOpen())
{
LATAN_ERROR(Io, "file '" + name_ + "' is not opened in read mode");
}
else
{
2014-06-03 12:32:57 +01:00
LATAN_ERROR(Io, "file '" + name_ + "' is not opened");
2014-02-20 23:52:45 +00:00
}
}
}
// parser //////////////////////////////////////////////////////////////////////
//// Bison/Flex parser declaration
int _Ascii_parse(AsciiFile::AsciiParserState *state);
void AsciiFile::parse()
{
fileStream_.seekg(0);
_Ascii_parse(state_.get());
isParsed_ = true;
}