File: binaryIO.cc

package info (click to toggle)
libosl 0.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 273,976 kB
  • sloc: cpp: 129,625; ansic: 7,145; ruby: 1,290; makefile: 558; perl: 413; sh: 35
file content (175 lines) | stat: -rw-r--r-- 3,827 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/* binaryIO.cc
 */
#include "osl/misc/binaryIO.h"
#include "osl/oslConfig.h"
#include <boost/serialization/serialization.hpp>
#include <boost/serialization/vector.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/iostreams/filtering_streambuf.hpp>
#include <boost/iostreams/filter/bzip2.hpp>
#include <iostream>

namespace osl
{
  namespace
  {
    const size_t split_limit = 8*1024;
    template <class T>
    void write_vector(std::ostream& os, const std::vector<T>& data)
    {
      boost::iostreams::filtering_streambuf<boost::iostreams::output> filter;
      filter.push(boost::iostreams::bzip2_compressor());
      filter.push(os);
      std::ostream out(&filter);
    
      boost::archive::text_oarchive oa(out);
      if (data.size() <= split_limit) {
	oa << data;
      }
      else {
	for (size_t p=0; p<data.size(); p+=split_limit) {
	  std::vector<T> tmp(data.begin()+p,
			     data.begin()+std::min(p+split_limit,
						   data.size()));
	  oa << tmp;
	}
      }
    }
  }
}

void osl::misc::BinaryWriter::
write(std::ostream& os, const std::vector<int>& data)
{
  write_vector(os, data);
}
void osl::misc::BinaryWriter::
write(std::ostream& os, const std::vector<double>& data)
{
  write_vector(os, data);
}


template <class T>
osl::misc::BinaryReader<T>::BinaryReader(std::istream& is)
  : state(new State(is))
{
}
template <class T>
osl::misc::BinaryReader<T>::~BinaryReader()
{
}

template <class T>
struct osl::misc::BinaryReader<T>::State
{
  boost::iostreams::filtering_streambuf<boost::iostreams::input> filter;
  boost::scoped_ptr<std::istream> in;
  boost::scoped_ptr<boost::archive::text_iarchive> ia;
  explicit State(std::istream &is)
  {
    if (!is)
      return;
    filter.push(boost::iostreams::bzip2_decompressor());
    filter.push(is);
    in.reset(new std::istream(&filter));
    ia.reset(new boost::archive::text_iarchive(*in));
  }
  bool read_vector(std::vector<T>& data)
  {
    if (! in || ! *in)
      return false;
    return (*ia) >> data, *in;
  }
};

template <class T>
bool osl::misc::BinaryReader<T>::
read(std::vector<T>& data)
{
  return state->read_vector(data);
}

template <class T>
size_t osl::misc::BinaryReader<T>::blockSize()
{
  return split_limit;
}


template <class T>
struct osl::misc::BinaryElementReader<T>::State
{
  BinaryReader<T> reader;
  std::vector<T> data;
  size_t cur;
  bool failed;
  explicit State(std::istream& is) : reader(is), cur(0), failed(!is)
  {
  }
  bool hasNext() 
  {
    tryRead();
    return cur < data.size();
  }
  T read()
  {
    if (! hasNext())
      throw std::logic_error("no data in BinaryReader::read");
    return data[cur++];
  }
  void tryRead()
  {
    if (cur < data.size())
      return;
    data.clear();
    cur = 0;
    try {
      failed = ! reader.read(data);
    } catch (boost::archive::archive_exception& e) {
      if (OslConfig::verbose() || 1)
	std::cerr << "read failed in BinaryReader " << e.what();
      cur = data.size();
      failed = true;
    }
  }
};
  
template <class T>
osl::misc::BinaryElementReader<T>::BinaryElementReader(std::istream& is)
  : state(new State(is))
{
}
template <class T>
osl::misc::BinaryElementReader<T>::~BinaryElementReader()
{
}
template <class T>
bool osl::misc::BinaryElementReader<T>::
hasNext() const
{
  return state->hasNext();
}
template <class T>
bool osl::misc::BinaryElementReader<T>::
failed() const
{
  return state->failed;
}
template <class T>
T osl::misc::BinaryElementReader<T>::read()
{
  return state->read();
}


template class osl::misc::BinaryReader<int>;
template class osl::misc::BinaryReader<double>;
template class osl::misc::BinaryElementReader<int>;
template class osl::misc::BinaryElementReader<double>;

// ;;; Local Variables:
// ;;; mode:c++
// ;;; c-basic-offset:2
// ;;; End: