LRAUV  revA
StrIOStream.h
Go to the documentation of this file.
1 
10 #ifndef STRIOSTREAM_H_
11 #define STRIOSTREAM_H_
12 
13 #include "IOStream.h"
14 #include "utils/Str.h"
15 
24 class StrIOStream : public IOStream
25 {
26 public:
27 
29  StrIOStream( Str& str )
30  : str_( str ),
31  readPos_( 0 )
32  {}
33 
34  virtual ~StrIOStream()
35  {}
36 
38  virtual InStream& read( char* buffer, size_t num )
39  {
40  if( readPos_ >= str_.length() )
41  {
42  num = 0;
43  }
44  if( str_.length() - readPos_ < num )
45  {
46  num = str_.length() - readPos_;
47  }
48  strncpy( buffer, str_.cStr() + readPos_, num );
49  //buffer[num] = 0; // Can cause buffer overflow!
50  readPos_ += num;
51  setBytesRead( num );
52  return *this;
53  }
54 
55  const char* getName()
56  {
57  return "String";
58  }
59 
60  void clear()
61  {
62  rewind();
64  }
65 
66  void rewind()
67  {
68  readPos_ = 0;
69  }
70 
72  virtual bool isReadable()
73  {
74  return !eof();
75  }
76 
78  virtual bool eof()
79  {
80  return str_.length() <= readPos_;
81  }
82 
84  virtual OutStream& write( const char* buffer, size_t num )
85  {
87  str_ += Str( buffer, num );
89  return *this;
90  }
91 
92  bool isWritable()
93  {
94  return true;
95  }
96 
97 protected:
99  size_t readPos_;
100 
101 private:
102  // Note that the copy constructor below is private and not given a body.
103  // Any attempt to call it will return a compiler error.
104  StrIOStream( const StrIOStream& old ); // disallow copy constructor
105 
106 };
107 
108 #endif /*STRIOSTREAM_H_*/
size_t readPos_
Definition: StrIOStream.h:99
virtual OutStream & write(const char *buffer, size_t num)
writes num bytes from buffer to the output buffer/stream
Definition: StrIOStream.h:84
This is a rather abstract class that can be implemented by classes that want to send and receive stre...
Definition: IOStream.h:30
size_t size() const
Definition: Str.h:200
virtual bool isReadable()
indicates whether the stream can actually be read from.
Definition: StrIOStream.h:72
virtual ~StrIOStream()
Definition: StrIOStream.h:34
void clear()
Definition: StrIOStream.h:60
size_t length() const
Definition: Str.h:196
StrIOStream(Str &str)
Constructor.
Definition: StrIOStream.h:29
Contains the IOStream and NullIOStream class declarations.
virtual InStream & read(char *buffer, size_t num)
reads num bytes from stream to the buffer
Definition: StrIOStream.h:38
This is a very abstract class that can be implemented by classes that want to receive stream input...
Definition: InStream.h:29
virtual bool eof()
Indicates if the end of file has been reached.
Definition: StrIOStream.h:78
bool isWritable()
indicates whether the stream can actually be written to.
Definition: StrIOStream.h:92
Wraps a Str with an IOStream class.
Definition: StrIOStream.h:24
Replacement for standard template class string.
Definition: Str.h:12
const char * cStr() const
Definition: Str.h:188
Str & str_
Definition: StrIOStream.h:98
static const Str EMPTY_STR
Definition: Str.h:19
This is a very abstract class that can be implemented by classes that want to send stream output...
Definition: OutStream.h:41
size_t lastWritten_
Definition: OutStream.h:214
void setBytesRead(size_t lastRead)
Definition: InStream.h:205
const char * getName()
returns name of the stream
Definition: StrIOStream.h:55
void rewind()
Definition: StrIOStream.h:66