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

  1.  
  2.  
  3. #ifndef _bytarray_h_
  4. #define _bytarray_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. // A ByteArray is an object that provides access to  an array of bytes.
  37. // This class is intended to manage a byte array owned by some {\it
  38. // client\/} object
  39. // {\it other than\/} this object; therefore, if the client is derived
  40. // from CL_Object, then the client's dependents must be consulted and
  41. // notified when this ByteArray is being modified. For this reason, the
  42. // constructors for CL_ByteArray require a pointer to the client. If this
  43. // pointer is \NULL, then the ByteArray assumes that the client does not
  44. // need dependent notification.
  45.  
  46. #include "base/object.h"
  47.  
  48.  
  49. #ifdef __GNUC__
  50. #pragma interface
  51. #endif
  52.  
  53.  
  54. class CL_EXPORT  CL_ByteString;
  55.  
  56. class CL_EXPORT  CL_ByteArray: public CL_Object {
  57.  
  58. public:
  59.  
  60.     //
  61.     // ---------------- Construction, destruction, conversion --------------
  62.     //
  63.     
  64.     CL_ByteArray (CL_Object* client = NULL);
  65.     // Default constructor.
  66.  
  67.     CL_ByteArray (uchar *buf, long size, CL_Object* client = NULL);
  68.     // Constructor: assume that the array of bytes in question begins
  69.     // at the given address, and is of the given size.
  70.  
  71.  
  72.     CL_ByteArray (const CL_ByteArray& b);
  73.     // Copy constructor: make copy of array address and size (i.e.,
  74.     // the {\it same\/} array is used by the copy).
  75.  
  76.     ~CL_ByteArray ();
  77.  
  78.     // ------------------------- Access -----------------------------------
  79.  
  80.     void SetAllBytesTo (uchar b);
  81.     // Set all the bytes in the byte array to the given value.
  82.     
  83.     virtual long Size() const;
  84.     // Return the number of bytes in the byte array.
  85.  
  86.     long Length () const;
  87.     // Return the number of bytes in the byte array.
  88.  
  89.     virtual operator uchar* () const;
  90.     // Return the address of the byte array.
  91.  
  92.     virtual uchar* AsPtr () const;
  93.     // Return the address of the byte array.
  94.  
  95.     
  96.     virtual uchar operator[] (long index);
  97.     // Retrieve the byte at the given index. As usual, 
  98.     // zero-origin indexing is assumed. Specifying an index outside
  99.     // the legal range causes a fatal error.
  100.  
  101.  
  102.     //
  103.     // ----------------------- Conversion ---------------------------------
  104.     //
  105.     virtual CL_String AsString () const;
  106.     // Return the contents of this ByteArray, with a null byte appended,
  107.     // as a string.
  108.  
  109.     //
  110.     // ------------------------- Comparison --------------------------------
  111.     //
  112.     
  113.     virtual bool operator== (const CL_Object& b) const;
  114.  
  115.     virtual bool operator== (const CL_ByteArray& b) const;
  116.  
  117.     virtual bool operator== (const uchar* b) const;
  118.     // This and other comparison methods that take character pointer
  119.     // arguments assume that the size of the memory segment pointed to by
  120.     // the parameter is the same as the
  121.     // size of this ByteArray.
  122.  
  123.     virtual bool operator!= (const CL_Object& b) const;
  124.  
  125.     virtual bool operator!= (const CL_ByteArray& b) const;
  126.  
  127.     virtual bool operator!= (const uchar* b) const;
  128.     
  129.  
  130.     virtual bool operator>= (const CL_Object& b) const;
  131.  
  132.     virtual bool operator>= (const CL_ByteArray& b) const;
  133.  
  134.     virtual bool operator>= (const uchar* b) const;
  135.     
  136.  
  137.     virtual bool operator<= (const CL_Object& b) const;
  138.  
  139.     virtual bool operator<= (const CL_ByteArray& b) const;
  140.  
  141.     virtual bool operator<= (const uchar* b) const;
  142.     
  143.  
  144.     virtual bool operator> (const CL_Object& b) const;
  145.  
  146.     virtual bool operator> (const CL_ByteArray& b) const;
  147.  
  148.     virtual bool operator> (const uchar* b) const;
  149.     
  150.  
  151.     virtual bool operator< (const CL_Object& b) const;
  152.  
  153.     virtual bool operator< (const CL_ByteArray& b) const;
  154.  
  155.     virtual bool operator< (const uchar* b) const;
  156.  
  157.  
  158.     
  159.     virtual short Compare (const CL_ByteArray& obj) const;
  160.     // "strcmp"-style comparison. 
  161.  
  162.     virtual short Compare (const CL_Object& obj) const;
  163.     // Override the method inherited from {\tt CL_Object}. The parameter
  164.     // is type-checked (via IsA) and cast into a CL_ByteArray before the
  165.     // comparison is made. 
  166.  
  167.     //
  168.     // ------------------ Sub-structure manipulation ------------------
  169.     //
  170.     
  171.     virtual CL_ByteArray operator() (long index, long size) const;
  172.     // Return the sub-array beginning at the given index (using
  173.     // 0-origin indexing) and of the given size. Note that the
  174.     // returned CL_ByteArray uses (part of) the same memory as this object,
  175.     // so it is possible to assign to the returned sub-array to effect
  176.     // copying into parts of this object.
  177.     // Also, assignment to sub-arrays does not change the size of the
  178.     // array itself.
  179.  
  180.     virtual CL_ByteArray Suffix (long index) const;
  181.     // Return the suffix beginning at the given index (using
  182.     // 0-origin indexing). Note that the
  183.     // returned CL_ByteArray uses (part of) the same memory as this
  184.     // ByteArray does.
  185.  
  186.     virtual long LongValueAt (long index) const;
  187.     // Obtain a long value from our array, beginning at the given
  188.     // position (i.e., treat the sizeof(long) bytes beginning at the
  189.     // given position as a long), and return it.
  190.     
  191.     virtual short ShortValueAt (long index) const;
  192.     // Extract a short value from our array, beginning at the given
  193.     // position, and return it.
  194.  
  195.     long CommonPrefixLength (const CL_ByteArray& b);
  196.     // Return the size of the longest common prefix with the
  197.     // parameter.
  198.  
  199.     long CommonPrefixLength (const uchar* p);
  200.  
  201.     // ----------------- Assignment, copying, modification ----------------
  202.  
  203.     virtual void operator= (const CL_ByteArray& b);
  204.     // Assign another byte array to this one.
  205.  
  206.     virtual void operator= (const uchar* p);
  207.     // Copy Size() bytes, from the given pointer position.
  208.  
  209.     virtual CL_ByteArray& operator= (long);
  210.     // Copy the given long value into ourselves, beginning at position
  211.     // 0. Do not copy more than Size() bytes. This method, as well
  212.     // as the operator= on short and string, can be used in conjunction with
  213.     // the operator() to put values at arbitrary positions.
  214.  
  215.     virtual CL_ByteArray& operator= (short);
  216.  
  217.     virtual void operator= (const CL_String&);
  218.     // Copy a string, including the null byte at the end. No more than
  219.     // Size() bytes are copied. 
  220.  
  221.  
  222.     virtual void operator= (const CL_Object&);
  223.     // Generic assignment: Check that the parameter object has the same class
  224.     // id as this object does, and perform a ByteArray assignment.
  225.  
  226.     virtual CL_ByteString operator+ (const CL_ByteArray& b) const;
  227.     // Return the result of concatenating b to the end of this byte array.
  228.  
  229.     virtual long CopyFrom (uchar* buffer, long count, long position = 0) const;
  230.     // Copy count bytes from the given buffer into our data segment,
  231.     // beginning at the given position in our data. But do not copy any
  232.     // more than Size()-position bytes. Return the actual number of bytes
  233.     // copied.
  234.     
  235.     virtual long CopyTo (uchar* buffer, long count, long position = 0);
  236.     // Copy count bytes of our data, beginning at position pos, into the
  237.     // given buffer. But do not copy any more than Size()-position bytes.
  238.     // Return the actual number of bytes copied.
  239.  
  240.  
  241.     virtual bool ChangeSize (long) {return FALSE;};
  242.     // Tell us to change our size, and return TRUE if successful.  Since
  243.     // the ByteArray manages someone else's space and therefore cannot
  244.     // change its size, this method always returns FALSE.
  245.  
  246.     // -------------------- Storage and restoration --------------
  247.     
  248.     virtual long StorableFormWidth () const;
  249.  
  250.     bool ReadFrom (const CL_Stream&);
  251.  
  252.     bool WriteTo  (CL_Stream&) const;
  253.  
  254.     //
  255.     // --------------------- Basic methods --------------------
  256.     //
  257.     
  258.     virtual const char*      ClassName () const {return "CL_ByteArray";};
  259.  
  260.     virtual CL_ClassId ClassId   () const {return _CL_ByteArray_CLASSID;};
  261.  
  262.     // ----------------------- End public protocol ------------------
  263.     
  264.  
  265.  
  266. protected:
  267.     long       _size;
  268.     uchar*     _array;
  269.     CL_Object* _client;
  270. };
  271.  
  272.  
  273. inline void CL_ByteArray::operator= (const CL_Object& s)
  274. {
  275.     if (CheckClassType (s, "CL_ByteArray::operator= (CL_Object&)"))
  276.         *this = (const CL_ByteArray&) s;
  277. }
  278.  
  279.  
  280. inline long CL_ByteArray::Size() const
  281. {
  282.     return _size;
  283. }
  284.  
  285. inline long CL_ByteArray::Length () const
  286. {
  287.     return _size;
  288. }
  289.  
  290.  
  291. inline CL_ByteArray::operator uchar* () const
  292. {
  293.     return _array;
  294. }
  295.  
  296.  
  297. inline uchar* CL_ByteArray::AsPtr () const
  298. {
  299.     return _array;
  300. }
  301.  
  302.  
  303.  
  304.     
  305. #endif
  306.  
  307.