]> git.tdb.fi Git - ext/subsurface.git/blob - main.c
Start moving some of the non-parsing stuff out of 'parse.c'
[ext/subsurface.git] / main.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <time.h>
4
5 #include "dive.h"
6
7 static void show_dive(int nr, struct dive *dive)
8 {
9         int i;
10         struct tm *tm;
11
12         tm = gmtime(&dive->when);
13
14         printf("At %02d:%02d:%02d %04d-%02d-%02d  (%d ft max, %d minutes)\n",
15                 tm->tm_hour, tm->tm_min, tm->tm_sec,
16                 tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday,
17                 to_feet(dive->maxdepth), dive->duration.seconds / 60);
18
19         if (!verbose)
20                 return;
21
22         for (i = 0; i < dive->samples; i++) {
23                 struct sample *s = dive->sample + i;
24
25                 printf("%4d:%02d: %3d ft, %2d C, %4d PSI\n",
26                         s->time.seconds / 60,
27                         s->time.seconds % 60,
28                         to_feet(s->depth),
29                         to_C(s->temperature),
30                         to_PSI(s->tankpressure));
31         }
32 }
33
34 static int sortfn(const void *_a, const void *_b)
35 {
36         const struct dive *a = *(void **)_a;
37         const struct dive *b = *(void **)_b;
38
39         if (a->when < b->when)
40                 return -1;
41         if (a->when > b->when)
42                 return 1;
43         return 0;
44 }
45
46 static void report_dives(void)
47 {
48         int i;
49
50         qsort(dive_table.dives, dive_table.nr, sizeof(struct dive *), sortfn);
51         for (i = 0; i < dive_table.nr; i++)
52                 show_dive(i+1, dive_table.dives[i]);
53 }
54
55 static void parse_argument(const char *arg)
56 {
57         const char *p = arg+1;
58
59         do {
60                 switch (*p) {
61                 case 'v':
62                         verbose++;
63                         continue;
64                 default:
65                         fprintf(stderr, "Bad argument '%s'\n", arg);
66                         exit(1);
67                 }
68         } while (*++p);
69 }
70
71 int main(int argc, char **argv)
72 {
73         int i;
74
75         parse_xml_init();
76
77         for (i = 1; i < argc; i++) {
78                 const char *a = argv[i];
79
80                 if (a[0] == '-') {
81                         parse_argument(a);
82                         continue;
83                 }
84                 parse_xml_file(a);
85         }
86         report_dives();
87         return 0;
88 }
89