summaryrefslogtreecommitdiff
path: root/reference/C/CONTRIB/SNIP/myio.h
blob: d57dae472fafae2feb2c903cc3b553634d36bfb4 (plain)
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
// Myio.h
// Specialised I/O class to demonstrate use of C++ iostream
// facilities in a customised environment
// Written by David L Nugent, June 1993.
//

# if !defined(_Myio_h)
# define _Myio_h 1

    // Foward declare classes

class Myio;
class Mystreambuf;
class Mystreambase;
class Mystream;

    // Forward declare iostream classes

class iostream;

    //
    // class Myio
    // This is a simplistic class which simply fields
    // input and output to a simulated stream device.
    //
    // In fact, it doesn't really do much at all other
    // than read input from and send output to a
    // circular queue, as though talking via a loopback
    // pipe to itself.
    //


class Myio
{
    friend class Mystreambuf;

  public:

    Myio (int sz =2048);                    // sz = buffer size to allocate
    virtual ~Myio (void);

    iostream & stream (void);               // Return (or create) stream

    int readok (void) const;                // Underflow check
    int writeok (void) const;               // Overflow check
    int gcount (void) const;                // Get # of chrs last read
    int pcount (void) const;                // Get # of chrs last written
    int count (void) const;                 // Get # of chrs in buffer
    int size (void) const;                  // Get size of buffer
    int dump (void) const;                  // Debugging - dumps buffer

    int write (char const * buf, int len);  // Put data into 'pipe'
    int read (char * buf, int max);         // Read data from our 'pipe'

  private:

    enum
    {
        overflow    = 0x0001,   // Last write only partial
        underflow   = 0x0002    // Last read only partial
    };

    unsigned stat;              // Last read/write status
    int _pcount;                // Last write count
    int _gcount;                // Last read count
    int bufsize;                // Size of our buffer
    int bufchars;               // Chrs in buffer now
    int bufidx;                 // Index into buffer (next put)
    char * bufaddr;             // Pointer to buffer
    Mystream * mystream;        // Stream assocated with this object

};

inline int
Myio::readok (void) const
    {   return ((stat & Myio::underflow) == 0); }

inline int
Myio::writeok (void) const
    {   return ((stat & Myio::overflow) == 0);  }

inline int
Myio::gcount (void) const
    {   return _gcount;     }

inline int
Myio::pcount (void) const
    {   return _pcount;     }

inline int
Myio::count (void) const
    {   return bufchars;    }

inline int
Myio::size (void) const
    {   return bufsize;     }

# endif     // _Myio_h