1 |
bearsoft |
1.1 |
#include "Parse.h"
|
2 |
|
|
|
3 |
|
|
#include "../System/System.h"
|
4 |
|
|
#include "../System/SystemDefine.h"
|
5 |
|
|
#include <math.h>
|
6 |
|
|
#include "StringTools.h"
|
7 |
|
|
|
8 |
|
|
Parse::Parse()
|
9 |
|
|
{
|
10 |
|
|
}
|
11 |
|
|
|
12 |
|
|
Parse::~Parse()
|
13 |
|
|
{
|
14 |
|
|
}
|
15 |
|
|
|
16 |
|
|
//////////////////////////////////////////////////////////////////////////////////////
|
17 |
|
|
//add a Integer String to a Decimal String and returns the result as a char array
|
18 |
|
|
//////////////////////////////////////////////////////////////////////////////////////
|
19 |
|
|
|
20 |
|
|
char *Parse::addIntToDecimal(char *source, int *sourceLength, char *dest, int *destLength, int *totalLength)
|
21 |
|
|
{
|
22 |
|
|
int forward=0;
|
23 |
|
|
|
24 |
|
|
totalLength[0]=(sourceLength[0]+destLength[0]);
|
25 |
|
|
|
26 |
|
|
char *intDecimalDest=(char*)malloc(totalLength[0]*sizeof(char));
|
27 |
|
|
|
28 |
|
|
for ( int s=0 ; s<sourceLength[0] ; s++ )
|
29 |
|
|
{
|
30 |
|
|
intDecimalDest[forward]=source[s];
|
31 |
|
|
forward++;
|
32 |
|
|
}
|
33 |
|
|
|
34 |
|
|
for ( int s2=0 ; s2<(destLength[0]+1) ; s2++ )
|
35 |
|
|
{
|
36 |
|
|
intDecimalDest[forward]=dest[s2];
|
37 |
|
|
forward++;
|
38 |
|
|
}
|
39 |
|
|
|
40 |
|
|
return intDecimalDest;
|
41 |
|
|
}
|
42 |
|
|
|
43 |
|
|
//////////////////////////////////////////////////////////////////////////////////////
|
44 |
|
|
//converts a int to a String (char buffer)
|
45 |
|
|
//////////////////////////////////////////////////////////////////////////////////////
|
46 |
|
|
|
47 |
|
|
char* Parse::intToString(int integer, int *length)
|
48 |
|
|
{
|
49 |
|
|
return intToString(integer, length, false);
|
50 |
|
|
}
|
51 |
|
|
|
52 |
|
|
char* Parse::intToString(int integer, int *length, bool decimalStatement)
|
53 |
|
|
{
|
54 |
|
|
char *intDest=(char*)malloc(12*sizeof(char));
|
55 |
|
|
|
56 |
|
|
if ( integer == 0 )
|
57 |
|
|
{
|
58 |
|
|
if (decimalStatement)
|
59 |
|
|
{
|
60 |
|
|
intDest[0]='-';
|
61 |
|
|
intDest[1]='0';
|
62 |
|
|
intDest[2]=0;
|
63 |
|
|
length[0]=2;
|
64 |
|
|
}
|
65 |
|
|
else
|
66 |
|
|
{
|
67 |
|
|
intDest[0]='0';
|
68 |
|
|
intDest[1]=0;
|
69 |
|
|
length[0]=1;
|
70 |
|
|
}
|
71 |
|
|
|
72 |
|
|
return intDest;
|
73 |
|
|
}
|
74 |
|
|
|
75 |
|
|
int size=9;
|
76 |
|
|
|
77 |
|
|
int div=100000000;
|
78 |
|
|
int forward=0;
|
79 |
|
|
|
80 |
|
|
if ( integer < 0 )
|
81 |
|
|
{
|
82 |
|
|
integer*=-1;
|
83 |
|
|
intDest[0]=(char)'-';
|
84 |
|
|
forward++;
|
85 |
|
|
|
86 |
|
|
}
|
87 |
|
|
|
88 |
|
|
int start=0;
|
89 |
|
|
|
90 |
|
|
for ( int h=0; h<size ; h++ )
|
91 |
|
|
{
|
92 |
|
|
int num=integer/div;
|
93 |
|
|
|
94 |
|
|
if ( num != 0 )
|
95 |
|
|
{
|
96 |
|
|
start=1;
|
97 |
|
|
}
|
98 |
|
|
|
99 |
|
|
intDest[forward]=(char)((int)'0'+num);
|
100 |
|
|
|
101 |
|
|
integer-=(num*div);
|
102 |
|
|
div/=10;
|
103 |
|
|
|
104 |
|
|
if ( start == 1)
|
105 |
|
|
{
|
106 |
|
|
forward++;
|
107 |
|
|
}
|
108 |
|
|
}
|
109 |
|
|
|
110 |
|
|
intDest[forward]=0;
|
111 |
|
|
|
112 |
|
|
length[0]=forward;
|
113 |
|
|
|
114 |
|
|
return intDest;
|
115 |
|
|
}
|
116 |
|
|
|
117 |
|
|
//////////////////////////////////////////////////////////////////////////////////////
|
118 |
|
|
//converts the decimal part of the float to a String
|
119 |
|
|
//////////////////////////////////////////////////////////////////////////////////////
|
120 |
|
|
|
121 |
|
|
char* Parse::floatToStringDecimal(float source, int *length)
|
122 |
|
|
{
|
123 |
|
|
char *decimalDest=(char*)malloc(7*sizeof(char));
|
124 |
|
|
|
125 |
|
|
if ( source == 0 )
|
126 |
|
|
{
|
127 |
|
|
decimalDest[0]=0;
|
128 |
|
|
return decimalDest;
|
129 |
|
|
}
|
130 |
|
|
|
131 |
|
|
int size=4;
|
132 |
|
|
int div=1*10*10*10;
|
133 |
|
|
|
134 |
|
|
source*=div*10;
|
135 |
|
|
|
136 |
|
|
int hdec=(int)floor(source);
|
137 |
|
|
|
138 |
|
|
decimalDest[0]='.';
|
139 |
|
|
|
140 |
|
|
int forward=1;
|
141 |
|
|
|
142 |
|
|
for ( int h=0; h<size ; h++ )
|
143 |
|
|
{
|
144 |
|
|
int num=hdec/div;
|
145 |
|
|
|
146 |
|
|
decimalDest[forward]=(char)((int)'0'+num);
|
147 |
|
|
|
148 |
|
|
hdec-=(num*div);
|
149 |
|
|
div/=10;
|
150 |
|
|
|
151 |
|
|
forward++;
|
152 |
|
|
}
|
153 |
|
|
|
154 |
|
|
decimalDest[forward]=0;
|
155 |
|
|
|
156 |
|
|
length[0]=forward;
|
157 |
|
|
|
158 |
|
|
return decimalDest;
|
159 |
|
|
}
|
160 |
|
|
|
161 |
|
|
//////////////////////////////////////////////////////////////////////////////////////
|
162 |
|
|
//converts a float to a String then it returns it
|
163 |
|
|
//////////////////////////////////////////////////////////////////////////////////////
|
164 |
|
|
|
165 |
|
|
char* Parse::floatToString(float source, int *length)
|
166 |
|
|
{
|
167 |
|
|
char *intDest=0;
|
168 |
|
|
int integer=0;
|
169 |
|
|
float floatSource=0;
|
170 |
|
|
bool decimalStatement=false;
|
171 |
|
|
int floatLength[1]={0};
|
172 |
|
|
int intLength[1]={0};
|
173 |
|
|
|
174 |
|
|
if ( source >= 0 )
|
175 |
|
|
{
|
176 |
|
|
floatSource=source;
|
177 |
|
|
integer=(int)floor((double)source);
|
178 |
|
|
intDest=intToString(integer, intLength);
|
179 |
|
|
floatSource-=(float)integer;
|
180 |
|
|
}
|
181 |
|
|
else
|
182 |
|
|
{
|
183 |
|
|
floatSource=source*-1;
|
184 |
|
|
integer=(int)ceil((double)source);
|
185 |
|
|
intDest=intToString(integer, intLength, true);
|
186 |
|
|
floatSource+=(float)integer;
|
187 |
|
|
}
|
188 |
|
|
|
189 |
|
|
char *decimalDest=floatToStringDecimal(floatSource, floatLength);
|
190 |
|
|
char *intDecimalDest=addIntToDecimal(intDest, intLength, decimalDest, floatLength, length);
|
191 |
|
|
free(intDest);
|
192 |
|
|
free(decimalDest);
|
193 |
|
|
return intDecimalDest;
|
194 |
|
|
}
|
195 |
|
|
|
196 |
|
|
//////////////////////////////////////////////////////////////////////////////////////
|
197 |
|
|
//converts a String to a float if that is now possible
|
198 |
|
|
//////////////////////////////////////////////////////////////////////////////////////
|
199 |
|
|
|
200 |
|
|
float Parse::stringToFloat(char *string)
|
201 |
|
|
{
|
202 |
|
|
float integer=0;
|
203 |
|
|
float decimal=0;
|
204 |
|
|
float floatValue=0;
|
205 |
|
|
int statement=0;
|
206 |
|
|
int neg=1;
|
207 |
|
|
float ggr=1;
|
208 |
|
|
int r=0;
|
209 |
|
|
|
210 |
|
|
while ( string[r] != 0 && statement == 0 )
|
211 |
|
|
{
|
212 |
|
|
if ( string[r] == 'e' )
|
213 |
|
|
{
|
214 |
|
|
statement=1;
|
215 |
|
|
}
|
216 |
|
|
|
217 |
|
|
r++;
|
218 |
|
|
}
|
219 |
|
|
|
220 |
|
|
if ( statement == 0 )
|
221 |
|
|
{
|
222 |
|
|
r=0;
|
223 |
|
|
|
224 |
|
|
if ( string[r] == '-' )
|
225 |
|
|
{
|
226 |
|
|
neg=-1;
|
227 |
|
|
r++;
|
228 |
|
|
}
|
229 |
|
|
|
230 |
|
|
int stop=0;
|
231 |
|
|
|
232 |
|
|
while(string[r] != 0 && stop == 0)
|
233 |
|
|
{
|
234 |
|
|
if (string[r] == '.')
|
235 |
|
|
{
|
236 |
|
|
stop=1;
|
237 |
|
|
}
|
238 |
|
|
else
|
239 |
|
|
{
|
240 |
|
|
integer*=ggr;
|
241 |
|
|
integer+=(string[r]-'0');
|
242 |
|
|
integer*=10;
|
243 |
|
|
}
|
244 |
|
|
|
245 |
|
|
r++;
|
246 |
|
|
}
|
247 |
|
|
|
248 |
|
|
if ( integer != 0 )
|
249 |
|
|
{
|
250 |
|
|
integer/=10;
|
251 |
|
|
}
|
252 |
|
|
|
253 |
|
|
if ( stop == 1 )
|
254 |
|
|
{
|
255 |
|
|
ggr=1;
|
256 |
|
|
|
257 |
|
|
while(string[r] != 0)
|
258 |
|
|
{
|
259 |
|
|
float tempDecimal=(float)(string[r]-'0');
|
260 |
|
|
ggr*=10;
|
261 |
|
|
tempDecimal/=ggr;
|
262 |
|
|
decimal+=tempDecimal;
|
263 |
|
|
|
264 |
|
|
r++;
|
265 |
|
|
}
|
266 |
|
|
}
|
267 |
|
|
|
268 |
|
|
floatValue=(integer+decimal)*neg;
|
269 |
|
|
}
|
270 |
|
|
|
271 |
|
|
return floatValue;
|
272 |
|
|
}
|
273 |
|
|
|
274 |
|
|
|
275 |
|
|
//////////////////////////////////////////////////////////////////////////////////////
|
276 |
|
|
//converts a String to a int, if that is now possible
|
277 |
|
|
//////////////////////////////////////////////////////////////////////////////////////
|
278 |
|
|
|
279 |
|
|
int Parse::stringToInt(char *string)
|
280 |
|
|
{
|
281 |
|
|
int integer=0;
|
282 |
|
|
int statement=0;
|
283 |
|
|
int neg=1;
|
284 |
|
|
int ggr=1;
|
285 |
|
|
int r=0;
|
286 |
|
|
|
287 |
|
|
while ( string[r] != 0 && statement == 0 )
|
288 |
|
|
{
|
289 |
|
|
if ( string[r] == 'e' )
|
290 |
|
|
{
|
291 |
|
|
statement=1;
|
292 |
|
|
}
|
293 |
|
|
|
294 |
|
|
r++;
|
295 |
|
|
}
|
296 |
|
|
|
297 |
|
|
if ( statement == 0 )
|
298 |
|
|
{
|
299 |
|
|
r=0;
|
300 |
|
|
|
301 |
|
|
if ( string[r] == '-' )
|
302 |
|
|
{
|
303 |
|
|
neg=-1;
|
304 |
|
|
r++;
|
305 |
|
|
}
|
306 |
|
|
|
307 |
|
|
int stop=0;
|
308 |
|
|
|
309 |
|
|
while(string[r] != 0 )
|
310 |
|
|
{
|
311 |
|
|
integer*=ggr;
|
312 |
|
|
integer+=(string[r]-'0');
|
313 |
|
|
integer*=10;
|
314 |
|
|
|
315 |
|
|
r++;
|
316 |
|
|
}
|
317 |
|
|
|
318 |
|
|
if ( integer != 0 )
|
319 |
|
|
{
|
320 |
|
|
integer/=10;
|
321 |
|
|
}
|
322 |
|
|
}
|
323 |
|
|
|
324 |
|
|
return integer;
|
325 |
|
|
}
|
326 |
|
|
|