Swarm-NG  1.1
binarystream.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Mario Juric *
3  * mjuric@astro.Princeton.EDU *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
20 
27 #define HAVE_BOOST_IOSTREAMS
28 #ifdef HAVE_BOOST_IOSTREAMS
29 
30 #include "binarystream.hpp"
31 
32 #include <iomanip>
33 
34 std::vector<peyton::io::binary::datatype_info> peyton::io::binary::manifest;
36 
37 std::ostream&
38 peyton::io::binary::operator<<(std::ostream &out, const datatype_info &di)
39 {
40  return out << std::setw(62) << di.name
41  << std::setw(8) << di.type_size
42  << std::setw(8) << di.ispod;
43 }
44 
45 std::ostream&
46 peyton::io::binary::operator<< (std::ostream &out, const manifest_t &manifest)
47 {
48  out << "------------------------------------------------------------------------------\n";
49  out << " Binary datatype output manifest \n";
50  out << " data type size is_pod\n";
51  out << "------------------------------------------------------------------------------\n";
52 
53  for(int i = 0; i != manifest.size(); i++)
54  {
55  out << manifest[i] << "\n";
56  }
57  return out;
58 }
59 
60 #include <fstream>
61 #include <map>
62 #include <valarray>
63 
64 #pragma pack(1)
65 struct test_t {
66  char c;
67  short s;
68  float f;
69  double d;
70 };
71 #pragma pack()
72 BLESS_POD(test_t);
73 
74 int demo_binarystream()
75 {
76  using namespace peyton::io;
77  using namespace std;
78 
79  // (Not quite) typical usage with an iostream, both reading and writing
80  // A more typical usage would be with istream/ibstream (for input) and
81  // ostream/obstream (for output). Alternatively, the ?bstream classes'
82  // constructor accepts std::streambufs, so you can use things like
83  // boost::iostreams to attain filtering, compression, etc...
84  fstream io("bla.bin", ios::in | ios::out | ios::binary | ios::trunc);
85  bstream bio(io);
86  bio.exceptions(ifstream::eofbit | ifstream::failbit | ifstream::badbit);
87 
88  // This writes a valarray to disk
89  valarray<int> v, w;
90  v.resize(20);
91  FOR(0, v.size()) { v[i] = i; }
92  bio << v;
93  // This appends a plain structure
94  test_t t = { 'a', -2, 1., 2. }, x;
95  bio << t;
96  // This serializes a std::map<int, string>
97  typedef map<int, std::string> M;
98  M a, b;
99  a[1] = "jedan"; a[2] = "dva"; a[3] = "tri";
100  bio << a;
101 
102  // This reads the valarray back
103  bio.seekg(0);
104  bio >> w;
105  cout << "number of elements read: " << w.size() << "\nelements:";
106  FOR(0, w.size()) { cout << " " << w[i]; }
107  cout << "\n\n";
108  // This reads the POD-like structure back
109  bio >> x;
110  cout << "User defined plain struct: " << x.c << " " << x.s << " " << x.f << " " << x.d << "\n\n";
111  // This reads the map back in
112  bio >> b;
113  cout << "map size: " << b.size() << "\n";
114  cout << "map elements:";
115  FOREACH2(M::const_iterator, b) { cout << " (" << (*i).first << "->" << (*i).second << ")"; }
116  cout << "\n\n";
117 
118  // This prints out all datatypes used in binary output, together
119  // with their size (given by sizeof()), and weather have they been
120  // declared as PODs (plain-ol'-datatypes) or not. Good for debugging.
121  // Collection of this information can be turned off by setting
122  // peyton::io::binary::track_manifest to false. It's turned on by
123  // default.
124  cout << binary::manifest << "\n";
125 
126  return 0;
127 }
128 
129 #if 0
130 #include <boost/iostreams/filtering_stream.hpp>
131 #include <boost/iostreams/filter/bzip2.hpp>
132 #include <boost/iostreams/filter/gzip.hpp>
133 #include <boost/iostreams/device/file.hpp>
134 
135 void demo_binarystream_with_boost_iostreams()
136 {
137  using namespace boost::iostreams;
138  using namespace peyton::io;
139 
140  filtering_streambuf<output> out(bzip2_compressor() | file_sink("bla.bin.bz2"));
141  obstream bout(&out);
142 
143  std::valarray<int> v, w;
144  v.resize(5);
145  FOR(0, v.size()) { v[i] = i; }
146  bout << v;
147  out.reset();
148 
149  filtering_streambuf<input> in(bzip2_decompressor() | file_source("bla.bin.bz2"));
150  ibstream bin(&in);
151  bin >> w;
152  std::cout << "size=" << w.size() << "\n";
153  FOR(0, w.size()) { std::cout << w[i] << " "; } std::cout << "\n";
154 }
155 #endif
156 
157 #else // HAVE_BOOST_IOSTREAMS
158 
159 #include <iostream>
160 
161 int demo_binarystream()
162 {
163  std::cerr << "Boost.Iostream support not compiled in.";
164  return -1;
165 }
166 
167 #endif // HAVE_BOOST_IOSTREAMS