Grid 0.7.0
TextIO.cc
Go to the documentation of this file.
1 /*************************************************************************************
2
3 Grid physics library, www.github.com/paboyle/Grid
4
5 Source file: ./lib/serialisation/TextIO.cc
6
7 Copyright (C) 2015
8
9 Author: Antonin Portelli <antonin.portelli@me.com>
10 Author: paboyle <paboyle@ph.ed.ac.uk>
11 Author: Guido Cossu<guido.cossu@ed.ac.uk>
12
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
17
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
22
23 You should have received a copy of the GNU General Public License along
24 with this program; if not, write to the Free Software Foundation, Inc.,
25 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26
27 See the full license in the file "LICENSE" in the top level distribution directory
28 *************************************************************************************/
29 /* END LEGAL */
30#include <Grid/GridCore.h>
31
32#define GRID_TEXT_INDENT 2 //number of spaces for indentation of levels
33
35// Writer implementation ///////////////////////////////////////////////////////
36TextWriter::TextWriter(const std::string &fileName)
37: file_(fileName, std::ios::out)
38{}
39
40void TextWriter::push(const std::string &s)
41{
42 level_++;
43};
44
46{
47 level_--;
48};
49
51{
52 for (int i = 0; i < level_; ++i)
53 for (int t = 0; t < GRID_TEXT_INDENT; t++)
54 file_ << ' ';
55};
56
57// Reader implementation ///////////////////////////////////////////////////////
58TextReader::TextReader(const std::string &fileName)
59{
60 file_.open(fileName, std::ios::in);
61 if (!file_.is_open()) {
62 std::cout << GridLogMessage << "TextReader: Error opening file " << fileName << std::endl;
63 exit(1);// write better error handling
64 }
65}
66
67bool TextReader::push(const std::string &s)
68{
69 level_++;
70 return true;
71};
72
74{
75 level_--;
76};
77
79{
80 char c;
81
82 for (int i = 0; i < level_; ++i)
83 {
84 bool check = true;
85 for (int t = 0; t< GRID_TEXT_INDENT; t++){
86 file_.get(c);
87 check = check && isspace(c);
88 }
89 if (!check)
90 {
91 std::cerr << "TextReader: mismatch on level " << level_ << std::endl;
92 exit(1);
93 }
94 }
95}
96
97template <>
98void TextReader::readDefault(const std::string &s, std::string &output)
99{
100 checkIndent();
101 output.clear();
102 getline(file_, output);
103}
GridLogger GridLogMessage(1, "Message", GridLogColours, "NORMAL")
#define NAMESPACE_BEGIN(A)
Definition Namespace.h:35
#define NAMESPACE_END(A)
Definition Namespace.h:36
#define GRID_TEXT_INDENT
Definition TextIO.cc:32
TextReader(const std::string &fileName)
Definition TextIO.cc:58
void pop(void)
Definition TextIO.cc:73
std::ifstream file_
Definition TextIO.h:79
void readDefault(const std::string &s, U &output)
Definition TextIO.h:119
void checkIndent(void)
Definition TextIO.cc:78
bool push(const std::string &s)
Definition TextIO.cc:67
void indent(void)
Definition TextIO.cc:50
TextWriter(const std::string &fileName)
Definition TextIO.cc:36
void push(const std::string &s)
Definition TextIO.cc:40
std::ofstream file_
Definition TextIO.h:59
void pop(void)
Definition TextIO.cc:45