소스 검색

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 년 전
부모
커밋
65ff5357f1
1개의 변경된 파일25개의 추가작업 그리고 9개의 파일을 삭제
  1. 25
    9
      src/core/misc.c

+ 25
- 9
src/core/misc.c 파일 보기

@@ -150,18 +150,34 @@ int inet_aton ( const char *cp, struct in_addr *inp ) {
150 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 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 178
 		*endp = ( char * ) p;
163
-	return(ret);
164
-	
179
+
180
+	return ( ret );
165 181
 }
166 182
 
167 183
 /*

Loading…
취소
저장