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