Disable all code related to libXp
[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 librararies 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(USL) || defined(__osf__) || defined(linux) || \
27     defined(CSRG_BASED) || defined(sun)
28 #include <stdlib.h>
29 #else
30 #include <libc.h>
31 #endif
32
33 #define DEF_BUF_SIZ 4096
34
35 streambuf::streambuf() : _size(0), _capacity(DEF_BUF_SIZ), _alloc(1),
36    _pcount(0), _gcount(0)
37 {
38    base = (char*)malloc(DEF_BUF_SIZ);
39    end = base + DEF_BUF_SIZ;
40    get_ptr = put_ptr = base;
41 }
42
43 streambuf::streambuf(char* p, int l, int bufferFull) :
44     base(p), end(p+l), put_ptr(p), get_ptr(p), _size(0), _capacity(l), 
45     _alloc(0), _pcount(0), _gcount(0)
46 {
47    if (bufferFull) {
48      _size = l;
49    }
50 }
51
52 streambuf::~streambuf()
53 {
54    if ( _alloc )
55       free(base);
56 }
57
58 int streambuf::examine()
59 {
60    notify(GET);
61
62    if ( empty() && underflow() == EOF )
63       return EOF;
64
65    return (unsigned char)(*get_ptr);
66 }
67
68 int streambuf::get()
69 {
70    notify(GET);
71
72    if ( empty() && underflow() == EOF )
73       return EOF;
74
75    int x = (unsigned char)(*get_ptr);
76
77    move_get_ptr(+1);
78
79    _size--;
80
81    _gcount++;
82
83    return x;
84 }
85
86 int streambuf::putback(char c)
87 {
88    if ( full() )
89       return EOF;
90
91    move_get_ptr(-1);
92
93    _size++;
94
95    *get_ptr = c;
96    return 0;
97 }
98
99 int streambuf::put(char c)
100 {
101    notify(PUT);
102
103    if ( full() && overflow() == EOF )
104       return EOF;
105
106    *put_ptr = c;
107
108    move_put_ptr(1);
109
110    _size++;
111    _pcount++;
112
113    return 0;
114 }
115
116 int streambuf::move_get_ptr(int one)
117 {
118    switch (one) {
119      case 1:
120
121        get_ptr++;  
122        if ( get_ptr == end )
123          get_ptr = base;
124
125        return 0;
126
127      case -1:
128
129        get_ptr--;  
130        if ( get_ptr == base-1 )
131          get_ptr = end-1;
132
133        return 0;
134
135      default:
136        return EOF;
137    }
138
139 }
140
141 int streambuf::move_put_ptr(int one)
142 {
143    switch (one) {
144      case 1:
145
146        if ( get_ptr == 0 )
147           get_ptr = put_ptr;
148
149        put_ptr++; 
150
151        if ( put_ptr == end )
152          put_ptr = base;
153
154        return 0;
155
156       default:
157           return EOF;
158    }
159 }
160