Debian Codegraph @

Contrib/Math/Source/MyMath.c

raw
1 // Unicode support by Jim Park -- 08/22/2007
2
3 #include <windows.h>
4 #include <stdio.h>
5 #include "MyMath.h"
6 #include "Math.h"
7
8 // Converts String to Int (Dec, Hex) or Float value
9 void StringToItem(TCHAR *&s, ExpressionItem *item, int options)
10 {
11 item->type = IT_CONST | ITC_INT;
12 __int64 &v=*((__int64*)&(item->param1));
13 v = 0;
14
15 // Check for right input
16 if (!s) return;
17
18 // String-value
19 if ((((options & (STI_FLOAT | STI_INT)) == 0) || *s == _T('\'') || *s == _T('\"') || *s == _T('`') ||
20 ((*s != _T('+')) && (*s != _T('-')) && ((*s < _T('0')) || (*s > _T('9')))))
21 && (options & STI_STRING))
22 {
23 // end of string char
24 TCHAR eol = 0;
25 if (*s == _T('\'') || *s == _T('\"') || *s == _T('`')) eol = *s;
26 else s--;
27
28 item->type = IT_CONST | ITC_STRING;
29 // allocate memory buffer for string
30 TCHAR *sp;
31 sp = *((TCHAR**)&(item->param1)) = AllocString();
32 while (*(++s) && (*s != eol))
33 {
34 *(sp++) = *s;
35 }
36 if (*s == eol) s++;
37 *sp = 0;
38 } else
39 {
40 // strip leading spaces and tabs
41 while ((*s == _T(' ')) || (*s == _T('\t'))) s++;
42 // Hex-value
43 if ((options & STI_INT) && *s == _T('0') && (s[1] == _T('x') || s[1] == _T('X')))
44 {
45 s++;
46 while (*(s+1) == _T('0')) s++;
47 for (;;)
48 {
49 int c=*(++s);
50 if (c >= _T('0') && c <= _T('9')) c-=_T('0');
51 else if (c >= _T('a') && c <= _T('f')) c-=_T('a')-10;
52 else if (c >= _T('A') && c <= _T('F')) c-=_T('A')-10;
53 else break;
54 v<<=4;
55 v+=c;
56 }
57 }
58 // Dec-value, possible floating-point
59 else
60 {
61 int sign=0, numsignif = 0;
62 if (*s == _T('-')) sign++; else s--;
63 while (*(s+1) == _T('0')) s++;
64 for (;;)
65 {
66 int c=*(++s) - _T('0'); numsignif++;
67 if ((options & STI_FLOAT) &&
68 ((c == (_T('e')-_T('0'))) || (c==(_T('E')-_T('0'))) || (c==(_T('.')-_T('0')))
69 || (numsignif > 18)))
70 {
71 // Switch to floating point conversion routine
72 item->type = IT_CONST | ITC_FLOAT;
73 double& d = *((double*)&(item->param1));
74 d = (double) v;
75
76 while ((c >= 0) && (c <= 9))
77 {
78 d = d*10.0 + (double) c;
79 c=*(++s) - _T('0');
80 }
81
82 // sub-decimal part
83 if (c == (_T('.')-_T('0')))
84 {
85 double pwr = 1.0, dec = 0.0;
86 for (;;)
87 {
88 c=*(++s) - _T('0');
89 if ((c < 0) || (c > 9)) break;
90 dec = dec*10.0 + (double) c;
91 pwr *= 10.0;
92 }
93 d += dec/pwr;
94 }
95 // exponental part
96 if ((c == (_T('E')-_T('0'))) || (c == (_T('e')-_T('0'))))
97 {
98 int expc = 0, esign = 0;
99 s++;
100 // detect exponential sign
101 if ((*s == _T('+')) || (*s == _T('-')))
102 esign = (*s == _T('-'));
103 else s--;
104
105 // detect exp value
106 for (;;)
107 {
108 c=*(++s) - _T('0');
109 if ((c < 0) || (c > 9)) break;
110 expc = expc*10 + c;
111 }
112
113 if (expc >= DBL_MAX_EXP)
114 {
115 d = HUGE_VAL;
116 expc = 0;
117 }
118
119 double pwr = 1;
120 while (expc > 99) { pwr *= 1.0e100; expc -= 100; }
121 while (expc > 9) { pwr *= 1.0e10; expc -= 10; }
122 while (expc) { pwr *= 10.0; expc--; }
123 if (esign) d /= pwr;
124 else d *= pwr;
125 }
126 if (sign) d = -d;
127 return;
128 }
129 if (c < 0 || c > 9) break;
130 v*=10;
131 v+=c;
132 }
133 if (sign) v = -v;
134 if ((options & STI_FLOAT) && ((options & STI_INT) == 0))
135 {
136 double& d = *((double*)&(item->param1));
137 d = (double) v;
138 item->type = IT_CONST | ITC_FLOAT;
139 }
140 }
141 }
142 }
143
144 void ItemToString(TCHAR *sbuf, ExpressionItem *item)
145 {
146 if ((item == NULL) || ((item->type & ITEMTYPE) != IT_CONST))
147 {
148 *sbuf = 0;
149 return;
150 }
151
152 switch (item->type & ITEMSUBTYPE)
153 {
154 case ITC_STRING:
155 {
156 TCHAR *ptr = *((TCHAR**)&(item->param1));
157 while ( (*(sbuf++) = *(ptr++)) );
158 }
159 break;
160 case ITC_ARRAY:
161 {
162 ArrayDesc *ad = (ArrayDesc *) item->param1;
163 for (int index = 0; index < ad->count; index++)
164 if ((ad->array[index]) &&
165 ((ad->array[index]->type & (ITEMTYPE|ITEMSUBTYPE)) == (IT_CONST | ITC_INT)))
166 if ((*(sbuf++) = (TCHAR) *((__int64*)&(ad->array[index]->param1))) == 0)
167 break;
168 }
169 break;
170 case ITC_FLOAT:
171 FloatFormat(sbuf, *((double*)&(item->param1)), 6);
172 break;
173 case ITC_INT:
174 itoa64(*((__int64*)&(item->param1)), sbuf);
175 break;
176 }
177 }
178
179 void itoa64(__int64 i, TCHAR *buffer)
180 {
181 TCHAR buf[128], *b = buf;
182
183 if (i < 0)
184 {
185 *(buffer++) = _T('-');
186 i = -i;
187 }
188 if (i == 0) *(buffer++) = _T('0');
189 else
190 {
191 while (i > 0)
192 {
193 *(b++) = _T('0') + ((TCHAR) (i%10));
194 i /= 10;
195 }
196 while (b > buf) *(buffer++) = *(--b);
197 }
198 *buffer = 0;
199 }
200
201 #define POS_INFINITY _T("#INF")
202 #define NEG_INFINITY _T("-#INF")
203
204 void FloatFormat(TCHAR *s, double value, int options)
205 {
206 TCHAR format[128];
207 int prec = options & 0xF;
208
209 *s = 0;
210
211 if(value == HUGE_VAL)
212 {
213 lstrcpy(s, POS_INFINITY);
214 return;
215 } else if(value == -HUGE_VAL) {
216 lstrcpy(s, NEG_INFINITY);
217 return;
218 }
219
220 if (options & FF_NOEXP)
221 {
222 _stprintf(format, _T("%%.%df"), prec);
223 }
224 else if (options & FF_EXP)
225 {
226 _stprintf(format, _T("%%.%de"), prec);
227 }
228 else if (options & FF_LEXP)
229 {
230 _stprintf(format, _T("%%.%dE"), prec);
231 }
232 else
233 {
234 _stprintf(format, _T("%%.%dg"), prec);
235 }
236
237 _stprintf(s, format, value);
238 }
239
240 int lstrcmpn(TCHAR *s1, const TCHAR *s2, int chars)
241 {
242 while ((chars > 0) && (*s1) && (*s2) && (*(s1) == *(s2))) chars--, s1++, s2++;
243 if ((chars == 0) || (*s1 == *s2)) return 0;
244 return (*s1 - *s2);
245 }