- Move ReactOS specific code to ReactOS specific files/directories.
[reactos.git] / reactos / boot / freeldr / freeldr / rtl / stdlib.c
1 /*
2 * FreeLoader
3 * Copyright (C) 1998-2003 Brian Palmer <brianp@sginet.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 /*
21 * convert_to_ascii() - converts a number to it's ascii equivalent
22 * from:
23 * GRUB -- GRand Unified Bootloader
24 * Copyright (C) 1996 Erich Boleyn <erich@uruk.org>
25 */
26 char *convert_to_ascii(char *buf, int c, int num)
27 {
28 unsigned long mult = 10;
29 char *ptr = buf;
30
31 if (c == 'x')
32 mult = 16;
33
34 if ((num & 0x80000000uL) && c == 'd')
35 {
36 num = (~num)+1;
37 *(ptr++) = '-';
38 buf++;
39 }
40
41 do
42 {
43 int dig = num % mult;
44 *(ptr++) = ( (dig > 9) ? dig + 'a' - 10 : '0' + dig );
45 }
46 while (num /= mult);
47
48 /* reorder to correct direction!! */
49 {
50 char *ptr1 = ptr-1;
51 char *ptr2 = buf;
52 while (ptr1 > ptr2)
53 {
54 int c = *ptr1;
55 *ptr1 = *ptr2;
56 *ptr2 = c;
57 ptr1--;
58 ptr2++;
59 }
60 }
61
62 return ptr;
63 }
64
65 char *convert_i64_to_ascii(char *buf, int c, unsigned long long num)
66 {
67 int mult = 10;
68 char *ptr = buf;
69
70 if (c == 'x')
71 mult = 16;
72
73 if ((num & 0x8000000000000000uLL) && c == 'd')
74 {
75 num = (~num)+1;
76 *(ptr++) = '-';
77 buf++;
78 }
79
80 do
81 {
82 int dig = num % mult;
83 *(ptr++) = ( (dig > 9) ? dig + 'a' - 10 : '0' + dig );
84 }
85 while (num /= mult);
86
87 /* reorder to correct direction!! */
88 {
89 char *ptr1 = ptr-1;
90 char *ptr2 = buf;
91 while (ptr1 > ptr2)
92 {
93 int c = *ptr1;
94 *ptr1 = *ptr2;
95 *ptr2 = c;
96 ptr1--;
97 ptr2++;
98 }
99 }
100
101 return ptr;
102 }
103
104 char *itoa(int value, char *string, int radix)
105 {
106 if(radix == 16)
107 *convert_to_ascii(string, 'x', value) = 0;
108 else
109 *convert_to_ascii(string, 'd', value) = 0;
110
111 return string;
112 }
113
114 int toupper(int c)
115 {
116 if((c >= 'a') && (c <= 'z'))
117 c -= 32;
118
119 return c;
120 }
121
122 int tolower(int c)
123 {
124 if((c >= 'A') && (c <= 'Z'))
125 c += 32;
126
127 return c;
128 }
129
130 int atoi(const char *string)
131 {
132 int base;
133 int result = 0;
134 const char *str;
135
136 if((string[0] == '0') && (string[1] == 'x'))
137 {
138 base = 16;
139 str = string + 2;
140 }
141 else
142 {
143 base = 10;
144 str = string;
145 }
146
147 while(1)
148 {
149 if(base == 16)
150 {
151 if(((*str < '0') || (*str > '9')) && ((*str < 'a') || (*str > 'f')) && ((*str < 'A') || (*str > 'F')))
152 break;
153
154 result *= base;
155 if((*str >= '0') && (*str <= '9'))
156 result += (*str - '0');
157 if((*str >= 'a') && (*str <= 'f'))
158 result += (*str - 'a') + 10;
159 if((*str >= 'A') && (*str <= 'F'))
160 result += (*str - 'A') + 10;
161 str++;
162 }
163 else //if(base == 10)
164 {
165 if((*str < '0') || (*str > '9'))
166 break;
167
168 result *= base;
169 result += (*str - '0');
170 str++;
171 }
172 }
173
174 return result;
175 }
176
177 int isspace(int c)
178 {
179 return(c == ' ' || (c >= 0x09 && c <= 0x0D));
180 }
181
182 int isdigit(int c)
183 {
184 return(c >= '0' && c <= '9');
185 }
186
187 int isxdigit(int c)
188 {
189 return((c >= '0' && c <= '9')||(c >= 'a' && c <= 'f')||(c >= 'A' && c <= 'F'));
190 }