home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / yacl-012.zip / base / bytestrm.h < prev    next >
C/C++ Source or Header  |  1995-04-08  |  4KB  |  159 lines

  1.  
  2.  
  3. #ifndef _bytestrm_h_ /* Tue Feb 22 12:15:35 1994 */
  4. #define _bytestrm_h_
  5.  
  6.  
  7.  
  8.  
  9.  
  10. /*
  11.  *
  12.  *          Copyright (C) 1994, M. A. Sridhar
  13.  *  
  14.  *
  15.  *     This software is Copyright M. A. Sridhar, 1994. You are free
  16.  *     to copy, modify or distribute this software  as you see fit,
  17.  *     and to use  it  for  any  purpose, provided   this copyright
  18.  *     notice and the following   disclaimer are included  with all
  19.  *     copies.
  20.  *
  21.  *                        DISCLAIMER
  22.  *
  23.  *     The author makes no warranties, either expressed or implied,
  24.  *     with respect  to  this  software, its  quality, performance,
  25.  *     merchantability, or fitness for any particular purpose. This
  26.  *     software is distributed  AS IS.  The  user of this  software
  27.  *     assumes all risks  as to its quality  and performance. In no
  28.  *     event shall the author be liable for any direct, indirect or
  29.  *     consequential damages, even if the  author has been  advised
  30.  *     as to the possibility of such damages.
  31.  *
  32.  */
  33.  
  34.  
  35.  
  36. // This class defines a ByteArray-based in-memory data stream.
  37.  
  38. #ifdef __GNUC__
  39. #pragma interface
  40. #endif
  41.  
  42. #include "base/stream.h"
  43. #include "base/bytstrng.h"
  44.  
  45. class CL_EXPORT CL_ByteStreamData;
  46.  
  47. class CL_EXPORT CL_ByteStream: public CL_Stream {
  48.  
  49. public:
  50.     CL_ByteStream (CL_ByteArray& b);
  51.     // Create a ByteStream that uses the given ByteArray.
  52.  
  53.     
  54.     // ----------------- Read operations -------------------------
  55.  
  56.     virtual long Read (uchar* buffer, long num_bytes) const; 
  57.     // Read from current position. Returns number of bytes read, 0 on eof,
  58.     // -1 on error.
  59.  
  60.     bool Read (CL_Object& obj) const
  61.         {return CL_Stream::Read (obj);};
  62.     // Override the inherited method; forward the call to parent class.
  63.     // This is to prevent the "hides virtual" warning.
  64.     
  65.     bool Read (CL_ObjectPtr& obj) const
  66.         {return CL_Stream::Read (obj);};
  67.     // Override the inherited method; forward the call to parent class.
  68.     // This is to prevent the "hides virtual" warning.
  69.     
  70.     // ----------------- Write operations ------------------------
  71.  
  72.     virtual bool Write (uchar* buffer, long num_bytes);
  73.     // Write at current position. Return TRUE on success, FALSE if fewer
  74.     // than num_bytes were written.
  75.  
  76.     bool Write (const CL_Object& p)
  77.         {return CL_Stream::Write (p);};
  78.     // Override the inherited method; forward the call to parent class.
  79.     // This is to prevent the "hides virtual" warning.
  80.     
  81.     bool Write (CL_ObjectPtr p)
  82.         {return CL_Stream::Write (p);};
  83.     // Override the inherited method; forward the call to parent class.
  84.     // This is to prevent the "hides virtual" warning.
  85.     
  86.     
  87.     // ----------------- Seek operations -------------------------
  88.     
  89.     virtual bool SeekTo (CL_Offset position) const;
  90.     // Change the current position. Returns TRUE on success.
  91.  
  92.     virtual bool SeekToEnd () const;
  93.  
  94.     virtual bool SeekRelative (long change) const;
  95.     
  96.     virtual bool ChangeSize (long new_size);
  97.  
  98.     virtual long Size () const;
  99.     // Return the current size of the stream.
  100.     
  101.     virtual bool Eof () const;
  102.     // Are we at the end of the stream?
  103.     
  104.     virtual long Offset () const;
  105.     // Return the current position.
  106.  
  107.  
  108.     // ------------------ Basic functions ------------------------------
  109.     
  110.     const char* ClassName() const {return "CL_ByteStream";};
  111.  
  112.     CL_ClassId ClassId() const { return _CL_ByteStream_CLASSID;};
  113.  
  114.     // ------------------- End public protocol ------------------------ 
  115.  
  116.  
  117.  
  118.  
  119.     
  120. protected:
  121.     CL_ByteArray& _buffer;
  122.     long          _position;
  123. };
  124.  
  125.  
  126.  
  127.  
  128.  
  129. // ---------------------- Querying ----------------------
  130.  
  131. // Are we at the end of the file?
  132. inline bool CL_ByteStream::Eof () const
  133. {
  134.     return _position >= _buffer.Size();
  135. }
  136.  
  137.  
  138. // Return the current position
  139. inline long CL_ByteStream::Offset () const
  140. {
  141.     return _position;
  142. }
  143.  
  144.  
  145. // Return the size of the byte block
  146. inline long CL_ByteStream::Size () const
  147. {
  148.     return _buffer.Size();
  149. }
  150.  
  151.  
  152. inline bool CL_ByteStream::ChangeSize (long new_size)
  153. {
  154.     return _buffer.ChangeSize (new_size);
  155. }
  156.  
  157.  
  158. #endif /* _bytestrm_h_ */
  159.