Add GNU LGPL headers to all .c .C and .h files
[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    for ( int count = 0 ; count < lim-1; count++ ) {
87
88       i = sbuf -> get();
89
90       if ( i == EOF || i == delim ) {
91 //fprintf(stderr, "prematual break in _getline(): i=%d, count = %d\n", i, count);
92         break;
93       }
94
95       b[count] = char(i);
96    }
97
98    if ( fill_zero )
99       b[count] = 0;
100
101    return *this;
102 }
103
104 istream&        
105 istream::read(char* s, int n)
106 {
107    return _getline(s, n+1, EOF, 0);
108 }
109
110 int             
111 istream::gcount() 
112 {
113    return sbuf -> gcount();
114 }
115
116 istream& istream::operator>>(char& c)
117 {
118    int x;
119
120    if ( (x=sbuf->examine()) == EOF ) return *this;
121
122    c = (char)x;
123
124    sbuf -> get();
125
126    return *this;
127 }
128
129 int istream::eatw()
130 {
131    if ( fail() ) return EOF;
132
133    int c = sbuf->examine();
134
135    if (c == EOF) set_fail();
136    
137    while (isspace(c) && c != EOF) {
138       sbuf->get();
139       c = sbuf->examine();
140    }
141
142    if ( c == EOF ) set_fail();
143
144    return c;
145 }
146
147 istream& istream::operator>>(char* s)
148 {
149    int c;
150    if ( (c=eatw()) == EOF ) return *this;
151
152    do {
153       *s++ = c;
154       sbuf->get();
155       c = sbuf -> examine();
156    } while (!isspace(c) && c != EOF) ;
157    
158    *s = '\0';
159
160    if (c == EOF) {
161      set_fail();
162    }
163
164    return *this;
165 }
166
167 istream&        
168 istream::operator>>(unsigned short& x)
169 {
170    unsigned int l = 0;
171
172    *this >> l;
173
174    x = (unsigned short)l;
175
176    return *this;
177 }
178
179 istream&        
180 istream::operator>>(unsigned int& n)
181 {
182    int x;
183    if ( (x=eatw()) == EOF ) return *this;
184
185    n = 0;
186
187    if ( isdigit(x) ) {
188       do {
189           sbuf -> get();
190           n = n*10+x-'0';
191       } while (isdigit(x=sbuf->examine()));
192    } else {
193        set_fail();
194        return *this;
195    }
196
197    return *this;
198
199
200 }
201
202 istream&        
203 istream::operator>>(int& x)
204 {
205    long l = 0;
206
207    *this >> l;
208
209    x = int(l);
210
211    return *this;
212 }
213
214 istream&        
215 istream::operator>>(long& n)
216 {
217    int x;
218
219    if ( (x=eatw()) == EOF ) return *this;
220
221    int sign = '+';
222
223    switch (x) {
224      case '+':
225      case '-':
226        sign = x;
227        sbuf -> get();
228        x = sbuf -> examine();
229        break;
230      case EOF:
231        set_fail();
232        return *this;
233    }
234
235    n = 0;
236    if ( isdigit(x) ) {
237       do {
238           sbuf -> get();
239           n = n*10+x-'0';
240       } while (isdigit(x=sbuf->examine()));
241       if (sign=='-') 
242         n = -n;
243    } else {
244        set_fail();
245        return *this;
246    }
247
248    return *this;
249 }
250
251 /////////////////////
252 ostream&        
253 ostream::operator<<(void* a)
254 {
255    if ( fail() ) return *this;
256    long x = (long)a;
257    *this << x;
258    return *this;
259 }
260
261 ostream&        
262 ostream::operator<<(const char* str)
263 {
264    if ( str == 0 ) {
265      set_bad();
266      return *this;
267    }
268
269    while (*str) {
270      if (sbuf->put(*str++) == EOF) {
271         set_fail();
272         break;
273      }
274    } 
275
276    return *this;
277 }
278
279 ostream&        
280 ostream::operator<<(char c)
281 {
282    if ( fail() ) return *this;
283    sbuf -> put(c);
284    return *this;
285 }
286
287 ostream&        
288 ostream::operator<<(int i)
289 {
290    long x = i;
291    *this << x;
292    return *this;
293 }
294
295 ostream&        
296 ostream::operator<<(unsigned int l)
297 {
298    long x = l;
299    *this << x;
300    return *this;
301 }
302
303 ostream&        
304 ostream::operator<<(long x)
305 {
306    if ( fail() ) return *this;
307
308    char buf[32];
309    char *p = buf;
310
311    if (x < 0) {
312      sbuf->put('-');
313      x = -x;
314    } 
315
316    do {
317       *p++ = '0' + char(x%10);
318       x /= 10;
319    } while (x > 0);
320
321    do {
322      if (sbuf->put(*--p) == EOF) {
323         set_fail();
324         break;
325      }
326    } while (p != buf);
327
328
329    return *this;
330 }
331
332 ostream&        
333 ostream::operator<< (ostream& (*f)(ostream&))
334 {
335    return (*f)(*this) ;
336 }
337
338 ostream& ostream::put(char c)
339 {
340    if ( fail() ) return *this;
341    sbuf -> put(c);
342    return *this;
343 }
344
345 ostream& ostream::flush() 
346 {
347    if ( fail() ) return *this;
348    sbuf -> flush();
349    return *this;
350 }
351
352 ostream&        
353 ostream::write(const char* s, int n)
354 {
355    for ( int i=0; i<n; i++ ) {
356      if ( sbuf->put(s[i]) == EOF )
357         break;
358    }
359    return *this;
360 }
361
362 ostream& endl(ostream& out) 
363 {
364    return out << '\n';
365 }
366
367 istream::istream(streambuf* sb) : ios(sb)
368 {
369    sbuf = sb;
370 }
371
372 ostream::ostream(streambuf* sb) : ios(sb)
373 {
374    sbuf = sb;
375 }
376
377 iostream::iostream(streambuf* sb) : istream(sb), ostream(sb)
378 {
379    sbuf = sb;
380 }
381