numparser2.h

Sun, 21 May 2000 17:33:48 +0200

author
tuomov
date
Sun, 21 May 2000 17:33:48 +0200
changeset 12
5fd153b29d40
parent 9
55e7f2ff6021
child 15
9f5760af87d9
permissions
-rw-r--r--

trunk: changeset 15
- Optparser generates --help from option descriptions in the
OptParserOpt structure

- Changed the options --license, --authors and --proginfo to single
--about

0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
1 /*
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
2 * libtu/numparser2.h
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
3 *
9
55e7f2ff6021 trunk: changeset 12
tuomov
parents: 3
diff changeset
4 * Copyright (c) Tuomo Valkonen 1999-2000.
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
5 * See the included file LICENSE for details.
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
6 */
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
7
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
8 #define MAX_MANTISSA 10 /* should be enough for our needs */
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
9 #define ULONG_SIZE (sizeof(ulong)*8)
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
10
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
11 enum{
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
12 NPNUM_INT,
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
13 NPNUM_FLOAT
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
14 };
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
15
3
b1fbfab67908 trunk: changeset 6
tuomov
parents: 2
diff changeset
16 #ifdef NP_SIMPLE_IMPL
b1fbfab67908 trunk: changeset 6
tuomov
parents: 2
diff changeset
17
b1fbfab67908 trunk: changeset 6
tuomov
parents: 2
diff changeset
18 typedef struct _NPNum{
b1fbfab67908 trunk: changeset 6
tuomov
parents: 2
diff changeset
19 int type;
b1fbfab67908 trunk: changeset 6
tuomov
parents: 2
diff changeset
20 int base;
b1fbfab67908 trunk: changeset 6
tuomov
parents: 2
diff changeset
21 bool negative;
b1fbfab67908 trunk: changeset 6
tuomov
parents: 2
diff changeset
22 long double fval;
b1fbfab67908 trunk: changeset 6
tuomov
parents: 2
diff changeset
23 ulong ival;
b1fbfab67908 trunk: changeset 6
tuomov
parents: 2
diff changeset
24 } NPNum;
b1fbfab67908 trunk: changeset 6
tuomov
parents: 2
diff changeset
25
b1fbfab67908 trunk: changeset 6
tuomov
parents: 2
diff changeset
26 #define NUM_INIT {0, 0, 0, 0.0, 0}
b1fbfab67908 trunk: changeset 6
tuomov
parents: 2
diff changeset
27
b1fbfab67908 trunk: changeset 6
tuomov
parents: 2
diff changeset
28 static int npnum_mulbase_add(NPNum *num, long base, long v)
b1fbfab67908 trunk: changeset 6
tuomov
parents: 2
diff changeset
29 {
b1fbfab67908 trunk: changeset 6
tuomov
parents: 2
diff changeset
30 long double iold=num->ival;
b1fbfab67908 trunk: changeset 6
tuomov
parents: 2
diff changeset
31
b1fbfab67908 trunk: changeset 6
tuomov
parents: 2
diff changeset
32 num->fval=num->fval*base+(long double)v;
b1fbfab67908 trunk: changeset 6
tuomov
parents: 2
diff changeset
33
b1fbfab67908 trunk: changeset 6
tuomov
parents: 2
diff changeset
34 num->ival*=base;
b1fbfab67908 trunk: changeset 6
tuomov
parents: 2
diff changeset
35
b1fbfab67908 trunk: changeset 6
tuomov
parents: 2
diff changeset
36 if(num->ival<iold)
b1fbfab67908 trunk: changeset 6
tuomov
parents: 2
diff changeset
37 num->type=NPNUM_FLOAT;
b1fbfab67908 trunk: changeset 6
tuomov
parents: 2
diff changeset
38
b1fbfab67908 trunk: changeset 6
tuomov
parents: 2
diff changeset
39 num->ival+=v;
b1fbfab67908 trunk: changeset 6
tuomov
parents: 2
diff changeset
40
b1fbfab67908 trunk: changeset 6
tuomov
parents: 2
diff changeset
41 return 0;
b1fbfab67908 trunk: changeset 6
tuomov
parents: 2
diff changeset
42 }
b1fbfab67908 trunk: changeset 6
tuomov
parents: 2
diff changeset
43
b1fbfab67908 trunk: changeset 6
tuomov
parents: 2
diff changeset
44 #else /* NP_SIMPLE_IMPL */
b1fbfab67908 trunk: changeset 6
tuomov
parents: 2
diff changeset
45
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
46 typedef struct _NPNum{
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
47 unsigned char nmantissa;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
48 int type;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
49 int base;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
50 bool negative;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
51 ulong mantissa[MAX_MANTISSA];
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
52 long exponent;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
53 } NPNum;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
54
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
55 #define NUM_INIT {0, 0, 0, 0, {0,}, 0}
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
56
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
57 #define ADD_EXP(NUM, X) (NUM)->exponent+=(X);
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
58
3
b1fbfab67908 trunk: changeset 6
tuomov
parents: 2
diff changeset
59 #if defined(__GNUG__) && defined(i386) && !defined(NP_NO_I386_ASM)
b1fbfab67908 trunk: changeset 6
tuomov
parents: 2
diff changeset
60 #define NP_I386_ASM
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
61 #endif
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
62
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
63 static int npnum_mulbase_add(NPNum *num, long base, long v)
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
64 {
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
65 long i, j;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
66 ulong overflow;
3
b1fbfab67908 trunk: changeset 6
tuomov
parents: 2
diff changeset
67 #ifndef NP_I386_ASM
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
68 ulong val;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
69 #endif
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
70
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
71 for(i=num->nmantissa;i>=0;i--){
3
b1fbfab67908 trunk: changeset 6
tuomov
parents: 2
diff changeset
72 #ifdef NP_I386_ASM
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
73 __asm__("mul %4\n"
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
74 : "=a" (num->mantissa[i]), "=d" (overflow)
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
75 : "0" (num->mantissa[i]), "1" (0), "q" (base)
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
76 : "eax", "edx");
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
77 #else
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
78 overflow=0;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
79 val=num->mantissa[i];
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
80
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
81 if(val<ULONG_MAX/base){
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
82 val*=base;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
83 }else if(val){
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
84 ulong tmp=val;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
85 ulong old=val;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
86 for(j=0; j<base; j++){
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
87 val+=tmp;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
88 if(val<=old)
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
89 overflow++;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
90 old=val;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
91 }
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
92 }
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
93 num->mantissa[i]=val;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
94 #endif
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
95 if(overflow){
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
96 if(i==num->nmantissa){
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
97 if(num->nmantissa==MAX_MANTISSA)
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
98 return E_TOKZ_TOOBIG;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
99 num->nmantissa++;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
100 }
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
101 num->mantissa[i+1]+=overflow;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
102 }
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
103 }
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
104 num->mantissa[0]+=v;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
105
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
106 return 0;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
107 }
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
108
3
b1fbfab67908 trunk: changeset 6
tuomov
parents: 2
diff changeset
109 #undef NP_I386_ASM
b1fbfab67908 trunk: changeset 6
tuomov
parents: 2
diff changeset
110
b1fbfab67908 trunk: changeset 6
tuomov
parents: 2
diff changeset
111 #endif /* NP_SIMPLE_IMPL */
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
112
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
113
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
114 /* */
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
115
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
116
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
117 static bool is_end(int c)
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
118 {
1
6e704fc09528 trunk: changeset 4
tuomov
parents: 0
diff changeset
119 /* oops... EOF was missing */
6e704fc09528 trunk: changeset 4
tuomov
parents: 0
diff changeset
120 return (c==EOF || (c!='.' && ispunct(c)) || isspace(c) || iscntrl(c));
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
121 }
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
122
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
123
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
124 /* */
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
125
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
126
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
127 static int parse_exponent(NPNum *num, Tokenizer *tokz, int c)
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
128 {
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
129 long exp=0;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
130 bool neg=FALSE;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
131 int err=0;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
132
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
133 c=GETCH();
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
134
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
135 if(c=='-' || c=='+'){
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
136 if(c=='-')
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
137 neg=TRUE;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
138 c=GETCH();
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
139 }
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
140
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
141 for(; 1; c=GETCH()){
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
142 if(isdigit(c)){
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
143 exp*=10;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
144 exp+=c-'0';
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
145 }else if(is_end(c)){
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
146 UNGETCH(c);
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
147 break;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
148 }else{
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
149 err=E_TOKZ_NUMFMT;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
150 }
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
151 }
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
152
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
153 if(neg)
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
154 exp*=-1;
3
b1fbfab67908 trunk: changeset 6
tuomov
parents: 2
diff changeset
155
b1fbfab67908 trunk: changeset 6
tuomov
parents: 2
diff changeset
156 #ifndef NP_SIMPLE_IMPL
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
157 ADD_EXP(num, exp);
3
b1fbfab67908 trunk: changeset 6
tuomov
parents: 2
diff changeset
158 #else
b1fbfab67908 trunk: changeset 6
tuomov
parents: 2
diff changeset
159 num->fval*=pow(num->base, exp);
b1fbfab67908 trunk: changeset 6
tuomov
parents: 2
diff changeset
160 #endif
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
161 return err;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
162 }
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
163
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
164
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
165 static int parse_number(NPNum *num, Tokenizer *tokz, int c)
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
166 {
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
167 int base=10;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
168 int dm=1;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
169 int err=0;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
170 int tmp;
3
b1fbfab67908 trunk: changeset 6
tuomov
parents: 2
diff changeset
171 #ifdef NP_SIMPLE_IMPL
b1fbfab67908 trunk: changeset 6
tuomov
parents: 2
diff changeset
172 long double divisor=base;
b1fbfab67908 trunk: changeset 6
tuomov
parents: 2
diff changeset
173 #endif
b1fbfab67908 trunk: changeset 6
tuomov
parents: 2
diff changeset
174
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
175 if(c=='-' || c=='+'){
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
176 if(c=='-')
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
177 num->negative=TRUE;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
178 c=GETCH();
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
179 if(!isdigit(c))
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
180 err=E_TOKZ_NUMFMT;
3
b1fbfab67908 trunk: changeset 6
tuomov
parents: 2
diff changeset
181 }
b1fbfab67908 trunk: changeset 6
tuomov
parents: 2
diff changeset
182
b1fbfab67908 trunk: changeset 6
tuomov
parents: 2
diff changeset
183 if(c=='0'){
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
184 dm=0;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
185 c=GETCH();
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
186 if(c=='x' || c=='X'){
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
187 base=16;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
188 c=GETCH();
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
189 }else if(c=='b' || c=='B'){
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
190 base=2;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
191 c=GETCH();
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
192 }else if('0'<=c && c<='7'){
3
b1fbfab67908 trunk: changeset 6
tuomov
parents: 2
diff changeset
193 base=8;
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
194 }else{
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
195 dm=2;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
196 }
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
197 }
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
198
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
199 num->base=base;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
200
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
201 for(; 1; c=GETCH()){
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
202 if((c=='e' || c=='E') && dm!=0){
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
203 if(dm<2){
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
204 err=E_TOKZ_NUMFMT;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
205 continue;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
206 }
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
207 tmp=parse_exponent(num, tokz, c);
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
208 if(err==0)
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
209 err=tmp;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
210 break;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
211 }
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
212
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
213 if(isxdigit(c)){
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
214 if('0'<=c && c<='9')
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
215 c-='0';
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
216 else if(isupper(c))
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
217 c-='A'-10;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
218 else
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
219 c-='a'-10;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
220
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
221 if(c>=base)
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
222 err=E_TOKZ_NUMFMT;
3
b1fbfab67908 trunk: changeset 6
tuomov
parents: 2
diff changeset
223
b1fbfab67908 trunk: changeset 6
tuomov
parents: 2
diff changeset
224 #ifdef NP_SIMPLE_IMPL
b1fbfab67908 trunk: changeset 6
tuomov
parents: 2
diff changeset
225 if(dm==3){
b1fbfab67908 trunk: changeset 6
tuomov
parents: 2
diff changeset
226 num->fval+=(long double)c/divisor;
b1fbfab67908 trunk: changeset 6
tuomov
parents: 2
diff changeset
227 divisor*=base;
b1fbfab67908 trunk: changeset 6
tuomov
parents: 2
diff changeset
228 }else
b1fbfab67908 trunk: changeset 6
tuomov
parents: 2
diff changeset
229 #endif
b1fbfab67908 trunk: changeset 6
tuomov
parents: 2
diff changeset
230 {
b1fbfab67908 trunk: changeset 6
tuomov
parents: 2
diff changeset
231 tmp=npnum_mulbase_add(num, base, c);
b1fbfab67908 trunk: changeset 6
tuomov
parents: 2
diff changeset
232 if(err==0)
b1fbfab67908 trunk: changeset 6
tuomov
parents: 2
diff changeset
233 err=tmp;
b1fbfab67908 trunk: changeset 6
tuomov
parents: 2
diff changeset
234 }
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
235
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
236 if(dm==1)
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
237 dm=2;
3
b1fbfab67908 trunk: changeset 6
tuomov
parents: 2
diff changeset
238 #ifndef NP_SIMPLE_IMPL
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
239 else if(dm==3)
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
240 ADD_EXP(num, -1);
3
b1fbfab67908 trunk: changeset 6
tuomov
parents: 2
diff changeset
241 #endif
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
242 continue;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
243 }
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
244
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
245 if(c=='.'){
3
b1fbfab67908 trunk: changeset 6
tuomov
parents: 2
diff changeset
246 if(dm!=2){
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
247 err=E_TOKZ_NUMFMT;
3
b1fbfab67908 trunk: changeset 6
tuomov
parents: 2
diff changeset
248 }
b1fbfab67908 trunk: changeset 6
tuomov
parents: 2
diff changeset
249 dm=3;
b1fbfab67908 trunk: changeset 6
tuomov
parents: 2
diff changeset
250 #ifdef NP_SIMPLE_IMPL
b1fbfab67908 trunk: changeset 6
tuomov
parents: 2
diff changeset
251 num->type=NPNUM_FLOAT;
b1fbfab67908 trunk: changeset 6
tuomov
parents: 2
diff changeset
252 divisor=base;
b1fbfab67908 trunk: changeset 6
tuomov
parents: 2
diff changeset
253 #endif
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
254 continue;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
255 }
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
256
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
257 if(is_end(c)){
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
258 UNGETCH(c);
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
259 break;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
260 }
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
261
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
262 err=E_TOKZ_NUMFMT;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
263 }
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
264
3
b1fbfab67908 trunk: changeset 6
tuomov
parents: 2
diff changeset
265 #ifndef NP_SIMPLE_IMPL
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
266 num->type=(num->exponent==0 ? NPNUM_INT : NPNUM_FLOAT);
3
b1fbfab67908 trunk: changeset 6
tuomov
parents: 2
diff changeset
267 #endif
b1fbfab67908 trunk: changeset 6
tuomov
parents: 2
diff changeset
268
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
269 return err;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
270 }

mercurial