-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdcmdedit.c
505 lines (443 loc) · 10.9 KB
/
dcmdedit.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
/* $Id$ */
/*
* Copyright (c) 2017--2018 Kristaps Dzonsons <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "config.h"
#include <sys/queue.h>
#include <assert.h>
#include <ctype.h>
#if HAVE_ERR
# include <err.h>
#endif
#include <limits.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <expat.h>
#include "parser.h"
enum pmode {
PMODE_JOIN,
PMODE_SPLIT,
PMODE_NONE
};
int verbose = 0;
static FILE *
file_open(const struct dive *d, const char *out)
{
char path[PATH_MAX];
FILE *f;
snprintf(path, sizeof(path),
"%s/dive-%lld.xml", out, d->datetime);
if (NULL == (f = fopen(path, "w")))
err(EXIT_FAILURE, "%s", path);
return(f);
}
/*
* Put all dive samples into the same dive profile.
* We insert "top-side" dives during the surface interval between dive
* sets.
*/
static void
print_join(FILE *f, const struct dive *d, time_t *last, time_t first)
{
struct samp *s1, *s2, *s;
struct samp tmp;
time_t span;
/*
* First, compute how this dive computer records times (e.g.,
* once per second, etc.).
* We'll use this to create our fake entries.
*/
if (NULL == (s1 = TAILQ_FIRST(&d->samps)) ||
NULL == (s2 = TAILQ_NEXT(s1, entries)))
return;
assert(s1->time < s2->time);
span = s2->time - s1->time;
/*
* Print a fake sample for each spanned interval between the
* last dive (if specified) and the current one.
*/
if (*last && (*last += span) < d->datetime) {
memset(&tmp, 0, sizeof(struct samp));
tmp.flags |= SAMP_DEPTH;
do {
tmp.time = *last - first;
divecmd_print_dive_sample(f, &tmp);
*last += span;
} while (*last < d->datetime);
}
/* Now print the samples. */
TAILQ_FOREACH(s, &d->samps, entries) {
tmp = *s;
tmp.time = (s->time + d->datetime) - first;
divecmd_print_dive_sample(f, &tmp);
*last = s->time + d->datetime;
}
}
/*
* Given a single dive entry, split it into multiple dive entries when
* we have two consecutive dives <1 metre.
* We then ignore samples until the next dive is >1 metre.
*/
static void
print_split(const struct dive *d, const char *out, size_t *num)
{
time_t start;
double lastdepth = 100.0;
struct dive dive;
const struct samp *s;
struct samp tmp;
int rc, dopen = 0;
FILE *f = stdout;
assert(NULL != d->fprint);
start = d->datetime;
s = TAILQ_FIRST(&d->samps);
assert(NULL != s);
again:
for ( ; NULL != s; s = TAILQ_NEXT(s, entries)) {
if ( ! (SAMP_DEPTH & s->flags) &&
! (SAMP_TEMP & s->flags))
continue;
if (0 == dopen) {
if (NULL != out) {
f = file_open(d, out);
divecmd_print_open(f, d->log);
divecmd_print_diveq_open(f);
}
memset(&dive, 0, sizeof(struct dive));
dive.datetime = start;
dive.num = (*num)++;
dive.mode = d->mode;
/* Create a fake fingerprint. */
divecmd_print_dive_open(f, &dive);
rc = asprintf(&dive.fprint,
"%s-%.10zu", d->fprint, dive.num);
if (rc < 0)
err(EXIT_FAILURE, NULL);
divecmd_print_dive_fingerprint(f, &dive);
divecmd_print_dive_gasmixes(f, d);
free(dive.fprint);
divecmd_print_dive_sampleq_open(f);
dopen = 1;
}
/* Print sample data. */
tmp = *s;
tmp.time = (s->time + d->datetime) - start;
divecmd_print_dive_sample(f, &tmp);
/* Are we still "at depth"? */
if ( ! (SAMP_DEPTH & s->flags))
continue;
if (lastdepth >= 1.0 || s->depth >= 1.0) {
lastdepth = s->depth;
continue;
}
divecmd_print_dive_sampleq_close(f);
divecmd_print_dive_close(f);
if (NULL != out) {
divecmd_print_diveq_close(f);
divecmd_print_close(f);
fclose(f);
f = stdout;
}
dopen = 0;
/*
* We're below depth for two in a row.
* Continue reading til we're back at depth.
*/
for ( ; NULL != s; s = TAILQ_NEXT(s, entries)) {
if ( ! (SAMP_DEPTH & s->flags))
continue;
start = d->datetime + s->time;
if (s->depth >= 1.0)
break;
}
goto again;
}
if (dopen) {
divecmd_print_dive_sampleq_close(f);
divecmd_print_dive_close(f);
if (NULL != out) {
divecmd_print_diveq_close(f);
divecmd_print_close(f);
fclose(f);
f = stdout;
}
}
}
static uint32_t
jhash(const char *key)
{
size_t i = 0, length = strlen(key);
uint32_t hash = 0;
while (i != length) {
hash += key[i++];
hash += hash << 10;
hash ^= hash >> 6;
}
hash += hash << 3;
hash ^= hash >> 11;
hash += hash << 15;
return hash;
}
static int
stringeq(const char *p1, const char *p2)
{
if ((NULL == p1 && NULL != p2) ||
(NULL != p1 && NULL == p2))
return(0);
if (NULL == p1 && NULL == p2)
return(1);
return(0 == strcmp(p1, p2));
}
static int
dlogeq(const struct dlog *d1, const struct dlog *d2)
{
return(stringeq(d1->vendor, d2->vendor) &&
stringeq(d1->product, d2->product) &&
stringeq(d1->model, d2->model));
}
/*
* Take a single input file and either split or join it.
*/
static int
print_all(enum pmode pmode, const char *out, const struct diveq *dq)
{
const struct dive *d;
struct dive tmp;
const struct dlog *dl;
size_t idx, i, num = 1;
time_t last, first;
FILE *f = stdout;
const struct dive ***htab = NULL;
const size_t htabsz = 4096;
TAILQ_FOREACH(d, dq, entries)
if (0 == d->datetime) {
warnx("%s:%zu: no <dive> timestamp",
d->log->file, d->line);
return(0);
}
/*
* If we don't have a mode specified, simply print out each dive
* into its own file (if "out" is specified) or print them back
* to stdout as one big standalone file.
* They must be from the same divelog (i.e., dive computer),
* however, and the fingerprints must also be unique.
*/
switch (pmode) {
case PMODE_NONE:
htab = calloc(htabsz, sizeof(struct dive **));
if (NULL == htab)
err(EXIT_FAILURE, NULL);
if (NULL == out) {
divecmd_print_open(f, TAILQ_FIRST(dq)->log);
divecmd_print_diveq_open(f);
}
assert(NULL != TAILQ_FIRST(dq));
dl = TAILQ_FIRST(dq)->log;
TAILQ_FOREACH(d, dq, entries) {
if ( ! dlogeq(dl, d->log)) {
warnx("%s:%zu: dive has mismatched "
"computer (from %s:%zu)",
d->log->file, d->line,
dl->file, dl->line);
continue;
}
/* Look up in hashtable. */
if (NULL == d->fprint) {
warnx("%s:%zu: no <fingerprint>",
d->log->file, d->line);
continue;
}
idx = jhash(d->fprint) % htabsz;
if (NULL == htab[idx]) {
htab[idx] = calloc
(1, sizeof(struct dive *));
if (NULL == htab[idx])
err(EXIT_FAILURE, NULL);
}
for (i = 0; NULL != htab[idx][i]; i++)
if (0 == strcmp
(htab[idx][i]->fprint, d->fprint))
break;
if (NULL != htab[idx][i]) {
warnx("%s:%zu: duplicate dive from "
"%s:%zu", d->log->file,
d->line,
htab[idx][i]->log->file,
htab[idx][i]->line);
continue;
}
/* Add to hash line. */
htab[idx] = reallocarray
(htab[idx], i + 2, sizeof(struct dive *));
if (NULL == htab[idx])
err(EXIT_FAILURE, NULL);
htab[idx][i] = d;
htab[idx][i + 1] = NULL;
/* Print dive. */
if (NULL != out) {
f = file_open(d, out);
divecmd_print_open(f, d->log);
divecmd_print_diveq_open(f);
}
divecmd_print_dive(f, d);
if (NULL != out) {
divecmd_print_diveq_close(f);
divecmd_print_close(f);
fclose(f);
f = stdout;
}
}
if (NULL == out) {
divecmd_print_diveq_close(f);
divecmd_print_close(f);
}
for (i = 0; i < htabsz; i++)
free(htab[i]);
free(htab);
break;
case PMODE_SPLIT:
assert(NULL != TAILQ_FIRST(dq));
dl = TAILQ_FIRST(dq)->log;
if (NULL == out) {
divecmd_print_open(f, dl);
divecmd_print_diveq_open(f);
}
TAILQ_FOREACH(d, dq, entries) {
if (NULL == d->fprint)
warnx("%s:%zu: missing fingerprint",
d->log->file, d->line);
else if ( ! dlogeq(dl, d->log))
warnx("%s:%zu: dive has mismatched "
"computer (from %s:%zu)",
d->log->file, d->line,
dl->file, dl->line);
else
print_split(d, out, &num);
}
if (NULL == out) {
divecmd_print_diveq_close(f);
divecmd_print_close(f);
}
break;
case PMODE_JOIN:
d = TAILQ_FIRST(dq);
assert(NULL != d);
dl = d->log;
if (NULL != out)
f = file_open(d, out);
divecmd_print_open(f, dl);
divecmd_print_diveq_open(f);
divecmd_print_dive_open(f, d);
tmp = *d;
if (asprintf(&tmp.fprint, "%s-join", d->fprint) < 0)
err(EXIT_FAILURE, NULL);
divecmd_print_dive_fingerprint(f, &tmp);
free(tmp.fprint);
divecmd_print_dive_gasmixes(f, d);
divecmd_print_dive_sampleq_open(f);
first = d->datetime;
last = 0;
TAILQ_FOREACH(d, dq, entries)
if ( ! dlogeq(dl, d->log))
warnx("%s:%zu: dive has mismatched "
"computer (from %s:%zu)",
d->log->file, d->line,
dl->file, dl->line);
else
print_join(f, d, &last, first);
divecmd_print_dive_sampleq_close(f);
divecmd_print_dive_close(f);
divecmd_print_diveq_close(f);
divecmd_print_close(f);
if (NULL != out) {
fclose(f);
f = stdout;
}
break;
default:
abort();
}
return(1);
}
int
main(int argc, char *argv[])
{
int c, rc = 1;
size_t i;
enum pmode mode = PMODE_NONE;
XML_Parser p;
struct diveq dq;
struct divestat st;
const char *out = NULL;
#if HAVE_PLEDGE
if (-1 == pledge("stdio rpath wpath cpath", NULL))
err(EXIT_FAILURE, "pledge");
#endif
while (-1 != (c = getopt(argc, argv, "jo:sv")))
switch (c) {
case ('j'):
mode = PMODE_JOIN;
break;
case ('o'): /* XXX: undocumented */
out = optarg;
break;
case ('s'):
mode = PMODE_SPLIT;
break;
case ('v'):
verbose = 1;
break;
default:
goto usage;
}
argc -= optind;
argv += optind;
divecmd_init(&p, &dq, &st,
GROUP_NONE, GROUPSORT_DATETIME);
if (0 == argc)
rc = divecmd_parse("-", p, &dq, &st);
for (i = 0; i < (size_t)argc; i++)
if ( ! (rc = divecmd_parse(argv[i], p, &dq, &st)))
break;
#if HAVE_PLEDGE
if (NULL == out) {
if (-1 == pledge("stdio", NULL))
err(EXIT_FAILURE, "pledge");
} else {
if (-1 == pledge("stdio wpath cpath", NULL))
err(EXIT_FAILURE, "pledge");
}
#endif
XML_ParserFree(p);
if ( ! rc)
goto out;
if (TAILQ_EMPTY(&dq)) {
warnx("no dives to display");
goto out;
}
rc = print_all(mode, out, &dq);
out:
divecmd_free(&dq, &st);
return(rc ? EXIT_SUCCESS : EXIT_FAILURE);
usage:
fprintf(stderr, "usage: %s "
"[-jsv] "
"[file ...]\n", getprogname());
return(EXIT_FAILURE);
}