3 * The Cheat - The legendary universal game trainer for Mac OS X.
4 * http://www.brokenzipper.com/trac/wiki/TheCheat
6 * Copyright (c) 2003-2011, Charles McGarvey et al.
8 * Distributable under the terms and conditions of the 2-clause BSD
9 * license; see the file COPYING for the legal text of the license.
12 #import "Conversions.h"
15 BOOL StringToInt32( NSString
*string
, SInt32
*value
)
19 unichar characters
[16];
21 BOOL hasReadNumbers
= NO
, doneReading
= NO
;
23 // fart around with the string
24 string
= [string stringByTrimmingCharactersInSet
:[NSCharacterSet whitespaceAndNewlineCharacterSet
]];
25 stringlen
= [string length
];
26 if ( stringlen
== 0 || stringlen
>= 16 ) {
27 // the string has the wrong size to be processed further
31 [string getCharacters
:characters
];
33 // parse the string backwards
34 for ( i
= stringlen
-1; i
>= 0; i
-- ) {
35 // if we're done reading but still in the loop, there's a problem
39 if ( characters
[i
] >= 0x30 && characters
[i
] <= 0x39 ) {
40 // the character is a number, update our value
41 integer
+= (characters
[i
]-48) * place
;
45 else if ( characters
[i
] == 0x2C /*comma*/ ) {
46 // ignore these characters
48 else if ( hasReadNumbers
) {
49 // check for plus or minus signs
50 if ( characters
[i
] == 0x2D ) {
54 else if ( characters
[i
] == 0x2B ) {
62 // the character is invalid, abort
67 // success, return the value
72 BOOL StringToInt16( NSString
*string
, SInt16
*value
)
76 unichar characters
[16];
78 BOOL hasReadNumbers
= NO
, doneReading
= NO
;
80 string
= [string stringByTrimmingCharactersInSet
:[NSCharacterSet whitespaceAndNewlineCharacterSet
]];
81 stringlen
= [string length
];
82 if ( stringlen
== 0 || stringlen
>= 16 ) {
85 [string getCharacters
:characters
];
87 for ( i
= stringlen
-1; i
>= 0; i
-- ) {
91 if ( characters
[i
] >= 0x30 && characters
[i
] <= 0x39 ) {
92 integer
+= (characters
[i
]-48) * place
;
96 else if ( characters
[i
] == 0x2C /*comma*/ ) {
98 else if ( hasReadNumbers
) {
99 if ( characters
[i
] == 0x2D ) {
103 else if ( characters
[i
] == 0x2B ) {
119 BOOL StringToInt8( NSString
*string
, SInt8
*value
)
123 unichar characters
[16];
125 BOOL hasReadNumbers
= NO
, doneReading
= NO
;
127 string
= [string stringByTrimmingCharactersInSet
:[NSCharacterSet whitespaceAndNewlineCharacterSet
]];
128 stringlen
= [string length
];
129 if ( stringlen
== 0 || stringlen
>= 16 ) {
132 [string getCharacters
:characters
];
134 for ( i
= stringlen
-1; i
>= 0; i
-- ) {
138 if ( characters
[i
] >= 0x30 && characters
[i
] <= 0x39 ) {
139 integer
+= (characters
[i
]-48) * place
;
141 hasReadNumbers
= YES
;
143 else if ( characters
[i
] == 0x2C /*comma*/ ) {
145 else if ( hasReadNumbers
) {
146 if ( characters
[i
] == 0x2D ) {
150 else if ( characters
[i
] == 0x2B ) {
167 BOOL StringToUInt32( NSString
*string
, UInt32
*value
)
171 unichar characters
[16];
173 BOOL hasReadNumbers
= NO
, doneReading
= NO
;
175 string
= [string stringByTrimmingCharactersInSet
:[NSCharacterSet whitespaceAndNewlineCharacterSet
]];
176 stringlen
= [string length
];
177 if ( stringlen
== 0 || stringlen
>= 16 ) {
180 [string getCharacters
:characters
];
182 for ( i
= stringlen
-1; i
>= 0; i
-- ) {
186 if ( characters
[i
] >= 0x30 && characters
[i
] <= 0x39 ) {
187 integer
+= (characters
[i
]-48) * place
;
189 hasReadNumbers
= YES
;
191 else if ( characters
[i
] == 0x2C /*comma*/ ) {
193 else if ( hasReadNumbers
) {
194 if ( characters
[i
] == 0x2D ) {
198 else if ( characters
[i
] == 0x2B ) {
214 BOOL StringToUInt16( NSString
*string
, UInt16
*value
)
218 unichar characters
[16];
220 BOOL hasReadNumbers
= NO
, doneReading
= NO
;
222 string
= [string stringByTrimmingCharactersInSet
:[NSCharacterSet whitespaceAndNewlineCharacterSet
]];
223 stringlen
= [string length
];
224 if ( stringlen
== 0 || stringlen
>= 16 ) {
227 [string getCharacters
:characters
];
229 for ( i
= stringlen
-1; i
>= 0; i
-- ) {
233 if ( characters
[i
] >= 0x30 && characters
[i
] <= 0x39 ) {
234 integer
+= (characters
[i
]-48) * place
;
236 hasReadNumbers
= YES
;
238 else if ( characters
[i
] == 0x2C /*comma*/ ) {
240 else if ( hasReadNumbers
) {
241 if ( characters
[i
] == 0x2D ) {
245 else if ( characters
[i
] == 0x2B ) {
261 BOOL StringToUInt8( NSString
*string
, UInt8
*value
)
265 unichar characters
[16];
267 BOOL hasReadNumbers
= NO
, doneReading
= NO
;
269 string
= [string stringByTrimmingCharactersInSet
:[NSCharacterSet whitespaceAndNewlineCharacterSet
]];
270 stringlen
= [string length
];
271 if ( stringlen
== 0 || stringlen
>= 16 ) {
274 [string getCharacters
:characters
];
276 for ( i
= stringlen
-1; i
>= 0; i
-- ) {
280 if ( characters
[i
] >= 0x30 && characters
[i
] <= 0x39 ) {
281 integer
+= (characters
[i
]-48) * place
;
283 hasReadNumbers
= YES
;
285 else if ( characters
[i
] == 0x2C /*comma*/ ) {
287 else if ( hasReadNumbers
) {
288 if ( characters
[i
] == 0x2D ) {
292 else if ( characters
[i
] == 0x2B ) {
309 BOOL StringToFloat( NSString
*string
, float *value
)
314 stringlen
= [string length
];
315 if ( stringlen
== 0 ) {
319 decimal
= [string floatValue
];
325 BOOL StringToDouble( NSString
*string
, double *value
)
330 stringlen
= [string length
];
331 if ( stringlen
== 0 ) {
335 decimal
= [string doubleValue
];
343 BOOL StringToValue( NSString
*string
, void *value
, VariableType type
)
347 return StringToInt64( string
, value
);
349 return StringToInt32( string
, value
);
351 return StringToInt16( string
, value
);
353 return StringToInt8( string
, value
);
355 return StringToUInt64( string
, value
);
357 return StringToUInt32( string
, value
);
359 return StringToUInt16( string
, value
);
361 return StringToUInt8( string
, value
);
367 NSString
*Int32ToString( SInt32 value
)
369 return [NSString stringWithFormat
:@
"%i", value
];
372 NSString
*Int16ToString( SInt16 value
)
374 return [NSString stringWithFormat
:@
"%i", value
];
377 NSString
*Int8ToString( SInt8 value
)
379 return [NSString stringWithFormat
:@
"%i", value
];
383 NSString
*UInt32ToString( UInt32 value
)
385 return [NSString stringWithFormat
:@
"%u", value
];
388 NSString
*UInt16ToString( UInt16 value
)
390 return [NSString stringWithFormat
:@
"%u", value
];
393 NSString
*UInt8ToString( UInt8 value
)
395 return [NSString stringWithFormat
:@
"%u", value
];
399 NSString
*FloatToString( float value
)
401 return [NSString stringWithFormat
:@
"%f", value
];
404 NSString
*DoubleToString( double value
)
406 return [NSString stringWithFormat
:@
"%f", value
];
410 NSString
*ValueToString( void *value
, VariableType type
)
414 return Int64ToString( *(SInt64
*)value
);
416 return Int32ToString( *(SInt32
*)value
);
418 return Int16ToString( *(SInt16
*)value
);
420 return Int8ToString( *(SInt8
*)value
);
422 return UInt64ToString( *(UInt64
*)value
);
424 return UInt32ToString( *(UInt32
*)value
);
426 return UInt16ToString( *(UInt16
*)value
);
428 return UInt8ToString( *(UInt8
*)value
);