pocolog_cpp
Read.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2005-2006 LAAS/CNRS <openrobots@laas.fr>
3  * Sylvain Joyeux <sylvain.joyeux@m4x.org>
4  *
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are
9  * met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
25  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
27  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
28  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
29  * DAMAGE.
30  */
31 
32 
33 #ifndef POCOLOG_CPP_READ_HPP
34 #define POCOLOG_CPP_READ_HPP
35 
36 #include "pocolog_cpp/Format.hpp"
37 
38 #include <string>
39 #include <vector>
40 #include <iosfwd>
41 #include <typelib/value_ops.hh>
42 
43 #include <boost/noncopyable.hpp>
44 
45 namespace Typelib
46 {
47  class Registry;
48  class Type;
49 }
50 
51 namespace pocolog_cpp
52 {
53  class Stream;
54  class DataStream;
55  class DataInputIterator;
56 
57  class Input
58  {
59  public:
60  static const size_t npos = static_cast<size_t>(-1);
61  static const size_t nstream = static_cast<size_t>(-1);
62 
63  private:
64  std::istream* m_input;
65 
66  typedef std::vector<Stream*> Streams;
67  Streams m_streams;
68 
69  void readBlockData(std::vector<uint8_t>& buffer, size_t size);
70  bool readStreamDeclaration(const std::vector<uint8_t>& buffer, size_t stream_idx, size_t pos, size_t size);
71  BlockHeader skip(const BlockHeader& header);
72 
73  public:
74  Input();
75  ~Input();
76 
77  void init(std::istream& input);
78 
79  size_t size() const;
80  Stream& operator [] (size_t index) const;
81 
83  DataStream& getDataStream( const std::string& name ) const;
84 
86  static void readBlockData(std::istream& stream, std::vector<uint8_t>& buffer, size_t size);
87  static BlockHeader skip(std::istream& stream, const BlockHeader& header);
88  };
89 
90 
91  class Stream
92  : boost::noncopyable
93  {
94  friend class Input;
95 
96  std::istream& m_input;
97  StreamType m_type;
98  size_t m_index;
99 
100  size_t m_size,
101  m_datasize,
102  m_begin,
103  m_end;
104 
105  protected:
106  Stream(std::istream& input, StreamType type = UnknownStreamType, size_t index = Input::nstream)
107  : m_input(input)
108  , m_type(type)
109  , m_index(index)
110  , m_size(0), m_datasize(0)
111  , m_begin(Input::npos)
112  , m_end(Input::npos) {}
113 
114  virtual BlockHeader newDataBlock(BlockHeader header) = 0;
117  void newBlockAt(size_t pos, size_t size)
118  {
119  if (m_begin == Input::npos)
120  m_begin = m_end = pos;
121  else if (m_end == Input::npos || m_end < pos)
122  m_end = pos;
123 
124  m_datasize += m_datasize;
125  ++m_size;
126  }
127 
128  std::istream& getInputStream() { return m_input; }
129  public:
130  virtual ~Stream() {}
131 
132  bool isValid() const { return m_type != UnknownStreamType; }
133 
134  StreamType getType() const { return m_type; }
135  size_t getIndex() const { return m_index; }
136  size_t getBeginPos() const { return m_begin; }
137  size_t getEndPos() const { return m_end; }
138  size_t getSize() const { return m_size; }
139  size_t getDataSize() const { return m_datasize; }
140  };
141 
142  class DataStream : public Stream
143  {
144  friend class Input;
145  std::string m_name;
146  std::string m_type_name;
147 
148  Typelib::Type* m_type;
149  Typelib::Registry* m_registry;
150 
151  BlockHeader m_firstheader;
152  base::Time m_begin, m_end;
153 
154  protected:
155  DataStream
156  ( std::istream& input, size_t index
157  , const std::string& name, const std::string& type_name
158  , Typelib::Registry* registry );
159 
160  public:
161  ~DataStream();
162  std::string getName() const;
163  std::string getTypeName() const;
164  Typelib::Type const* getType() const;
165  Typelib::Registry const* getTypeRegistry() const;
166 
167  DataInputIterator begin();
168  DataInputIterator end();
169 
170  base::Time getBeginTime() const;
171  base::Time getEndTime() const;
172 
173  virtual BlockHeader newDataBlock(BlockHeader header);
174  };
175 
176  class ControlStream : public Stream
177  {
178  friend class Input;
179  protected:
180  ControlStream(std::istream& input, size_t index);
181 
182  public:
183  virtual BlockHeader newDataBlock(BlockHeader header);
184  };
185 
186 
187 
188 
190  {
191  friend class DataStream;
192 
193  Typelib::Type const* m_sample_type;
194  std::istream* m_input;
195  size_t m_index;
196  size_t m_pos;
197 
198  BlockHeader m_block_header;
199  SampleHeader m_sample_header;
200  std::vector<uint8_t> m_buffer;
201 
202  private:
204  ( Typelib::Type const* sample_type
205  , std::istream& input, size_t stream_index
206  , size_t first_block, const BlockHeader& first_header);
207  void failed();
208  void readCurBlock(size_t size);
209 
210  public:
213 
214  base::Time getRealtime() const;
215  base::Time getTimestamp() const;
216 
217  bool isValid() const;
218 
219  size_t getPos() const { return m_pos; }
220 
221  size_t getDataSize() const;
222  const uint8_t* getData() const;
223  template<typename T>
224  T getData() const
225  {
226  T out;
227  getData<T>(out);
228  return out;
229  }
230 
231  template<typename T>
232  void getData(T& out) const
233  {
234  std::vector<uint8_t> buffer(m_buffer.begin() + SAMPLE_HEADER_SIZE, m_buffer.end());
235  Typelib::Value v(&out, *m_sample_type);
236  Typelib::load(v, buffer);
237  }
238 
239  DataInputIterator& operator += (size_t offset);
240  DataInputIterator operator + (size_t offset) const;
241  DataInputIterator& operator ++();
242 
243  bool operator == (const DataInputIterator& with) const;
244  bool operator != (const DataInputIterator& with) const
245  { return ! ((*this) == with); }
246  };
247 
248 
249 
250 
251 
252  class FileException : public std::exception
253  {
254  size_t m_pos;
255  public:
256  FileException(size_t pos = Input::npos)
257  : m_pos(pos) {}
258  ~FileException() throw() {}
259 
260  virtual void output(std::ostream& display) throw();
261  };
262  struct Truncated : public FileException
263  { virtual void output(std::ostream& display) throw(); };
264  struct BadMagic : public FileException
265  { virtual void output(std::ostream& display) throw(); };
266 
267  class BadStream : public FileException
268  {
269  const size_t m_index;
270  public:
271  BadStream(size_t index, size_t pos)
272  : FileException(pos), m_index(index) {}
273  virtual void output(std::ostream& display) throw();
274  };
275 
277  {
278  const size_t m_type;
279  public:
280  BadBlockType(size_t type, size_t pos)
281  : FileException(pos), m_type(type) {}
282  virtual void output(std::ostream& display) throw();
283  };
285  {
286  const BlockType m_type;
287 
288  public:
290  : FileException(pos), m_type(type) {}
291  ~DataBlockException() throw() {}
292 
293  virtual void output(std::ostream& display) throw();
294  };
296  {
298  : DataBlockException(type, pos) {}
299 
300  virtual void output(std::ostream& display) throw();
301  };
303  {
305  : DataBlockException(type, pos) {}
306 
307  virtual void output(std::ostream& display) throw();
308  };
309  struct BadStreamType : public FileException
310  {
311  BadStreamType(size_t pos)
312  : FileException(pos) {}
313 
314  virtual void output(std::ostream& display) throw();
315  };
316  struct NoSuchStream : public FileException
317  {
318  const std::string name;
319 
320  NoSuchStream(const std::string& name) : name(name) {};
321  ~NoSuchStream() throw() {}
322  virtual void output(std::ostream& display) throw();
323  };
324 }
325 
326 #endif
327 
Definition: Read.hpp:264
DataBlockException(BlockType type, size_t pos)
Definition: Read.hpp:289
Definition: Read.hpp:142
BadStreamType(size_t pos)
Definition: Read.hpp:311
bool isValid() const
Definition: Read.hpp:132
~FileException()
Definition: Read.hpp:258
Definition: Format.hpp:122
StreamType getType() const
Definition: Read.hpp:134
size_t getPos() const
Definition: Read.hpp:219
DataSizeMismatch(BlockType type, size_t pos)
Definition: Read.hpp:304
void newBlockAt(size_t pos, size_t size)
Definition: Read.hpp:117
Definition: Read.hpp:252
Definition: Read.hpp:262
Definition: Read.hpp:284
~DataBlockException()
Definition: Read.hpp:291
StreamType
Definition: Format.hpp:157
uint8_t type
Definition: Format.hpp:106
Definition: Read.hpp:302
Definition: Read.hpp:267
uint16_t stream_idx
Definition: Format.hpp:108
Definition: Format.hpp:141
size_t getSize() const
Definition: Read.hpp:138
size_t getIndex() const
Definition: Read.hpp:135
BadDataSize(BlockType type, size_t pos)
Definition: Read.hpp:297
Definition: InputDataStream.hpp:8
Definition: Read.hpp:316
Definition: Format.hpp:159
Definition: Read.hpp:176
std::istream & getInputStream()
Definition: Read.hpp:128
T getData() const
Definition: Read.hpp:224
Definition: Read.hpp:309
size_t getBeginPos() const
Definition: Read.hpp:136
BlockType
Definition: Format.hpp:150
Definition: Read.hpp:189
virtual ~Stream()
Definition: Read.hpp:130
FileException(size_t pos=Input::npos)
Definition: Read.hpp:256
BadStream(size_t index, size_t pos)
Definition: Read.hpp:271
Stream(std::istream &input, StreamType type=UnknownStreamType, size_t index=Input::nstream)
Definition: Read.hpp:106
size_t getEndPos() const
Definition: Read.hpp:137
NoSuchStream(const std::string &name)
Definition: Read.hpp:320
Definition: FileStream.hpp:7
const std::string name
Definition: Read.hpp:318
Definition: Read.hpp:57
Stream * stream
Definition: MultiFileIndex.hpp:150
BadBlockType(size_t type, size_t pos)
Definition: Read.hpp:280
~NoSuchStream()
Definition: Read.hpp:321
Definition: Read.hpp:295
size_t getDataSize() const
Definition: Read.hpp:139
void getData(T &out) const
Definition: Read.hpp:232
Definition: Read.hpp:276
Definition: Read.hpp:91