00001
00002
00003
00004
00005
00006
00007
00008
00010
00011
00012 #ifndef __LIDAR_STREAM_H__
00013 #define __LIDAR_STREAM_H__
00014
00015 #include "lidar/IO.h"
00016 #include "lidar/Endian.h"
00017
00018 LT_BEGIN_LIDAR_NAMESPACE
00019
00023 class Stream
00024 {
00025 SIMPLE_OBJECT(Stream);
00026 public:
00030 enum { DefaultBufferSize = 1 << 12 };
00031
00035 enum Mode
00036 {
00038 MODE_SET = 0,
00040 MODE_CUR = 1,
00042 MODE_END = 2
00043 };
00044
00048 typedef IO::offset_type offset_type;
00049
00053 IO *getIO();
00054
00055 protected:
00056 ~Stream(void);
00057 Stream(void);
00058
00059 typedef unsigned char byte_t;
00060
00061 IO *m_io;
00062 size_t m_size;
00063 offset_type m_pos;
00064 byte_t *m_head;
00065 byte_t *m_cur;
00066 byte_t *m_tail;
00067 };
00068
00075 class StreamReader : public Stream
00076 {
00077 SIMPLE_OBJECT(StreamReader);
00078 public:
00079 ~StreamReader(void);
00080 StreamReader(void);
00081
00091 void init(IO *io, bool open, size_t bufferSize = DefaultBufferSize);
00100 void init(const IO::Location &location,
00101 size_t bufferSize = DefaultBufferSize);
00102
00110 void open(size_t bufferSize = DefaultBufferSize);
00111
00117 void close(void);
00118
00125 void flush(void);
00126
00135 void seek(offset_type offset, Mode whence);
00136
00142 offset_type tell(void);
00143
00153 size_t read(void *buf, size_t nbytes);
00154
00163 template<typename TYPE> bool get_le(TYPE &value)
00164 {
00165 size_t nbytes = read(&value, sizeof(TYPE));
00166 if(HOST_IS_BIG_ENDIAN)
00167 Endian::swap<sizeof(TYPE)>(&value);
00168 return nbytes == sizeof(TYPE);
00169 }
00170
00179 template<typename TYPE> bool get_be(TYPE &value)
00180 {
00181 size_t nbytes = read(&value, sizeof(TYPE));
00182 if(HOST_IS_LITTLE_ENDIAN)
00183 Endian::swap<sizeof(TYPE)>(&value);
00184 return nbytes == sizeof(TYPE);
00185 }
00186
00198 bool get_str(char *&line, size_t &length);
00199 };
00200
00204 class StreamWriter : public Stream
00205 {
00206 SIMPLE_OBJECT(StreamWriter);
00207 public:
00208 ~StreamWriter(void);
00209 StreamWriter(void);
00210
00220 void init(IO *io, bool open, size_t bufferSize = DefaultBufferSize);
00221
00229 void open(size_t bufferSize = DefaultBufferSize);
00230
00237 void close(bool doFlush = true);
00238
00244 void flush(void);
00245
00255 void seek(offset_type offset, Mode whence);
00256
00262 offset_type tell(void);
00263
00273 size_t write(const void *buf, size_t nbytes);
00274
00283 template<typename TYPE> bool put_le(TYPE value)
00284 {
00285 if(HOST_IS_BIG_ENDIAN)
00286 Endian::swap<sizeof(TYPE)>(&value);
00287 return write(&value, sizeof(TYPE)) == sizeof(TYPE);
00288 }
00289
00298 template<typename TYPE> bool put_be(TYPE value)
00299 {
00300 if(HOST_IS_LITTLE_ENDIAN)
00301 Endian::swap<sizeof(TYPE)>(&value);
00302 return write(&value, sizeof(TYPE)) == sizeof(TYPE);
00303 }
00304
00315 bool put_str(const char *str, size_t length = static_cast<size_t>(-1));
00316
00327 bool copy(IO *io, offset_type offset, offset_type length);
00328
00337 bool copy(IO::Location &location);
00338
00348 bool copy(StreamReader &stream, offset_type length);
00349 };
00350
00351 LT_END_LIDAR_NAMESPACE
00352 #endif // __LIDAR_STREAM_H__