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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
|
/* info.c
*
* UI toolkit independent logic used for the info frame
*
* bool gps_changed(struct dive *dive, struct dive *master, const char *gps_text);
* void print_gps_coordinates(char *buffer, int len, int lat, int lon);
* void save_equipment_data(struct dive *dive);
* void update_equipment_data(struct dive *dive, struct dive *master);
* void update_time_depth(struct dive *dive, struct dive *edited);
* const char *get_window_title(struct dive *dive);
* char *evaluate_string_change(const char *newstring, char **textp, const char *master);
* int text_changed(const char *old, const char *new);
* bool parse_gps_text(const char *gps_text, double *latitude, double *longitude);
* int divename(char *buf, size_t size, struct dive *dive, char *trailer);
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>
#include <sys/time.h>
#include "gettext.h"
#include "dive.h"
#include "display.h"
#include "divelist.h"
#if USE_GTK_UI
/* old is NULL or a valid string, new is a valid string
* NOTE: NULL and "" need to be treated as "unchanged" */
int text_changed(const char *old, const char *new)
{
return (old && strcmp(old,new)) ||
(!old && strcmp("",new));
}
static const char *skip_space(const char *str)
{
if (str) {
while (g_ascii_isspace(*str))
str++;
if (!*str)
str = NULL;
}
return str;
}
/*
* should this string be changed?
* The "master" string is the string of the current dive - we only consider it
* changed if the old string is either empty, or matches that master string.
*/
char *evaluate_string_change(const char *newstring, char **textp, const char *master)
{
char *old = *textp;
const char *old_text;
old_text = skip_space(old);
master = skip_space(master);
/*
* If we had a master string, and it doesn't match our old
* string, we will always pick the old value (it means that
* we're editing another dive's info that already had a
* valid value).
*/
if (master && old_text)
if (strcmp(master, old_text))
return NULL;
while (g_ascii_isspace(*newstring))
newstring++;
/* If the master string didn't change, don't change other dives either! */
if (!text_changed(master, newstring))
return NULL;
if (!text_changed(old, newstring))
return NULL;
free(old);
*textp = strdup(newstring);
return *textp;
}
int divename(char *buf, size_t size, struct dive *dive, char *trailer)
{
struct tm tm;
utc_mkdate(dive->when, &tm);
/*++GETTEXT 80 char buffer: dive nr, weekday, month, day, year, hour, min <trailing text>*/
return snprintf(buf, size, tr("Dive #%1$d - %2$s %3$02d/%4$02d/%5$04d at %6$d:%7$02d %8$s"),
dive->number,
weekday(tm.tm_wday),
tm.tm_mon+1, tm.tm_mday,
tm.tm_year+1900,
tm.tm_hour, tm.tm_min,
trailer);
}
/* caller should free the string returned after it is no longer needed */
const char *get_window_title(struct dive *dive)
{
const char *text;
const int maxlen = 128;
char *basename;
char *title;
char *buffer = NULL;
int len1, len2, sz;
if (!dive) {
if (existing_filename) {
char *basename;
basename = g_path_get_basename(existing_filename);
len1 = sizeof("Subsurface: ");
len2 = g_utf8_strlen(basename, -1);
sz = (len1 + len2) * sizeof(gunichar);
title = malloc(sz);
strncpy(title, "Subsurface: ", len1);
g_utf8_strncpy(title + len1, basename, len2);
} else {
title = strdup("Subsurface");
}
} else {
/* dive number and location (or lacking that, the date) go in the window title */
text = dive->location;
if (!text)
text = "";
if (*text) {
if (dive->number) {
len1 = g_utf8_strlen(text, -1);
sz = (len1 + 32) * sizeof(gunichar);
buffer = malloc(sz);
snprintf(buffer, sz, tr("Dive #%d - "), dive->number);
g_utf8_strncpy(buffer + strlen(buffer), text, len1);
text = buffer;
}
} else {
sz = (maxlen + 32) * sizeof(gunichar);
buffer = malloc(sz);
divename(buffer, sz, dive, "");
text = buffer;
}
/* put it all together */
if (existing_filename) {
basename = g_path_get_basename(existing_filename);
len1 = g_utf8_strlen(basename, -1);
len2 = g_utf8_strlen(text, -1);
if (len2 > maxlen)
len2 = maxlen;
sz = (len1 + len2 + 3) * sizeof(gunichar); /* reserver space for ": " */
title = malloc(sz);
g_utf8_strncpy(title, basename, len1);
strncpy(title + strlen(basename), (const char *)": ", 2);
g_utf8_strncpy(title + strlen(basename) + 2, text, len2);
} else {
title = strdup(text);
}
if (buffer)
free(buffer);
}
return title;
}
/* this is used to skip the cardinal directions (or check if they are
* present). You pass in the text and a STRING with the direction.
* This checks for both the standard english text (just one character)
* and the translated text (possibly longer) and returns 0 if not found
* and the number of chars to skip otherwise. */
static int string_advance_cardinal(const char *text, const char *look)
{
char *trans;
int len = strlen(look);
if (!strncasecmp(text, look, len))
return len;
trans = tr(look);
len = strlen(trans);
if (!strncasecmp(text, trans, len))
return len;
return 0;
}
/* this has to be done with UTF8 as people might want to enter the degree symbol */
bool parse_gps_text(const char *gps_text, double *latitude, double *longitude)
{
const char *text = gps_text;
char *endptr;
bool south = FALSE;
bool west = FALSE;
double parselat, parselong;
gunichar degrees = UCS4_DEGREE;
gunichar c;
int incr;
while (g_unichar_isspace(g_utf8_get_char(text)))
text = g_utf8_next_char(text);
/* an empty string is interpreted as 0.0,0.0 and therefore "no gps location" */
if (!*text) {
*latitude = 0.0;
*longitude = 0.0;
return TRUE;
}
/* ok, let's parse by hand - first degrees of latitude */
text += string_advance_cardinal(text, "N");
if ((incr = string_advance_cardinal(text, "S")) > 0) {
text += incr;
south = TRUE;
}
parselat = g_ascii_strtod(text, &endptr);
if (text == endptr)
return FALSE;
text = endptr;
if (parselat < 0.0) {
south = TRUE;
parselat *= -1;
}
/* next optional minutes as decimal, skipping degree symbol */
while (g_unichar_isspace(c = g_utf8_get_char(text)) || c == degrees)
text = g_utf8_next_char(text);
incr = string_advance_cardinal(text, "E") + string_advance_cardinal(text, "W");
if (!incr && c != ';' && c != ',') {
parselat += g_ascii_strtod(text, &endptr) / 60.0;
if (text == endptr)
return FALSE;
text = endptr;
/* skip trailing minute symbol */
if (g_utf8_get_char(text) == '\'')
text = g_utf8_next_char(text);
}
/* skip seperator between latitude and longitude */
while (g_unichar_isspace(c = g_utf8_get_char(text)) || c == ';' || c == ',')
text = g_utf8_next_char(text);
/* next degrees of longitude */
text += string_advance_cardinal(text, "E");
if ((incr = string_advance_cardinal(text, "W")) > 0) {
text += incr;
west = TRUE;
}
parselong = g_ascii_strtod(text, &endptr);
if (text == endptr)
return FALSE;
text = endptr;
if (parselong < 0.0) {
west = TRUE;
parselong *= -1;
}
/* next optional minutes as decimal, skipping degree symbol */
while (g_unichar_isspace(c = g_utf8_get_char(text)) || c == degrees)
text = g_utf8_next_char(text);
if (*text) {
parselong += g_ascii_strtod(text, &endptr) / 60.0;
if (text == endptr)
return FALSE;
text = endptr;
/* skip trailing minute symbol */
if (g_utf8_get_char(text) == '\'')
text = g_utf8_next_char(text);
/* make sure there's nothing else left on the input */
while (g_unichar_isspace(g_utf8_get_char(text)))
text = g_utf8_next_char(text);
if (*text)
return FALSE;
}
if (west && parselong > 0.0)
parselong *= -1;
if (south && parselat > 0.0)
parselat *= -1;
*latitude = parselat;
*longitude = parselong;
return TRUE;
}
bool gps_changed(struct dive *dive, struct dive *master, const char *gps_text)
{
double latitude, longitude;
int latudeg, longudeg;
/* if we have a master and the dive's gps address is different from it,
* don't change the dive */
if (master && (master->latitude.udeg != dive->latitude.udeg ||
master->longitude.udeg != dive->longitude.udeg))
return FALSE;
if (!parse_gps_text(gps_text, &latitude, &longitude))
return FALSE;
latudeg = rint(1000000 * latitude);
longudeg = rint(1000000 * longitude);
/* if dive gps didn't change, nothing changed */
if (dive->latitude.udeg == latudeg && dive->longitude.udeg == longudeg)
return FALSE;
/* ok, update the dive and mark things changed */
dive->latitude.udeg = latudeg;
dive->longitude.udeg = longudeg;
return TRUE;
}
#endif
/* take latitude and longitude in udeg and print them in a human readable
* form, without losing precision */
void print_gps_coordinates(char *buffer, int len, int lat, int lon)
{
unsigned int latdeg, londeg;
#if 0
double latmin, lonmin;
#endif
const char *lath, *lonh;
char dbuf_lat[32], dbuf_lon[32];
if (!lat && !lon) {
*buffer = 0;
return;
}
lath = lat >= 0 ? tr("N") : tr("S");
lonh = lon >= 0 ? tr("E") : tr("W");
lat = abs(lat);
lon = abs(lon);
latdeg = lat / 1000000;
londeg = lon / 1000000;
#if 0
latmin = (lat % 1000000) * 60.0 / 1000000.0;
lonmin = (lon % 1000000) * 60.0 / 1000000.0;
*dbuf_lat = *dbuf_lon = 0;
g_ascii_formatd(dbuf_lat, sizeof(dbuf_lat), "%8.5f", latmin);
g_ascii_formatd(dbuf_lon, sizeof(dbuf_lon), "%8.5f", lonmin);
#else
int ilatmin = (lat % 1000000) * 60;
int ilonmin = (lon % 1000000) * 60;
snprintf(dbuf_lat, sizeof(dbuf_lat), "%2d.%05d", ilatmin / 1000000, (ilatmin % 1000000) / 10);
snprintf(dbuf_lon, sizeof(dbuf_lon), "%2d.%05d", ilonmin / 1000000, (ilonmin % 1000000) / 10);
#endif
if (!*dbuf_lat || !*dbuf_lon) {
*buffer = 0;
return;
}
snprintf(buffer, len, "%s%u%s %s\' , %s%u%s %s\'",
lath, latdeg, UTF8_DEGREE, dbuf_lat,
lonh, londeg, UTF8_DEGREE, dbuf_lon);
}
/* we use these to find out if we edited the cylinder or weightsystem entries */
static cylinder_t remember_cyl[MAX_CYLINDERS];
static weightsystem_t remember_ws[MAX_WEIGHTSYSTEMS];
#define CYL_BYTES sizeof(cylinder_t) * MAX_CYLINDERS
#define WS_BYTES sizeof(weightsystem_t) * MAX_WEIGHTSYSTEMS
void save_equipment_data(struct dive *dive)
{
if (dive) {
memcpy(remember_cyl, dive->cylinder, CYL_BYTES);
memcpy(remember_ws, dive->weightsystem, WS_BYTES);
}
}
/* Empty and NULL compare equal */
static int same_string(const char *a, const char *b)
{
/* Both NULL or same */
if (a == b)
return 1;
/* Both non-NULL: strcmp */
if (a && b)
return !strcmp(a, b);
/* One non-NULL? Is that one empty? */
return !*(a ? a : b);
}
static int same_type(cylinder_t *dst, cylinder_t *src)
{
return dst->type.size.mliter == src->type.size.mliter &&
dst->type.workingpressure.mbar == src->type.workingpressure.mbar &&
same_string(dst->type.description, src->type.description);
}
static void copy_type(cylinder_t *dst, cylinder_t *src)
{
dst->type.size = src->type.size;
dst->type.workingpressure = src->type.workingpressure;
if (dst->type.description)
free((void *)dst->type.description);
if (!src->type.description || !*src->type.description)
dst->type.description = NULL;
else
dst->type.description = strdup((char *)src->type.description);
}
static int same_gasmix(cylinder_t *dst, cylinder_t *src)
{
return !memcmp(&dst->gasmix, &src->gasmix, sizeof(dst->gasmix));
}
static void copy_gasmix(cylinder_t *dst, cylinder_t *src)
{
memcpy(&dst->gasmix, &src->gasmix, sizeof(dst->gasmix));
}
static int same_press(cylinder_t *dst, cylinder_t *src)
{
return dst->start.mbar == src->start.mbar &&
dst->end.mbar == src->end.mbar;
}
static void copy_press(cylinder_t *dst, cylinder_t *src)
{
dst->start = src->start;
dst->end = src->end;
}
/*
* When we update the cylinder information, we do it individually
* by type/gasmix/pressure, so that you can change them separately.
*
* The rule is: the destination has to be the same as the original
* field, and the source has to have changed. If so, we change the
* destination field.
*/
static void update_cylinder(cylinder_t *dst, cylinder_t *src, cylinder_t *orig)
{
/* Destination type same? Change it */
if (same_type(dst, orig) && !same_type(src, orig))
copy_type(dst, src);
/* Destination gasmix same? Change it */
if (same_gasmix(dst, orig) && !same_gasmix(src, orig))
copy_gasmix(dst, src);
/* Destination pressures the same? */
if (same_press(dst, orig) && !same_press(src, orig))
copy_press(dst, src);
}
/* the editing happens on the master dive; we copy the equipment
data if it has changed in the master dive and the other dive
either has no entries for the equipment or the same entries
as the master dive had before it was edited */
void update_equipment_data(struct dive *dive, struct dive *master)
{
int i;
if (dive == master)
return;
for (i = 0; i < MAX_CYLINDERS; i++)
update_cylinder(dive->cylinder+i, master->cylinder+i, remember_cyl+i);
if (! weightsystems_equal(remember_ws, master->weightsystem) &&
(no_weightsystems(dive->weightsystem) ||
weightsystems_equal(dive->weightsystem, remember_ws)))
memcpy(dive->weightsystem, master->weightsystem, WS_BYTES);
}
/* we can simply overwrite these - this only gets called if we edited
* a single dive and the dive was first copied into edited - so we can
* just take those values */
void update_time_depth(struct dive *dive, struct dive *edited)
{
dive->when = edited->when;
dive->dc.duration.seconds = edited->dc.duration.seconds;
dive->dc.maxdepth.mm = edited->dc.maxdepth.mm;
dive->dc.meandepth.mm = edited->dc.meandepth.mm;
}
void add_people(const char *string)
{
/* add names to the completion list for people */
}
void add_location(const char *string)
{
/* add names to the completion list for locations */
}
void add_suit(const char *string)
{
/* add names to the completion list for suits */
}
|