]> www.ginac.de Git - cln.git/blob - src/float/input/cl_F_read_stream.cc
889c6ae6c74f8a3518cca454d49d8e5ff8740834
[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 "cl_sysdep.h"
7
8 // Specification.
9 #include "cl_float_io.h"
10
11
12 // Implementation.
13
14 #include "cl_input.h"
15 #include "cl_io.h"
16 #include "cl_spushstring.h"
17
18 // We read an entire token (or even more, if it begins with #C) into a
19 // buffer and then call read_float() on the buffer.
20
21 class pushstring_hack : public cl_spushstring {
22 public:
23         char* start_pointer (void) { return buffer; }
24         char* end_pointer (void) { return buffer+index; }
25 };
26
27 static cl_boolean number_char_p (char c)
28 {
29         if ((c >= '0') && (c <= '9'))
30                 return cl_true;
31         if (((c >= 'A') && (c <= 'Z')) || ((c >= 'a') && (c <= 'z')))
32                 return cl_true;
33         switch (c) {
34                 case '+': case '-': case '.': case '_': case '/':
35                         return cl_true;
36                 default:
37                         return cl_false;
38         }
39 }
40
41 const cl_F read_float (cl_istream stream, const cl_read_flags& flags)
42 {
43         // One pre-allocated buffer. This reduces the allocation/free cost.
44         static pushstring_hack buffer;
45
46         var int c;
47         // Skip whitespace at the beginning.
48         loop {
49                 c = freadchar(stream);
50                 if (c == cl_EOF) goto eof;
51                 if ((c == ' ') || (c == '\t') || (c == '\n'))
52                         continue;
53                 else
54                         break;
55         }
56         // Found first non-whitespace character.
57         // Numbers cannot cross lines. We can treat EOF and '\n' the same way.
58         buffer.reset();
59         if (c == '#') {
60                 if (!(flags.lsyntax & lsyntax_commonlisp))
61                         goto syntax1;
62                 buffer.push(c);
63                 // Read some digits, then a letter, then a token.
64                 loop {
65                         c = freadchar(stream);
66                         if (c == cl_EOF) goto eof;
67                         buffer.push(c);
68                         if ((c >= '0') && (c <= '9'))
69                                 continue;
70                         else
71                                 break;
72                 }
73                 if (!(((c >= 'A') && (c <= 'Z')) || ((c >= 'a') && (c <= 'z'))))
74                         goto syntax1;
75                 buffer.push(c);
76                 c = freadchar(stream);
77                 if (c == cl_EOF) goto eof;
78         }
79         // Read a number token.
80         if (!number_char_p(c))
81                 goto syntax1;
82         loop {
83                 buffer.push(c);
84                 c = freadchar(stream);
85                 if (c == cl_EOF)
86                         break;
87                 if (!number_char_p(c)) {
88                         funreadchar(stream,c);
89                         break;
90                 }
91         }
92         // Parse the number.
93         return read_float(flags,
94                           buffer.start_pointer(), buffer.end_pointer(),
95                           NULL
96                          );
97
98         // Handle syntax error.
99 syntax1:        buffer.push(c);
100         read_number_bad_syntax(buffer.start_pointer(),buffer.end_pointer());
101
102         // Handle premature EOF.
103 eof:    read_number_eof();
104 }