Fix coding style
[survex.git] / src / diffpos.c
blob824778a82c145daac04aca0d59c2f64213c7b62a
1 /* diffpos.c */
2 /* Utility to compare two SURVEX .pos or .3d files */
3 /* Copyright (C) 1994,1996,1998-2003,2010,2011,2013,2014 Olly Betts
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.
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.
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <math.h>
29 #include "cmdline.h"
30 #include "debug.h"
31 #include "filelist.h"
32 #include "hash.h"
33 #include "img_hosted.h"
34 #include "namecmp.h"
35 #include "useful.h"
37 /* Don't complain if values mismatch by a tiny amount (1e-6m i.e. 0.001mm) */
38 #define TOLERANCE 1e-6
40 /* default threshold is 1cm */
41 #define DFLT_MAX_THRESHOLD 0.01
43 static double threshold = DFLT_MAX_THRESHOLD;
45 static const struct option long_opts[] = {
46 /* const char *name; int has_arg (0 no_argument, 1 required_*, 2 optional_*); int *flag; int val; */
47 {"survey", required_argument, 0, 's'},
48 {"help", no_argument, 0, HLP_HELP},
49 {"version", no_argument, 0, HLP_VERSION},
50 {0, 0, 0, 0}
53 #define short_opts "s:"
55 static struct help_msg help[] = {
56 /* <-- */
57 {HLP_ENCODELONG(0), /*only load the sub-survey with this prefix*/199, 0},
58 {0, 0, 0}
61 /* We use a hashtable with linked list buckets - this is how many hash table
62 * entries we have. 0x2000 with sizeof(void *) == 4 uses 32K. */
63 #define TREE_SIZE 0x2000
65 typedef struct station {
66 struct station *next;
67 char *name;
68 img_point pt;
69 } station;
71 typedef struct added {
72 struct added *next;
73 char *name;
74 } added;
76 static int old_separator, new_separator, sort_separator;
78 static int
79 cmp_pname(const void *a, const void *b)
81 return name_cmp(*(const char **)a, *(const char **)b, sort_separator);
84 static station **htab;
85 static bool fChanged = fFalse;
87 static added *added_list = NULL;
88 static OSSIZE_T c_added = 0;
90 static void
91 tree_init(void)
93 size_t i;
94 htab = osmalloc(TREE_SIZE * ossizeof(station *));
95 for (i = 0; i < TREE_SIZE; i++) htab[i] = NULL;
98 static void
99 tree_insert(const char *name, const img_point *pt)
101 int v = hash_string(name) & (TREE_SIZE - 1);
102 station * stn = osnew(station);
103 stn->name = osstrdup(name);
104 stn->pt = *pt;
105 stn->next = htab[v];
106 htab[v] = stn;
109 static int
110 close_enough(const img_point * p1, const img_point * p2)
112 return fabs(p1->x - p2->x) - threshold <= TOLERANCE &&
113 fabs(p1->y - p2->y) - threshold <= TOLERANCE &&
114 fabs(p1->z - p2->z) - threshold <= TOLERANCE;
117 static void
118 tree_remove(const char *name, const img_point *pt)
120 /* We need to handle duplicate labels - normal .3d files shouldn't have them
121 * (though some older ones do due to a couple of bugs in earlier versions of
122 * Survex) but extended .3d files repeat the label where a loop is broken,
123 * and data read from foreign formats might repeat labels.
125 int v = hash_string(name) & (TREE_SIZE - 1);
126 station **prev;
127 station *p;
128 station **found = NULL;
129 bool was_close_enough = fFalse;
131 for (prev = &htab[v]; *prev; prev = &((*prev)->next)) {
132 if (strcmp((*prev)->name, name) == 0) {
133 /* Handle stations with the same name. Stations are inserted at the
134 * start of the linked list, so pick the *last* matching station in
135 * the list as then we match the first stations with the same name in
136 * each file.
138 if (close_enough(pt, &((*prev)->pt))) {
139 found = prev;
140 was_close_enough = fTrue;
141 } else if (!was_close_enough) {
142 found = prev;
147 if (!found) {
148 added *add = osnew(added);
149 add->name = osstrdup(name);
150 add->next = added_list;
151 added_list = add;
152 c_added++;
153 fChanged = fTrue;
154 return;
157 if (!was_close_enough) {
158 /* TRANSLATORS: for diffpos: */
159 printf(msg(/*Moved by (%3.2f,%3.2f,%3.2f): %s*/500),
160 pt->x - (*found)->pt.x,
161 pt->y - (*found)->pt.y,
162 pt->z - (*found)->pt.z,
163 name);
164 putnl();
165 fChanged = fTrue;
168 osfree((*found)->name);
169 p = *found;
170 *found = p->next;
171 osfree(p);
174 static int
175 tree_check(void)
177 size_t c = 0;
178 char **names;
179 size_t i;
181 if (c_added) {
182 names = osmalloc(c_added * ossizeof(char *));
183 for (i = 0; i < c_added; i++) {
184 added *old;
185 SVX_ASSERT(added_list);
186 names[i] = added_list->name;
187 old = added_list;
188 added_list = old->next;
189 osfree(old);
191 SVX_ASSERT(added_list == NULL);
192 sort_separator = new_separator;
193 qsort(names, c_added, sizeof(char *), cmp_pname);
194 for (i = 0; i < c_added; i++) {
195 /* TRANSLATORS: for diffpos: */
196 printf(msg(/*Added: %s*/501), names[i]);
197 putnl();
198 osfree(names[i]);
200 osfree(names);
203 for (i = 0; i < TREE_SIZE; i++) {
204 station *p;
205 for (p = htab[i]; p; p = p->next) c++;
207 if (c == 0) return fChanged;
209 names = osmalloc(c * ossizeof(char *));
210 c = 0;
211 for (i = 0; i < TREE_SIZE; i++) {
212 station *p;
213 for (p = htab[i]; p; p = p->next) names[c++] = p->name;
215 sort_separator = old_separator;
216 qsort(names, c, sizeof(char *), cmp_pname);
217 for (i = 0; i < c; i++) {
218 /* TRANSLATORS: for diffpos: */
219 printf(msg(/*Deleted: %s*/502), names[i]);
220 putnl();
222 return fTrue;
225 static int
226 parse_file(const char *fnm, const char *survey,
227 void (*tree_func)(const char *, const img_point *))
229 img_point pt;
230 int result;
231 int separator;
233 img *pimg = img_open_survey(fnm, survey);
234 if (!pimg) fatalerror(img_error2msg(img_error()), fnm);
235 separator = pimg->separator;
237 do {
238 result = img_read_item(pimg, &pt);
239 switch (result) {
240 case img_MOVE:
241 case img_LINE:
242 break;
243 case img_LABEL:
244 tree_func(pimg->label, &pt);
245 break;
246 case img_BAD:
247 img_close(pimg);
248 fatalerror(img_error2msg(img_error()), fnm);
250 } while (result != img_STOP);
252 img_close(pimg);
253 return separator;
257 main(int argc, char **argv)
259 char *fnm1, *fnm2;
260 const char *survey = NULL;
262 msg_init(argv);
264 /* TRANSLATORS: Part of diffpos --help */
265 cmdline_set_syntax_message(/*FILE1 FILE2 [THRESHOLD]*/218,
266 /* TRANSLATORS: Part of diffpos --help */
267 /*FILE1 and FILE2 can be .pos or .3d files\nTHRESHOLD is the max. ignorable change along any axis in metres (default %s)*/255,
268 STRING(DFLT_MAX_THRESHOLD));
269 cmdline_init(argc, argv, short_opts, long_opts, NULL, help, 2, 3);
270 while (1) {
271 int opt = cmdline_getopt();
272 if (opt == EOF) break;
273 if (opt == 's') survey = optarg;
275 fnm1 = argv[optind++];
276 fnm2 = argv[optind++];
277 if (argv[optind]) {
278 optarg = argv[optind];
279 threshold = cmdline_double_arg();
282 tree_init();
284 old_separator = parse_file(fnm1, survey, tree_insert);
286 new_separator = parse_file(fnm2, survey, tree_remove);
288 return tree_check() ? EXIT_FAILURE : EXIT_SUCCESS;