]> www.ginac.de Git - cln.git/blob - src/base/string/input/cl_st_gettoken.cc
Initial revision
[cln.git] / src / base / string / input / cl_st_gettoken.cc
1 // operator>>.
2
3 // General includes.
4 #include "cl_sysdep.h"
5
6 // Specification.
7 #include "cl_string.h"
8
9
10 // Implementation.
11
12 #ifdef CL_IO_IOSTREAM
13
14 #include "cl_io.h"
15 #include "cl_spushstring.h"
16 #include <ctype.h>
17
18 #if ((defined(__sparc__) || defined(__rs6000__) || defined(__mips__)) && !defined(__GNUC__))
19 // Sun C++ doesn't have istream::unget().
20   #define unget()  putback(c)
21 #endif
22
23 cl_istream operator>> (cl_istream stream, cl_string& str)
24 {
25         var cl_spushstring buffer;
26         var int n = stream.width();
27         // Handling of eofp is tricky: EOF is reached when (!stream.good()) || (stream.get()==EOF).
28         int c;
29         // Skip whitespace.
30         while (stream.good()) {
31                 c = stream.get();
32                 if (c==EOF)
33                         break;
34                 if (!isspace(c)) {
35                         if (--n == 0) {
36                                 // stream.width()==1, means no characters.
37                                 stream.unget();
38                                 break;
39                         }
40                         // If stream.width()==0, n gets negative and never 0.
41                         goto nonws;
42                 }
43         }
44         goto done;
45         // Read non-whitespace.
46         while (stream.good()) {
47                 c = stream.get();
48                 if (c==EOF)
49                         break;
50                 if (isspace(c)) {
51                         stream.unget();
52                         break;
53                 }
54             nonws:
55                 buffer.push(c);
56                 if (--n == 0)
57                         break;
58         }
59     done:
60         str = buffer.contents();
61         stream.width(0);
62         return stream;
63 }
64
65 #endif