2 // **********************************************************************
3 // The Cheat - A universal game cheater for Mac OS X
4 // (C) 2003-2004 Chaz McGarvey (BrokenZipper)
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)
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.
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.
21 #import "Conversions.h"
24 BOOL StringToInt32( NSString
*string
, SInt32
*value
)
28 unichar characters
[16];
30 BOOL hasReadNumbers
= NO
, doneReading
= NO
;
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
40 [string getCharacters
:characters
];
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
48 if ( characters
[i
] >= 0x30 && characters
[i
] <= 0x39 ) {
49 // the character is a number, update our value
50 integer
+= (characters
[i
]-48) * place
;
54 else if ( characters
[i
] == 0x2C /*comma*/ ) {
55 // ignore these characters
57 else if ( hasReadNumbers
) {
58 // check for plus or minus signs
59 if ( characters
[i
] == 0x2D ) {
63 else if ( characters
[i
] == 0x2B ) {
71 // the character is invalid, abort
76 // success, return the value
81 BOOL StringToInt16( NSString
*string
, SInt16
*value
)
85 unichar characters
[16];
87 BOOL hasReadNumbers
= NO
, doneReading
= NO
;
89 string
= [string stringByTrimmingCharactersInSet
:[NSCharacterSet whitespaceAndNewlineCharacterSet
]];
90 stringlen
= [string length
];
91 if ( stringlen
== 0 || stringlen
>= 16 ) {
94 [string getCharacters
:characters
];
96 for ( i
= stringlen
-1; i
>= 0; i
-- ) {
100 if ( characters
[i
] >= 0x30 && characters
[i
] <= 0x39 ) {
101 integer
+= (characters
[i
]-48) * place
;
103 hasReadNumbers
= YES
;
105 else if ( characters
[i
] == 0x2C /*comma*/ ) {
107 else if ( hasReadNumbers
) {
108 if ( characters
[i
] == 0x2D ) {
112 else if ( characters
[i
] == 0x2B ) {
128 BOOL StringToInt8( NSString
*string
, SInt8
*value
)
132 unichar characters
[16];
134 BOOL hasReadNumbers
= NO
, doneReading
= NO
;
136 string
= [string stringByTrimmingCharactersInSet
:[NSCharacterSet whitespaceAndNewlineCharacterSet
]];
137 stringlen
= [string length
];
138 if ( stringlen
== 0 || stringlen
>= 16 ) {
141 [string getCharacters
:characters
];
143 for ( i
= stringlen
-1; i
>= 0; i
-- ) {
147 if ( characters
[i
] >= 0x30 && characters
[i
] <= 0x39 ) {
148 integer
+= (characters
[i
]-48) * place
;
150 hasReadNumbers
= YES
;
152 else if ( characters
[i
] == 0x2C /*comma*/ ) {
154 else if ( hasReadNumbers
) {
155 if ( characters
[i
] == 0x2D ) {
159 else if ( characters
[i
] == 0x2B ) {
176 BOOL StringToUInt32( NSString
*string
, UInt32
*value
)
180 unichar characters
[16];
182 BOOL hasReadNumbers
= NO
, doneReading
= NO
;
184 string
= [string stringByTrimmingCharactersInSet
:[NSCharacterSet whitespaceAndNewlineCharacterSet
]];
185 stringlen
= [string length
];
186 if ( stringlen
== 0 || stringlen
>= 16 ) {
189 [string getCharacters
:characters
];
191 for ( i
= stringlen
-1; i
>= 0; i
-- ) {
195 if ( characters
[i
] >= 0x30 && characters
[i
] <= 0x39 ) {
196 integer
+= (characters
[i
]-48) * place
;
198 hasReadNumbers
= YES
;
200 else if ( characters
[i
] == 0x2C /*comma*/ ) {
202 else if ( hasReadNumbers
) {
203 if ( characters
[i
] == 0x2D ) {
207 else if ( characters
[i
] == 0x2B ) {
223 BOOL StringToUInt16( NSString
*string
, UInt16
*value
)
227 unichar characters
[16];
229 BOOL hasReadNumbers
= NO
, doneReading
= NO
;
231 string
= [string stringByTrimmingCharactersInSet
:[NSCharacterSet whitespaceAndNewlineCharacterSet
]];
232 stringlen
= [string length
];
233 if ( stringlen
== 0 || stringlen
>= 16 ) {
236 [string getCharacters
:characters
];
238 for ( i
= stringlen
-1; i
>= 0; i
-- ) {
242 if ( characters
[i
] >= 0x30 && characters
[i
] <= 0x39 ) {
243 integer
+= (characters
[i
]-48) * place
;
245 hasReadNumbers
= YES
;
247 else if ( characters
[i
] == 0x2C /*comma*/ ) {
249 else if ( hasReadNumbers
) {
250 if ( characters
[i
] == 0x2D ) {
254 else if ( characters
[i
] == 0x2B ) {
270 BOOL StringToUInt8( NSString
*string
, UInt8
*value
)
274 unichar characters
[16];
276 BOOL hasReadNumbers
= NO
, doneReading
= NO
;
278 string
= [string stringByTrimmingCharactersInSet
:[NSCharacterSet whitespaceAndNewlineCharacterSet
]];
279 stringlen
= [string length
];
280 if ( stringlen
== 0 || stringlen
>= 16 ) {
283 [string getCharacters
:characters
];
285 for ( i
= stringlen
-1; i
>= 0; i
-- ) {
289 if ( characters
[i
] >= 0x30 && characters
[i
] <= 0x39 ) {
290 integer
+= (characters
[i
]-48) * place
;
292 hasReadNumbers
= YES
;
294 else if ( characters
[i
] == 0x2C /*comma*/ ) {
296 else if ( hasReadNumbers
) {
297 if ( characters
[i
] == 0x2D ) {
301 else if ( characters
[i
] == 0x2B ) {
318 BOOL StringToFloat( NSString
*string
, float *value
)
323 stringlen
= [string length
];
324 if ( stringlen
== 0 ) {
328 decimal
= [string floatValue
];
334 BOOL StringToDouble( NSString
*string
, double *value
)
339 stringlen
= [string length
];
340 if ( stringlen
== 0 ) {
344 decimal
= [string doubleValue
];
352 BOOL StringToValue( NSString
*string
, void *value
, VariableType type
)
356 return StringToInt64( string
, value
);
358 return StringToInt32( string
, value
);
360 return StringToInt16( string
, value
);
362 return StringToInt8( string
, value
);
364 return StringToUInt64( string
, value
);
366 return StringToUInt32( string
, value
);
368 return StringToUInt16( string
, value
);
370 return StringToUInt8( string
, value
);
376 NSString
*Int32ToString( SInt32 value
)
378 return [NSString stringWithFormat
:@
"%i", value
];
381 NSString
*Int16ToString( SInt16 value
)
383 return [NSString stringWithFormat
:@
"%i", value
];
386 NSString
*Int8ToString( SInt8 value
)
388 return [NSString stringWithFormat
:@
"%i", value
];
392 NSString
*UInt32ToString( UInt32 value
)
394 return [NSString stringWithFormat
:@
"%u", value
];
397 NSString
*UInt16ToString( UInt16 value
)
399 return [NSString stringWithFormat
:@
"%u", value
];
402 NSString
*UInt8ToString( UInt8 value
)
404 return [NSString stringWithFormat
:@
"%u", value
];
408 NSString
*FloatToString( float value
)
410 return [NSString stringWithFormat
:@
"%f", value
];
413 NSString
*DoubleToString( double value
)
415 return [NSString stringWithFormat
:@
"%f", value
];
419 NSString
*ValueToString( void *value
, VariableType type
)
423 return Int64ToString( *(SInt64
*)value
);
425 return Int32ToString( *(SInt32
*)value
);
427 return Int16ToString( *(SInt16
*)value
);
429 return Int8ToString( *(SInt8
*)value
);
431 return UInt64ToString( *(UInt64
*)value
);
433 return UInt32ToString( *(UInt32
*)value
);
435 return UInt16ToString( *(UInt16
*)value
);
437 return UInt8ToString( *(UInt8
*)value
);