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

  1.     // bdrdem5.cpp
  2.     // Demo Binder with generic heterogeneous
  3.     //   streamable nodes.
  4.  
  5.     #include <string.h>
  6.     #include <fstream.h>
  7.     #include <sbnode.hpp>
  8.  
  9.     #define DID_Int    2
  10.  
  11.     #define ID_SBN_CMP  1
  12.  
  13.  
  14.  
  15.     // display integers and strings
  16.     
  17.     void display(SBN N)
  18.     {
  19.         cout << endl << "DID: " << setw(6)
  20.             << N->DID()
  21.             << "  sizeofData: " << setw(6)
  22.             << N->SizeofData() << "  D:  ";
  23.         switch (N->DID())  {
  24.         case DID_Generic:
  25.             cout << "unknown";
  26.             break;
  27.         case DID_String:
  28.             cout << (char *)(voiD) * N;
  29.             break;
  30.         case DID_Int:
  31.             cout << *(int *)(voiD) * N;
  32.             break;
  33.         }
  34.  
  35.     }
  36.  
  37.  
  38.     // sort integers and strings
  39.     
  40.     int sbncmp(SBN N1, SBN N2)
  41.     {
  42.         // integers sorted to the front
  43.         // strings sorted to the rear
  44.  
  45.         if (N1->DID() == DID_Generic)
  46.             return 1;
  47.         else if (N2->DID() == DID_Generic)
  48.             return -1;
  49.         if (N1->DID() == DID_Int)
  50.             if (N2->DID() == DID_Int)
  51.                 return *(int *)(voiD)*N1
  52.                     - *(int *)(voiD)*N2;
  53.             else
  54.                 return -1;
  55.         else
  56.             if (N2->DID() == DID_Int)
  57.                 return 1;
  58.             else
  59.                 return strcmp((const char *)
  60.                     (voiD)*N1,
  61.                     (const char *)
  62.                     (voiD)*N2);
  63.     }
  64.  
  65.  
  66.     main()
  67.     {
  68.         Binder::registerClass();
  69.         SBNode::registerClass();
  70.  
  71.  
  72.         Binder B(Binder::STREAMABLE_NODES);
  73.  
  74.         B.push(new SBNode("Hello LDB!"));
  75.         B.insQ(new SBNode("Goodbye linked"));
  76.         B.insQ(new SBNode("list programming!"));
  77.         B.insQ(((StreamablE)B.bottom())->link());
  78.         B.insQ(new SBNode(
  79.             "Line above tests multilinking!"));
  80.  
  81.         for (int i = 3; i; i--)
  82.             B.insQ(new SBNode(&i,sizeof(i),
  83.                 DID_Int));
  84.  
  85.         cout << "\n\nBinder of streamable integers"
  86.             << " and strings!\n\n";
  87.  
  88.         B.forEach((BDRforEachBlocK)display);
  89.  
  90.         cout << "\n\nPress enter to continue ...";
  91.         cin.get();
  92.  
  93.  
  94.         B.setComparE((BDRcomparE)sbncmp);
  95.  
  96.         RegisterFunction(ID_SBN_CMP,
  97.             (GenericFnC)sbncmp);
  98.  
  99.         ofstream oS("bdrdem5.txt");
  100.         if (oS)  {
  101.  
  102.           oS << (StreamablE) B;
  103.  
  104.           B.restream();
  105.  
  106.           // Don't stream B again
  107.           // without restreaming!!!
  108.  
  109.           oS.close();
  110.           ifstream iS("bdrdem5.txt");
  111.           if (iS)  {
  112.  
  113.             StreamablE C;
  114.  
  115.             cout << "\nStreamed and "
  116.             << "reloaded Binder with "
  117.             << "multiple links maintained "
  118.             << "\nand sorted with streamed "
  119.             << "compare fnc, ints in front:"
  120.             << " \n";
  121.  
  122.             iS >> C;
  123.  
  124.             RestreamRegistry();
  125.  
  126.             // Don't load again from
  127.             // any stream with
  128.             // restreaming!!!
  129.  
  130.             if (C)
  131.             {
  132.               ((BindeR)C)->sort();
  133.               ((BindeR)C)->forEach(
  134.             (BDRforEachBlocK)display);
  135.               delete C;
  136.             }
  137.             else
  138.               cout << "\n\nUnable to reload"
  139.             << " Binder \n\n";
  140.           }
  141.           else
  142.             cout << "\n\nUnable to reopen"
  143.               << " stream for input of"
  144.               << " of Binder \n\n";
  145.         }
  146.  
  147.  
  148.         return 0;
  149.     }