scroll mode for very long start menus
[reactos.git] / 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 /*
22 * convert_to_ascii() - converts a number to it's ascii equivalent
23 * from:
24 * GRUB -- GRand Unified Bootloader
25 * Copyright (C) 1996 Erich Boleyn <erich@uruk.org>
26 */
27 char *convert_to_ascii(char *buf, int c, ...)
28 {
29 unsigned long num = *((&c) + 1), mult = 10;
30 char *ptr = buf;
31
32 if (c == 'x')
33 mult = 16;
34
35 if ((num & 0x80000000uL) && c == 'd')
36 {
37 num = (~num)+1;
38 *(ptr++) = '-';
39 buf++;
40 }
41
42 do
43 {
44 int dig = num % mult;
45 *(ptr++) = ( (dig > 9) ? dig + 'a' - 10 : '0' + dig );
46 }
47 while (num /= mult);
48
49 /* reorder to correct direction!! */
50 {
51 char *ptr1 = ptr-1;
52 char *ptr2 = buf;
53 while (ptr1 > ptr2)
54 {
55 int c = *ptr1;
56 *ptr1 = *ptr2;
57 *ptr2 = c;
58 ptr1--;
59 ptr2++;
60 }
61 }
62
63 return ptr;
64 }
65
66 char *convert_i64_to_ascii(char *buf, int c, ...)
67 {
68 unsigned long long num = *(long long*)((&c) + 1);
69 int mult = 10;
70 char *ptr = buf;
71
72 if (c == 'x')
73 mult = 16;
74
75 if ((num & 0x8000000000000000uLL) && c == 'd')
76 {
77 num = (~num)+1;
78 *(ptr++) = '-';
79 buf++;
80 }
81
82 do
83 {
84 int dig = num % mult;
85 *(ptr++) = ( (dig > 9) ? dig + 'a' - 10 : '0' + dig );
86 }
87 while (num /= mult);
88
89 /* reorder to correct direction!! */
90 {
91 char *ptr1 = ptr-1;
92 char *ptr2 = buf;
93 while (ptr1 > ptr2)
94 {
95 int c = *ptr1;
96 *ptr1 = *ptr2;
97 *ptr2 = c;
98 ptr1--;
99 ptr2++;
100 }
101 }
102
103 return ptr;
104 }
105
106 char *itoa(int value, char *string, int radix)
107 {
108 if(radix == 16)
109 *convert_to_ascii(string, 'x', value) = 0;
110 else
111 *convert_to_ascii(string, 'd', value) = 0;
112
113 return string;
114 }
115
116 int toupper(int c)
117 {
118 if((c >= 'a') && (c <= 'z'))
119 c -= 32;
120
121 return c;
122 }
123
124 int tolower(int c)
125 {
126 if((c >= 'A') && (c <= 'Z'))
127 c += 32;
128
129 return c;
130 }
131
132 int atoi(char *string)
133 {
134 int base;
135 int result = 0;
136 char *str;
137
138 if((string[0] == '0') && (string[1] == 'x'))
139 {
140 base = 16;
141 str = string + 2;
142 }
143 else
144 {
145 base = 10;
146 str = string;
147 }
148
149 while(1)
150 {
151 if(base == 16)
152 {
153 if(((*str < '0') || (*str > '9')) && ((*str < 'a') || (*str > 'f')) && ((*str < 'A') || (*str > 'F')))
154 break;
155
156 result *= base;
157 if((*str >= '0') && (*str <= '9'))
158 result += (*str - '0');
159 if((*str >= 'a') && (*str <= 'f'))
160 result += (*str - 'a') + 10;
161 if((*str >= 'A') && (*str <= 'F'))
162 result += (*str - 'A') + 10;
163 str++;
164 }
165 else //if(base == 10)
166 {
167 if((*str < '0') || (*str > '9'))
168 break;
169
170 result *= base;
171 result += (*str - '0');
172 str++;
173 }
174 }
175
176 return result;
177 }
178
179 int isspace(int c)
180 {
181 return(c == ' ' || (c >= 0x09 && c <= 0x0D));
182 }
183
184 int isdigit(int c)
185 {
186 return(c >= '0' && c <= '9');
187 }
188
189 int isxdigit(int c)
190 {
191 return((c >= '0' && c <= '9')||(c >= 'a' && c <= 'f')||(c >= 'A' && c <= 'F'));
192 }