dune-fem 2.8.0
Loading...
Searching...
No Matches
asciistreams.hh
Go to the documentation of this file.
1#ifndef DUNE_FEM_ASCIISTREAMS_HH
2#define DUNE_FEM_ASCIISTREAMS_HH
3
4#include <iostream>
5#include <fstream>
6
8
9namespace Dune
10{
11
12 namespace Fem
13 {
14
15 class ASCIIOutStream;
16 class ASCIIInStream;
17
19 {
21 };
22
23
35 : public OutStreamInterface< ASCIIOutStreamTraits >
36 {
39
40 public:
43
44 protected:
45 std::ostream &stream_;
47
48 protected:
50
51 public:
56 explicit ASCIIOutStream ( std::ostream &stream )
57 : stream_( stream ),
58 mustFreeStream_( false )
59 {}
60
65 explicit ASCIIOutStream ( const std::string &filename )
66 : stream_( *(new std :: ofstream( filename.c_str() )) ),
67 mustFreeStream_( true )
68 {}
69
72 {
73 if( mustFreeStream_ )
74 delete &stream_;
75 }
76
78 void flush ()
79 {
80 stream_.flush();
81 }
82
84 void writeDouble ( const double value )
85 {
86 stream_.setf( std ::ios_base :: scientific, std :: ios_base :: floatfield );
87 stream_ .precision( 16 );
88 stream_ << value << std :: endl;
89 if( !valid () )
90 writeError();
91 }
92
94 void writeFloat ( const float value )
95 {
96 stream_.setf( std ::ios_base :: scientific, std :: ios_base :: floatfield );
97 stream_ .precision( 7 );
98 stream_ << value << std :: endl;
99 if( !valid() )
100 writeError();
101 }
102
104 void writeInt ( const int value )
105 {
106 stream_ << value << std :: endl;
107 if( !valid () )
108 writeError();
109 }
110
112 void writeChar ( const char value )
113 {
114 // make sure char is written as number
115 int val = (int) value;
116 writeInt( val );
117 }
118
120 void writeBool ( const bool value )
121 {
122 std::string val( ( value == true ) ? "true" : "false" );
123 writeString( val );
124 }
125
131 void writeString ( const std::string &s )
132 {
133 const unsigned int length = s.length();
134 stream_ << length;
135 for( unsigned int i = 0; i < length; ++i )
136 stream_.put( s[ i ] );
137 stream_ << std :: endl;
138 if( !valid () )
139 writeError();
140 }
141
143 void writeUnsignedInt ( unsigned int value )
144 {
145 stream_ << value << std::endl;
146 if( !valid () )
147 writeError();
148 }
149
151 void writeUnsignedInt64 ( uint64_t value )
152 {
153 stream_ << value << std::endl;
154 if( !valid () )
155 writeError();
156 }
157
158 protected:
159 bool valid () const
160 {
161 return stream_.good() | stream_.eof();
162 }
163 };
164
165
166
168 {
170 };
171
172
173
184 : public InStreamInterface< ASCIIInStreamTraits >
185 {
186 typedef ASCIIInStream ThisType;
188
189 public:
192
193 protected:
194 std::istream &stream_;
196
197 protected:
199
200 public:
205 explicit ASCIIInStream ( std::istream &stream )
206 : stream_( stream ),
207 mustFreeStream_( false )
208 {}
209
214 ASCIIInStream ( const std::string &filename )
215 : stream_( *(new std :: ifstream( filename.c_str() )) ),
216 mustFreeStream_( true )
217 {}
218
221 {
222 if( mustFreeStream_ )
223 delete &stream_;
224 }
225
227 void readDouble ( double &value )
228 {
229 stream_ >> value;
230 if( !valid () )
231 readError();
232 }
233
235 void readFloat ( float &value )
236 {
237 stream_ >> value;
238 if( !valid () )
239 readError();
240 }
241
243 void readInt ( int &value )
244 {
245 stream_ >> value;
246 if( !valid () )
247 readError();
248 }
249
251 void readChar ( char &value )
252 {
253 int val;
254 readInt( val );
255 value = (char) val;
256 }
257
259 void readBool ( bool &value )
260 {
261 std::string val;
262 readString( val );
263
264 if( val == "true" )
265 value = true;
266 else if ( val == "false" )
267 value = false;
268 else
269 readError();
270 }
271
277 void readString ( std::string &s )
278 {
279 unsigned int length;
280 stream_ >> length;
281 for( unsigned int i = 0; i < length; ++i )
282 s += stream_.get();
283 if( !valid () )
284 readError();
285 }
286
288 void readUnsignedInt ( unsigned int &value )
289 {
290 stream_ >> value;
291 if( !valid () )
292 readError();
293 }
294
296 void readUnsignedInt64 (uint64_t &value )
297 {
298 stream_ >> value;
299 if( !valid () )
300 readError();
301 }
302
303 protected:
304 bool valid () const
305 {
306 return stream_.good() | stream_.eof();
307 }
308 };
309
310 } // namespace Fem
311
312} // namespace Dune
313
314#endif // #ifndef DUNE_FEM_ASCIISTREAMS_HH
STL namespace.
Definition: bindguard.hh:11
Definition: asciistreams.hh:19
ASCIIOutStream OutStreamType
Definition: asciistreams.hh:20
output stream writing into an STL output stream using ASCII encoding
Definition: asciistreams.hh:36
void writeUnsignedInt(unsigned int value)
write an unsigned int to the stream
Definition: asciistreams.hh:143
void flush()
flush the stream
Definition: asciistreams.hh:78
void writeString(const std::string &s)
write a string to the stream
Definition: asciistreams.hh:131
void writeChar(const char value)
write a char to the stream
Definition: asciistreams.hh:112
void writeInt(const int value)
write an int to the stream
Definition: asciistreams.hh:104
bool valid() const
Definition: asciistreams.hh:159
std::ostream & stream_
Definition: asciistreams.hh:45
ASCIIOutStream(std::ostream &stream)
constructor
Definition: asciistreams.hh:56
ASCIIOutStreamTraits Traits
type of the traits
Definition: asciistreams.hh:42
ASCIIOutStream(const std::string &filename)
constructor
Definition: asciistreams.hh:65
void writeDouble(const double value)
write a double to the stream
Definition: asciistreams.hh:84
bool mustFreeStream_
Definition: asciistreams.hh:46
void writeFloat(const float value)
write a float to the stream
Definition: asciistreams.hh:94
~ASCIIOutStream()
destructor
Definition: asciistreams.hh:71
void writeBool(const bool value)
write a char to the stream
Definition: asciistreams.hh:120
void writeUnsignedInt64(uint64_t value)
write an uint64_t to the stream
Definition: asciistreams.hh:151
Definition: asciistreams.hh:168
ASCIIInStream InStreamType
Definition: asciistreams.hh:169
input stream reading from an STL input stream using ASCII decoding
Definition: asciistreams.hh:185
void readBool(bool &value)
read a bool from the stream
Definition: asciistreams.hh:259
ASCIIInStreamTraits Traits
type of the traits
Definition: asciistreams.hh:191
void readChar(char &value)
read a char from the stream
Definition: asciistreams.hh:251
void readInt(int &value)
read an int from the stream
Definition: asciistreams.hh:243
void readUnsignedInt(unsigned int &value)
read an unsigned int from the stream
Definition: asciistreams.hh:288
ASCIIInStream(const std::string &filename)
constructor
Definition: asciistreams.hh:214
void readUnsignedInt64(uint64_t &value)
read an uint64_t from the stream
Definition: asciistreams.hh:296
void readDouble(double &value)
read a double from the stream
Definition: asciistreams.hh:227
std::istream & stream_
Definition: asciistreams.hh:194
bool valid() const
Definition: asciistreams.hh:304
bool mustFreeStream_
Definition: asciistreams.hh:195
void readString(std::string &s)
read a string from the stream
Definition: asciistreams.hh:277
ASCIIInStream(std::istream &stream)
constructor
Definition: asciistreams.hh:205
void readFloat(float &value)
read a float from the stream
Definition: asciistreams.hh:235
~ASCIIInStream()
destructor
Definition: asciistreams.hh:220
abstract interface for an output stream
Definition: streams.hh:46
void writeError() const
Definition: streams.hh:147
abstract interface for an input stream
Definition: streams.hh:179
void readError() const
Definition: streams.hh:348
int readInt()
read an int from the stream
Definition: streams.hh:250