]> www.ginac.de Git - cln.git/blob - src/base/string/input/cl_st_get2.cc
79d92c06145487267f8dc9bb8ee89faafdba0ab4
[cln.git] / src / base / string / input / cl_st_get2.cc
1 // cl_fget().
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
17 #if ((defined(__sparc__) || defined(__rs6000__) || defined(__mips__)) && !defined(__GNUC__))
18 // Sun C++ doesn't have istream::unget() and istream::set().
19   #define unget()  putback(c)
20   #define set(x)  setf(x)
21 #endif
22
23 const cl_string cl_fget (cl_istream stream, int n, char delim)
24 {
25         var cl_spushstring buffer;
26         // Handling of eofp is tricky: EOF is reached when (!stream.good()) || (stream.get()==EOF).
27         while (stream.good()) {
28                 var int c = stream.get();
29                 if (c==EOF)
30                         break;  // ios::eofbit already set
31                 if (c==delim) {
32                         stream.unget();
33                         break;
34                 }
35                 if (--n <= 0) {
36                         stream.unget();
37                         #if defined(__GNUG__) && (__GNUC_MINOR__ < 8)
38                         stream.set(ios::failbit);
39                         #else // new ANSI C++
40                         stream.setstate(ios::failbit);
41                         #endif
42                         break;
43                 }
44                 buffer.push(c);
45         }
46         return buffer.contents();
47 }
48
49 #endif