home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / yacl-012.zip / base / bytstrng.cxx < prev    next >
C/C++ Source or Header  |  1995-04-04  |  6KB  |  299 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/bytstrng.h"
  38. #include "base/string.h"
  39. #include "base/stream.h"
  40.  
  41. #ifdef __BORLANDC__
  42. #include <mem.h>
  43. #ifndef __OS2__
  44. #define MEMCMP _fmemcmp
  45. #else
  46. #define MEMCMP memcmp
  47. #endif
  48. #else
  49. #include <string.h>
  50. #define MEMCMP memcmp
  51. #endif
  52.  
  53.  
  54. // #define NEW_OP new (__LINE__, __FILE__)
  55. #define NEW_OP new
  56.  
  57.  
  58.  
  59. CL_DEFINE_CLASS(CL_ByteString,  _CL_ByteString_CLASSID);
  60.  
  61.  
  62.  
  63. CL_ByteString::CL_ByteString()
  64.     : CL_ByteArray (this)
  65. {
  66.     _array = NULL;
  67.     _size = 0;
  68. }
  69.  
  70.  
  71. CL_ByteString::CL_ByteString (long size)
  72. : CL_ByteArray (this)
  73. {
  74.     _array = NEW_OP uchar [_size = size];
  75.     if (_array)
  76.         SetAllBytesTo (0);
  77.     else
  78.         _size = 0;  // No memory
  79. }
  80.  
  81.  
  82. CL_ByteString::CL_ByteString (const CL_ByteString& b)
  83. : CL_ByteArray (this)
  84. {
  85.     long n = b.Size();
  86.     _array = NEW_OP uchar [n];
  87.     if (!_array) { // No memory
  88.         _size = 0;
  89.         return;
  90.     }
  91.     CL_ByteArray q(_array, n);
  92.     q = b;
  93.     _size = n;
  94. }
  95.  
  96.  
  97.  
  98. CL_ByteString::CL_ByteString (uchar* b, long size)
  99. : CL_ByteArray (this)
  100. {
  101.     _array = NEW_OP uchar [_size = size];
  102.     if (!_array) {
  103.         _size = 0;
  104.         return;
  105.     }
  106.     CL_ByteArray q(_array, size);
  107.     q = b;
  108. }
  109.  
  110.  
  111.  
  112. CL_ByteString::CL_ByteString (const CL_ByteArray& b)
  113. : CL_ByteArray (this)
  114. {
  115.     _array = NEW_OP uchar [_size = b.Size()];
  116.     if (!_array) {
  117.         _size = 0;
  118.         return;
  119.     }
  120.     CL_ByteArray q(_array, _size);
  121.     q = b;
  122. }
  123.  
  124.  
  125.  
  126.  
  127.  
  128. CL_ByteString::CL_ByteString (const CL_String& s)
  129. : CL_ByteArray (this)
  130. {
  131.     _array = NEW_OP uchar [_size = s.Size() + 1];
  132.     if (!_array) {
  133.         _size = 0;
  134.         return;
  135.     }
  136.     CL_ByteArray q(_array, _size+1);
  137.     q = (uchar*) s.AsPtr();
  138. }
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145. CL_ByteString::~CL_ByteString()
  146. {
  147.     if (_array != 0)
  148.         delete [] _array;
  149. }
  150.  
  151.  
  152.  
  153. void CL_ByteString::operator= (const CL_ByteArray& b)
  154. {
  155.     if (this == &b)
  156.         return;
  157.     if (!PrepareToChange())
  158.         return;
  159.     long len = b.Size();
  160.     if (_size < len) {
  161.         if (_array != NULL) delete [] _array;
  162.         _array = NEW_OP uchar [_size = len];
  163.     }
  164.     if (!_array) {
  165.         _size = 0;
  166.         return;
  167.     }
  168.     CL_ByteArray q(_array, _size);
  169.     q = b;
  170.     Notify ();
  171. }
  172.  
  173.  
  174.  
  175. void CL_ByteString::operator= (const CL_String& b)
  176. {
  177.     if (!PrepareToChange())
  178.         return;
  179.     long len = b.Size() + 1;
  180.     if (_size < len) {
  181.         if (_array != NULL) delete [] _array;
  182.         _array = NEW_OP uchar [_size = len];
  183.     }
  184.     if (!_array) {
  185.         _size = 0;
  186.         return;
  187.     }
  188.     CL_ByteArray q(_array, _size);
  189.     q = b;
  190.     Notify ();
  191. }
  192.  
  193.  
  194.  
  195. CL_ByteArray& CL_ByteString::operator= (long p)
  196. {
  197.     if (_size < sizeof (long))
  198.         if (!ChangeSize (sizeof (long) + _size))
  199.             return *this;
  200.     CL_ByteArray::operator = (p);
  201.     return *this;
  202. }
  203.  
  204.  
  205.  
  206. CL_ByteArray& CL_ByteString::operator= (short p)
  207. {
  208.     if (_size < sizeof (short))
  209.         if (!ChangeSize (sizeof (short) + _size))
  210.             return *this;
  211.     CL_ByteArray::operator = (p);
  212.     return *this;
  213. }
  214.  
  215.  
  216.  
  217.  
  218.  
  219.  
  220. CL_ByteString& CL_ByteString::operator+= (const CL_ByteArray& b)
  221. {
  222.     if (!PrepareToChange())
  223.         return *this;
  224.     long l = b.Size ();
  225.     if (ChangeSize (_size +l)) {
  226.         (*this) (_size - l, l) = b;
  227.         Notify ();
  228.     }
  229.     return *this;
  230. }
  231.  
  232.  
  233.  
  234.  
  235.  
  236.  
  237. bool CL_ByteString::ChangeSize (long new_size)
  238. {
  239.     long n = new_size;
  240.     if (n < 0)
  241.         return FALSE;
  242.     if (n == 0) {
  243.         delete _array;
  244.         _array = NULL;
  245.         _size = 0L;
  246.         return TRUE;
  247.     }
  248.     uchar* p = NEW_OP uchar [n];
  249.     if (!p)
  250.         return FALSE;
  251.     if (_array) {
  252.         memcpy (p, _array, minl (n, _size));
  253.         delete [] _array;
  254.     }
  255.     _array = p;
  256.     _size = n;
  257.     return TRUE;
  258. }
  259.  
  260.  
  261.  
  262. // -------------------- Storage and restoration --------------
  263.  
  264. long CL_ByteString::StorableFormWidth () const
  265. {
  266.     return sizeof (CL_ClassId) +_size + sizeof (long);
  267. }
  268.  
  269.  
  270. bool CL_ByteString::ReadFrom (const CL_Stream& s)
  271. {
  272.     if (!PrepareToChange())
  273.         return FALSE;
  274.     if (!ReadClassId (s))
  275.         return FALSE;
  276.     long size;
  277.     uchar* p;
  278.     if (!s.Read (size))
  279.         return FALSE;
  280.     p = new uchar [size];
  281.     if (!p || !s.Read (p, size))
  282.     if (_array)
  283.         delete [] _array;
  284.     _array = p;
  285.     _size = size;
  286.     Notify();
  287.     return TRUE;
  288. }
  289.  
  290.  
  291. bool CL_ByteString::WriteTo  (CL_Stream& s) const
  292. {
  293.     return s.Write (ClassId())  &&
  294.         s.Write (_size) && s.Write (_array, _size);
  295. }
  296.  
  297.  
  298.  
  299.