-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdivecmd2csv.c
108 lines (92 loc) · 2.36 KB
/
divecmd2csv.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
/* $Id$ */
/*
* Copyright (c) 2017 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>
#if HAVE_ERR
# include <err.h>
#endif
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <expat.h>
#include "parser.h"
int verbose = 0;
static void
print_all(const struct diveq *dq)
{
const struct dive *d;
const struct samp *s;
TAILQ_FOREACH(d, dq, entries)
TAILQ_FOREACH(s, &d->samps, entries) {
printf("%zu,%zu,", d->num, s->time);
if (SAMP_DEPTH & s->flags)
printf("%g", s->depth);
fputc(',', stdout);
if (SAMP_TEMP & s->flags)
printf("%g", s->temp);
fputc('\n', stdout);
}
}
int
main(int argc, char *argv[])
{
int c, rc = 1;
size_t i;
XML_Parser p;
struct diveq dq;
struct divestat st;
#if HAVE_PLEDGE
if (-1 == pledge("stdio rpath", NULL))
err(EXIT_FAILURE, "pledge");
#endif
while (-1 != (c = getopt(argc, argv, "v")))
switch (c) {
case ('v'):
verbose = 1;
break;
default:
goto usage;
}
argc -= optind;
argv += optind;
divecmd_init(&p, &dq, &st, GROUP_DIVER, 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 (-1 == pledge("stdio", NULL))
err(EXIT_FAILURE, "pledge");
#endif
XML_ParserFree(p);
if ( ! rc)
goto out;
if (TAILQ_EMPTY(&dq)) {
warnx("no dives to display");
goto out;
}
print_all(&dq);
out:
divecmd_free(&dq, &st);
return(rc ? EXIT_SUCCESS : EXIT_FAILURE);
usage:
fprintf(stderr, "usage: %s [-v] [file]\n", getprogname());
return(EXIT_FAILURE);
}