10 struct units output_units;
12 /* random helper functions, used here or elsewhere */
13 static int sortfn(const void *_a, const void *_b)
15 const struct dive *a = *(void **)_a;
16 const struct dive *b = *(void **)_b;
18 if (a->when < b->when)
20 if (a->when > b->when)
25 const char *weekday(int wday)
27 static const char wday_array[7][4] = {
28 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
30 return wday_array[wday];
33 const char *monthname(int mon)
35 static const char month_array[12][4] = {
36 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
37 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
39 return month_array[mon];
43 * When adding dives to the dive table, we try to renumber
44 * the new dives based on any old dives in the dive table.
46 * But we only do it if:
48 * - the last dive in the old dive table was numbered
50 * - all the new dives are strictly at the end (so the
51 * "last dive" is at the same location in the dive table
52 * after re-sorting the dives.
54 * - none of the new dives have any numbers
56 * This catches the common case of importing new dives from
57 * a dive computer, and gives them proper numbers based on
58 * your old dive list. But it tries to be very conservative
59 * and not give numbers if there is *any* question about
60 * what the numbers should be - in which case you need to do
61 * a manual re-numbering.
63 static void try_to_renumber(struct dive *last, int preexisting)
68 * If the new dives aren't all strictly at the end,
69 * we're going to expect the user to do a manual
72 if (get_dive(preexisting-1) != last)
76 * If any of the new dives already had a number,
77 * we'll have to do a manual renumbering.
79 for (i = preexisting; i < dive_table.nr; i++) {
80 struct dive *dive = get_dive(i);
89 for (i = preexisting; i < dive_table.nr; i++) {
90 struct dive *dive = get_dive(i);
96 * track whether we switched to importing dives
98 static gboolean imported = FALSE;
101 * This doesn't really report anything at all. We just sort the
102 * dives, the GUI does the reporting
104 void report_dives(gboolean is_imported)
107 int preexisting = dive_table.preexisting;
110 /* This does the right thing for -1: NULL */
111 last = get_dive(preexisting-1);
113 qsort(dive_table.dives, dive_table.nr, sizeof(struct dive *), sortfn);
115 for (i = 1; i < dive_table.nr; i++) {
116 struct dive **pp = &dive_table.dives[i-1];
117 struct dive *prev = pp[0];
118 struct dive *dive = pp[1];
121 if (prev->when + prev->duration.seconds < dive->when)
124 merged = try_to_merge(prev, dive);
132 memmove(pp+1, pp+2, sizeof(*pp)*(dive_table.nr - i));
134 /* Redo the new 'i'th dive */
139 /* Was the previous dive table state numbered? */
140 if (last && last->number)
141 try_to_renumber(last, preexisting);
143 /* did we have dives in the table and added more? */
144 if (last && preexisting != dive_table.nr)
145 mark_divelist_changed(TRUE);
147 dive_table.preexisting = dive_table.nr;
148 dive_list_update_dives();
151 static void parse_argument(const char *arg)
153 const char *p = arg+1;
161 /* long options with -- */
162 if (strcmp(arg,"--import") == 0) {
163 /* mark the dives so far as the base,
164 * everything after is imported */
171 /* ignore process serial number argument when run as native macosx app */
172 if (strncmp(arg, "-psn_", 5) == 0) {
177 fprintf(stderr, "Bad argument '%s'\n", arg);
183 void update_dive(struct dive *new_dive)
185 static struct dive *buffered_dive;
186 struct dive *old_dive = buffered_dive;
189 flush_divelist(old_dive);
192 show_dive_info(new_dive);
193 show_dive_equipment(new_dive, W_IDX_PRIMARY);
194 show_dive_stats(new_dive);
196 buffered_dive = new_dive;
199 void renumber_dives(int nr)
203 for (i = 0; i < dive_table.nr; i++) {
204 struct dive *dive = dive_table.dives[i];
205 dive->number = nr + i;
206 flush_divelist(dive);
208 mark_divelist_changed(TRUE);
211 int main(int argc, char **argv)
215 output_units = SI_units;
219 init_ui(&argc, &argv);
221 for (i = 1; i < argc; i++) {
222 const char *a = argv[i];
228 GError *error = NULL;
229 parse_file(a, &error);
239 report_dives(imported);