MrSID Decode SDK for LiDAR Reference Manual  1.1.4.4709
Stream.h
Go to the documentation of this file.
1 /* //////////////////////////////////////////////////////////////////////////
2 // //
3 // This code is Copyright (c) 2008-2010 LizardTech, Inc, 1008 Western //
4 // Avenue, Suite 200, Seattle, WA 98104. Unauthorized use or distribution //
5 // prohibited. Access to and use of this code is permitted only under //
6 // license from LizardTech, Inc. Portions of the code are protected by //
7 // US and foreign patents and other filings. All Rights Reserved. //
8 // //
10 /* PUBLIC */
11 
12 #ifndef __LIDAR_STREAM_H__
13 #define __LIDAR_STREAM_H__
14 
15 #include "lidar/IO.h"
16 #include "lidar/Endian.h"
17 
18 LT_BEGIN_LIDAR_NAMESPACE
19 
23 class Stream
24 {
25  SIMPLE_OBJECT(Stream);
26 public:
30  enum { DefaultBufferSize = 1 << 12 };
31 
35  enum Mode
36  {
38  MODE_SET = 0,
40  MODE_CUR = 1,
43  };
44 
49 
53  IO *getIO();
54 
55 protected:
56  ~Stream(void);
57  Stream(void);
58 
59  typedef unsigned char byte_t;
60 
61  IO *m_io;
62  size_t m_size;
63  offset_type m_pos;
64  byte_t *m_head;
65  byte_t *m_cur;
66  byte_t *m_tail;
67 };
68 
75 class StreamReader : public Stream
76 {
77  SIMPLE_OBJECT(StreamReader);
78 public:
79  ~StreamReader(void);
80  StreamReader(void);
81 
91  void init(IO *io, bool open, size_t bufferSize = DefaultBufferSize);
100  void init(const IO::Location &location,
101  size_t bufferSize = DefaultBufferSize);
102 
110  void open(size_t bufferSize = DefaultBufferSize);
111 
117  void close(void);
118 
125  void flush(void);
126 
135  void seek(offset_type offset, Mode whence);
136 
142  offset_type tell(void);
143 
153  size_t read(void *buf, size_t nbytes);
154 
163  template<typename TYPE> bool get_le(TYPE &value)
164  {
165  size_t nbytes = read(&value, sizeof(TYPE));
167  Endian::swap<sizeof(TYPE)>(&value);
168  return nbytes == sizeof(TYPE);
169  }
170 
179  template<typename TYPE> bool get_be(TYPE &value)
180  {
181  size_t nbytes = read(&value, sizeof(TYPE));
183  Endian::swap<sizeof(TYPE)>(&value);
184  return nbytes == sizeof(TYPE);
185  }
186 
198  bool get_str(char *&line, size_t &length);
199 };
200 
204 class StreamWriter : public Stream
205 {
206  SIMPLE_OBJECT(StreamWriter);
207 public:
208  ~StreamWriter(void);
209  StreamWriter(void);
210 
220  void init(IO *io, bool open, size_t bufferSize = DefaultBufferSize);
221 
229  void open(size_t bufferSize = DefaultBufferSize);
230 
237  void close(bool doFlush = true);
238 
244  void flush(void);
245 
255  void seek(offset_type offset, Mode whence);
256 
262  offset_type tell(void);
263 
273  size_t write(const void *buf, size_t nbytes);
274 
283  template<typename TYPE> bool put_le(TYPE value)
284  {
286  Endian::swap<sizeof(TYPE)>(&value);
287  return write(&value, sizeof(TYPE)) == sizeof(TYPE);
288  }
289 
298  template<typename TYPE> bool put_be(TYPE value)
299  {
301  Endian::swap<sizeof(TYPE)>(&value);
302  return write(&value, sizeof(TYPE)) == sizeof(TYPE);
303  }
304 
315  bool put_str(const char *str, size_t length = static_cast<size_t>(-1));
316 
327  bool copy(IO *io, offset_type offset, offset_type length);
328 
337  bool copy(IO::Location &location);
338 
348  bool copy(StreamReader &stream, offset_type length);
349 };
350 
351 LT_END_LIDAR_NAMESPACE
352 #endif // __LIDAR_STREAM_H__
IO::offset_type offset_type
Integer data type for seek() and tell() offsets.
Definition: Stream.h:48
byte_t * m_tail
Definition: Stream.h:66
Mode
Seek offset origin.
Definition: Stream.h:35
Stream is the base class for buffered input and output for IO objects.
Definition: Stream.h:23
lt_int64 offset_type
Integer data type for file offsets and sizes.
Definition: IO.h:36
IO is the base class for binary input and output.
Definition: IO.h:29
unsigned char byte_t
Definition: Stream.h:59
~Stream(void)
bool get_le(TYPE &value)
Read little endian data.
Definition: Stream.h:163
StreamWriter implements buffered writes to IO objects.
Definition: Stream.h:204
offset_type m_pos
Definition: Stream.h:63
byte_t * m_cur
Definition: Stream.h:65
bool put_be(TYPE value)
Write little endian data.
Definition: Stream.h:298
StreamReader implements buffered reads from IO objects.
Definition: Stream.h:75
byte_t * m_head
Definition: Stream.h:64
Location is a helper structure for holding the location of data in a IO object.
Definition: IO.h:140
IO * getIO()
Get the underlying IO object.
seek from the current location in the file
Definition: Stream.h:40
#define HOST_IS_BIG_ENDIAN
Use this macro when you need to know the host is big endian.
Definition: Endian.h:20
#define HOST_IS_LITTLE_ENDIAN
Use this macro when you need to know the host is little endian.
Definition: Endian.h:22
IO * m_io
Definition: Stream.h:61
bool put_le(TYPE value)
Write little endian data.
Definition: Stream.h:283
bool get_be(TYPE &value)
Read big endian data.
Definition: Stream.h:179
seek from the end of the file
Definition: Stream.h:42
size_t m_size
Definition: Stream.h:62
seek from the begining of the file
Definition: Stream.h:38
Stream(void)

LizardTech