MrSID Decode SDK for LiDAR Reference Manual  1.1.5.5244
Stream.h
Go to the documentation of this file.
1 //
2 // This code is property of Celartem, Inc dba Extensis.
3 // Copyright (c) 1995-2023. All rights reserved.
4 // Use permitted only under license from Extensis.
5 //
6 /* PUBLIC */
7 
8 #ifndef __LIDAR_STREAM_H__
9 #define __LIDAR_STREAM_H__
10 
11 #include "lidar/IO.h"
12 #include "lidar/Endian.h"
13 
14 LT_BEGIN_LIDAR_NAMESPACE
15 
19 class Stream
20 {
21  SIMPLE_OBJECT(Stream);
22 public:
26  enum { DefaultBufferSize = 1 << 12 };
27 
31  enum Mode
32  {
34  MODE_SET = 0,
36  MODE_CUR = 1,
38  MODE_END = 2
39  };
40 
45 
49  IO *getIO();
50 
51 protected:
52  ~Stream(void);
53  Stream(void);
54 
55  typedef unsigned char byte_t;
56 
57  IO *m_io;
58  size_t m_size;
63 };
64 
71 class StreamReader : public Stream
72 {
73  SIMPLE_OBJECT(StreamReader);
74 public:
76  StreamReader(void);
77 
87  void init(IO *io, bool open, size_t bufferSize = DefaultBufferSize);
96  void init(const IO::Location &location,
97  size_t bufferSize = DefaultBufferSize);
98 
106  void open(size_t bufferSize = DefaultBufferSize);
107 
113  void close(void);
114 
121  void flush(void);
122 
131  void seek(offset_type offset, Mode whence);
132 
139 
149  size_t read(void *buf, size_t nbytes);
150 
159  template<typename TYPE> bool get_le(TYPE &value)
160  {
161  size_t nbytes = read(&value, sizeof(TYPE));
163  Endian::swap<sizeof(TYPE)>(&value);
164  return nbytes == sizeof(TYPE);
165  }
166 
175  template<typename TYPE> bool get_be(TYPE &value)
176  {
177  size_t nbytes = read(&value, sizeof(TYPE));
179  Endian::swap<sizeof(TYPE)>(&value);
180  return nbytes == sizeof(TYPE);
181  }
182 
194  bool get_str(char *&line, size_t &length);
195 };
196 
200 class StreamWriter : public Stream
201 {
202  SIMPLE_OBJECT(StreamWriter);
203 public:
206 
216  void init(IO *io, bool open, size_t bufferSize = DefaultBufferSize);
217 
225  void open(size_t bufferSize = DefaultBufferSize);
226 
233  void close(bool doFlush = true);
234 
240  void flush(void);
241 
251  void seek(offset_type offset, Mode whence);
252 
259 
269  size_t write(const void *buf, size_t nbytes);
270 
279  template<typename TYPE> bool put_le(TYPE value)
280  {
282  Endian::swap<sizeof(TYPE)>(&value);
283  return write(&value, sizeof(TYPE)) == sizeof(TYPE);
284  }
285 
294  template<typename TYPE> bool put_be(TYPE value)
295  {
297  Endian::swap<sizeof(TYPE)>(&value);
298  return write(&value, sizeof(TYPE)) == sizeof(TYPE);
299  }
300 
311  bool put_str(const char *str, size_t length = static_cast<size_t>(-1));
312 
323  bool copy(IO *io, offset_type offset, offset_type length);
324 
333  bool copy(IO::Location &location);
334 
344  bool copy(StreamReader &stream, offset_type length);
345 };
346 
347 LT_END_LIDAR_NAMESPACE
348 #endif // __LIDAR_STREAM_H__
#define HOST_IS_LITTLE_ENDIAN
Use this macro when you need to know the host is little endian.
Definition: Endian.h:18
#define HOST_IS_BIG_ENDIAN
Use this macro when you need to know the host is big endian.
Definition: Endian.h:16
IO is the base class for binary input and output.
Definition: IO.h:26
lt_int64 offset_type
Integer data type for file offsets and sizes.
Definition: IO.h:32
Location is a helper structure for holding the location of data in a IO object.
Definition: IO.h:137
Stream is the base class for buffered input and output for IO objects.
Definition: Stream.h:20
offset_type m_pos
Definition: Stream.h:59
size_t m_size
Definition: Stream.h:58
@ DefaultBufferSize
Definition: Stream.h:26
Stream(void)
IO * m_io
Definition: Stream.h:57
byte_t * m_cur
Definition: Stream.h:61
byte_t * m_tail
Definition: Stream.h:62
byte_t * m_head
Definition: Stream.h:60
~Stream(void)
Mode
Seek offset origin.
Definition: Stream.h:32
@ MODE_SET
seek from the begining of the file
Definition: Stream.h:34
@ MODE_END
seek from the end of the file
Definition: Stream.h:38
@ MODE_CUR
seek from the current location in the file
Definition: Stream.h:36
IO * getIO()
Get the underlying IO object.
unsigned char byte_t
Definition: Stream.h:55
IO::offset_type offset_type
Integer data type for seek() and tell() offsets.
Definition: Stream.h:44
StreamReader implements buffered reads from IO objects.
Definition: Stream.h:72
size_t read(void *buf, size_t nbytes)
Read data.
void init(IO *io, bool open, size_t bufferSize=DefaultBufferSize)
Initalize the reader.
void close(void)
Close the IO object.
bool get_str(char *&line, size_t &length)
Read the next line of text.
void open(size_t bufferSize=DefaultBufferSize)
Open the IO object.
~StreamReader(void)
bool get_le(TYPE &value)
Read little endian data.
Definition: Stream.h:159
void flush(void)
Flush the memory buffer.
StreamReader(void)
void seek(offset_type offset, Mode whence)
Set the file offset.
bool get_be(TYPE &value)
Read big endian data.
Definition: Stream.h:175
void init(const IO::Location &location, size_t bufferSize=DefaultBufferSize)
Initalize the reader.
offset_type tell(void)
Get the current file offset.
StreamWriter implements buffered writes to IO objects.
Definition: Stream.h:201
bool copy(IO *io, offset_type offset, offset_type length)
Copy data from an IO object.
void close(bool doFlush=true)
Close the IO object.
void flush(void)
Flush the memory buffer.
void seek(offset_type offset, Mode whence)
Set the file offset.
bool put_be(TYPE value)
Write little endian data.
Definition: Stream.h:294
void init(IO *io, bool open, size_t bufferSize=DefaultBufferSize)
Initalize the writer.
bool copy(StreamReader &stream, offset_type length)
Copy data from an ReaderStream.
bool put_str(const char *str, size_t length=static_cast< size_t >(-1))
Write a string.
~StreamWriter(void)
offset_type tell(void)
Get the current file offset.
size_t write(const void *buf, size_t nbytes)
Write data.
bool copy(IO::Location &location)
Copy data from an IO object.
StreamWriter(void)
bool put_le(TYPE value)
Write little endian data.
Definition: Stream.h:279
void open(size_t bufferSize=DefaultBufferSize)
Open the IO object.

Extensis