source: git/src/diffpos.c @ d69255f

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

Removed unused macros.

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

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