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

  1.     // bdrdem4.cpp
  2.     // Demo binder with streamable nodes
  3.     
  4.     #include <string.h>
  5.     #include <fstream.h>
  6.     #include <iomanip.h>
  7.     #include <binder.hpp>
  8.  
  9.     #define ID_StreamableString        3
  10.  
  11.     class StreamableString : Streamable  {
  12.         char * s;
  13.         int slen;
  14.     protected:
  15.         void construct(char *s, int slenNotClone);
  16.     public:
  17.         STREAMABLE(StreamableString,
  18.             ID_StreamableString,
  19.             Streamable);
  20.         StreamableString(char *s,
  21.             int slenNotClone = 0)
  22.             : Streamable(UNIQUE_STREAMABLE,
  23.                 CLASS_ID)
  24.             { construct(s,slenNotClone); }
  25.         ostream& printOn(ostream& os);
  26.         operator char *() { return s; }
  27.         virtual ~StreamableString()
  28.             { delete s; }
  29.     };
  30.     typedef StreamableString * StreamableStrinG;
  31.  
  32.  
  33.     ostream& StreamableString::store(ostream& os)
  34.     {
  35.         if (!(os << slen << endm))
  36.             serror("unable to store"
  37.                 "string length, id: ");
  38.         else if (slen)  {
  39.             os.write(s,slen);
  40.             if (!os)
  41.                 serror("unable to store"
  42.                 "string, id: ");
  43.         }
  44.         return os;
  45.     }
  46.  
  47.     StreamablE StreamableString::load(istream& is,
  48.         StreamablE InstancE)
  49.     {
  50.  
  51.         int slen;
  52.         char * s;
  53.  
  54.         if (!(is >> slen >> nextm))  {
  55.             lserror("loading slen",
  56.                 CLASS_ID);
  57.             return StreamablE0;
  58.         }
  59.         else if ((s = new char[slen+1])
  60.             != (char *)0)  {
  61.             if (slen)  {
  62.                 is.read(s,slen);
  63.                 if (!is)  {
  64.                     lserror("loading "
  65.                         "string",
  66.                         CLASS_ID);
  67.                     delete s;
  68.                     return StreamablE0;
  69.                 }
  70.             }
  71.             s[slen] = '\0';            
  72.         }
  73.         else  {
  74.             lserror("unable to allocate memory "
  75.                 "for string",CLASS_ID);
  76.             return StreamablE0;
  77.         }
  78.         if (!InstancE) if ((InstancE = (StreamablE)
  79.             new StreamableString
  80.             (UNIQUE_STREAMABLE)) == StreamablE0)  {
  81.             lserror("unable to construct"
  82.                 " StreamableString",
  83.                 CLASS_ID);
  84.             delete s;
  85.             return InstancE;
  86.         }
  87.         ((StreamableStrinG)InstancE)
  88.             ->construct(s,slen);
  89.         return InstancE;
  90.     }
  91.     
  92.     void StreamableString::restream()
  93.         {  Streamable::restream(); }
  94.  
  95.  
  96.     void StreamableString::construct(char *s,
  97.         int slenNotClone)
  98.     {
  99.         if (slenNotClone)  {
  100.             this->s = s;
  101.             slen = slenNotClone;
  102.         }
  103.         else if (s)
  104.             if ((this->s = strdup(s))
  105.                 != (char *)0)
  106.                 slen = strlen(s);
  107.             else
  108.                 slen = 0;
  109.         else  {
  110.             this->s = (char *) 0;
  111.             slen = 0;
  112.         }
  113.     }
  114.  
  115.     ostream& StreamableString::printOn(ostream& os)
  116.     {
  117.         if (s) if (strlen(s))
  118.             return os << s << endl;
  119.         else
  120.             return os << "empty string" << endl;
  121.         return os << "NULL string" << endl;
  122.     }
  123.  
  124.     void display(StreamableStrinG S)
  125.     {
  126.         cout << "Address: " << S
  127.             << "   String:   ";
  128.         S->printOn(cout);
  129.     }
  130.  
  131.  
  132.  
  133.     main()
  134.     {
  135.         Binder::registerClass();
  136.         StreamableString::registerClass();
  137.  
  138.         Binder B(Binder::STREAMABLE_NODES);
  139.  
  140.         B.push(new
  141.             StreamableString("Now is the time"));
  142.         B.insQ(new
  143.             StreamableString("for all programmers"));
  144.         B.atIns(B.Nodes(),new
  145.             StreamableString("to stop reinventing"));
  146.         B.insQ(new
  147.             StreamableString("the linked list!"));
  148.         B.insQ(((StreamablE)B.bottom())->link());
  149.         B.insQ(new
  150.             StreamableString("Line above for "
  151.                 "testing multiple linking!"));
  152.  
  153.         B.forEach((BDRforEachBlocK)display);
  154.  
  155.         cout << "\n\npress enter to continue ...";
  156.         cin.get();
  157.  
  158.  
  159.         ofstream oS("bdrdem4.txt");
  160.         if (oS)  {
  161.  
  162.             oS << (StreamablE) B;
  163.             B.restream();
  164.             oS.close();
  165.             ifstream iS("bdrdem4.txt");
  166.             if (iS)  {
  167.                 StreamablE C;
  168.                 iS >> C;
  169.                 RestreamRegistry();
  170.                 if (C)
  171.                 {
  172.                   cout << "\n\nStreamed and "
  173.                     << "reloaded Binder "
  174.                     << "with streamable nodes \n\n";
  175.                   ((BindeR)C)->forEach(
  176.                       (BDRforEachBlocK)display);
  177.                   delete C;
  178.                 }
  179.                 else
  180.                   cout << "\n\nUnable to reload"
  181.                     << " Binder \n\n";
  182.             }
  183.             else
  184.                 cout << "\n\nUnable to reopen"
  185.                   << " stream for input of"
  186.                   << " of Binder \n\n";
  187.         }
  188.  
  189.         return 0;
  190.     }