LRAUV  revA
InStream.h
Go to the documentation of this file.
1 
10 #ifndef INSTREAM_H_
11 #define INSTREAM_H_
12 
13 #include "utils/Str.h"
14 
29 class InStream
30 {
31 public:
32 
34  virtual ~InStream()
35  {}
36  ;
37 
39  virtual InStream& read( char* buffer, size_t num ) = 0;
40 
41 
43  virtual const char* getName() = 0;
44 
46  virtual bool isReadable() = 0;
47 
49  InStream& readChar( signed char& value )
50  {
51  return read( ( char* )&value, sizeof( value ) );
52  }
53 
55  InStream& readCharHex( signed char& byte );
56 
58  InStream& readUCharHex( unsigned char& byte );
59 
60  // Fills buffer until: buffer size is exceeded, or (CR/LF) character is received
61  // Leading LF characters are skipped.
62  virtual InStream& readLine( char* buf, unsigned int bufsize );
63 
65  InStream& readShort( short& value )
66  {
67  return read( ( char* )&value, sizeof( value ) );
68  }
69 
71  InStream& readShort( unsigned short& value )
72  {
73  return read( ( char* )&value, sizeof( value ) );
74  }
75 
77  InStream& readShortHex( short& word );
78 
80  InStream& readUShortHex( unsigned short& value );
81 
87  InStream& readUShortCompact( unsigned short& value );
88 
91  InStream& readInt24( int& medInt )
92  {
93  InStream& result = read( ( char* ) & medInt, 3 );
94  if( ( medInt & 0x00800000 ) == 0 )
95  {
96  medInt &= 0x00FFFFFF;
97  }
98  else
99  {
100  medInt |= 0xFF000000;
101  }
102  return result;
103  }
104 
106  InStream& readInt( int& value )
107  {
108  return read( ( char* )&value, sizeof( value ) );
109  }
110 
112  InStream& readLongLong( long long& value )
113  {
114  return read( ( char* )&value, sizeof( value ) );
115  }
116 
128  InStream& readLongLongCompact( long long& value );
129 
131  InStream& readFloat( float& value )
132  {
133  return read( ( char* )&value, sizeof( value ) );
134  }
135 
137  InStream& readFloat2( float& value )
138  {
139  value = 0;
140  return read( ( ( char* )&value ) + 2, 2 );
141  }
142 
144  InStream& readFloat3( float& value )
145  {
146  value = 0;
147  return read( ( ( char* )&value ) + 1, 3 );
148  }
149 
151  InStream& readDouble( double& value )
152  {
153  return read( ( char* )&value, sizeof( value ) );
154  }
155 
156  virtual InStream& readUntil( char* buf, unsigned int num, unsigned char match );
157 
159  virtual bool eof() = 0;
160 
162  size_t bytesRead()
163  {
164  return lastRead_;
165  }
166 
168  size_t getBytesRead()
169  {
170  return lastRead_;
171  }
172 
175  {
176  return totalBytesRead_;
177  }
178 
179 protected:
180 
183  : lastRead_( 0 ),
184  totalBytesRead_( 0 )
185  {}
186  ;
187 
188  inline void zeroBytesRead()
189  {
190  lastRead_ = 0;
191  }
192 
193  inline void incBytesRead()
194  {
195  ++lastRead_;
196  ++totalBytesRead_;
197  }
198 
199  inline void incBytesRead( size_t incRead )
200  {
201  lastRead_ += incRead;
202  totalBytesRead_ += incRead;
203  }
204 
205  inline void setBytesRead( size_t lastRead )
206  {
207  lastRead_ = lastRead;
208  totalBytesRead_ += lastRead;
209  }
210 
211  inline void zeroTotalBytesRead()
212  {
213  zeroBytesRead();
214  totalBytesRead_ = 0;
215  }
216 
217 private:
218 
219  size_t lastRead_;
220 
222 
223  // Note that the copy constructor below is private and not given a body.
224  // Any attempt to call it will return a compiler error.
225  InStream( const InStream& old ); // disallow copy constructor
226 
227 };
228 
229 class NullInStream: public InStream
230 {
231 public:
232 
234  virtual InStream& read( char* buffer, unsigned int num )
235  {
236  return *this;
237  }
238 
240  virtual bool isReadable()
241  {
242  return false;
243  }
244 
246  virtual bool eof()
247  {
248  return true;
249  }
250 
251 };
252 
253 #endif /*INSTREAM_H_*/
InStream & readShort(unsigned short &value)
reads a single short from the input buffer/stream
Definition: InStream.h:71
Definition: InStream.h:229
InStream & readFloat2(float &value)
reads a 2-byte float to the input buffer/stream
Definition: InStream.h:137
InStream & readFloat3(float &value)
reads a 3-byte float to the input buffer/stream
Definition: InStream.h:144
size_t bytesRead()
Indicates the number of bytes read in the last operation.
Definition: InStream.h:162
InStream & readUCharHex(unsigned char &byte)
reads a single unsigned char from the input buffer/stream as 2 hex chars
Definition: InStream.cpp:24
size_t getBytesRead()
Indicates the number of bytes read in the last operation.
Definition: InStream.h:168
void incBytesRead()
Definition: InStream.h:193
InStream & readFloat(float &value)
reads a 4-byte float to the input buffer/stream
Definition: InStream.h:131
virtual ~InStream()
Destructor.
Definition: InStream.h:34
InStream & readInt24(int &medInt)
reads a 3-byte integer to the input buffer/stream only works on little-endian machines! ...
Definition: InStream.h:91
InStream & readCharHex(signed char &byte)
reads a single unsigned char from the input buffer/stream as 2 hex chars
Definition: InStream.cpp:15
size_t lastRead_
Definition: InStream.h:219
virtual bool isReadable()=0
indicates whether the stream can actually be read from.
virtual InStream & read(char *buffer, unsigned int num)
doesn't really read num bytes from stream to the buffer
Definition: InStream.h:234
virtual InStream & readLine(char *buf, unsigned int bufsize)
Definition: InStream.cpp:33
InStream & readUShortHex(unsigned short &value)
reads a single unsigned short from the input buffer/stream as 4 hex chars
Definition: InStream.cpp:76
This is a very abstract class that can be implemented by classes that want to receive stream input...
Definition: InStream.h:29
size_t totalBytesRead_
Definition: InStream.h:221
void incBytesRead(size_t incRead)
Definition: InStream.h:199
InStream & readShort(short &value)
reads a single short from the input buffer/stream
Definition: InStream.h:65
InStream()
Protected Constructor for abstract class.
Definition: InStream.h:182
InStream & readUShortCompact(unsigned short &value)
reads an unsigned short integer as 1 to 3 bytes, encoded as follows: xxxxxxx0 - 7 bit value...
Definition: InStream.cpp:90
virtual bool isReadable()
indicates that the stream can't actually be read from.
Definition: InStream.h:240
InStream & readLongLongCompact(long long &value)
reads a signed long long integer as 1 to 9 bytes, encoded as follows: 0xxxxxxx - 7 bit value...
Definition: InStream.cpp:130
InStream & readDouble(double &value)
reads a 8-byte double to the input buffer/stream
Definition: InStream.h:151
void zeroTotalBytesRead()
Definition: InStream.h:211
virtual bool eof()
Indicates that the end of file has been reached.
Definition: InStream.h:246
virtual bool eof()=0
Indicates if the end of file has been reached.
void zeroBytesRead()
Definition: InStream.h:188
InStream & readInt(int &value)
reads a 4-byte integer to the input buffer/stream
Definition: InStream.h:106
void setBytesRead(size_t lastRead)
Definition: InStream.h:205
virtual InStream & read(char *buffer, size_t num)=0
reads num bytes from stream to the buffer
virtual const char * getName()=0
returns name of the stream
InStream & readLongLong(long long &value)
reads a 8-byte integer to the input buffer/stream
Definition: InStream.h:112
InStream & readShortHex(short &word)
reads a single short from the input buffer/stream as 4 hex chars
Definition: InStream.cpp:66
size_t getTotalBytesRead()
Indicates the total number of bytes read;.
Definition: InStream.h:174
virtual InStream & readUntil(char *buf, unsigned int num, unsigned char match)
Definition: InStream.cpp:242
InStream & readChar(signed char &value)
reads a single byte to the input buffer/stream
Definition: InStream.h:49