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
34
NAMESPACE_BEGIN
(
Grid
);
35
// Writer implementation ///////////////////////////////////////////////////////
36
TextWriter::TextWriter
(
const
std::string &fileName)
37
:
file_
(fileName, std::ios::out)
38
{}
39
40
void
TextWriter::push
(
const
std::string &s)
41
{
42
level_
++;
43
};
44
45
void
TextWriter::pop
(
void
)
46
{
47
level_
--;
48
};
49
50
void
TextWriter::indent
(
void
)
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 ///////////////////////////////////////////////////////
58
TextReader::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
67
bool
TextReader::push
(
const
std::string &s)
68
{
69
level_
++;
70
return
true
;
71
};
72
73
void
TextReader::pop
(
void
)
74
{
75
level_
--;
76
};
77
78
void
TextReader::checkIndent
(
void
)
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
97
template
<>
98
void
TextReader::readDefault
(
const
std::string &s, std::string &output)
99
{
100
checkIndent
();
101
output.clear();
102
getline(
file_
, output);
103
}
104
NAMESPACE_END
(
Grid
);
GridCore.h
GridLogMessage
GridLogger GridLogMessage(1, "Message", GridLogColours, "NORMAL")
NAMESPACE_BEGIN
#define NAMESPACE_BEGIN(A)
Definition
Namespace.h:35
NAMESPACE_END
#define NAMESPACE_END(A)
Definition
Namespace.h:36
GRID_TEXT_INDENT
#define GRID_TEXT_INDENT
Definition
TextIO.cc:32
Grid::TextReader::TextReader
TextReader(const std::string &fileName)
Definition
TextIO.cc:58
Grid::TextReader::level_
int level_
Definition
TextIO.h:80
Grid::TextReader::pop
void pop(void)
Definition
TextIO.cc:73
Grid::TextReader::file_
std::ifstream file_
Definition
TextIO.h:79
Grid::TextReader::readDefault
void readDefault(const std::string &s, U &output)
Definition
TextIO.h:119
Grid::TextReader::checkIndent
void checkIndent(void)
Definition
TextIO.cc:78
Grid::TextReader::push
bool push(const std::string &s)
Definition
TextIO.cc:67
Grid::TextWriter::indent
void indent(void)
Definition
TextIO.cc:50
Grid::TextWriter::TextWriter
TextWriter(const std::string &fileName)
Definition
TextIO.cc:36
Grid::TextWriter::level_
int level_
Definition
TextIO.h:60
Grid::TextWriter::push
void push(const std::string &s)
Definition
TextIO.cc:40
Grid::TextWriter::file_
std::ofstream file_
Definition
TextIO.h:59
Grid::TextWriter::pop
void pop(void)
Definition
TextIO.cc:45
Grid
Definition
Deflation.h:31
Grid
serialisation
TextIO.cc
Generated by
1.16.1