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

  1. /*
  2.  
  3.     cstream.cpp
  4.     5-28-91
  5.     class stream
  6.  
  7.     Copyright 1991
  8.     John W. Small
  9.     All rights reserved
  10.     Use freely but acknowledge authorship and copyright.
  11.     CIS: 73757,2233
  12.  
  13.     PSW / Power SoftWare
  14.     P.O. Box 10072
  15.     McLean, Virginia 22102 8072
  16.     USA (703) 759-3838
  17.  
  18. */
  19.  
  20. #include <cstream.hpp>
  21.  
  22. char StreamableClass::FieldTermChar = '\n';
  23.  
  24.  
  25. // end of field manipulator
  26.  
  27. ostream& endf(ostream& os)
  28. {
  29.     return os << StreamableClass::FieldTermChar
  30.         << flush;
  31. }
  32.  
  33.  
  34. void StreamRegistry::reg(unsigned id, StreamC (*loader)
  35.     (istream& is, StreamC C))
  36. {
  37.     unsigned i;
  38.  
  39.     for (i = 0; i < count; i++)
  40.         if (recs[i].id == id)
  41.             break;
  42.     if (i < count)
  43.         if (recs[i].load == loader)
  44.             return;
  45.         else {
  46.             error("StreamRegistry id "
  47.                 "conflict: ",id);
  48.             abort();
  49.         }
  50.     if (count < limit)  {
  51.         recs[count].id = id;
  52.         recs[count].load = loader;
  53.         count++;
  54.     }
  55.     else  {
  56.         error("StreamRegistry overflow, id: ",id);
  57.         abort();
  58.     }
  59. }
  60.  
  61. istream& StreamRegistry::get(istream& is, StreamC& C)
  62. {
  63.     unsigned id, sizeofClass, i;
  64.  
  65.     C = StreamC0;
  66.     if (!(is >> id))
  67.         return is;
  68.     if (!is.get()) // field term char
  69.         return is;
  70.     for (i = 0; i < count; i++)
  71.         if (recs[i].id == id)
  72.             break;
  73.     if (i >= count)  {
  74.         error("StreamRegistry, attempted load "
  75.             "of unknown class: ",id);
  76.         return is;
  77.     }
  78.     C = (*recs[i].load)(is,StreamC0);
  79.     return is;
  80. }
  81.  
  82. ostream& StreamRegistry::put(ostream& os, StreamC C)
  83. {
  84.     unsigned id, i;
  85.  
  86.     if (!C)
  87.         return os;
  88.     id = C->ID();
  89.     for (i = 0; i < count; i++)
  90.         if (recs[i].id == id)
  91.             break;
  92.     if (i >= count)  {
  93.         error("StreamRegistry, attempted store "
  94.             "of unknown class: ",id);
  95.     }
  96.     else  {
  97.         os << id << endf;
  98.         C->store(os);
  99.     }
  100.     return os;
  101. }
  102.