source: git/src/diffpos.c @ 11f9067

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 11f9067 was b65db17, checked in by Olly Betts <olly@…>, 23 years ago

More file reading checks.

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

  • Property mode set to 100644
File size: 8.2 KB
Line 
1/* > diffpos.c */
2/* Utility to compare two SURVEX .pos or .3d 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#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27#include <math.h>
28
29#include "cavern.h" /* just for REAL_EPSILON */
30#include "cmdline.h"
31#include "debug.h"
32#include "filelist.h"
33#include "hash.h"
34#include "img.h"
35#include "namecmp.h"
36
37/* macro to just convert argument to a string */
38#define STRING(X) _STRING(X)
39#define _STRING(X) #X
40
41/* very small value for comparing floating point numbers with */
42#define EPSILON (REAL_EPSILON * 1000)
43
44/* default threshold is 1cm */
45#define DFLT_MAX_THRESHOLD 0.01
46
47static double threshold = DFLT_MAX_THRESHOLD;
48
49static const struct option long_opts[] = {
50   /* const char *name; int has_arg (0 no_argument, 1 required_*, 2 optional_*); int *flag; int val; */
51   {"survey", required_argument, 0, 's'},
52   {"help", no_argument, 0, HLP_HELP},
53   {"version", no_argument, 0, HLP_VERSION},
54   {0, 0, 0, 0}
55};
56
57#define short_opts "s:"
58
59static struct help_msg help[] = {
60/*                              <-- */
61   {HLP_ENCODELONG(0),          "Only load the sub-survey with this prefix"},
62   {0, 0}
63};
64
65typedef struct station {
66   struct station *next;
67   char *name;
68   img_point pt;
69} station;
70
71typedef struct added {
72   struct added *next;
73   char *name;
74} added;
75
76static int
77cmp_pname(const void *a, const void *b)
78{
79   return name_cmp(*(const char **)a, *(const char **)b);
80}
81
82static station **htab;
83static bool fChanged = fFalse;
84
85static added *added_list = NULL;
86static OSSIZE_T c_added = 0;
87
88static void
89tree_init(void)
90{
91   size_t i;
92   htab = osmalloc(0x2000 * sizeof(int));
93   for (i = 0; i < 0x2000; i++) htab[i] = NULL;
94}
95
96static void
97tree_insert(const char *name, const img_point *pt)
98{
99   int v = hash_string(name) & 0x1fff;
100   station *stn;
101
102   /* Catch duplicate labels - .3d files shouldn't have them, but some do
103    * due to a couple of bugs in some versions of Survex
104    */
105   for (stn = htab[v]; stn; stn = stn->next) {
106      if (strcmp(stn->name, name) == 0) return; /* found dup */
107   }
108
109   stn = osnew(station);
110   stn->name = osstrdup(name);
111   stn->pt = *pt;
112   stn->next = htab[v];
113   htab[v] = stn;
114}
115
116static void
117tree_remove(const char *name, const img_point *pt)
118{
119   int v = hash_string(name) & 0x1fff;
120   station **prev;
121   station *p;
122   
123   for (prev = &htab[v]; *prev; prev = &((*prev)->next)) {
124      if (strcmp((*prev)->name, name) == 0) break;
125   }
126   
127   if (!*prev) {
128      added *add = osnew(added);
129      add->name = osstrdup(name);
130      add->next = added_list;
131      added_list = add;
132      c_added++;
133      fChanged = fTrue;
134      return;
135   }
136   
137   if (fabs(pt->x - (*prev)->pt.x) - threshold > EPSILON ||
138       fabs(pt->y - (*prev)->pt.y) - threshold > EPSILON ||
139       fabs(pt->z - (*prev)->pt.z) - threshold > EPSILON) {
140      printf("Moved by (%3.2f,%3.2f,%3.2f): %s\n",
141             pt->x - (*prev)->pt.x,
142             pt->y - (*prev)->pt.y,
143             pt->z - (*prev)->pt.z,
144             name);
145      fChanged = fTrue;
146   }
147   
148   osfree((*prev)->name);
149   p = *prev;
150   *prev = p->next;
151   osfree(p);
152}
153
154static int
155tree_check(void)
156{
157   size_t c = 0;
158   char **names;
159   size_t i;
160
161   if (c_added) {
162      names = osmalloc(c_added * ossizeof(char *));
163      for (i = 0; i < c_added; i++) {
164         added *old;
165         ASSERT(added_list);
166         names[i] = added_list->name;
167         old = added_list;
168         added_list = old->next;
169         osfree(old);
170      }
171      ASSERT(added_list == NULL);
172      qsort(names, c_added, sizeof(char *), cmp_pname);
173      for (i = 0; i < c_added; i++) {
174         printf("Added: %s\n", names[i]);
175         osfree(names[i]);
176      }
177      osfree(names);
178   }
179
180   for (i = 0; i < 0x2000; i++) {
181      station *p;
182      for (p = htab[i]; p; p = p->next) c++;
183   }
184   if (c == 0) return fChanged;
185
186   names = osmalloc(c * ossizeof(char *));
187   c = 0;
188   for (i = 0; i < 0x2000; i++) {
189      station *p;
190      for (p = htab[i]; p; p = p->next) names[c++] = p->name;
191   }
192   qsort(names, c, sizeof(char *), cmp_pname);
193   for (i = 0; i < c; i++) {
194      printf("Deleted: %s\n", names[i]);
195   }
196   return fTrue;
197}
198
199static void
200parse_3d_file(const char *fnm, const char *survey,
201              void (*tree_func)(const char *, const img_point *))
202{
203   img_point pt;
204   int result;
205 
206   img *pimg = img_open_survey(fnm, NULL, NULL, survey);
207   if (!pimg) fatalerror(img_error(), fnm);
208
209   do {
210      result = img_read_item(pimg, &pt);
211      switch (result) {
212       case img_MOVE:
213       case img_LINE:
214         break;
215       case img_LABEL:
216         tree_func(pimg->label, &pt);
217         break;
218       case img_BAD:
219         img_close(pimg);
220         fatalerror(img_error(), fnm);
221      }
222   } while (result != img_STOP);
223     
224   img_close(pimg);
225}
226
227static void
228parse_pos_file(const char *fnm, const char *survey,
229               void (*tree_func)(const char *, const img_point *))
230{
231   FILE *fh;
232   img_point pt;
233   char *buf;
234   size_t buf_len = 256;
235
236   char *pfx = NULL;
237   size_t pfx_len = 0;
238
239   if (survey) {
240      pfx_len = strlen(survey);
241      if (pfx_len) {
242         if (survey[pfx_len - 1] == '.') pfx_len--;
243         if (pfx_len) {
244            pfx = osmalloc(pfx_len + 2);
245            memcpy(pfx, survey, pfx_len);
246            pfx[pfx_len++] = '.';
247            pfx[pfx_len] = '\0';
248         }
249      }
250   }
251   
252   buf = osmalloc(buf_len);
253   
254   fh = fopen(fnm, "rb");
255   if (!fh) fatalerror(/*Couldn't open file `%s'*/93, fnm);
256
257   while (1) {
258      size_t off = 0;
259      long fp = ftell(fh);
260      if (fp == -1) break;
261      if (fscanf(fh, "(%lf,%lf,%lf ) ", &pt.x, &pt.y, &pt.z) != 3) {
262         int ch;
263         if (fseek(fh, fp, SEEK_SET) == -1) break;
264         ch = getc(fh);
265         if (ch == EOF) break;
266
267         printf("%s: Ignoring: ", fnm);
268         while (ch != '\n' && ch != '\r' && ch != EOF) {
269            putchar(ch);
270            ch = getc(fh);
271         }
272         putchar('\n');
273         
274         if (feof(fh)) break;
275         continue;
276      }
277
278      if (ferror(fh))
279         fatalerror_in_file(fnm, 0, /*Error reading file*/18);
280
281      buf[0] = '\0';
282      while (!feof(fh)) {
283         if (!fgets(buf + off, buf_len - off, fh))
284            fatalerror_in_file(fnm, 0, /*Error reading file*/18);
285
286         off += strlen(buf + off);
287         if (off && buf[off - 1] == '\n') {
288            buf[off - 1] = '\0';
289            break;
290         }
291         buf_len += buf_len;
292         buf = osrealloc(buf, buf_len);
293      }
294
295      if (!pfx_len || strncmp(buf, pfx, pfx_len) == 0) {
296         tree_func(buf + pfx_len, &pt);
297      }
298   }
299   fclose(fh);
300
301   osfree(buf);
302   osfree(pfx);
303}
304
305static void
306parse_file(const char *fnm, const char *survey,
307           void (*tree_func)(const char *, const img_point *))
308{
309   static const char ext3d[] = EXT_SVX_3D;
310   size_t len = strlen(fnm);
311   if (len > sizeof(ext3d) && fnm[len - sizeof(ext3d)] == FNM_SEP_EXT &&
312       strcmp(fnm + len - sizeof(ext3d) + 1, ext3d) == 0) {
313      parse_3d_file(fnm, survey, tree_func);
314   } else {
315      parse_pos_file(fnm, survey, tree_func);
316   }
317}
318
319int
320main(int argc, char **argv)
321{
322   char *fnm1, *fnm2;
323   const char *survey = NULL;   
324
325   msg_init(argv[0]);
326
327   cmdline_set_syntax_message("FILE1 FILE2 [THRESHOLD]",
328                              "FILE1 and FILE2 can be .pos or .3d files\n"
329                              "THRESHOLD is the max. ignorable change along "
330                              "any axis in metres (default "
331                              STRING(DFLT_MAX_THRESHOLD)")");
332   cmdline_init(argc, argv, short_opts, long_opts, NULL, help, 2, 3);
333   while (1) {
334      int opt = cmdline_getopt();
335      if (opt == EOF) break;
336      if (opt == 's') survey = optarg;
337   }
338   fnm1 = argv[optind++];
339   fnm2 = argv[optind++];
340   if (argv[optind]) {
341      optarg = argv[optind];
342      threshold = cmdline_double_arg();
343   }
344
345   tree_init();
346
347   parse_file(fnm1, survey, tree_insert);
348
349   parse_file(fnm2, survey, tree_remove);
350   
351   return tree_check() ? EXIT_FAILURE : EXIT_SUCCESS;
352}
Note: See TracBrowser for help on using the repository browser.