1
0
mirror of https://github.com/paboyle/Grid.git synced 2024-11-10 07:55:35 +00:00
Grid/lib/serialisation/TextIO.cc

67 lines
1.1 KiB
C++
Raw Normal View History

2015-11-16 18:14:37 +00:00
#include <Grid.h>
using namespace Grid;
using namespace std;
2015-11-16 18:14:37 +00:00
// Writer implementation ///////////////////////////////////////////////////////
TextWriter::TextWriter(const string &fileName)
: file_(fileName, ios::out)
2015-11-16 18:14:37 +00:00
{}
void TextWriter::push(const string &s)
2015-11-16 18:14:37 +00:00
{
level_++;
};
void TextWriter::pop(void)
{
level_--;
};
void TextWriter::indent(void)
{
for (int i = 0; i < level_; ++i)
{
file_ << '\t';
}
};
// Reader implementation ///////////////////////////////////////////////////////
TextReader::TextReader(const string &fileName)
: file_(fileName, ios::in)
2015-11-16 18:14:37 +00:00
{}
void TextReader::push(const string &s)
2015-11-16 18:14:37 +00:00
{
level_++;
};
void TextReader::pop(void)
{
level_--;
};
void TextReader::checkIndent(void)
{
char c;
for (int i = 0; i < level_; ++i)
{
file_.get(c);
if (c != '\t')
{
cerr << "mismatch on tab " << c << " level " << level_;
cerr << " i "<< i << endl;
abort();
2015-11-16 18:14:37 +00:00
}
}
}
template <>
2015-11-29 11:14:44 +00:00
void TextReader::readDefault(const std::string &s, std::string &output)
2015-11-16 18:14:37 +00:00
{
checkIndent();
output.clear();
getline(file_, output);
2015-11-29 11:14:44 +00:00
}