|
Revision 207, 1.5 kB
(checked in by root, 5 years ago)
|
Initial import.
|
| Line | |
|---|
| 1 |
/* +-------------------------------------------------------------------+ */ |
|---|
| 2 |
/* | Copyright 1988,1991, David Koblas. | */ |
|---|
| 3 |
/* | Permission to use, copy, modify, and distribute this software | */ |
|---|
| 4 |
/* | and its documentation for any purpose and without fee is hereby | */ |
|---|
| 5 |
/* | granted, provided that the above copyright notice appear in all | */ |
|---|
| 6 |
/* | copies and that both that copyright notice and this permission | */ |
|---|
| 7 |
/* | notice appear in supporting documentation. This software is | */ |
|---|
| 8 |
/* | provided "as is" without express or implied warranty. | */ |
|---|
| 9 |
/* +-------------------------------------------------------------------+ */ |
|---|
| 10 |
|
|---|
| 11 |
#include <string.h> |
|---|
| 12 |
#include <ctype.h> |
|---|
| 13 |
|
|---|
| 14 |
#ifdef TEST |
|---|
| 15 |
main(argc,argv) |
|---|
| 16 |
int argc; |
|---|
| 17 |
char **argv; |
|---|
| 18 |
{ |
|---|
| 19 |
int i; |
|---|
| 20 |
for (i=1;i<argc;i++) |
|---|
| 21 |
printf("%10s == %d\n",argv[i],atov(argv[i],0)); |
|---|
| 22 |
} |
|---|
| 23 |
#endif |
|---|
| 24 |
|
|---|
| 25 |
int atov(str,type) |
|---|
| 26 |
char *str; |
|---|
| 27 |
int type; |
|---|
| 28 |
{ |
|---|
| 29 |
int sign = 1; |
|---|
| 30 |
int i; |
|---|
| 31 |
char c; |
|---|
| 32 |
int val=0,n; |
|---|
| 33 |
|
|---|
| 34 |
i=0; |
|---|
| 35 |
while ((str[i]==' ') || (str[i]=='\t')) i++; |
|---|
| 36 |
if (str[i]=='-') { |
|---|
| 37 |
sign = -1; |
|---|
| 38 |
i++; |
|---|
| 39 |
} else if (str[i]=='+') { |
|---|
| 40 |
sign = 1; |
|---|
| 41 |
i++; |
|---|
| 42 |
} |
|---|
| 43 |
if (type==0) { |
|---|
| 44 |
if (str[i]=='0') { |
|---|
| 45 |
i++; |
|---|
| 46 |
if (str[i]=='%') { |
|---|
| 47 |
i++; |
|---|
| 48 |
type=2; |
|---|
| 49 |
} else if (str[i]=='x') { |
|---|
| 50 |
i++; |
|---|
| 51 |
type=16; |
|---|
| 52 |
} else { |
|---|
| 53 |
type=8; |
|---|
| 54 |
} |
|---|
| 55 |
} else { |
|---|
| 56 |
type=10; |
|---|
| 57 |
} |
|---|
| 58 |
} |
|---|
| 59 |
for (;i<strlen(str);i++) { |
|---|
| 60 |
c=str[i]; |
|---|
| 61 |
if (isdigit(c)) { |
|---|
| 62 |
n = c - '0'; |
|---|
| 63 |
} else if (isupper(c)) { |
|---|
| 64 |
n = c - 'A' + 10; |
|---|
| 65 |
} else if (islower(c)) { |
|---|
| 66 |
n = c - 'a' + 10; |
|---|
| 67 |
} else { |
|---|
| 68 |
goto out; |
|---|
| 69 |
} |
|---|
| 70 |
if (n>=type) |
|---|
| 71 |
goto out; |
|---|
| 72 |
val = (val*type)+n; |
|---|
| 73 |
} |
|---|
| 74 |
out: |
|---|
| 75 |
return(val * sign); |
|---|
| 76 |
} |
|---|