DUNE-FEM (unstable)

asciistreams.hh
1#ifndef DUNE_FEM_ASCIISTREAMS_HH
2#define DUNE_FEM_ASCIISTREAMS_HH
3
4#include <iostream>
5#include <fstream>
6
7#include <dune/fem/io/streams/streams.hh>
8
9namespace Dune
10{
11
12 namespace Fem
13 {
14
15 class ASCIIOutStream;
16 class ASCIIInStream;
17
18 struct ASCIIOutStreamTraits
19 {
20 typedef ASCIIOutStream OutStreamType;
21 };
22
23
35 : public OutStreamInterface< ASCIIOutStreamTraits >
36 {
39
40 public:
42 typedef ASCIIOutStreamTraits Traits;
43
44 protected:
45 std::ostream &stream_;
46 bool mustFreeStream_;
47
48 protected:
49 using BaseType::writeError;
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 writeSignedInt64 ( int64_t value )
113 {
114 stream_ << value << std::endl;
115 if( !valid () )
116 writeError();
117 }
118
120 void writeChar ( const char value )
121 {
122 // make sure char is written as number
123 int val = (int) value;
124 writeInt( val );
125 }
126
128 void writeBool ( const bool value )
129 {
130 std::string val( ( value == true ) ? "true" : "false" );
131 writeString( val );
132 }
133
139 void writeString ( const std::string &s )
140 {
141 const unsigned int length = s.length();
142 stream_ << length;
143 for( unsigned int i = 0; i < length; ++i )
144 stream_.put( s[ i ] );
145 stream_ << std :: endl;
146 if( !valid () )
147 writeError();
148 }
149
151 void writeUnsignedInt ( unsigned int value )
152 {
153 stream_ << value << std::endl;
154 if( !valid () )
155 writeError();
156 }
157
159 void writeUnsignedInt64 ( uint64_t value )
160 {
161 stream_ << value << std::endl;
162 if( !valid () )
163 writeError();
164 }
165
166 protected:
167 bool valid () const
168 {
169 return stream_.good() | stream_.eof();
170 }
171 };
172
173
174
175 struct ASCIIInStreamTraits
176 {
177 typedef ASCIIInStream InStreamType;
178 };
179
180
181
192 : public InStreamInterface< ASCIIInStreamTraits >
193 {
194 typedef ASCIIInStream ThisType;
196
197 public:
199 typedef ASCIIInStreamTraits Traits;
200
201 protected:
202 std::istream &stream_;
203 bool mustFreeStream_;
204
205 protected:
206 using BaseType::readError;
207
208 public:
213 explicit ASCIIInStream ( std::istream &stream )
214 : stream_( stream ),
215 mustFreeStream_( false )
216 {}
217
222 ASCIIInStream ( const std::string &filename )
223 : stream_( *(new std :: ifstream( filename.c_str() )) ),
224 mustFreeStream_( true )
225 {}
226
229 {
230 if( mustFreeStream_ )
231 delete &stream_;
232 }
233
235 void readDouble ( double &value )
236 {
237 stream_ >> value;
238 if( !valid () )
239 readError();
240 }
241
243 void readFloat ( float &value )
244 {
245 stream_ >> value;
246 if( !valid () )
247 readError();
248 }
249
251 void readInt ( int &value )
252 {
253 stream_ >> value;
254 if( !valid () )
255 readError();
256 }
257
259 void readSignedInt64 (int64_t &value )
260 {
261 stream_ >> value;
262 if( !valid () )
263 readError();
264 }
265
267 void readChar ( char &value )
268 {
269 int val;
270 readInt( val );
271 value = (char) val;
272 }
273
275 void readBool ( bool &value )
276 {
277 std::string val;
278 readString( val );
279
280 if( val == "true" )
281 value = true;
282 else if ( val == "false" )
283 value = false;
284 else
285 readError();
286 }
287
293 void readString ( std::string &s )
294 {
295 unsigned int length;
296 stream_ >> length;
297 for( unsigned int i = 0; i < length; ++i )
298 s += stream_.get();
299 if( !valid () )
300 readError();
301 }
302
304 void readUnsignedInt ( unsigned int &value )
305 {
306 stream_ >> value;
307 if( !valid () )
308 readError();
309 }
310
312 void readUnsignedInt64 (uint64_t &value )
313 {
314 stream_ >> value;
315 if( !valid () )
316 readError();
317 }
318
319 protected:
320 bool valid () const
321 {
322 return stream_.good() | stream_.eof();
323 }
324 };
325
326 } // namespace Fem
327
328} // namespace Dune
329
330#endif // #ifndef DUNE_FEM_ASCIISTREAMS_HH
input stream reading from an STL input stream using ASCII decoding
Definition: asciistreams.hh:193
void readBool(bool &value)
read a bool from the stream
Definition: asciistreams.hh:275
ASCIIInStreamTraits Traits
type of the traits
Definition: asciistreams.hh:199
void readChar(char &value)
read a char from the stream
Definition: asciistreams.hh:267
void readInt(int &value)
read an int from the stream
Definition: asciistreams.hh:251
void readUnsignedInt(unsigned int &value)
read an unsigned int from the stream
Definition: asciistreams.hh:304
ASCIIInStream(const std::string &filename)
constructor
Definition: asciistreams.hh:222
void readUnsignedInt64(uint64_t &value)
read an uint64_t from the stream
Definition: asciistreams.hh:312
void readDouble(double &value)
read a double from the stream
Definition: asciistreams.hh:235
void readString(std::string &s)
read a string from the stream
Definition: asciistreams.hh:293
ASCIIInStream(std::istream &stream)
constructor
Definition: asciistreams.hh:213
void readFloat(float &value)
read a float from the stream
Definition: asciistreams.hh:243
void readSignedInt64(int64_t &value)
read an int64_t from the stream
Definition: asciistreams.hh:259
~ASCIIInStream()
destructor
Definition: asciistreams.hh:228
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:151
void flush()
flush the stream
Definition: asciistreams.hh:78
void writeSignedInt64(int64_t value)
write an int64_t to the stream
Definition: asciistreams.hh:112
void writeString(const std::string &s)
write a string to the stream
Definition: asciistreams.hh:139
void writeChar(const char value)
write a char to the stream
Definition: asciistreams.hh:120
void writeInt(const int value)
write an int to the stream
Definition: asciistreams.hh:104
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
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:128
void writeUnsignedInt64(uint64_t value)
write an uint64_t to the stream
Definition: asciistreams.hh:159
abstract interface for an input stream
Definition: streams.hh:190
int readInt()
read an int from the stream
Definition: streams.hh:261
abstract interface for an output stream
Definition: streams.hh:48
Dune namespace.
Definition: alignedallocator.hh:13
STL namespace.
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Jul 27, 22:29, 2024)