2014-02-06 18:52:13 +00:00
|
|
|
/*
|
|
|
|
* Io.hpp, 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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef Latan_Io_hpp_
|
|
|
|
#define Latan_Io_hpp_
|
2014-01-22 15:57:47 +00:00
|
|
|
|
|
|
|
#include <fstream>
|
2014-02-13 19:23:39 +00:00
|
|
|
#include <memory>
|
2014-02-17 18:46:55 +00:00
|
|
|
#include <queue>
|
2014-01-22 15:57:47 +00:00
|
|
|
#include <sstream>
|
|
|
|
#include <string>
|
2014-02-13 19:23:39 +00:00
|
|
|
#include <unordered_map>
|
2014-01-22 15:57:47 +00:00
|
|
|
#include <latan/Global.hpp>
|
|
|
|
#include <latan/IoObject.hpp>
|
|
|
|
#include <latan/Mat.hpp>
|
|
|
|
#include <latan/ParserState.hpp>
|
2014-02-10 16:01:39 +00:00
|
|
|
#include <latan/RandGen.hpp>
|
2014-01-22 15:57:47 +00:00
|
|
|
#include <latan/Sample.hpp>
|
|
|
|
|
2014-02-06 18:52:13 +00:00
|
|
|
BEGIN_NAMESPACE
|
2014-01-22 15:57:47 +00:00
|
|
|
|
|
|
|
/******************************************************************************
|
2014-02-20 22:54:11 +00:00
|
|
|
* Abstract datafile class *
|
2014-01-22 15:57:47 +00:00
|
|
|
******************************************************************************/
|
2014-02-13 19:23:39 +00:00
|
|
|
typedef std::unordered_map<std::string, std::unique_ptr<IoObject>> IoDataTable;
|
2014-01-22 15:57:47 +00:00
|
|
|
|
|
|
|
class File
|
|
|
|
{
|
|
|
|
public:
|
2014-02-10 11:20:40 +00:00
|
|
|
class Mode
|
2014-01-22 15:57:47 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
null = 0,
|
|
|
|
write = 1 << 0,
|
|
|
|
read = 1 << 1,
|
|
|
|
append = 1 << 2
|
|
|
|
};
|
|
|
|
};
|
|
|
|
public:
|
|
|
|
// constructors
|
|
|
|
File(void);
|
2014-02-06 18:08:46 +00:00
|
|
|
File(const std::string &name, const unsigned int mode);
|
2014-01-22 15:57:47 +00:00
|
|
|
// destructor
|
|
|
|
virtual ~File(void);
|
|
|
|
// access
|
2014-02-17 18:46:55 +00:00
|
|
|
const std::string & getName(void) const;
|
|
|
|
unsigned int getMode(void) const;
|
2014-02-06 18:08:46 +00:00
|
|
|
template <typename IoT>
|
2014-02-17 18:46:55 +00:00
|
|
|
const IoT & read(const std::string &name = "");
|
2014-02-10 16:01:39 +00:00
|
|
|
virtual void save(const DMat &m, const std::string &name) = 0;
|
2014-02-17 18:46:55 +00:00
|
|
|
virtual void save(const DMatSample &state, const std::string &name) = 0;
|
2014-02-10 16:01:39 +00:00
|
|
|
virtual void save(const RandGen::State &state, const std::string &name) = 0;
|
2014-01-22 15:57:47 +00:00
|
|
|
// tests
|
|
|
|
virtual bool isOpen(void) const = 0;
|
2014-02-06 18:08:46 +00:00
|
|
|
// IO
|
|
|
|
virtual void close(void) = 0;
|
|
|
|
virtual void open(const std::string &name, const unsigned int mode) = 0;
|
2014-01-22 15:57:47 +00:00
|
|
|
protected:
|
2014-02-17 18:46:55 +00:00
|
|
|
// access
|
|
|
|
void setName(const std::string &name);
|
|
|
|
void setMode(const unsigned int mode);
|
2014-01-22 15:57:47 +00:00
|
|
|
// data access
|
2014-02-17 18:46:55 +00:00
|
|
|
void deleteData(void);
|
2014-02-10 11:20:40 +00:00
|
|
|
// error checking
|
|
|
|
void checkWritability(void);
|
2014-02-17 18:46:55 +00:00
|
|
|
private:
|
|
|
|
// data access
|
|
|
|
template <typename IoT>
|
|
|
|
const IoT& getData(const std::string &name = "") const;
|
|
|
|
// IO
|
|
|
|
virtual std::string load(const std::string &name = "") = 0;
|
2014-01-22 15:57:47 +00:00
|
|
|
protected:
|
|
|
|
std::string name_;
|
|
|
|
unsigned int mode_;
|
|
|
|
IoDataTable data_;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Template implementations
|
2014-01-23 14:06:50 +00:00
|
|
|
template <typename IoT>
|
2014-02-06 18:08:46 +00:00
|
|
|
const IoT& File::read(const std::string &name)
|
|
|
|
{
|
2014-02-17 18:46:55 +00:00
|
|
|
std::string dataName;
|
|
|
|
|
|
|
|
dataName = load(name);
|
2014-02-06 18:08:46 +00:00
|
|
|
|
2014-02-17 18:46:55 +00:00
|
|
|
return getData<IoT>(dataName);
|
2014-02-06 18:08:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename IoT>
|
|
|
|
const IoT& File::getData(const std::string &name) const
|
2014-01-22 15:57:47 +00:00
|
|
|
{
|
2014-02-13 19:23:39 +00:00
|
|
|
try
|
2014-01-22 15:57:47 +00:00
|
|
|
{
|
2014-02-13 19:23:39 +00:00
|
|
|
return dynamic_cast<const IoT &>(*(data_.at(name)));
|
2014-01-22 15:57:47 +00:00
|
|
|
}
|
2014-02-13 19:23:39 +00:00
|
|
|
catch(std::out_of_range)
|
2014-01-22 15:57:47 +00:00
|
|
|
{
|
2014-02-17 18:46:55 +00:00
|
|
|
LATAN_ERROR(Definition, "no data with name '" + name + "' in file "
|
|
|
|
+ name_);
|
2014-01-22 15:57:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* ASCII datafile class *
|
|
|
|
******************************************************************************/
|
|
|
|
class AsciiFile: public File
|
|
|
|
{
|
2014-02-06 18:08:46 +00:00
|
|
|
public:
|
|
|
|
class AsciiParserState: public ParserState<IoDataTable>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
// constructor
|
2014-02-13 19:23:39 +00:00
|
|
|
explicit AsciiParserState(std::istream *stream, std::string *name,
|
2014-02-10 11:20:40 +00:00
|
|
|
IoDataTable *data);
|
2014-02-06 18:08:46 +00:00
|
|
|
// destructor
|
|
|
|
virtual ~AsciiParserState(void);
|
2014-02-17 18:46:55 +00:00
|
|
|
// first element reference
|
|
|
|
bool isFirst;
|
|
|
|
std::string first;
|
2014-02-13 19:23:39 +00:00
|
|
|
// parsing buffers
|
|
|
|
RandGen::State stateBuf;
|
2014-02-17 18:46:55 +00:00
|
|
|
DMatSample dMatSampleBuf;
|
|
|
|
std::queue<DMat> dMatQueue;
|
|
|
|
std::queue<double> doubleQueue;
|
|
|
|
std::queue<int> intQueue;
|
2014-02-06 18:08:46 +00:00
|
|
|
private:
|
|
|
|
// allocation/deallocation functions defined in IoAsciiLexer.lpp
|
|
|
|
virtual void initScanner(void);
|
|
|
|
virtual void destroyScanner(void);
|
|
|
|
};
|
2014-01-22 15:57:47 +00:00
|
|
|
public:
|
|
|
|
// constructors
|
|
|
|
AsciiFile(void);
|
2014-02-06 18:08:46 +00:00
|
|
|
AsciiFile(const std::string &name, const unsigned int mode);
|
2014-01-22 15:57:47 +00:00
|
|
|
// destructor
|
|
|
|
virtual ~AsciiFile(void);
|
2014-02-10 11:20:40 +00:00
|
|
|
// access
|
|
|
|
virtual void save(const DMat &m, const std::string &name);
|
2014-02-17 18:46:55 +00:00
|
|
|
virtual void save(const DMatSample &s, const std::string &name);
|
2014-02-10 16:01:39 +00:00
|
|
|
virtual void save(const RandGen::State &state, const std::string &name);
|
2014-01-22 15:57:47 +00:00
|
|
|
// tests
|
|
|
|
virtual bool isOpen(void) const;
|
2014-02-06 18:08:46 +00:00
|
|
|
// IO
|
2014-01-22 15:57:47 +00:00
|
|
|
virtual void close(void);
|
2014-02-06 18:08:46 +00:00
|
|
|
virtual void open(const std::string &name, const unsigned int mode);
|
2014-01-22 15:57:47 +00:00
|
|
|
private:
|
2014-02-06 18:08:46 +00:00
|
|
|
// IO
|
2014-02-17 18:46:55 +00:00
|
|
|
virtual std::string load(const std::string &name = "");
|
2014-02-06 18:08:46 +00:00
|
|
|
// parser
|
2014-01-22 15:57:47 +00:00
|
|
|
void parse(void);
|
|
|
|
private:
|
2014-02-13 19:23:39 +00:00
|
|
|
std::fstream fileStream_;
|
|
|
|
bool isParsed_;
|
|
|
|
std::unique_ptr<AsciiParserState> state_;
|
2014-01-22 15:57:47 +00:00
|
|
|
};
|
|
|
|
|
2014-02-06 18:52:13 +00:00
|
|
|
END_NAMESPACE
|
2014-01-22 15:57:47 +00:00
|
|
|
|
|
|
|
#endif
|