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

  1.  
  2.  
  3.  
  4.  
  5. /*
  6.  *
  7.  *          Copyright (C) 1994, M. A. Sridhar
  8.  *  
  9.  *
  10.  *     This software is Copyright M. A. Sridhar, 1994. You are free
  11.  *     to copy, modify or distribute this software  as you see fit,
  12.  *     and to use  it  for  any  purpose, provided   this copyright
  13.  *     notice and the following   disclaimer are included  with all
  14.  *     copies.
  15.  *
  16.  *                        DISCLAIMER
  17.  *
  18.  *     The author makes no warranties, either expressed or implied,
  19.  *     with respect  to  this  software, its  quality, performance,
  20.  *     merchantability, or fitness for any particular purpose. This
  21.  *     software is distributed  AS IS.  The  user of this  software
  22.  *     assumes all risks  as to its quality  and performance. In no
  23.  *     event shall the author be liable for any direct, indirect or
  24.  *     consequential damages, even if the  author has been  advised
  25.  *     as to the possibility of such damages.
  26.  *
  27.  */
  28.  
  29.  
  30.  
  31.  
  32. #ifdef __GNUC__
  33. #pragma implementation
  34. #endif
  35.  
  36.  
  37. #include "base/bytestrm.h"
  38.  
  39.  
  40. CL_ByteStream::CL_ByteStream (CL_ByteArray& s)
  41. : _buffer (s)
  42. {
  43.     _position = 0;
  44. }
  45.  
  46.  
  47. // ----------------- Read operations -------------------------
  48.  
  49. // Read from current position. Returns # bytes read, 0 on eof, -1
  50. // on error.
  51. long CL_ByteStream::Read (uchar* output, long num_bytes) const
  52. {
  53.     long ret = maxl (0, minl (_buffer.Size() - _position, num_bytes));
  54.     if (ret > 0) {
  55.         _buffer.CopyTo (output, ret, _position);
  56.         ((CL_ByteStream*) this)->_position += ret; // const cast away
  57.     }
  58.     return ret;
  59. }
  60.  
  61.     
  62. // ----------------- Write operations ------------------------
  63.  
  64. // Write at current position.
  65. bool CL_ByteStream::Write (uchar* buffer, long num_bytes)
  66. {
  67.     long size = _buffer.Size();
  68.     if (size - _position < num_bytes) {
  69.         long new_size = _position + num_bytes;
  70.         if (!_buffer.ChangeSize (new_size))
  71.             return FALSE;
  72.     }
  73.         
  74.     if (num_bytes > 0) {
  75.         _buffer.CopyFrom (buffer, num_bytes, _position);
  76.         _position += num_bytes;
  77.     }
  78.     return num_bytes >= 0;
  79. }
  80.  
  81.  
  82.  
  83. // ----------------- Seek operations -------------------------
  84.  
  85. #define DEFAULT_EXPANSION 64
  86.  
  87. // Change the current position. Returns TRUE on success.
  88. bool CL_ByteStream::SeekTo (CL_Offset position) const
  89. {
  90.     if (position < 0)
  91.         return FALSE;
  92.     long size = _buffer.Size();
  93.     if (position > size && !_buffer.ChangeSize (size + DEFAULT_EXPANSION))
  94.         return FALSE;
  95.     ((CL_ByteStream*) this)->_position = position; // const cast away
  96.     return TRUE;
  97. }
  98.  
  99.  
  100. bool CL_ByteStream::SeekToEnd () const
  101. {
  102.     ((CL_ByteStream*) this)->_position = _buffer.Size(); // Cast away const
  103.     return TRUE;
  104. }
  105.  
  106.  
  107. bool CL_ByteStream::SeekRelative (long change) const
  108. {
  109.     CL_Offset new_pos = maxl (0, _position + change);
  110.     ((CL_ByteStream*) this)->_position = new_pos; // Cast away const
  111.     return TRUE;
  112. }
  113.  
  114.  
  115.  
  116.  
  117.  
  118.