Browse Source

Extend strtoul() to cope with hex as well as decimal. Doesn't cope

with octal yet, but we can probably live without that.
tags/v0.9.3
Michael Brown 18 years ago
parent
commit
65ff5357f1
1 changed files with 25 additions and 9 deletions
  1. 25
    9
      src/core/misc.c

+ 25
- 9
src/core/misc.c View File

150
 	return 0;
150
 	return 0;
151
 }
151
 }
152
 
152
 
153
-unsigned long strtoul(const char *p, char **endp, int base)
154
-{
153
+unsigned long strtoul ( const char *p, char **endp, int base ) {
155
 	unsigned long ret = 0;
154
 	unsigned long ret = 0;
156
-	if (base != 10) return 0;
157
-	while((*p >= '0') && (*p <= '9')) {
158
-		ret = ret*10 + (*p - '0');
159
-		p++;
155
+	unsigned int charval;
156
+
157
+	if ( base == 0 ) {
158
+		if ( ( p[0] == '0' ) && ( ( p[1] | 0x20 ) == 'x' ) ) {
159
+			base = 16;
160
+			p += 2;
161
+		} else {
162
+			base = 10;
163
+		}
164
+	}
165
+
166
+	while ( 1 ) {
167
+		charval = *(p++) - '0';
168
+		if ( charval > ( 'A' - '0' - 10 ) )
169
+			charval -= ( 'A' - '0' - 10 );
170
+		if ( charval > ( 'a' - 'A' ) )
171
+			charval -= ( 'a' - 'A' );
172
+		if ( charval >= ( unsigned int ) base )
173
+			break;
174
+		ret = ( ( ret * base ) + charval );
160
 	}
175
 	}
161
-	if (endp)
176
+
177
+	if ( endp )
162
 		*endp = ( char * ) p;
178
 		*endp = ( char * ) p;
163
-	return(ret);
164
-	
179
+
180
+	return ( ret );
165
 }
181
 }
166
 
182
 
167
 /*
183
 /*

Loading…
Cancel
Save