]> www.ginac.de Git - cln.git/blob - src/integer/input/cl_I_read_stream.cc
Finalize CLN 1.3.7 release.
[cln.git] / src / integer / input / cl_I_read_stream.cc
1 // read_integer().
2 // This file contains a slimmed down version of read_rational().
3 // It does not pull in all the rational number code.
4
5 // General includes.
6 #include "base/cl_sysdep.h"
7
8 // Specification.
9 #include "cln/integer_io.h"
10
11
12 // Implementation.
13
14 #include "cln/input.h"
15 #include "cln/io.h"
16 #include "base/string/cl_spushstring.h"
17
18 namespace cln {
19
20 // We read an entire token (or even more, if it begins with #C) into a
21 // buffer and then call read_integer() on the buffer.
22
23 class pushstring_hack : public cl_spushstring {
24 public:
25         char* start_pointer (void) { return buffer; }
26         char* end_pointer (void) { return buffer+index; }
27 };
28
29 static bool number_char_p (char c)
30 {
31         if ((c >= '0') && (c <= '9'))
32                 return true;
33         if (((c >= 'A') && (c <= 'Z')) || ((c >= 'a') && (c <= 'z')))
34                 return true;
35         switch (c) {
36                 case '+': case '-': case '.': case '_': case '/':
37                         return true;
38                 default:
39                         return false;
40         }
41 }
42
43 const cl_I read_integer (std::istream& stream, const cl_read_flags& flags)
44 {
45         // One pre-allocated buffer. This reduces the allocation/free cost.
46         static pushstring_hack buffer;
47
48         var int c;
49         // Skip whitespace at the beginning.
50         loop {
51                 c = stream.get();
52                 if (stream.eof() || stream.fail()) goto eof;
53                 if ((c == ' ') || (c == '\t') || (c == '\n'))
54                         continue;
55                 else
56                         break;
57         }
58         // Found first non-whitespace character.
59         // Numbers cannot cross lines. We can treat EOF and '\n' the same way.
60         buffer.reset();
61         if (c == '#') {
62                 if (!(flags.lsyntax & lsyntax_commonlisp))
63                         goto syntax1;
64                 buffer.push(c);
65                 // Read some digits, then a letter, then a token.
66                 loop {
67                         c = stream.get();
68                         if (stream.eof() || stream.fail()) goto eof;
69                         buffer.push(c);
70                         if ((c >= '0') && (c <= '9'))
71                                 continue;
72                         else
73                                 break;
74                 }
75                 if (!(((c >= 'A') && (c <= 'Z')) || ((c >= 'a') && (c <= 'z'))))
76                         goto syntax1;
77                 c = stream.get();
78                 if (stream.eof() || stream.fail()) goto eof;
79         }
80         // Read a number token.
81         if (!number_char_p(c))
82                 goto syntax1;
83         loop {
84                 buffer.push(c);
85                 c = stream.peek();  // Avoid fail state on EOF.
86                 if (stream.eof() || stream.fail() || !number_char_p(c))
87                         break;
88                 c = stream.get();
89         }
90         // Parse the number.
91         return read_integer(flags,
92                             buffer.start_pointer(), buffer.end_pointer(),
93                             NULL
94                            );
95
96         // Handle syntax error.
97 syntax1:        buffer.push(c);
98         throw read_number_bad_syntax_exception(buffer.start_pointer(),buffer.end_pointer());
99
100         // Handle premature EOF.
101 eof:    throw read_number_eof_exception();
102 }
103
104 }  // namespace cln