1 | /* > diffpos.c */ |
---|
2 | /* (Originally quick and dirty) program to compare two SURVEX .pos files */ |
---|
3 | /* Copyright (C) 1994,1996,1998 Olly Betts */ |
---|
4 | |
---|
5 | #ifdef HAVE_CONFIG_H |
---|
6 | # include <config.h> |
---|
7 | #endif |
---|
8 | |
---|
9 | /* size of line buffer */ |
---|
10 | #define BUFLEN 256 |
---|
11 | |
---|
12 | #include <stdio.h> |
---|
13 | #include <stdlib.h> |
---|
14 | #include <string.h> |
---|
15 | #include <math.h> |
---|
16 | |
---|
17 | #define sqrd(X) ((X)*(X)) |
---|
18 | |
---|
19 | /* macro to just convert argument to a string */ |
---|
20 | #define STRING(X) _STRING(X) |
---|
21 | #define _STRING(X) #X |
---|
22 | |
---|
23 | /* very small value for comparing floating point numbers with */ |
---|
24 | /* (ought to use a real epsilon value) */ |
---|
25 | #define EPSILON 0.00001 |
---|
26 | |
---|
27 | /* default threshold is 1cm */ |
---|
28 | #define DFLT_MAX_THRESHOLD 0.01 |
---|
29 | |
---|
30 | static void diff_pos(FILE *fh1, FILE *fh2, double threshold); |
---|
31 | static int read_line(FILE *fh, double *px, double *py, double *pz, char *id); |
---|
32 | |
---|
33 | int |
---|
34 | main(int argc, char *argv[]) |
---|
35 | { |
---|
36 | double threshold = DFLT_MAX_THRESHOLD; |
---|
37 | char *fnm1, *fnm2; |
---|
38 | FILE *fh1, *fh2; |
---|
39 | if (argc != 3) { |
---|
40 | char *p; |
---|
41 | if (argc == 4) threshold = strtod(argv[3], &p); |
---|
42 | if (argc != 4 || *p) { |
---|
43 | /* FIXME put these in the messages file */ |
---|
44 | /* complain if not 4 args, or threshold didn't parse cleanly */ |
---|
45 | printf("Syntax: %s <pos file> <pos file> [<threshold>]\n", argv[0]); |
---|
46 | printf(" where <threshold> is the max. permitted change along " |
---|
47 | "any axis in metres\n" |
---|
48 | " (default <threshold> is "STRING(DFLT_MAX_THRESHOLD)"m)\n"); |
---|
49 | exit(1); |
---|
50 | } |
---|
51 | } |
---|
52 | fnm1 = argv[1]; |
---|
53 | fnm2 = argv[2]; |
---|
54 | fh1 = fopen(fnm1, "rb"); |
---|
55 | if (!fh1) { |
---|
56 | printf("Can't open file '%s'\n", fnm1); |
---|
57 | exit(1); |
---|
58 | } |
---|
59 | fh2 = fopen(fnm2, "rb"); |
---|
60 | if (!fh2) { |
---|
61 | printf("Can't open file '%s'\n", fnm2); |
---|
62 | exit(1); |
---|
63 | } |
---|
64 | diff_pos(fh1, fh2, threshold); |
---|
65 | return 0; |
---|
66 | } |
---|
67 | |
---|
68 | static void |
---|
69 | diff_pos(FILE *fh1, FILE *fh2, double threshold) |
---|
70 | { |
---|
71 | double x1, y1, z1, x2, y2, z2; |
---|
72 | int cmp; |
---|
73 | char id1[BUFLEN], id2[BUFLEN]; |
---|
74 | int fRead1 = 1, fRead2 = 1; |
---|
75 | while (!feof(fh1) && !feof(fh2)) { |
---|
76 | if (fRead1) { |
---|
77 | if (!read_line(fh1, &x1, &y1, &z1, id1)) continue; |
---|
78 | fRead1 = 0; |
---|
79 | } |
---|
80 | if (fRead2) { |
---|
81 | if (!read_line(fh2, &x2, &y2, &z2, id2)) continue; |
---|
82 | fRead2 = 0; |
---|
83 | } |
---|
84 | cmp = strcmp(id1, id2); |
---|
85 | if (cmp == 0) { |
---|
86 | if (fabs(x1 - x2) - threshold > EPSILON || |
---|
87 | fabs(y1 - y2) - threshold > EPSILON || |
---|
88 | fabs(z1 - z2) - threshold > EPSILON) { |
---|
89 | printf("Moved by (%3.2f,%3.2f,%3.2f): %s\n", |
---|
90 | x1 - x2, y1 - y2, z1 - z2, id1); |
---|
91 | } |
---|
92 | fRead1 = fRead2 = 1; |
---|
93 | } else { |
---|
94 | if (cmp < 0) { |
---|
95 | printf("Deleted: %s\n", id1); |
---|
96 | fRead1 = 1; |
---|
97 | } else { |
---|
98 | printf("Added: %s\n", id2); |
---|
99 | fRead2 = 1; |
---|
100 | } |
---|
101 | } |
---|
102 | } |
---|
103 | while (!feof(fh1) && read_line(fh1, &x1, &y1, &z1, id1)) |
---|
104 | printf("Deleted: %s (at end of file)\n", id1); |
---|
105 | while (!feof(fh2) && read_line(fh2, &x2, &y2, &z2, id2)) |
---|
106 | printf("Added: %s (at end of file)\n", id2); |
---|
107 | } |
---|
108 | |
---|
109 | static int |
---|
110 | read_line(FILE *fh, double *px, double *py, double *pz, char *id) |
---|
111 | { |
---|
112 | char buf[BUFLEN]; |
---|
113 | if (!fgets(buf, BUFLEN, fh)) return 0; |
---|
114 | if (sscanf(buf, "(%lf,%lf,%lf )%s", px, py, pz, id) < 4) { |
---|
115 | printf("Ignoring line: %s\n", buf); |
---|
116 | return 0; |
---|
117 | } |
---|
118 | return 1; |
---|
119 | } |
---|