home *** CD-ROM | disk | FTP | other *** search
/ World of Shareware - Software Farm 2 / wosw_2.zip / wosw_2 / CPROG / CSTREAM.ZIP / CSDEMO.CPP next >
C/C++ Source or Header  |  1991-05-28  |  4KB  |  179 lines

  1. /*
  2.     csdemo.cpp
  3.     5-28-91
  4.     class stream demo
  5.  
  6.     Copyright 1991
  7.     John W. Small
  8.     All rights reserved
  9.     Use freely but acknowledge authorship and copyright.
  10.     CIS: 73757,2233
  11.  
  12.     PSW / Power SoftWare
  13.     P.O. Box 10072
  14.     McLean, Virginia 22102 8072
  15.     USA (703) 759-3838
  16.  
  17. */
  18.  
  19.  
  20. #include <string.h>
  21. #include <fstream.h>
  22. #include <cstream.hpp>
  23.  
  24.  
  25.  
  26. #define MAX_STR_BUF  80
  27.  
  28. class Employee : StreamableClass {
  29.     char *name, *address, *cityStateZip;
  30. static char nameBuf[], addressBuf[], cityStateZipBuf[];
  31. public:
  32.     StreamableClassID(2);
  33.     Employee(char *name, char *address,
  34.         char *cityStateZip)
  35.         : StreamableClass(CLASS_ID)
  36.     {
  37.         this->name = strdup(name);
  38.         this->address = strdup(address);
  39.         this->cityStateZip = strdup(cityStateZip);
  40.     }
  41.  
  42.     ~Employee() { delete name; delete address;
  43.         delete cityStateZip; }
  44. };
  45.  
  46. char Employee::nameBuf[MAX_STR_BUF];
  47. char Employee::addressBuf[MAX_STR_BUF];
  48. char Employee::cityStateZipBuf[MAX_STR_BUF];
  49.  
  50. void Employee::store(ostream& os)
  51. {
  52.     os << name << endf << address << endf
  53.         << cityStateZip << endf;
  54. }
  55.  
  56.  
  57. StreamC Employee::load(istream& is, StreamC C)
  58. {
  59.     char *name, *address, *cityStateZip;
  60.  
  61.     if (C)
  62.         return C;
  63.     is.getline(nameBuf,MAX_STR_BUF,
  64.         StreamableClass::FieldTermChar);
  65.     is.getline(addressBuf,MAX_STR_BUF,
  66.         StreamableClass::FieldTermChar);
  67.     is.getline(cityStateZipBuf,MAX_STR_BUF
  68.         ,StreamableClass::FieldTermChar);
  69.     return new Employee(nameBuf,addressBuf,
  70.         cityStateZipBuf);
  71. }
  72.  
  73.  
  74.  
  75. class Product : StreamableClass {
  76.     char *name, * type;
  77.     unsigned price;
  78. static char nameBuf[], typeBuf[];
  79. static unsigned priceBuf;
  80. public:
  81.  
  82.     StreamableClassID(3);
  83.     Product(char *name, char *type, unsigned price)
  84.         : StreamableClass(CLASS_ID)
  85.     {
  86.         this->name = strdup(name);
  87.         this->type = strdup(type);
  88.         this->price = price;
  89.     }
  90.     ~Product() { delete name; delete type; }
  91. };
  92.  
  93. char Product::nameBuf[MAX_STR_BUF];
  94. char Product::typeBuf[MAX_STR_BUF];
  95. unsigned Product::priceBuf;
  96.  
  97. void Product::store(ostream& os)
  98. {
  99.     os << name  << endf << type << endf
  100.         << price << endf;
  101. }
  102.  
  103. StreamC Product::load(istream& is, StreamC C)
  104. {
  105.     if (C)
  106.         return C;
  107.     is.getline(nameBuf,MAX_STR_BUF
  108.         ,StreamableClass::FieldTermChar);
  109.     is.getline(typeBuf,MAX_STR_BUF
  110.         ,StreamableClass::FieldTermChar);
  111.     is >> priceBuf;
  112.     is.get();  // field term char
  113.     return new Product(nameBuf,typeBuf,priceBuf);
  114. }
  115.  
  116.  
  117.  
  118. StreamableClasses(20);
  119.  
  120.  
  121.  
  122.  
  123. main()
  124. {
  125.  
  126.  
  127.  
  128.     RegisterClass(StreamableClass);
  129.     RegisterClass(Employee);
  130.     RegisterClass(Product);
  131.  
  132.  
  133.  
  134.     Employee e1("Frank Borland","Sierra Nevada Mts.",
  135.         "California");
  136.     Employee e2("Philippe Kahn","1800 Green Hills Road",
  137.         "Scotts Valley, CA 95067");
  138.     Employee e3("Mike Slater","1700 Green Hills Road",
  139.         "Scotts Valley, CA 95067");
  140.  
  141.     Product  p1("Borland C++","Language",99);
  142.     Product  p2("Quatro Pro 3.0","Business/GUI",99);
  143.     Product  p3("Paradox Engine 2.0","Applications",99);
  144.  
  145.     cout << "Stream three Employee Records\n" << endl;
  146.     cout << e1 << e2 << e3 << "press enter ... " << flush;
  147.     cin.get(); cout << endl;
  148.     cout << "Stream three Product Records\n" << endl;
  149.     cout << p1 << p2 << p3 << "press enter ... " << flush;
  150.     cin.get(); cout << endl;
  151.  
  152.     ofstream oS("emp$prod.cls");
  153.     if (!oS)
  154.         return 1;
  155.  
  156.     oS << p1 << e2 << p3 << e1 << p2 << e3;
  157.     oS.close();
  158.  
  159.     ifstream iS("emp$prod.cls");
  160.     if (!iS)
  161.         return 2;
  162.  
  163.     StreamableClass *S1, *S2, *S3;
  164.  
  165.     iS >> S1 >> S2 >> S3;
  166.     cout << "Stream three classes\n" << endl;
  167.     cout << S1 << S2 << S3 << "press enter ... " << flush;
  168.     cin.get(); cout << endl;
  169.     delete S1; delete S2;  delete S3;
  170.  
  171.     iS >> S1 >> S2 >> S3;
  172.     cout << "Stream three more classes\n" << endl;
  173.     cout << S1 << S2 << S3 << "press enter ... " << flush;
  174.     cin.get(); cout << endl;
  175.     delete S1; delete S2; delete S3;
  176.  
  177.     return 0;
  178. }
  179.