home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c017 / 34.ddi / CSTRM.ZIP / SBNODE.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-27  |  2.2 KB  |  121 lines

  1. /*
  2.  
  3.     sbnode.cpp
  4.     8-27-91
  5.     Streamable Binder Nodes
  6.  
  7.     Copyright 1991
  8.     John W. Small
  9.     All rights reserved
  10.     Use freely but acknowledge authorship and copyright.
  11.  
  12.     PSW / Power SoftWare
  13.     P.O. Box 10072
  14.     McLean, Virginia 22102 8072 USA
  15.  
  16.     John Small
  17.     Voice: (703) 759-3838
  18.     CIS: 73757,2233
  19.  
  20. */
  21.  
  22. #include <sbnode.hpp>
  23. #include <fstream.h>
  24. #include <iomanip.h>
  25. #include <string.h>
  26.  
  27.  
  28. void SBNode::construct(voiD D, unsigned sizeofData,
  29.     unsigned Did, int dup)
  30. {
  31.     this->Did = Did;
  32.     if (((this->dup = dup) != 0) && D)  {
  33.         this->D = new char [sizeofData];
  34.         if (this->D)
  35.             memcpy(this->D,D,
  36.                 this->sizeofData
  37.                 = sizeofData);
  38.         else
  39.             this->sizeofData = 0;
  40.     }
  41.     else if ((this->D = D) != voiD0)
  42.         this->sizeofData = sizeofData;
  43.     else
  44.         this->sizeofData = 0;
  45. }
  46.  
  47. ostream& SBNode::store(ostream& os)
  48. {
  49.     os << sizeofData << endm << Did << endm;
  50.     if (!os)
  51.         serror("unable to store SBNode"
  52.             ": sizeofData and Did");
  53.     else if (sizeofData)  {
  54.         os.write((const char *)D,sizeofData);
  55.         if (!os)
  56.             serror("unable to store"
  57.                 " SBNode data");
  58.     }
  59.     return os;
  60. }
  61.  
  62. StreamablE SBNode::load(istream& is,
  63.     StreamablE InstancE)
  64. {
  65.     unsigned sizeofData, Did;
  66.     char * D = (char *) 0;
  67.  
  68.     if (!(is >> sizeofData >> nextm >> Did
  69.         >> nextm))  {
  70.         lserror("unable to load SBNode"
  71.             ": sizeofData and Did",
  72.             CLASS_ID);
  73.         return StreamablE0;
  74.     }
  75.     if (sizeofData) {
  76.         if ((D = new char[sizeofData])
  77.             != (char *)0)  {
  78.             is.read(D,sizeofData);
  79.             if (!is)  {
  80.                 lserror("loading "
  81.                   "SBNode data",
  82.                   CLASS_ID);
  83.               delete D;
  84.               return StreamablE0;
  85.             }
  86.         }
  87.         else  {
  88.             lserror("unable to allocate "
  89.                 "memory for SBNode data",
  90.                 CLASS_ID);
  91.             is.ignore(sizeofData);
  92.             return StreamablE0;
  93.         }
  94.     }
  95.     if (!InstancE)
  96.         if ((InstancE =    (StreamablE)
  97.             new SBNode
  98.             (UNIQUE_STREAMABLE))
  99.             == StreamablE0)  {
  100.             lserror("unable to construct"
  101.                 " SBNode",CLASS_ID);
  102.             delete D;
  103.             return StreamablE0;
  104.         }
  105.     ((SBN)InstancE)->
  106.         construct(D,sizeofData,Did,0);
  107.     ((SBN)InstancE)->dup = 1;
  108.     
  109.     return InstancE;
  110. }
  111.  
  112. void SBNode::restream()
  113.     { Streamable::restream(); }
  114.  
  115. SBNode::SBNode(char * s, int dup)
  116.     : Streamable(UNIQUE_STREAMABLE,CLASS_ID)
  117. {
  118.     construct(s,(s?(strlen(s)+1):0),
  119.         DID_String,dup);
  120. }
  121.