35ce8eaa4b16bcbfec665ebfb4cf8548e6e27ff8
[oweals/cde.git] / cde / programs / dtinfo / DtMmdb / utility / streambuf.C
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these libraries and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /* $XConsortium: streambuf.C /main/8 1996/08/21 15:55:14 drk $ */
24 #include "utility/c_streambuf.h"
25
26 #if defined(linux) || defined(CSRG_BASED) || defined(sun)
27 #include <stdlib.h>
28 #else
29 #include <libc.h>
30 #endif
31
32 #define DEF_BUF_SIZ 4096
33
34 streambuf::streambuf() : _size(0), _capacity(DEF_BUF_SIZ), _alloc(1),
35    _pcount(0), _gcount(0)
36 {
37    base = (char*)malloc(DEF_BUF_SIZ);
38    end = base + DEF_BUF_SIZ;
39    get_ptr = put_ptr = base;
40 }
41
42 streambuf::streambuf(char* p, int l, int bufferFull) :
43     base(p), end(p+l), put_ptr(p), get_ptr(p), _size(0), _capacity(l), 
44     _alloc(0), _pcount(0), _gcount(0)
45 {
46    if (bufferFull) {
47      _size = l;
48    }
49 }
50
51 streambuf::~streambuf()
52 {
53    if ( _alloc )
54       free(base);
55 }
56
57 int streambuf::examine()
58 {
59    notify(GET);
60
61    if ( empty() && underflow() == EOF )
62       return EOF;
63
64    return (unsigned char)(*get_ptr);
65 }
66
67 int streambuf::get()
68 {
69    notify(GET);
70
71    if ( empty() && underflow() == EOF )
72       return EOF;
73
74    int x = (unsigned char)(*get_ptr);
75
76    move_get_ptr(+1);
77
78    _size--;
79
80    _gcount++;
81
82    return x;
83 }
84
85 int streambuf::putback(char c)
86 {
87    if ( full() )
88       return EOF;
89
90    move_get_ptr(-1);
91
92    _size++;
93
94    *get_ptr = c;
95    return 0;
96 }
97
98 int streambuf::put(char c)
99 {
100    notify(PUT);
101
102    if ( full() && overflow() == EOF )
103       return EOF;
104
105    *put_ptr = c;
106
107    move_put_ptr(1);
108
109    _size++;
110    _pcount++;
111
112    return 0;
113 }
114
115 int streambuf::move_get_ptr(int one)
116 {
117    switch (one) {
118      case 1:
119
120        get_ptr++;  
121        if ( get_ptr == end )
122          get_ptr = base;
123
124        return 0;
125
126      case -1:
127
128        get_ptr--;  
129        if ( get_ptr == base-1 )
130          get_ptr = end-1;
131
132        return 0;
133
134      default:
135        return EOF;
136    }
137
138 }
139
140 int streambuf::move_put_ptr(int one)
141 {
142    switch (one) {
143      case 1:
144
145        if ( get_ptr == 0 )
146           get_ptr = put_ptr;
147
148        put_ptr++; 
149
150        if ( put_ptr == end )
151          put_ptr = base;
152
153        return 0;
154
155       default:
156           return EOF;
157    }
158 }
159