-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlist.c
106 lines (89 loc) · 2.63 KB
/
list.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
/* $Id$ */
/*
* Copyright (C) 2016 Jef Driesen
* Copyright (C) 2016 Kristaps Dzonsons
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
#include "config.h"
#if HAVE_ERR
# include <err.h>
#endif
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "extern.h"
struct dcmd_list {
FILE *ostream; /* output file */
};
struct dcmd_out *
output_list_new(void)
{
struct dcmd_list *p;
if (NULL == (p = malloc(sizeof(struct dcmd_list))))
err(EXIT_FAILURE, NULL);
p->ostream = stdout;
return((struct dcmd_out *)p);
}
dc_status_t
output_list_write(struct dcmd_out *arg,
size_t num, dc_parser_t *parser, const char *fpr)
{
struct dcmd_list *output = (struct dcmd_list *)arg;
dc_status_t status = DC_STATUS_SUCCESS;
dc_datetime_t dt;
dc_divemode_t divemode = DC_DIVEMODE_OC;
const char *dm = NULL;
memset(&dt, 0, sizeof(dc_datetime_t));
/* Parse date and time of dive. */
status = dc_parser_get_datetime(parser, &dt);
if (status != DC_STATUS_SUCCESS &&
status != DC_STATUS_UNSUPPORTED) {
warnx("error parsing datetime");
return(DC_STATUS_SUCCESS);
}
/* Parse mode of dive. */
status = dc_parser_get_field
(parser, DC_FIELD_DIVEMODE, 0, &divemode);
if (status != DC_STATUS_SUCCESS &&
status != DC_STATUS_UNSUPPORTED) {
warnx("error parsing dive mode");
return(DC_STATUS_SUCCESS);
} else if (status != DC_STATUS_UNSUPPORTED) {
if (0 == divemode)
dm = "free-dive";
else if (1 == divemode)
dm = "gauge";
else if (2 == divemode)
dm = "open-circuit";
else if (3 == divemode)
dm = "closed-circuit";
}
fprintf(output->ostream,
"%4zu. %04i-%02i-%02i, %02i:%02i:%02i (%s): %s\n",
num, dt.year, dt.month, dt.day,
dt.hour, dt.minute, dt.second,
NULL != dm ? dm : "unknown", fpr);
return(DC_STATUS_SUCCESS);
}
dc_status_t
output_list_free(struct dcmd_out *arg)
{
struct dcmd_list *output = (struct dcmd_list *)arg;
if (NULL != output)
fclose(output->ostream);
return(DC_STATUS_SUCCESS);
}