]> www.ginac.de Git - cln.git/blob - src/float/input/cl_F_read_stream.cc
Use paths relative the `src' directory in the #include directives.
[cln.git] / src / float / input / cl_F_read_stream.cc
1 // read_float().
2 // This file contains a slimmed down version of read_real().
3 // It does not pull in all the generic real number code.
4
5 // General includes.
6 #include "base/cl_sysdep.h"
7
8 // Specification.
9 #include "cln/float_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_float() 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_F read_float (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 = freadchar(stream);
52                 if (c == cl_EOF) 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 = freadchar(stream);
68                         if (c == cl_EOF) 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 = freadchar(stream);
78                 if (c == cl_EOF) 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 = freadchar(stream);
86                 if (c == cl_EOF)
87                         break;
88                 if (!number_char_p(c)) {
89                         funreadchar(stream,c);
90                         break;
91                 }
92         }
93         // Parse the number.
94         return read_float(flags,
95                           buffer.start_pointer(), buffer.end_pointer(),
96                           NULL
97                          );
98
99         // Handle syntax error.
100 syntax1:        buffer.push(c);
101         throw read_number_bad_syntax_exception(buffer.start_pointer(),buffer.end_pointer());
102
103         // Handle premature EOF.
104 eof:    throw read_number_eof_exception();
105 }
106
107 }  // namespace cln