]> git.tdb.fi Git - ext/subsurface.git/blob - main.c
Move 'dive_list_update_dives()' call into 'report_dives()'
[ext/subsurface.git] / main.c
1 /* main.c */
2 #include <stdio.h>
3 #include <string.h>
4 #include <stdlib.h>
5 #include <time.h>
6
7 #include <gconf/gconf-client.h>
8
9 #include "dive.h"
10 #include "divelist.h"
11
12 GConfClient *gconf;
13 struct units output_units;
14
15 #define GCONF_NAME(x) "/apps/subsurface/" #x
16
17 /* random helper functions, used here or elsewhere */
18 static int sortfn(const void *_a, const void *_b)
19 {
20         const struct dive *a = *(void **)_a;
21         const struct dive *b = *(void **)_b;
22
23         if (a->when < b->when)
24                 return -1;
25         if (a->when > b->when)
26                 return 1;
27         return 0;
28 }
29
30 const char *weekday(int wday)
31 {
32         static const char wday_array[7][4] = {
33                 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
34         };
35         return wday_array[wday];
36 }
37
38 const char *monthname(int mon)
39 {
40         static const char month_array[12][4] = {
41                 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
42                 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
43         };
44         return month_array[mon];
45 }
46
47 /*
48  * This doesn't really report anything at all. We just sort the
49  * dives, the GUI does the reporting
50  */
51 void report_dives(void)
52 {
53         int i;
54
55         qsort(dive_table.dives, dive_table.nr, sizeof(struct dive *), sortfn);
56
57         for (i = 1; i < dive_table.nr; i++) {
58                 struct dive **pp = &dive_table.dives[i-1];
59                 struct dive *prev = pp[0];
60                 struct dive *dive = pp[1];
61                 struct dive *merged;
62
63                 if (prev->when + prev->duration.seconds < dive->when)
64                         continue;
65
66                 merged = try_to_merge(prev, dive);
67                 if (!merged)
68                         continue;
69
70                 free(prev);
71                 free(dive);
72                 *pp = merged;
73                 dive_table.nr--;
74                 memmove(pp+1, pp+2, sizeof(*pp)*(dive_table.nr - i));
75
76                 /* Redo the new 'i'th dive */
77                 i--;
78         }
79
80         dive_list_update_dives();
81 }
82
83 static void parse_argument(const char *arg)
84 {
85         const char *p = arg+1;
86
87         do {
88                 switch (*p) {
89                 case 'v':
90                         verbose++;
91                         continue;
92                 default:
93                         fprintf(stderr, "Bad argument '%s'\n", arg);
94                         exit(1);
95                 }
96         } while (*++p);
97 }
98
99 void update_dive(struct dive *new_dive)
100 {
101         static struct dive *buffered_dive;
102         struct dive *old_dive = buffered_dive;
103
104         if (old_dive) {
105                 flush_dive_info_changes(old_dive);
106                 flush_dive_equipment_changes(old_dive);
107                 flush_divelist(old_dive);
108         }
109         if (new_dive) {
110                 show_dive_info(new_dive);
111                 show_dive_equipment(new_dive);
112         }
113         buffered_dive = new_dive;
114 }
115
116 void renumber_dives(int nr)
117 {
118         int i;
119
120         for (i = 0; i < dive_table.nr; i++) {
121                 struct dive *dive = dive_table.dives[i];
122                 dive->number = nr + i;
123         }
124 }
125
126 int main(int argc, char **argv)
127 {
128         int i;
129
130         output_units = SI_units;
131
132         parse_xml_init();
133
134         init_ui(argc, argv);
135         
136         for (i = 1; i < argc; i++) {
137                 const char *a = argv[i];
138
139                 if (a[0] == '-') {
140                         parse_argument(a);
141                         continue;
142                 }
143                 GError *error = NULL;
144                 parse_xml_file(a, &error);
145                 
146                 if (error != NULL)
147                 {
148                         report_error(error);
149                         g_error_free(error);
150                         error = NULL;
151                 }
152         }
153
154         report_dives();
155
156         run_ui();
157         return 0;
158 }