]> Dogcows Code - chaz/thecheat/blob - Conversions.m
The Cheat 1.2
[chaz/thecheat] / Conversions.m
1
2 // **********************************************************************
3 // The Cheat - A universal game cheater for Mac OS X
4 // (C) 2003-2004 Chaz McGarvey (BrokenZipper)
5 //
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 1, or (at your option)
9 // any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 //
20
21 #import "Conversions.h"
22
23
24 BOOL StringToInt32( NSString *string, SInt32 *value )
25 {
26 SInt32 integer = 0;
27 int place = 1;
28 unichar characters[16];
29 int i, stringlen;
30 BOOL hasReadNumbers = NO, doneReading = NO;
31
32 // fart around with the string
33 string = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
34 stringlen = [string length];
35 if ( stringlen == 0 || stringlen >= 16 ) {
36 // the string has the wrong size to be processed further
37 return NO;
38 }
39 // read the string
40 [string getCharacters:characters];
41
42 // parse the string backwards
43 for ( i = stringlen-1; i >= 0; i-- ) {
44 // if we're done reading but still in the loop, there's a problem
45 if ( doneReading ) {
46 return NO;
47 }
48 if ( characters[i] >= 0x30 && characters[i] <= 0x39 ) {
49 // the character is a number, update our value
50 integer += (characters[i]-48) * place;
51 place *= 10;
52 hasReadNumbers = YES;
53 }
54 else if ( characters[i] == 0x2C /*comma*/ ) {
55 // ignore these characters
56 }
57 else if ( hasReadNumbers ) {
58 // check for plus or minus signs
59 if ( characters[i] == 0x2D ) {
60 integer = -integer;
61 doneReading = YES;
62 }
63 else if ( characters[i] == 0x2B ) {
64 doneReading = YES;
65 }
66 else {
67 return NO;
68 }
69 }
70 else {
71 // the character is invalid, abort
72 return NO;
73 }
74 }
75
76 // success, return the value
77 *value = integer;
78 return YES;
79 }
80
81 BOOL StringToInt16( NSString *string, SInt16 *value )
82 {
83 SInt16 integer = 0;
84 int place = 1;
85 unichar characters[16];
86 int i, stringlen;
87 BOOL hasReadNumbers = NO, doneReading = NO;
88
89 string = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
90 stringlen = [string length];
91 if ( stringlen == 0 || stringlen >= 16 ) {
92 return NO;
93 }
94 [string getCharacters:characters];
95
96 for ( i = stringlen-1; i >= 0; i-- ) {
97 if ( doneReading ) {
98 return NO;
99 }
100 if ( characters[i] >= 0x30 && characters[i] <= 0x39 ) {
101 integer += (characters[i]-48) * place;
102 place *= 10;
103 hasReadNumbers = YES;
104 }
105 else if ( characters[i] == 0x2C /*comma*/ ) {
106 }
107 else if ( hasReadNumbers ) {
108 if ( characters[i] == 0x2D ) {
109 integer = -integer;
110 doneReading = YES;
111 }
112 else if ( characters[i] == 0x2B ) {
113 doneReading = YES;
114 }
115 else {
116 return NO;
117 }
118 }
119 else {
120 return NO;
121 }
122 }
123
124 *value = integer;
125 return YES;
126 }
127
128 BOOL StringToInt8( NSString *string, SInt8 *value )
129 {
130 SInt8 integer = 0;
131 int place = 1;
132 unichar characters[16];
133 int i, stringlen;
134 BOOL hasReadNumbers = NO, doneReading = NO;
135
136 string = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
137 stringlen = [string length];
138 if ( stringlen == 0 || stringlen >= 16 ) {
139 return NO;
140 }
141 [string getCharacters:characters];
142
143 for ( i = stringlen-1; i >= 0; i-- ) {
144 if ( doneReading ) {
145 return NO;
146 }
147 if ( characters[i] >= 0x30 && characters[i] <= 0x39 ) {
148 integer += (characters[i]-48) * place;
149 place *= 10;
150 hasReadNumbers = YES;
151 }
152 else if ( characters[i] == 0x2C /*comma*/ ) {
153 }
154 else if ( hasReadNumbers ) {
155 if ( characters[i] == 0x2D ) {
156 integer = -integer;
157 doneReading = YES;
158 }
159 else if ( characters[i] == 0x2B ) {
160 doneReading = YES;
161 }
162 else {
163 return NO;
164 }
165 }
166 else {
167 return NO;
168 }
169 }
170
171 *value = integer;
172 return YES;
173 }
174
175
176 BOOL StringToUInt32( NSString *string, UInt32 *value )
177 {
178 UInt32 integer = 0;
179 int place = 1;
180 unichar characters[16];
181 int i, stringlen;
182 BOOL hasReadNumbers = NO, doneReading = NO;
183
184 string = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
185 stringlen = [string length];
186 if ( stringlen == 0 || stringlen >= 16 ) {
187 return NO;
188 }
189 [string getCharacters:characters];
190
191 for ( i = stringlen-1; i >= 0; i-- ) {
192 if ( doneReading ) {
193 return NO;
194 }
195 if ( characters[i] >= 0x30 && characters[i] <= 0x39 ) {
196 integer += (characters[i]-48) * place;
197 place *= 10;
198 hasReadNumbers = YES;
199 }
200 else if ( characters[i] == 0x2C /*comma*/ ) {
201 }
202 else if ( hasReadNumbers ) {
203 if ( characters[i] == 0x2D ) {
204 integer = -integer;
205 doneReading = YES;
206 }
207 else if ( characters[i] == 0x2B ) {
208 doneReading = YES;
209 }
210 else {
211 return NO;
212 }
213 }
214 else {
215 return NO;
216 }
217 }
218
219 *value = integer;
220 return YES;
221 }
222
223 BOOL StringToUInt16( NSString *string, UInt16 *value )
224 {
225 UInt16 integer = 0;
226 int place = 1;
227 unichar characters[16];
228 int i, stringlen;
229 BOOL hasReadNumbers = NO, doneReading = NO;
230
231 string = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
232 stringlen = [string length];
233 if ( stringlen == 0 || stringlen >= 16 ) {
234 return NO;
235 }
236 [string getCharacters:characters];
237
238 for ( i = stringlen-1; i >= 0; i-- ) {
239 if ( doneReading ) {
240 return NO;
241 }
242 if ( characters[i] >= 0x30 && characters[i] <= 0x39 ) {
243 integer += (characters[i]-48) * place;
244 place *= 10;
245 hasReadNumbers = YES;
246 }
247 else if ( characters[i] == 0x2C /*comma*/ ) {
248 }
249 else if ( hasReadNumbers ) {
250 if ( characters[i] == 0x2D ) {
251 integer = -integer;
252 doneReading = YES;
253 }
254 else if ( characters[i] == 0x2B ) {
255 doneReading = YES;
256 }
257 else {
258 return NO;
259 }
260 }
261 else {
262 return NO;
263 }
264 }
265
266 *value = integer;
267 return YES;
268 }
269
270 BOOL StringToUInt8( NSString *string, UInt8 *value )
271 {
272 UInt8 integer = 0;
273 int place = 1;
274 unichar characters[16];
275 int i, stringlen;
276 BOOL hasReadNumbers = NO, doneReading = NO;
277
278 string = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
279 stringlen = [string length];
280 if ( stringlen == 0 || stringlen >= 16 ) {
281 return NO;
282 }
283 [string getCharacters:characters];
284
285 for ( i = stringlen-1; i >= 0; i-- ) {
286 if ( doneReading ) {
287 return NO;
288 }
289 if ( characters[i] >= 0x30 && characters[i] <= 0x39 ) {
290 integer += (characters[i]-48) * place;
291 place *= 10;
292 hasReadNumbers = YES;
293 }
294 else if ( characters[i] == 0x2C /*comma*/ ) {
295 }
296 else if ( hasReadNumbers ) {
297 if ( characters[i] == 0x2D ) {
298 integer = -integer;
299 doneReading = YES;
300 }
301 else if ( characters[i] == 0x2B ) {
302 doneReading = YES;
303 }
304 else {
305 return NO;
306 }
307 }
308 else {
309 return NO;
310 }
311 }
312
313 *value = integer;
314 return YES;
315 }
316
317
318 BOOL StringToFloat( NSString *string, float *value )
319 {
320 float decimal;
321 int stringlen;
322
323 stringlen = [string length];
324 if ( stringlen == 0 ) {
325 return NO;
326 }
327
328 decimal = [string floatValue];
329
330 *value = decimal;
331 return YES;
332 }
333
334 BOOL StringToDouble( NSString *string, double *value )
335 {
336 double decimal;
337 int stringlen;
338
339 stringlen = [string length];
340 if ( stringlen == 0 ) {
341 return NO;
342 }
343
344 decimal = [string doubleValue];
345
346 *value = decimal;
347 return YES;
348 }
349
350
351 // general case:
352 BOOL StringToValue( NSString *string, void *value, VariableType type )
353 {
354 switch ( type ) {
355 case Integer64Type:
356 return StringToInt64( string, value );
357 case Integer32Type:
358 return StringToInt32( string, value );
359 case Integer16Type:
360 return StringToInt16( string, value );
361 case Integer08Type:
362 return StringToInt8( string, value );
363 case UInteger64Type:
364 return StringToUInt64( string, value );
365 case UInteger32Type:
366 return StringToUInt32( string, value );
367 case UInteger16Type:
368 return StringToUInt16( string, value );
369 case UInteger08Type:
370 return StringToUInt8( string, value );
371 }
372 return NO;
373 }
374
375
376 NSString *Int32ToString( SInt32 value )
377 {
378 return [NSString stringWithFormat:@"%i", value];
379 }
380
381 NSString *Int16ToString( SInt16 value )
382 {
383 return [NSString stringWithFormat:@"%i", value];
384 }
385
386 NSString *Int8ToString( SInt8 value )
387 {
388 return [NSString stringWithFormat:@"%i", value];
389 }
390
391
392 NSString *UInt32ToString( UInt32 value )
393 {
394 return [NSString stringWithFormat:@"%u", value];
395 }
396
397 NSString *UInt16ToString( UInt16 value )
398 {
399 return [NSString stringWithFormat:@"%u", value];
400 }
401
402 NSString *UInt8ToString( UInt8 value )
403 {
404 return [NSString stringWithFormat:@"%u", value];
405 }
406
407
408 NSString *FloatToString( float value )
409 {
410 return [NSString stringWithFormat:@"%f", value];
411 }
412
413 NSString *DoubleToString( double value )
414 {
415 return [NSString stringWithFormat:@"%f", value];
416 }
417
418 // general case:
419 NSString *ValueToString( void *value, VariableType type )
420 {
421 switch ( type ) {
422 case Integer64Type:
423 return Int64ToString( *(SInt64 *)value );
424 case Integer32Type:
425 return Int32ToString( *(SInt32 *)value );
426 case Integer16Type:
427 return Int16ToString( *(SInt16 *)value );
428 case Integer08Type:
429 return Int8ToString( *(SInt8 *)value );
430 case UInteger64Type:
431 return UInt64ToString( *(UInt64 *)value );
432 case UInteger32Type:
433 return UInt32ToString( *(UInt32 *)value );
434 case UInteger16Type:
435 return UInt16ToString( *(UInt16 *)value );
436 case UInteger08Type:
437 return UInt8ToString( *(UInt8 *)value );
438 }
439 return NO;
440 }
441
442
This page took 0.056441 seconds and 5 git commands to generate.