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