MrSID Decode SDK for LiDAR Reference Manual  1.1.3.4427
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;
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
void close(bool doFlush=true)
Close the IO object.
void seek(offset_type offset, Mode whence)
Set the file offset.
void open(size_t bufferSize=DefaultBufferSize)
Open the IO object.
bool get_le(TYPE &value)
Read little endian data.
Definition: Stream.h:163
~StreamWriter(void)
Stream is the base class for buffered input and output for IO objects.
Definition: Stream.h:23
#define HOST_IS_BIG_ENDIAN
Use this macro when you need to know the host is big endian.
Definition: Endian.h:20
Mode
Seek offset origin.
Definition: Stream.h:35
IO * m_io
Definition: Stream.h:61
size_t write(const void *buf, size_t nbytes)
Write data.
seek from the begining of the file
Definition: Stream.h:38
offset_type m_pos
Definition: Stream.h:63
StreamWriter(void)
bool get_be(TYPE &value)
Read big endian data.
Definition: Stream.h:179
#define HOST_IS_LITTLE_ENDIAN
Use this macro when you need to know the host is little endian.
Definition: Endian.h:22
byte_t * m_cur
Definition: Stream.h:65
unsigned char byte_t
Definition: Stream.h:59
~StreamReader(void)
void init(IO *io, bool open, size_t bufferSize=DefaultBufferSize)
Initalize the writer.
byte_t * m_head
Definition: Stream.h:64
size_t read(void *buf, size_t nbytes)
Read data.
bool put_be(TYPE value)
Write little endian data.
Definition: Stream.h:298
size_t m_size
Definition: Stream.h:62
bool copy(IO *io, offset_type offset, offset_type length)
Copy data from an IO object.
Location is a helper structure for holding the location of data in a IO object.
Definition: IO.h:140
byte_t * m_tail
Definition: Stream.h:66
StreamWriter implements buffered writes to IO objects.
Definition: Stream.h:204
~Stream(void)
void seek(offset_type offset, Mode whence)
Set the file offset.
void open(size_t bufferSize=DefaultBufferSize)
Open the IO object.
bool get_str(char *&line, size_t &length)
Read the next line of text.
bool put_le(TYPE value)
Write little endian data.
Definition: Stream.h:283
seek from the current location in the file
Definition: Stream.h:40
offset_type tell(void)
Get the current file offset.
seek from the end of the file
Definition: Stream.h:42
IO is the base class for binary input and output.
Definition: IO.h:29
void flush(void)
Flush the memory buffer.
bool put_str(const char *str, size_t length=static_cast< size_t >(-1))
Write a string.
offset_type tell(void)
Get the current file offset.
void close(void)
Close the IO object.
Stream(void)
void flush(void)
Flush the memory buffer.
StreamReader implements buffered reads from IO objects.
Definition: Stream.h:75
void init(IO *io, bool open, size_t bufferSize=DefaultBufferSize)
Initalize the reader.
IO * getIO()
Get the underlying IO object.
lt_int64 offset_type
Integer data type for file offsets and sizes.
Definition: IO.h:36
StreamReader(void)

LizardTech