home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / MYIODEMO.CPP < prev    next >
C/C++ Source or Header  |  1997-07-05  |  4KB  |  171 lines

  1. // +++Date last modified: 05-Jul-1997
  2.  
  3. // Myiodemo.cpp
  4. // This is a trivial program which uses the myio loopback class
  5. // to demonstrate the basics on writing an io interface using
  6. // the AT&T C++ iostream classes.
  7. // The program simply provides the ability to selectively add
  8. // to or read from a Myio instance and display information to
  9. // assist in understanding how it all works.
  10. //
  11.  
  12. # include "Mystream.h"      // This includes Myio.h and iostream.h
  13. # include "myLine.h"
  14. # include <conio.h>         // For getch()
  15. # include <ctype.h>         // For toupper()
  16. # include <string.h>
  17.  
  18. # define NL char('\n')
  19.  
  20.     // Let's do the "application is a class" trick
  21.  
  22. class myApplication
  23. {
  24.             // Defines a pointer to member function type
  25.             // used for dispatching the menu
  26.  
  27.     typedef void (myApplication::*pvf) (void);
  28.  
  29.   public:
  30.  
  31.     myApplication (void) : mio() {}
  32.     int execute (void);
  33.  
  34.   private:
  35.  
  36.     iostream & stream (void) { return mio.stream(); }
  37.     int domenu (void);
  38.     void send (void);
  39.     void read (void);
  40.     void disp (void);
  41.     void peek (void);
  42.     void flsh (void);
  43.     void stat (void);
  44.  
  45.     pvf choice;     // Function called to execute
  46.     Myio mio;       // IO object
  47.  
  48. };
  49.  
  50.  
  51. void
  52. myApplication::disp (void)
  53. {
  54.     cout << "Mystream status:" << NL
  55.          << "Chrs in output buffer = " << stream().rdbuf()->out_waiting() << NL
  56.  
  57.          << "Chrs in  input buffer = " << stream().rdbuf()->in_avail()    << NL
  58.  
  59.          << "Myio object status = "
  60.             << mio.count() << char('/') << mio.size()
  61.             << " LastWrite=" << (mio.writeok() ? "OK" : "Incomplete")
  62.             << " LastRead=" << (mio.readok() ? "OK" : "EOF")
  63.          << endl;
  64. }
  65.  
  66.     // Request a line and send it to the IO device
  67.  
  68. void
  69. myApplication::send (void)
  70. {
  71.     cout << NL << "Enter text to write - press <ENTER> when done\n:";
  72.     myLine L;
  73.     cin >> L;
  74.     int l = strlen(L);
  75.     if (!l)
  76.         cerr << "Nothing entered." << endl;
  77.     else
  78.     {
  79.         cout << "Writing '"
  80.              << L
  81.              << char('\'')
  82.              << endl;
  83.         stream() << L << NL;    // Send the entered data, NL terminated
  84.         cout << "Chrs written to Myio object = " << (l + 1) << NL;
  85.         disp ();
  86.     }
  87. }
  88.  
  89. void
  90. myApplication::read (void)
  91. {
  92.     cout << NL << "Reading a line from object:" << NL;
  93.     myLine L;
  94.     mio.stream().clear();
  95.     mio.stream() >> L;
  96.     int l = strlen(L);
  97.     if (!l)
  98.     {
  99.         cout << "Nothing read." << endl;
  100.         mio.stream().clear();       // Clear EOF status
  101.     }
  102.     else
  103.     {
  104.         cout << "Read '"
  105.              << L
  106.              << char('\'')
  107.              << endl;
  108.         cout << "Chrs read from Myio object = " << (l + 1) << NL;
  109.         disp ();
  110.     }
  111. }
  112.  
  113. void
  114. myApplication::flsh (void)
  115. {
  116.     cout << NL << "Flushing stream" << endl;
  117.     stream() << flush;
  118.     disp ();
  119. }
  120.  
  121. void
  122. myApplication::stat (void)
  123. {
  124.     cout << NL << "Myio object buffer dump:" << NL;
  125.     mio.dump();
  126.     disp ();
  127.     stream().rdbuf()->dbp();    // Dump stream info
  128. }
  129.  
  130. int
  131. myApplication::domenu (void)
  132. {
  133.     cout << NL
  134.          << "W)rite  R)ead  D)ump  F)lush  Q)uit\n"
  135.          << "Select: "
  136.          << flush;      // Need to flush here for portability
  137.     int key;
  138.     for (;;)
  139.     {
  140.         key = getch ();
  141.         switch (toupper(key))
  142.         {
  143.         case 'W': choice = &myApplication::send;    break;
  144.         case 'R': choice = &myApplication::read;    break;
  145.         case 'D': choice = &myApplication::stat;    break;
  146.         case 'F': choice = &myApplication::flsh;    break;
  147.         case 'Q': key = 0;                          break;
  148.         default:
  149.             continue;
  150.         }
  151.         cout << char(key) << endl;
  152.         break;
  153.     }
  154.     return key;
  155. }
  156.  
  157. int                         // This is really the application
  158. myApplication::execute (void)
  159. {
  160.     while (domenu ())
  161.         (this->*choice) ();
  162.     return 0;
  163. }
  164.  
  165. int
  166. main (void)
  167. {
  168.     myApplication Demo;     // Declare the application
  169.     return Demo.execute (); // go for it!
  170. }
  171.