| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
#include "gdtoa.h" |
| 42 |
#include <stdio.h> |
| 43 |
#include <stdlib.h> |
| 44 |
#include <string.h> |
| 45 |
|
| 46 |
static int W0, W1; |
| 47 |
typedef union { |
| 48 |
double d; |
| 49 |
ULong L[2]; |
| 50 |
} U; |
| 51 |
|
| 52 |
#define UL (unsigned long) |
| 53 |
|
| 54 |
static int |
| 55 |
process(char *fname, FILE *f) |
| 56 |
{ |
| 57 |
U a, b; |
| 58 |
char buf[2048]; |
| 59 |
char *s, *s1, *se; |
| 60 |
int line, n; |
| 61 |
|
| 62 |
line = n = 0; |
| 63 |
|
| 64 |
top: |
| 65 |
while(fgets(s = buf, sizeof(buf), f)) { |
| 66 |
line++; |
| 67 |
while(*s <= ' ') |
| 68 |
if (!*s++) |
| 69 |
goto top; |
| 70 |
if (*s == '#') |
| 71 |
continue; |
| 72 |
while(*s > ' ') |
| 73 |
s++; |
| 74 |
|
| 75 |
if ((a.L[0] = (ULong)strtoul(s, &s1,16), s1 <= s) |
| 76 |
|| (a.L[1] = (ULong)strtoul(s1,&se,16), se <= s1)) { |
| 77 |
printf("Badly formatted line %d of %s\n", |
| 78 |
line, fname); |
| 79 |
n++; |
| 80 |
continue; |
| 81 |
} |
| 82 |
b.d = strtod(buf,0); |
| 83 |
if (b.L[W0] != a.L[0] || b.L[W1] != a.L[1]) { |
| 84 |
n++; |
| 85 |
printf("Line %d of %s: got %lx %lx; expected %lx %lx\n", |
| 86 |
line, fname, UL b.L[W0], UL b.L[W1], UL a.L[0], UL a.L[1]); |
| 87 |
} |
| 88 |
} |
| 89 |
return n; |
| 90 |
} |
| 91 |
|
| 92 |
int |
| 93 |
main(int argc, char **argv) |
| 94 |
{ |
| 95 |
FILE *f; |
| 96 |
char *prog, *s; |
| 97 |
int n, rc; |
| 98 |
U u; |
| 99 |
|
| 100 |
prog = argv[0]; |
| 101 |
if (argc == 2 && !strcmp(argv[1],"-?")) { |
| 102 |
fprintf(stderr, "Usage: %s [file [file...]]\n" |
| 103 |
"\tto read data file(s) of tab-separated triples d x y with\n" |
| 104 |
"\t\td decimal string\n" |
| 105 |
"\t\tx = high-order Hex value expected from strtod\n" |
| 106 |
"\t\ty = low-order Hex value\n" |
| 107 |
"\tComplain about errors by strtod.\n" |
| 108 |
"\tIf no files, read triples from stdin.\n", |
| 109 |
prog); |
| 110 |
return 0; |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
u.d = 1.; |
| 116 |
W0 = u.L[0] == 0; |
| 117 |
W1 = 1 - W0; |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
n = rc = 0; |
| 122 |
if (argc <= 1) |
| 123 |
n = process("<stdin>", stdin); |
| 124 |
else |
| 125 |
while((s = *++argv)) |
| 126 |
if ((f = fopen(s,"r"))) { |
| 127 |
n += process(s, f); |
| 128 |
fclose(f); |
| 129 |
} |
| 130 |
else { |
| 131 |
rc = 2; |
| 132 |
fprintf(stderr, "Cannot open %s\n", s); |
| 133 |
} |
| 134 |
printf("%d bad conversions\n", n); |
| 135 |
if (n) |
| 136 |
rc |= 1; |
| 137 |
return rc; |
| 138 |
} |