]> www.ginac.de Git - cln.git/blob - src/base/string/input/cl_st_get2.cc
8ef1e260e43f3837203ecddbb4b254a554b2dc5c
[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 "cln/string.h"
8
9
10 // Implementation.
11
12 #include "cln/io.h"
13 #include "cl_spushstring.h"
14
15 namespace cln {
16
17 const cl_string cl_fget (std::istream& stream, int n, char delim)
18 {
19         var cl_spushstring buffer;
20         // Handling of eofp is tricky: EOF is reached when (!stream.good()) || (stream.get()==EOF).
21         while (stream.good()) {
22                 var int c = stream.get();
23                 if (c==EOF)
24                         break;  // ios::eofbit already set
25                 if (c==delim) {
26                         stream.unget();
27                         break;
28                 }
29                 if (--n <= 0) {
30                         stream.unget();
31                         stream.setstate(std::ios::failbit);
32                         break;
33                 }
34                 buffer.push(c);
35         }
36         return buffer.contents();
37 }
38
39 }  // namespace cln