source: git/src/diffpos.c @ eef4d8c

RELEASE/1.0RELEASE/1.1RELEASE/1.2debug-cidebug-ci-sanitisersfaster-cavernloglog-selectstereowalls-datawalls-data-hanging-as-warningwarn-only-for-hanging-survey
Last change on this file since eef4d8c was 8b0fcbc, checked in by Olly Betts <olly@…>, 23 years ago

Fixed compiler warning.

git-svn-id: file:///home/survex-svn/survex/trunk@1033 4b37db11-9a0c-4f06-9ece-9ab7cdaee568

  • Property mode set to 100644
File size: 4.3 KB
Line 
1/* > diffpos.c */
2/* Utility to compare two SURVEX .pos files */
3/* Copyright (C) 1994,1996,1998,1999,2001 Olly Betts
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 */
19
20#ifdef HAVE_CONFIG_H
21# include <config.h>
22#endif
23
24/* size of line buffer */
25#define BUFLEN 256
26
27#include <stdio.h>
28#include <stdlib.h>
29#include <string.h>
30#include <math.h>
31
32#include "cavern.h" /* just for REAL_EPSILON */
33#include "cmdline.h"
34
35#ifndef sqrd
36# define sqrd(X) ((X)*(X))
37#endif
38
39/* macro to just convert argument to a string */
40#define STRING(X) _STRING(X)
41#define _STRING(X) #X
42
43/* very small value for comparing floating point numbers with */
44#define EPSILON (REAL_EPSILON * 1000)
45
46/* default threshold is 1cm */
47#define DFLT_MAX_THRESHOLD 0.01
48
49static int diff_pos(FILE *fh1, FILE *fh2, double threshold);
50static int read_line(FILE *fh, double *px, double *py, double *pz, char *id);
51
52static const struct option long_opts[] = {
53   /* const char *name; int has_arg (0 no_argument, 1 required_*, 2 optional_*); int *flag; int val; */
54   {"help", no_argument, 0, HLP_HELP},
55   {"version", no_argument, 0, HLP_VERSION},
56   {0, 0, 0, 0}
57};
58
59#define short_opts ""
60
61static struct help_msg help[] = {
62/*                              <-- */
63   {0, 0}
64};
65
66int
67main(int argc, char **argv)
68{
69   double threshold = DFLT_MAX_THRESHOLD;
70   char *fnm1, *fnm2;
71   FILE *fh1, *fh2;
72
73   msg_init(argv[0]);
74
75   cmdline_set_syntax_message("POS_FILE POS_FILE [THRESHOLD]",
76                              "THRESHOLD is the max. ignorable change along "
77                              "any axis in metres (default "
78                              STRING(DFLT_MAX_THRESHOLD)")");
79   cmdline_init(argc, argv, short_opts, long_opts, NULL, help, 2, 3);
80   while (cmdline_getopt() != EOF) {
81      /* do nothing */
82   }
83   fnm1 = argv[optind++];
84   fnm2 = argv[optind++];
85   if (argv[optind]) {
86      optarg = argv[optind];
87      threshold = cmdline_double_arg();
88   }
89   fh1 = fopen(fnm1, "rb");
90   if (!fh1) {
91      printf("Can't open file `%s'\n", fnm1);
92      exit(1);
93   }
94   fh2 = fopen(fnm2, "rb");
95   if (!fh2) {
96      printf("Can't open file `%s'\n", fnm2);
97      exit(1);
98   }
99   return diff_pos(fh1, fh2, threshold);
100}
101
102typedef enum { Eof, Haveline, Needline } state;
103
104int
105diff_pos(FILE *fh1, FILE *fh2, double threshold)
106{
107   state pos1 = Needline, pos2 = Needline;
108   int result = 0;
109
110   while (1) {
111      double x1, y1, z1, x2, y2, z2;
112      char id1[BUFLEN], id2[BUFLEN];
113
114      if (pos1 == Needline) {
115         pos1 = Haveline;
116         if (!read_line(fh1, &x1, &y1, &z1, id1)) pos1 = Eof;
117      }
118
119      if (pos2 == Needline) {
120         pos2 = Haveline;
121         if (!read_line(fh2, &x2, &y2, &z2, id2)) pos2 = Eof;
122      }
123
124      if (pos1 == Eof) {
125         if (pos2 == Eof) break;
126         result = 1;
127         printf("Added: %s (at end of file)\n", id2);
128         pos2 = Needline;
129      } else if (pos2 == Eof) {
130         result = 1;
131         printf("Deleted: %s (at end of file)\n", id1);
132         pos1 = Needline;
133      } else {
134         int cmp = strcmp(id1, id2);
135         if (cmp == 0) {
136            if (fabs(x1 - x2) - threshold > EPSILON ||
137                fabs(y1 - y2) - threshold > EPSILON ||
138                fabs(z1 - z2) - threshold > EPSILON) {
139               result = 1;
140               printf("Moved by (%3.2f,%3.2f,%3.2f): %s\n",
141                      x1 - x2, y1 - y2, z1 - z2, id1);
142            }
143            pos1 = pos2 = Needline;
144         } else {
145            result = 1;
146            if (cmp < 0) {
147               printf("Deleted: %s\n", id1);
148               pos1 = Needline;
149            } else {
150               printf("Added: %s\n", id2);
151               pos2 = Needline;
152            }
153         }
154      }
155   }
156   return result;
157}
158
159static int
160read_line(FILE *fh, double *px, double *py, double *pz, char *id)
161{
162   char buf[BUFLEN];
163   while (1) {
164      if (!fgets(buf, BUFLEN, fh)) return 0;
165      if (sscanf(buf, "(%lf,%lf,%lf )%s", px, py, pz, id) == 4) break;
166      printf("Ignoring line: %s\n", buf);
167   }
168   return 1;
169}
Note: See TracBrowser for help on using the repository browser.