Grid 0.7.0
TextIO.h
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.h
6
7 Copyright (C) 2015
8
9Author: Antonin Portelli <antonin.portelli@me.com>
10Author: Peter Boyle <paboyle@ph.ed.ac.uk>
11
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License along
23 with this program; if not, write to the Free Software Foundation, Inc.,
24 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25
26 See the full license in the file "LICENSE" in the top level distribution directory
27 *************************************************************************************/
28 /* END LEGAL */
29#ifndef GRID_SERIALISATION_TEXT_READER_H
30#define GRID_SERIALISATION_TEXT_READER_H
31
32#include <iostream>
33#include <iomanip>
34#include <fstream>
35#include <sstream>
36#include <math.h>
37#include <vector>
38#include <cassert>
39
40namespace Grid
41{
42
43 class TextWriter: public Writer<TextWriter>
44 {
45 public:
46 TextWriter(const std::string &fileName);
47 virtual ~TextWriter(void) = default;
48 void push(const std::string &s);
49 void pop(void);
50 template <typename U>
51 void writeDefault(const std::string &s, const U &x);
52 template <typename U>
53 void writeDefault(const std::string &s, const std::vector<U> &x);
54 template <typename U>
55 void writeMultiDim(const std::string &s, const std::vector<size_t> & Dimensions, const U * pDataRowMajor, size_t NumElements);
56 private:
57 void indent(void);
58 private:
59 std::ofstream file_;
60 int level_{0};
61 };
62
63 class TextReader: public Reader<TextReader>
64 {
65 public:
66 TextReader(const std::string &fileName);
67 virtual ~TextReader(void) = default;
68 bool push(const std::string &s);
69 void pop(void);
70 template <typename U>
71 void readDefault(const std::string &s, U &output);
72 template <typename U>
73 void readDefault(const std::string &s, std::vector<U> &output);
74 template <typename U>
75 void readMultiDim(const std::string &s, std::vector<U> &buf, std::vector<size_t> &dim);
76 private:
77 void checkIndent(void);
78 private:
79 std::ifstream file_;
80 int level_{0};
81 };
82
83 // Writer template implementation ////////////////////////////////////////////
84 template <typename U>
85 void TextWriter::writeDefault(const std::string &s, const U &x)
86 {
87 indent();
88 file_ << std::boolalpha << x << std::endl;
89 }
90
91 template <typename U>
92 void TextWriter::writeDefault(const std::string &s, const std::vector<U> &x)
93 {
94 uint64_t sz = x.size();
95
96 write(s, sz);
97 for (uint64_t i = 0; i < sz; ++i)
98 {
99 write(s, x[i]);
100 }
101 }
102
103 template <typename U>
104 void TextWriter::writeMultiDim(const std::string &s, const std::vector<size_t> & Dimensions, const U * pDataRowMajor, size_t NumElements)
105 {
106 uint64_t Rank = Dimensions.size();
107 write(s, Rank);
108 for( uint64_t d : Dimensions )
109 write(s, d);
110 while( NumElements-- )
111 write(s, *pDataRowMajor++);
112 }
113
114 // Reader template implementation ////////////////////////////////////////////
115 template <>
116 void TextReader::readDefault(const std::string &s, std::string &output);
117
118 template <typename U>
119 void TextReader::readDefault(const std::string &s, U &output)
120 {
121 std::string buf;
122
123 readDefault(s, buf);
124 fromString(output, buf);
125 }
126
127 template <typename U>
128 void TextReader::readDefault(const std::string &s, std::vector<U> &output)
129 {
130 uint64_t sz;
131
132 read("", sz);
133 output.resize(sz);
134 for (uint64_t i = 0; i < sz; ++i)
135 {
136 read("", output[i]);
137 }
138 }
139
140 template <typename U>
141 void TextReader::readMultiDim(const std::string &s, std::vector<U> &buf, std::vector<size_t> &dim)
142 {
143 const char sz[] = "";
144 uint64_t Rank;
145 read(sz, Rank);
146 dim.resize( Rank );
147 size_t NumElements = 1;
148 for( auto &d : dim ) {
149 read(sz, d);
150 NumElements *= d;
151 }
152 buf.resize( NumElements );
153 for( auto &x : buf )
154 read(s, x);
155 }
156
157}
158
159#endif
160
static INTERNAL_PRECISION U
Definition Zolotarev.cc:230
void fromString(U &output, const std::string &s)
Definition BaseIO.h:518
std::enable_if< std::is_base_of< Serializable, U >::value, void >::type read(const std::string &s, U &output)
Definition BaseIO.h:393
TextReader(const std::string &fileName)
Definition TextIO.cc:58
void readMultiDim(const std::string &s, std::vector< U > &buf, std::vector< size_t > &dim)
Definition TextIO.h:141
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
virtual ~TextReader(void)=default
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 writeDefault(const std::string &s, const U &x)
Definition TextIO.h:85
void push(const std::string &s)
Definition TextIO.cc:40
std::ofstream file_
Definition TextIO.h:59
void writeMultiDim(const std::string &s, const std::vector< size_t > &Dimensions, const U *pDataRowMajor, size_t NumElements)
Definition TextIO.h:104
virtual ~TextWriter(void)=default
void pop(void)
Definition TextIO.cc:45
std::enable_if< std::is_base_of< Serializable, U >::value >::type write(const std::string &s, const U &output)
Definition BaseIO.h:260