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