Disable all code related to libXp
[oweals/cde.git] / cde / programs / dtinfo / DtMmdb / utility / iostream.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: iostream.C /main/4 1996/08/21 15:54:53 drk $
24
25 #include "utility/c_iostream.h"
26 #include <stdio.h>
27 #include <ctype.h>
28
29 istream&        
30 istream::seekg(streampos delta, ios::seek_dir d) 
31 {
32    if ( fail() ) return *this;
33
34    if ( d != ios::beg || sbuf -> seekg(delta) == EOF ) {
35      set_bad();
36      set_fail();
37    }
38    return *this;
39 }
40
41 int istream::get()
42 {
43    return sbuf -> get();
44 }
45
46 istream& istream::get(char& c)
47 {
48    if ( fail() ) return *this;
49
50    int i = sbuf -> get();
51
52    if ( i == EOF ) {
53      set_fail();
54      return *this;
55    }
56
57    c = i;
58    return *this;
59 }
60
61 istream&        
62 istream::putback(char c)
63 {
64    sbuf -> putback(c);
65    return *this;
66 }
67
68 istream& istream::getline(char* b, int lim, char delim)
69 {
70    return _getline(b, lim, delim, 1);
71 }
72
73 istream& istream::_getline(char* b, int lim, int delim, int fill_zero)
74 {
75    if ( fail() ) return *this;
76
77    if ( sbuf -> examine() == EOF ) {
78      set_fail();
79      return *this;
80    }
81
82    sbuf -> clear_gcount();
83
84    int i;
85
86    int count;
87    for ( count = 0 ; count < lim-1; count++ ) {
88
89       i = sbuf -> get();
90
91       if ( i == EOF || i == delim ) {
92 //fprintf(stderr, "prematual break in _getline(): i=%d, count = %d\n", i, count);
93         break;
94       }
95
96       b[count] = char(i);
97    }
98
99    if ( fill_zero )
100       b[count] = 0;
101
102    return *this;
103 }
104
105 istream&        
106 istream::read(char* s, int n)
107 {
108    return _getline(s, n+1, EOF, 0);
109 }
110
111 int             
112 istream::gcount() 
113 {
114    return sbuf -> gcount();
115 }
116
117 istream& istream::operator>>(char& c)
118 {
119    int x;
120
121    if ( (x=sbuf->examine()) == EOF ) return *this;
122
123    c = (char)x;
124
125    sbuf -> get();
126
127    return *this;
128 }
129
130 int istream::eatw()
131 {
132    if ( fail() ) return EOF;
133
134    int c = sbuf->examine();
135
136    if (c == EOF) set_fail();
137    
138    while (isspace(c) && c != EOF) {
139       sbuf->get();
140       c = sbuf->examine();
141    }
142
143    if ( c == EOF ) set_fail();
144
145    return c;
146 }
147
148 istream& istream::operator>>(char* s)
149 {
150    int c;
151    if ( (c=eatw()) == EOF ) return *this;
152
153    do {
154       *s++ = c;
155       sbuf->get();
156       c = sbuf -> examine();
157    } while (!isspace(c) && c != EOF) ;
158    
159    *s = '\0';
160
161    if (c == EOF) {
162      set_fail();
163    }
164
165    return *this;
166 }
167
168 istream&        
169 istream::operator>>(unsigned short& x)
170 {
171    unsigned int l = 0;
172
173    *this >> l;
174
175    x = (unsigned short)l;
176
177    return *this;
178 }
179
180 istream&        
181 istream::operator>>(unsigned int& n)
182 {
183    int x;
184    if ( (x=eatw()) == EOF ) return *this;
185
186    n = 0;
187
188    if ( isdigit(x) ) {
189       do {
190           sbuf -> get();
191           n = n*10+x-'0';
192       } while (isdigit(x=sbuf->examine()));
193    } else {
194        set_fail();
195        return *this;
196    }
197
198    return *this;
199
200
201 }
202
203 istream&        
204 istream::operator>>(int& x)
205 {
206    long l = 0;
207
208    *this >> l;
209
210    x = int(l);
211
212    return *this;
213 }
214
215 istream&        
216 istream::operator>>(long& n)
217 {
218    int x;
219
220    if ( (x=eatw()) == EOF ) return *this;
221
222    int sign = '+';
223
224    switch (x) {
225      case '+':
226      case '-':
227        sign = x;
228        sbuf -> get();
229        x = sbuf -> examine();
230        break;
231      case EOF:
232        set_fail();
233        return *this;
234    }
235
236    n = 0;
237    if ( isdigit(x) ) {
238       do {
239           sbuf -> get();
240           n = n*10+x-'0';
241       } while (isdigit(x=sbuf->examine()));
242       if (sign=='-') 
243         n = -n;
244    } else {
245        set_fail();
246        return *this;
247    }
248
249    return *this;
250 }
251
252 /////////////////////
253 ostream&        
254 ostream::operator<<(void* a)
255 {
256    if ( fail() ) return *this;
257    long x = (long)a;
258    *this << x;
259    return *this;
260 }
261
262 ostream&        
263 ostream::operator<<(const char* str)
264 {
265    if ( str == 0 ) {
266      set_bad();
267      return *this;
268    }
269
270    while (*str) {
271      if (sbuf->put(*str++) == EOF) {
272         set_fail();
273         break;
274      }
275    } 
276
277    return *this;
278 }
279
280 ostream&        
281 ostream::operator<<(char c)
282 {
283    if ( fail() ) return *this;
284    sbuf -> put(c);
285    return *this;
286 }
287
288 ostream&        
289 ostream::operator<<(int i)
290 {
291    long x = i;
292    *this << x;
293    return *this;
294 }
295
296 ostream&        
297 ostream::operator<<(unsigned int l)
298 {
299    long x = l;
300    *this << x;
301    return *this;
302 }
303
304 ostream&        
305 ostream::operator<<(long x)
306 {
307    if ( fail() ) return *this;
308
309    char buf[32];
310    char *p = buf;
311
312    if (x < 0) {
313      sbuf->put('-');
314      x = -x;
315    } 
316
317    do {
318       *p++ = '0' + char(x%10);
319       x /= 10;
320    } while (x > 0);
321
322    do {
323      if (sbuf->put(*--p) == EOF) {
324         set_fail();
325         break;
326      }
327    } while (p != buf);
328
329
330    return *this;
331 }
332
333 ostream&        
334 ostream::operator<< (ostream& (*f)(ostream&))
335 {
336    return (*f)(*this) ;
337 }
338
339 ostream& ostream::put(char c)
340 {
341    if ( fail() ) return *this;
342    sbuf -> put(c);
343    return *this;
344 }
345
346 ostream& ostream::flush() 
347 {
348    if ( fail() ) return *this;
349    sbuf -> flush();
350    return *this;
351 }
352
353 ostream&        
354 ostream::write(const char* s, int n)
355 {
356    for ( int i=0; i<n; i++ ) {
357      if ( sbuf->put(s[i]) == EOF )
358         break;
359    }
360    return *this;
361 }
362
363 ostream& endl(ostream& out) 
364 {
365    return out << '\n';
366 }
367
368 istream::istream(streambuf* sb) : ios(sb)
369 {
370    sbuf = sb;
371 }
372
373 ostream::ostream(streambuf* sb) : ios(sb)
374 {
375    sbuf = sb;
376 }
377
378 iostream::iostream(streambuf* sb) : istream(sb), ostream(sb)
379 {
380    sbuf = sb;
381 }
382