KD System Rewrite:
[reactos.git] / reactos / ntoskrnl / kdbg / kdb_string.c
1 /*
2 * ReactOS kernel
3 * Copyright (C) 2005 ReactOS Team
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 /* $Id$
20 *
21 * PROJECT: ReactOS kernel
22 * FILE: ntoskrnl/dbg/kdb_string.c
23 * PURPOSE: Kernel debugger string functions
24 * PROGRAMMER: Gregor Anich (blight@blight.eu.org)
25 * UPDATE HISTORY:
26 * Created 17/01/2005
27 */
28
29 /* INCLUDES ******************************************************************/
30 #include <ntoskrnl.h>
31 #include <ctype.h>
32
33 /* FUNCTIONS *****************************************************************/
34
35 #if 0
36 int
37 _stricmp(
38 const char *s1,
39 const char *s2)
40 {
41 char c1, c2;
42 for (;;)
43 {
44 c1 = tolower(*s1++);
45 c2 = tolower(*s2++);
46 if (c1 < c2)
47 return -1;
48 else if (c1 > c2)
49 return 1;
50 if (c1 == '\0')
51 break;
52 }
53 return 0;
54 }
55 #endif /* unused */
56
57 /*
58 * Convert a string to an unsigned long integer.
59 *
60 * Ignores `locale' stuff. Assumes that the upper and lower case
61 * alphabets and digits are each contiguous.
62 */
63 unsigned long
64 strtoul(const char *nptr, char **endptr, int base)
65 {
66 const char *s = nptr;
67 unsigned long acc;
68 int c;
69 unsigned long cutoff;
70 int neg = 0, any, cutlim;
71
72 /*
73 * See strtol for comments as to the logic used.
74 */
75 do {
76 c = *s++;
77 } while (isspace(c));
78 if (c == '-')
79 {
80 neg = 1;
81 c = *s++;
82 }
83 else if (c == '+')
84 c = *s++;
85 if ((base == 0 || base == 16) &&
86 c == '0' && (*s == 'x' || *s == 'X'))
87 {
88 c = s[1];
89 s += 2;
90 base = 16;
91 }
92 if (base == 0)
93 base = c == '0' ? 8 : 10;
94 cutoff = (unsigned long)ULONG_MAX / (unsigned long)base;
95 cutlim = (unsigned long)ULONG_MAX % (unsigned long)base;
96 for (acc = 0, any = 0;; c = *s++)
97 {
98 if (isdigit(c))
99 c -= '0';
100 else if (isalpha(c))
101 c -= isupper(c) ? 'A' - 10 : 'a' - 10;
102 else
103 break;
104 if (c >= base)
105 break;
106 if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
107 any = -1;
108 else {
109 any = 1;
110 acc *= base;
111 acc += c;
112 }
113 }
114 if (any < 0)
115 {
116 acc = ULONG_MAX;
117 }
118 else if (neg)
119 acc = -acc;
120 if (endptr != 0)
121 *endptr = any ? (char *)s - 1 : (char *)nptr;
122 return acc;
123 }
124