e5401ffc2005a1a52809f2eedf704c7b035283b1
[reactos.git] / freeldr / freeldr / rtl / stdlib.c
1 /*
2 * FreeLoader
3 * Copyright (C) 1999, 2000, 2001 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 *itoa(int value, char *string, int radix)
67 {
68 if(radix == 16)
69 *convert_to_ascii(string, 'x', value) = 0;
70 else
71 *convert_to_ascii(string, 'd', value) = 0;
72
73 return string;
74 }
75
76 int toupper(int c)
77 {
78 if((c >= 'a') && (c <= 'z'))
79 c -= 32;
80
81 return c;
82 }
83
84 int tolower(int c)
85 {
86 if((c >= 'A') && (c <= 'Z'))
87 c += 32;
88
89 return c;
90 }
91
92 int atoi(char *string)
93 {
94 int base;
95 int result = 0;
96 char *str;
97
98 if((string[0] == '0') && (string[1] == 'x'))
99 {
100 base = 16;
101 str = string + 2;
102 }
103 else
104 {
105 base = 10;
106 str = string;
107 }
108
109 while(1)
110 {
111 if((*str < '0') || (*str > '9'))
112 break;
113
114 result *= base;
115 result += (*str - '0');
116 str++;
117 }
118
119 return result;
120 }
121
122 int isspace(int c)
123 {
124 return(c == ' ' || (c >= 0x09 && c <= 0x0D));
125 }
126
127 int isdigit(int c)
128 {
129 return(c >= '0' && c <= '9');
130 }
131
132 int isxdigit(int c)
133 {
134 return((c >= '0' && c <= '9')||(c >= 'a' && c <= 'f')||(c >= 'A' && c <= 'F'));
135 }