source: git/src/diffpos.c @ 96ceb23

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 96ceb23 was 693388e, checked in by Olly Betts <olly@…>, 23 years ago

Factored hash_string into a separate source file.

Use a case sensitive hash function except for ini.c.

cavern: now pass prefix for leg to img library (img doesn't yet store
the prefix though).

extend: preserve the prefix on legs.

cavern: don't write equates or the fake legs inside fixsd-s into the
.3d file.

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

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