Initial snark14m import
[snark14.git] / src / snark / getnum.cpp
1 /*
2  ***********************************************************
3  $SNARK_Header: S N A R K  1 4 - A PICTURE RECONSTRUCTION PROGRAM $
4  $HeadURL: svn://dig.cs.gc.cuny.edu/snark/trunk/src/snark/getnum.cpp $
5  $LastChangedRevision: 80 $
6  $Date: 2014-07-01 21:01:54 -0400 (Tue, 01 Jul 2014) $
7  $Author: agulati $
8  ***********************************************************
9
10  GETNUM SCANS THE INPUT AREA FOR THE NEXT NUMBER AND RETURNS
11  THE INTERNAL FLOATING POINT REPRESENTATION OF THE NUMBER.
12  IF THE INPUT AREA DOES NOT CONTAIN A NUMBER, ZERO IS
13  RETURNED AND EOL IS SET TO .TRUE.
14  */
15
16 #include <cstdlib>    
17 #include <cstdio>
18 #include <cmath>
19 #include <cctype>
20
21 #include "blkdta.h"
22 #include "uiod.h"
23 #include "chtoin.h"
24
25 #include "inputfile.h"
26
27 REAL InputFile_class::getnum(BOOLEAN NewLine, BOOLEAN* eol)
28 {
29         CHAR nextch;
30         REAL mainval = 0, mainsign = 1;
31         REAL expval = 0, expsign = 1;
32         REAL numdigitsafterdot = 0;
33
34         *eol = FALSE;
35         if (NewLine)
36                 getnxt(FALSE);
37
38         // FIND START OF NUMBER (FIRST DIGIT OR DECIMAL POINT) AND SIGN
39
40         while (true)
41         {
42                 ptr++;
43                 nextch = data[ptr];
44                 if (ptr > DATASIZ)     // bug 190 - swr - 12/09/05
45                 {
46                         *eol = TRUE;
47                         return 0.0;
48                 }
49                 if (isdigit(nextch))
50                         break;
51                 if (nextch == '.')
52                         break;
53                 if (nextch == '-')
54                         mainsign = -1;
55                 else
56                         mainsign = 1;
57         }
58
59         if (isdigit(nextch))
60         {
61                 while (isdigit(nextch))
62                 {
63                         mainval = mainval * 10.0 + chtoin(nextch);
64                         ptr++;
65                         nextch = data[ptr];
66                 }
67                 if (nextch == '.')
68                 {
69                         ptr++;
70                         nextch = data[ptr];
71                         while (isdigit(nextch))
72                         {
73                                 // try to parse (digit*)
74                                 mainval = mainval * 10 + chtoin(nextch);
75                                 numdigitsafterdot += 1;  // number of digits after '.'
76                                 ptr++;
77                                 nextch = data[ptr];
78                         }
79                 } // --period read after digit
80         } // --digit read before (if at all) period
81         else
82         {
83                 if (nextch == '.')
84                 {
85                         // parse (.digit+)
86                         ptr++;
87                         nextch = data[ptr];
88                         if (!isdigit(nextch))
89                         {
90                                 // need a digit there
91                                 fprintf(output,
92                                                 "\n **** illegal floating point modifier encountered\n **** program aborted\n");
93                                 exit(-1);
94                         }
95                         while (isdigit(nextch))
96                         {
97                                 mainval = mainval * 10 + chtoin(nextch);
98                                 numdigitsafterdot += 1;  // number of digits after '.'
99                                 ptr++;
100                                 nextch = data[ptr];
101                         }
102                 } // --period read before any digit
103                 else
104                 {
105                         // {digit+[.digit*],.digit+} not found
106                         fprintf(output,
107                                         "\n **** illegal floating point modifier encountered\n **** program aborted\n");
108                         exit(-1);
109                 }
110         }
111
112         if ((nextch == 'e') || (nextch == 'E'))
113         {
114                 // try to parse ([+,-]digit[digit])
115                 ptr++;
116                 nextch = data[ptr];
117                 if (nextch == '-')
118                 {
119                         expsign = -1;
120                         ptr++;
121                         nextch = data[ptr];
122                 }
123                 else
124                 {
125                         if (nextch == '+')
126                         {
127                                 ptr++;
128                                 nextch = data[ptr];
129                         }
130                 }
131                 if (!isdigit(nextch))
132                 {
133                         fprintf(output,
134                                         "\n **** illegal floating point modifier encountered\n **** program aborted\n");
135                         exit(-1);
136                 }
137                 else
138                 {
139                         expval = chtoin(nextch);
140                         ptr++;
141                         nextch = data[ptr];
142                 }
143                 if (isdigit(nextch))
144                 {
145                         // second possible digit
146                         expval = expval * 10 + chtoin(nextch);
147                         ptr++;
148                         nextch = data[ptr];
149                 }
150         }
151
152         if (!isspace(nextch))
153         {
154                 // there is junk at the end
155                 fprintf(output,
156                                 "\n **** illegal floating point modifier encountered\n **** program aborted\n");
157                 exit(-1);
158         }
159
160         return mainsign * mainval * pow(10, (expsign * expval - numdigitsafterdot));
161 }