1 /*---------------------------------------------------------------------------*
2 Project: TwlSDK - libraries - STD
3 File: std_stdlib.c
4
5 Copyright 2005-2008 Nintendo. All rights reserved.
6
7 These coded instructions, statements, and computer programs contain
8 proprietary information of Nintendo of America Inc. and/or Nintendo
9 Company Ltd., and are protected by Federal copyright law. They may
10 not be disclosed to third parties or copied or duplicated in any form,
11 in whole or in part, without the prior written consent of Nintendo.
12
13 $Date:: 2008-11-07#$
14 $Rev: 9262 $
15 $Author: yosizaki $
16 *---------------------------------------------------------------------------*/
17 #include <nitro.h>
18 #include <limits.h>
19 //================================================================================
20 // Convert numeral String
21 //================================================================================
22 /*---------------------------------------------------------------------------*
23 Name: STD_ConvertAsciiToInt
24
25 Description: Convert numeral string to integer.
26 Same to atoi()
27
28 Arguments: s : pointer to string
29
30 Returns: value
31 *---------------------------------------------------------------------------*/
STD_ConvertAsciiToInt(const char * s)32 int STD_ConvertAsciiToInt( const char* s )
33 {
34 BOOL isPlus = TRUE;
35 unsigned int val = 0;
36
37 //---- skip space
38 while( *s == ' ' )
39 {
40 s ++;
41 }
42
43 //---- check sign
44 if ( *s == '-' )
45 {
46 isPlus = FALSE;
47 s ++;
48 }
49 else if ( *s == '+' )
50 {
51 s ++;
52 }
53
54 //---- loop while number
55 while( '0' <= *s && *s <= '9' )
56 {
57 val = (val * 10) + (*s - '0');
58 //---- check overflow
59 if ( val > INT_MAX )
60 {
61 return (isPlus)? INT_MAX: INT_MIN;
62 }
63 s++;
64 }
65 return (isPlus)? (int) val: (int) -val;
66 }
67
68 /*---------------------------------------------------------------------------*
69 Name: STD_ConvertAsciiToLong
70
71 Description: Convert numeral string to long integer.
72 Same to atol()
73
74 Arguments: s : pointer to string
75
76 Returns: value
77 *---------------------------------------------------------------------------*/
STD_ConvertAsciiToLong(const char * s)78 long int STD_ConvertAsciiToLong( const char* s )
79 {
80 BOOL isPlus = TRUE;
81 unsigned long int val = 0;
82
83 //---- skip space
84 while( *s == ' ' )
85 {
86 s ++;
87 }
88
89 //---- check sign
90 if ( *s == '-' )
91 {
92 isPlus = FALSE;
93 s ++;
94 }
95 else if ( *s == '+' )
96 {
97 s ++;
98 }
99
100 //---- loop while number
101 while( '0' <= *s && *s <= '9' )
102 {
103 val = (val * 10) + (*s - '0');
104 //---- check overflow
105 if ( val > LONG_MAX )
106 {
107 return (isPlus)? LONG_MAX: LONG_MIN;
108 }
109 s++;
110 }
111 return (isPlus)? (long int) val: (long int) -val;
112 }
113