Fix typo in license headers
[oweals/cde.git] / cde / programs / dtmail / include / DtMail / Buffer.hh
1 /*
2  *+SNOTICE
3  *
4  *
5  *      $XConsortium: Buffer.hh /main/4 1996/04/21 19:44:23 drk $
6  *
7  *      RESTRICTED CONFIDENTIAL INFORMATION:
8  *
9  *      The information in this document is subject to special
10  *      restrictions in a confidential disclosure agreement bertween
11  *      HP, IBM, Sun, USL, SCO and Univel.  Do not distribute this
12  *      document outside HP, IBM, Sun, USL, SCO, or Univel wihtout
13  *      Sun's specific written approval.  This documment and all copies
14  *      and derivative works thereof must be returned or destroyed at
15  *      Sun's request.
16  *
17  *      Copyright 1993 Sun Microsystems, Inc.  All rights reserved.
18  *
19  *+ENOTICE
20  */
21  
22 #ifndef _DTMAILBUFFER_HH
23 #define _DTMAILBUFFER_HH
24
25 #include <stdarg.h>
26
27 // these classes are provided in order to have a logical array whose
28 // size is not known at creation time.  The DtMailBuffer class
29 // holds the data, and for the moment holds the only "write" operation
30 // (which is append).  In addition, to be thread safe, there is
31 // a separate class for reading, so that you can have separate
32 // "seek points" in the read buffer in different procedures.
33
34 class BufReader;
35
36 class Buffer {
37
38
39     protected:
40         static const int defaultchunksize;
41
42     public:
43
44         Buffer();
45
46         // actually add data.  result is # of bytes written.  Can
47         // fail if underlying write fails, or if out of memory.
48         virtual int appendData(const char *user_buffer, int length) = 0;
49
50         // an interator function that gets called so that eventually
51         // the entire buffer has been passed through
52         typedef unsigned long (*CallBack)(const char *buffer, int len, va_list);
53         virtual unsigned long iterate(CallBack, ...) = 0;
54
55         // get a new reader object
56         virtual BufReader *getReader(void) = 0;
57
58         virtual int getSize(void) = 0;  // get total size of the buffer
59
60
61     protected:
62         virtual ~Buffer(void);
63     private:
64         Buffer(const Buffer&);          // try and avoid copies
65
66
67 };
68
69 // subsidiary classes
70 class BufReader {
71     public:
72         // actually read data into a user specified buffer
73         virtual int getData(char *user_buffer, int length) = 0;
74         virtual ~BufReader(void);
75
76     protected:
77         BufReader();
78     private:
79         BufReader(const BufReader &);
80 };
81
82
83 class BufReaderMemory;
84 class BufferMemory : public Buffer {
85
86     public:
87         class Chunk;
88         friend class BufReaderMemory;
89
90         BufferMemory(int chunksize);
91         BufferMemory();
92         virtual ~BufferMemory(void);
93
94         // actually add data.  result is # of bytes written.  Can
95         // fail if underlying write fails, or if out of memory.
96         virtual int appendData(const char *user_buffer, int length);
97
98         virtual unsigned long iterate(Buffer::CallBack, ...);
99
100         // get a new reader object
101         virtual BufReader *getReader(void);
102
103         virtual int getSize(void);      // get total size of the buffer
104
105     private:
106 #if !defined(linux) && !defined(CSRG_BASED) && !defined(sun)
107         class Chunk;
108 #endif
109
110         BufferMemory(const Buffer&);    // try and avoid copies
111         void initBuffer(int size);      // common constructor
112         int newChunk(int size);         // get a new data chunk
113
114         Chunk *_firstchunk;             // first chunk
115         Chunk *_lastchunk;              // last chunk
116         int _totalsize;                 // total size in all buffers
117         int _chunksize;                 // size to allocate new buffers
118
119         void *_mutex;
120
121         BufReaderMemory *_firstreader;  // linked list of readers
122
123
124     public:
125         // aux structure to hold data; no active functions
126         struct Chunk {
127             struct Chunk *_nextchunk;   // next element in linked list
128             char *_buffer;              // data buffer
129             int _chunksize;             // allocated size of buffer
130             int _currentend;            // current used space
131         };
132
133     // subsidiary classes
134 };
135
136 class BufReaderMemory : protected BufReader {
137     friend class BufferMemory;
138
139     public:
140         // actually read data into a user specified buffer
141         virtual int getData(char *user_buffer, int length);
142         virtual ~BufReaderMemory(void);
143
144     private:
145         BufReaderMemory(const BufReaderMemory &);
146         BufReaderMemory(BufferMemory *);
147
148         BufferMemory::Chunk *_currentchunk;
149         int _currentoffset;
150
151         BufferMemory *_buffer;
152         BufReaderMemory *_nextreader;   // maintain linked list of readers
153         BufReaderMemory *_prevreader;   // maintain linked list of readers
154 };
155
156 #endif // _DTMAILBUFFER_HH
157
158
159
160
161
162
163
164
165