dune-grid  2.2.0
streams.hh
Go to the documentation of this file.
1 // -*- tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 // vi: set et ts=8 sw=2 sts=2:
3 
4 #ifndef DUNE_GRID_IO_FILE_VTK_STREAMS_HH
5 #define DUNE_GRID_IO_FILE_VTK_STREAMS_HH
6 
7 #include <ostream>
8 
10 
11 namespace Dune {
12 
14  class Base64Stream {
15  std::ostream& s;
16  b64chunk chunk;
17  char obuf[4];
18 
19  public:
21 
25  Base64Stream(std::ostream& s_)
26  : s(s_)
27  {
28  // reset chunk
29  chunk.txt.read(0,0);
30  }
31 
33 
39  template <class X>
40  void write(X & data)
41  {
42  char* p = reinterpret_cast<char*>(&data);
43  for (size_t len = sizeof(X); len > 0; len--,p++)
44  {
45  chunk.txt.put(*p);
46  if (chunk.txt.size == 3)
47  {
48  chunk.data.write(obuf);
49  s.write(obuf,4);
50  }
51  }
52  }
53 
55 
62  void flush()
63  {
64  if (chunk.txt.size > 0)
65  {
66  chunk.data.write(obuf);
67  s.write(obuf,4);
68  }
69  }
70 
72 
76  flush();
77  }
78  };
79 
81  class RawStream
82  {
83  public:
85  inline RawStream (std::ostream& theStream)
86  : s(theStream)
87  {}
88 
90  template<class T>
91  void write (T data)
92  {
93  char* p = reinterpret_cast<char*>(&data);
94  s.write(p,sizeof(T));
95  }
96  private:
97  std::ostream& s;
98  };
99 
100 } // namespace Dune
101 
102 #endif // DUNE_GRID_IO_FILE_VTK_STREAMS_HH