2017-04-27 18:24:53 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2018-05-22 07:07:42 +00:00
|
|
|
#include "ssrf.h"
|
2014-03-07 03:27:28 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <git2.h>
|
2016-01-30 10:57:19 +00:00
|
|
|
#include <libdivecomputer/parser.h>
|
2014-03-07 03:27:28 +00:00
|
|
|
|
2015-02-15 02:00:35 +00:00
|
|
|
#include "gettext.h"
|
|
|
|
|
2020-05-01 11:43:52 +00:00
|
|
|
#include "dive.h"
|
2019-03-04 22:20:29 +00:00
|
|
|
#include "divesite.h"
|
2019-08-05 17:41:15 +00:00
|
|
|
#include "errorhelper.h"
|
2019-05-31 14:09:14 +00:00
|
|
|
#include "trip.h"
|
2018-05-11 15:25:41 +00:00
|
|
|
#include "subsurface-string.h"
|
2014-03-07 03:27:28 +00:00
|
|
|
#include "device.h"
|
|
|
|
#include "membuffer.h"
|
2015-06-13 15:01:06 +00:00
|
|
|
#include "git-access.h"
|
2020-04-10 07:42:14 +00:00
|
|
|
#include "picture.h"
|
2018-02-24 22:28:13 +00:00
|
|
|
#include "qthelper.h"
|
2019-05-30 16:29:36 +00:00
|
|
|
#include "tag.h"
|
2014-03-07 03:27:28 +00:00
|
|
|
|
2014-03-13 22:42:45 +00:00
|
|
|
const char *saved_git_id = NULL;
|
|
|
|
|
2019-07-24 21:54:46 +00:00
|
|
|
struct git_parser_state {
|
|
|
|
git_repository *repo;
|
|
|
|
struct divecomputer *active_dc;
|
|
|
|
struct dive *active_dive;
|
|
|
|
dive_trip_t *active_trip;
|
|
|
|
struct picture *active_pic;
|
|
|
|
struct dive_site *active_site;
|
2020-03-17 21:49:29 +00:00
|
|
|
struct dive_table *table;
|
|
|
|
struct trip_table *trips;
|
|
|
|
struct dive_site_table *sites;
|
2019-07-24 21:54:46 +00:00
|
|
|
int o2pressure_sensor;
|
|
|
|
};
|
|
|
|
|
2014-03-09 19:19:41 +00:00
|
|
|
struct keyword_action {
|
|
|
|
const char *keyword;
|
2019-07-24 21:54:46 +00:00
|
|
|
void (*fn)(char *, struct membuffer *, struct git_parser_state *);
|
2014-03-09 19:19:41 +00:00
|
|
|
};
|
|
|
|
#define ARRAY_SIZE(array) (sizeof(array)/sizeof(array[0]))
|
|
|
|
|
2019-07-24 21:17:26 +00:00
|
|
|
static git_blob *git_tree_entry_blob(git_repository *repo, const git_tree_entry *entry);
|
2015-06-21 14:43:35 +00:00
|
|
|
|
2014-03-09 21:26:39 +00:00
|
|
|
static temperature_t get_temperature(const char *line)
|
2014-03-09 19:19:41 +00:00
|
|
|
{
|
|
|
|
temperature_t t;
|
|
|
|
t.mkelvin = C_to_mkelvin(ascii_strtod(line, NULL));
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
2014-03-09 21:26:39 +00:00
|
|
|
static depth_t get_depth(const char *line)
|
2014-03-09 19:19:41 +00:00
|
|
|
{
|
|
|
|
depth_t d;
|
2019-07-24 22:26:51 +00:00
|
|
|
d.mm = lrint(1000 * ascii_strtod(line, NULL));
|
2014-03-09 19:19:41 +00:00
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
2014-03-09 21:45:20 +00:00
|
|
|
static volume_t get_volume(const char *line)
|
|
|
|
{
|
|
|
|
volume_t v;
|
2019-07-24 22:26:51 +00:00
|
|
|
v.mliter = lrint(1000 * ascii_strtod(line, NULL));
|
2014-03-09 21:45:20 +00:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
static weight_t get_weight(const char *line)
|
|
|
|
{
|
|
|
|
weight_t w;
|
2019-07-24 22:26:51 +00:00
|
|
|
w.grams = lrint(1000 * ascii_strtod(line, NULL));
|
2014-03-09 21:45:20 +00:00
|
|
|
return w;
|
|
|
|
}
|
|
|
|
|
2019-04-30 10:42:33 +00:00
|
|
|
static pressure_t get_airpressure(const char *line)
|
|
|
|
{
|
|
|
|
pressure_t p;
|
|
|
|
p.mbar = lrint(ascii_strtod(line, NULL));
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2014-03-09 21:26:39 +00:00
|
|
|
static pressure_t get_pressure(const char *line)
|
2014-03-09 19:19:41 +00:00
|
|
|
{
|
|
|
|
pressure_t p;
|
2019-07-24 22:26:51 +00:00
|
|
|
p.mbar = lrint(1000 * ascii_strtod(line, NULL));
|
2014-03-09 19:19:41 +00:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2014-03-10 03:55:29 +00:00
|
|
|
static int get_salinity(const char *line)
|
|
|
|
{
|
2019-07-24 22:26:51 +00:00
|
|
|
return lrint(10 * ascii_strtod(line, NULL));
|
2014-03-10 03:55:29 +00:00
|
|
|
}
|
|
|
|
|
2014-03-09 21:45:20 +00:00
|
|
|
static fraction_t get_fraction(const char *line)
|
|
|
|
{
|
|
|
|
fraction_t f;
|
2019-07-24 21:54:46 +00:00
|
|
|
f.permille = lrint(10 * ascii_strtod(line, NULL));
|
2014-03-09 21:45:20 +00:00
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
2014-03-09 21:26:39 +00:00
|
|
|
static void update_date(timestamp_t *when, const char *line)
|
2014-03-09 19:19:41 +00:00
|
|
|
{
|
|
|
|
unsigned yyyy, mm, dd;
|
|
|
|
struct tm tm;
|
|
|
|
|
|
|
|
if (sscanf(line, "%04u-%02u-%02u", &yyyy, &mm, &dd) != 3)
|
|
|
|
return;
|
|
|
|
utc_mkdate(*when, &tm);
|
2016-04-28 22:13:30 +00:00
|
|
|
tm.tm_year = yyyy;
|
2014-03-09 19:19:41 +00:00
|
|
|
tm.tm_mon = mm - 1;
|
|
|
|
tm.tm_mday = dd;
|
|
|
|
*when = utc_mktime(&tm);
|
|
|
|
}
|
|
|
|
|
2014-03-09 21:26:39 +00:00
|
|
|
static void update_time(timestamp_t *when, const char *line)
|
2014-03-09 19:19:41 +00:00
|
|
|
{
|
|
|
|
unsigned h, m, s = 0;
|
|
|
|
struct tm tm;
|
|
|
|
|
|
|
|
if (sscanf(line, "%02u:%02u:%02u", &h, &m, &s) < 2)
|
|
|
|
return;
|
|
|
|
utc_mkdate(*when, &tm);
|
|
|
|
tm.tm_hour = h;
|
|
|
|
tm.tm_min = m;
|
|
|
|
tm.tm_sec = s;
|
|
|
|
*when = utc_mktime(&tm);
|
|
|
|
}
|
|
|
|
|
2014-03-09 21:26:39 +00:00
|
|
|
static duration_t get_duration(const char *line)
|
2014-03-09 19:19:41 +00:00
|
|
|
{
|
|
|
|
int m = 0, s = 0;
|
|
|
|
duration_t d;
|
|
|
|
sscanf(line, "%d:%d", &m, &s);
|
2019-07-24 22:26:51 +00:00
|
|
|
d.seconds = m * 60 + s;
|
2014-03-09 19:19:41 +00:00
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
2018-05-08 14:24:51 +00:00
|
|
|
static enum divemode_t get_dctype(const char *line)
|
2014-11-16 23:11:18 +00:00
|
|
|
{
|
2018-05-08 14:24:51 +00:00
|
|
|
for (enum divemode_t i = 0; i < NUM_DIVEMODE; i++) {
|
2015-01-10 23:01:15 +00:00
|
|
|
if (strcmp(line, divemode_text[i]) == 0)
|
2014-11-16 23:11:18 +00:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-03-09 21:26:39 +00:00
|
|
|
static int get_index(const char *line)
|
2014-03-09 19:19:41 +00:00
|
|
|
{ return atoi(line); }
|
2014-11-22 13:13:13 +00:00
|
|
|
|
2014-03-09 21:26:39 +00:00
|
|
|
static int get_hex(const char *line)
|
2014-03-09 19:19:41 +00:00
|
|
|
{ return strtoul(line, NULL, 16); }
|
|
|
|
|
2019-07-24 21:54:46 +00:00
|
|
|
static void parse_dive_gps(char *line, struct membuffer *str, struct git_parser_state *state)
|
2015-02-12 20:28:54 +00:00
|
|
|
{
|
2018-05-22 07:07:42 +00:00
|
|
|
UNUSED(str);
|
2018-10-20 18:12:15 +00:00
|
|
|
location_t location;
|
2019-07-24 21:54:46 +00:00
|
|
|
struct dive_site *ds = get_dive_site_for_dive(state->active_dive);
|
2018-10-20 18:12:15 +00:00
|
|
|
|
|
|
|
parse_location(line, &location);
|
2015-02-13 09:14:33 +00:00
|
|
|
if (!ds) {
|
2020-03-17 21:49:29 +00:00
|
|
|
ds = get_dive_site_by_gps(&location, state->sites);
|
2018-10-23 10:42:01 +00:00
|
|
|
if (!ds)
|
2019-03-05 21:58:47 +00:00
|
|
|
ds = create_dive_site_with_gps("", &location, &dive_site_table);
|
2019-07-24 21:54:46 +00:00
|
|
|
add_dive_to_dive_site(state->active_dive, ds);
|
2015-02-13 09:14:33 +00:00
|
|
|
} else {
|
2018-10-20 18:12:15 +00:00
|
|
|
if (dive_site_has_gps_location(ds) && !same_location(&ds->location, &location)) {
|
2019-03-25 08:05:47 +00:00
|
|
|
char *coords = printGPSCoordsC(&location);
|
2015-02-15 02:00:35 +00:00
|
|
|
// we have a dive site that already has GPS coordinates
|
2015-09-18 14:23:10 +00:00
|
|
|
ds->notes = add_to_string(ds->notes, translate("gettextFromC", "multiple GPS locations for this dive site; also %s\n"), coords);
|
2019-03-24 20:50:01 +00:00
|
|
|
free(coords);
|
2015-02-15 02:00:35 +00:00
|
|
|
}
|
2018-10-20 18:12:15 +00:00
|
|
|
ds->location = location;
|
2015-02-13 09:14:33 +00:00
|
|
|
}
|
|
|
|
|
2015-02-12 20:28:54 +00:00
|
|
|
}
|
|
|
|
|
2019-07-24 21:54:46 +00:00
|
|
|
static void parse_dive_location(char *line, struct membuffer *str, struct git_parser_state *state)
|
2015-02-12 20:28:54 +00:00
|
|
|
{
|
2018-05-22 07:07:42 +00:00
|
|
|
UNUSED(line);
|
load-git: clean up string handling during parsing
We had some fairly obscure rules for how strings were parsed, and it
actually caused bugs when the same line had multiple strings in it.
That normally doesn't happen, and the cases where it was _supposed_ to
happen had special cases for it (divecomputer ID lines, and tag lines).
But by mistake, we had introduced a case of that for the event line
handling in commit b9174332d ("Read and write divemode changes (xml and
git)"), and nobody realized that the divemode string addition meant that
"oops, now it's corrupting the event name". An event line could look
like this:
event 40:00 type=8 divemode="OC" name="modechange"
where we now had both that "OC" and "modechange" strings, and the code
to pick the name just picked the first string. So we'd end up
effectively mis-parsing the above line as
event 40:00 type=8 divemode="OC" name="OC"
which is obviously wrong.
The dive mode didn't really need to be a string in the first place
(there is nothing to quote, and no spaces in it), but hey, here we are.
We can't just magially fix the existing broken saves.
So make it more straightforward to handle strings in the git format line
parser. We still stash the different decoded strings together in one
special memory buffer, but now the parser helpers automatically untangle
it as they traverse the key value pairs.
This is still overly subtle code, and it doesn't fix the cases where
we've saved the wrong data back. That comes later.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2020-02-14 19:49:28 +00:00
|
|
|
char *name = detach_cstring(str);
|
2019-07-24 21:54:46 +00:00
|
|
|
struct dive_site *ds = get_dive_site_for_dive(state->active_dive);
|
2015-02-13 09:14:33 +00:00
|
|
|
if (!ds) {
|
2019-02-26 21:26:11 +00:00
|
|
|
ds = get_dive_site_by_name(name, &dive_site_table);
|
2018-10-23 10:42:01 +00:00
|
|
|
if (!ds)
|
2019-03-05 21:58:47 +00:00
|
|
|
ds = create_dive_site(name, &dive_site_table);
|
2019-07-24 21:54:46 +00:00
|
|
|
add_dive_to_dive_site(state->active_dive, ds);
|
2015-02-13 09:14:33 +00:00
|
|
|
} else {
|
2015-02-15 02:00:35 +00:00
|
|
|
// we already had a dive site linked to the dive
|
2018-01-07 10:12:48 +00:00
|
|
|
if (empty_string(ds->name)) {
|
2015-07-07 22:48:27 +00:00
|
|
|
ds->name = strdup(name);
|
2015-02-15 02:00:35 +00:00
|
|
|
} else {
|
|
|
|
// and that dive site had a name. that's weird - if our name is different, add it to the notes
|
|
|
|
if (!same_string(ds->name, name))
|
|
|
|
ds->notes = add_to_string(ds->notes, translate("gettextFromC", "additional name for site: %s\n"), name);
|
|
|
|
}
|
2015-02-13 09:14:33 +00:00
|
|
|
}
|
2015-06-22 03:24:07 +00:00
|
|
|
free(name);
|
2015-02-12 20:28:54 +00:00
|
|
|
}
|
2014-03-09 19:19:41 +00:00
|
|
|
|
2019-07-24 21:54:46 +00:00
|
|
|
static void parse_dive_divemaster(char *line, struct membuffer *str, struct git_parser_state *state)
|
load-git: clean up string handling during parsing
We had some fairly obscure rules for how strings were parsed, and it
actually caused bugs when the same line had multiple strings in it.
That normally doesn't happen, and the cases where it was _supposed_ to
happen had special cases for it (divecomputer ID lines, and tag lines).
But by mistake, we had introduced a case of that for the event line
handling in commit b9174332d ("Read and write divemode changes (xml and
git)"), and nobody realized that the divemode string addition meant that
"oops, now it's corrupting the event name". An event line could look
like this:
event 40:00 type=8 divemode="OC" name="modechange"
where we now had both that "OC" and "modechange" strings, and the code
to pick the name just picked the first string. So we'd end up
effectively mis-parsing the above line as
event 40:00 type=8 divemode="OC" name="OC"
which is obviously wrong.
The dive mode didn't really need to be a string in the first place
(there is nothing to quote, and no spaces in it), but hey, here we are.
We can't just magially fix the existing broken saves.
So make it more straightforward to handle strings in the git format line
parser. We still stash the different decoded strings together in one
special memory buffer, but now the parser helpers automatically untangle
it as they traverse the key value pairs.
This is still overly subtle code, and it doesn't fix the cases where
we've saved the wrong data back. That comes later.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2020-02-14 19:49:28 +00:00
|
|
|
{ UNUSED(line); state->active_dive->divemaster = detach_cstring(str); }
|
2014-03-09 19:19:41 +00:00
|
|
|
|
2019-07-24 21:54:46 +00:00
|
|
|
static void parse_dive_buddy(char *line, struct membuffer *str, struct git_parser_state *state)
|
load-git: clean up string handling during parsing
We had some fairly obscure rules for how strings were parsed, and it
actually caused bugs when the same line had multiple strings in it.
That normally doesn't happen, and the cases where it was _supposed_ to
happen had special cases for it (divecomputer ID lines, and tag lines).
But by mistake, we had introduced a case of that for the event line
handling in commit b9174332d ("Read and write divemode changes (xml and
git)"), and nobody realized that the divemode string addition meant that
"oops, now it's corrupting the event name". An event line could look
like this:
event 40:00 type=8 divemode="OC" name="modechange"
where we now had both that "OC" and "modechange" strings, and the code
to pick the name just picked the first string. So we'd end up
effectively mis-parsing the above line as
event 40:00 type=8 divemode="OC" name="OC"
which is obviously wrong.
The dive mode didn't really need to be a string in the first place
(there is nothing to quote, and no spaces in it), but hey, here we are.
We can't just magially fix the existing broken saves.
So make it more straightforward to handle strings in the git format line
parser. We still stash the different decoded strings together in one
special memory buffer, but now the parser helpers automatically untangle
it as they traverse the key value pairs.
This is still overly subtle code, and it doesn't fix the cases where
we've saved the wrong data back. That comes later.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2020-02-14 19:49:28 +00:00
|
|
|
{ UNUSED(line); state->active_dive->buddy = detach_cstring(str); }
|
2014-03-09 19:19:41 +00:00
|
|
|
|
2019-07-24 21:54:46 +00:00
|
|
|
static void parse_dive_suit(char *line, struct membuffer *str, struct git_parser_state *state)
|
load-git: clean up string handling during parsing
We had some fairly obscure rules for how strings were parsed, and it
actually caused bugs when the same line had multiple strings in it.
That normally doesn't happen, and the cases where it was _supposed_ to
happen had special cases for it (divecomputer ID lines, and tag lines).
But by mistake, we had introduced a case of that for the event line
handling in commit b9174332d ("Read and write divemode changes (xml and
git)"), and nobody realized that the divemode string addition meant that
"oops, now it's corrupting the event name". An event line could look
like this:
event 40:00 type=8 divemode="OC" name="modechange"
where we now had both that "OC" and "modechange" strings, and the code
to pick the name just picked the first string. So we'd end up
effectively mis-parsing the above line as
event 40:00 type=8 divemode="OC" name="OC"
which is obviously wrong.
The dive mode didn't really need to be a string in the first place
(there is nothing to quote, and no spaces in it), but hey, here we are.
We can't just magially fix the existing broken saves.
So make it more straightforward to handle strings in the git format line
parser. We still stash the different decoded strings together in one
special memory buffer, but now the parser helpers automatically untangle
it as they traverse the key value pairs.
This is still overly subtle code, and it doesn't fix the cases where
we've saved the wrong data back. That comes later.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2020-02-14 19:49:28 +00:00
|
|
|
{ UNUSED(line); state->active_dive->suit = detach_cstring(str); }
|
2014-03-09 19:19:41 +00:00
|
|
|
|
2019-07-24 21:54:46 +00:00
|
|
|
static void parse_dive_notes(char *line, struct membuffer *str, struct git_parser_state *state)
|
load-git: clean up string handling during parsing
We had some fairly obscure rules for how strings were parsed, and it
actually caused bugs when the same line had multiple strings in it.
That normally doesn't happen, and the cases where it was _supposed_ to
happen had special cases for it (divecomputer ID lines, and tag lines).
But by mistake, we had introduced a case of that for the event line
handling in commit b9174332d ("Read and write divemode changes (xml and
git)"), and nobody realized that the divemode string addition meant that
"oops, now it's corrupting the event name". An event line could look
like this:
event 40:00 type=8 divemode="OC" name="modechange"
where we now had both that "OC" and "modechange" strings, and the code
to pick the name just picked the first string. So we'd end up
effectively mis-parsing the above line as
event 40:00 type=8 divemode="OC" name="OC"
which is obviously wrong.
The dive mode didn't really need to be a string in the first place
(there is nothing to quote, and no spaces in it), but hey, here we are.
We can't just magially fix the existing broken saves.
So make it more straightforward to handle strings in the git format line
parser. We still stash the different decoded strings together in one
special memory buffer, but now the parser helpers automatically untangle
it as they traverse the key value pairs.
This is still overly subtle code, and it doesn't fix the cases where
we've saved the wrong data back. That comes later.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2020-02-14 19:49:28 +00:00
|
|
|
{ UNUSED(line); state->active_dive->notes = detach_cstring(str); }
|
2014-03-09 19:19:41 +00:00
|
|
|
|
2019-07-24 21:54:46 +00:00
|
|
|
static void parse_dive_divesiteid(char *line, struct membuffer *str, struct git_parser_state *state)
|
|
|
|
{ UNUSED(str); add_dive_to_dive_site(state->active_dive, get_dive_site_by_uuid(get_hex(line), &dive_site_table)); }
|
2015-02-12 08:14:50 +00:00
|
|
|
|
2014-03-09 19:19:41 +00:00
|
|
|
/*
|
|
|
|
* We can have multiple tags in the membuffer. They are separated by
|
|
|
|
* NUL bytes.
|
|
|
|
*/
|
2019-07-24 21:54:46 +00:00
|
|
|
static void parse_dive_tags(char *line, struct membuffer *str, struct git_parser_state *state)
|
2014-03-09 19:19:41 +00:00
|
|
|
{
|
2018-05-22 07:07:42 +00:00
|
|
|
UNUSED(line);
|
2014-03-09 19:19:41 +00:00
|
|
|
const char *tag;
|
|
|
|
int len = str->len;
|
|
|
|
|
|
|
|
if (!len)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Make sure there is a NUL at the end too */
|
|
|
|
tag = mb_cstring(str);
|
|
|
|
for (;;) {
|
|
|
|
int taglen = strlen(tag);
|
|
|
|
if (taglen)
|
2019-07-24 21:54:46 +00:00
|
|
|
taglist_add_tag(&state->active_dive->tag_list, tag);
|
2014-03-09 19:19:41 +00:00
|
|
|
len -= taglen;
|
|
|
|
if (!len)
|
|
|
|
return;
|
2019-07-24 21:54:46 +00:00
|
|
|
tag += taglen + 1;
|
2014-03-09 19:19:41 +00:00
|
|
|
len--;
|
|
|
|
}
|
2014-03-08 23:59:39 +00:00
|
|
|
}
|
|
|
|
|
2019-07-24 21:54:46 +00:00
|
|
|
static void parse_dive_airtemp(char *line, struct membuffer *str, struct git_parser_state *state)
|
|
|
|
{ UNUSED(str); state->active_dive->airtemp = get_temperature(line); }
|
2014-03-09 19:19:41 +00:00
|
|
|
|
2019-07-24 21:54:46 +00:00
|
|
|
static void parse_dive_watertemp(char *line, struct membuffer *str, struct git_parser_state *state)
|
|
|
|
{ UNUSED(str); state->active_dive->watertemp = get_temperature(line); }
|
2014-03-09 19:19:41 +00:00
|
|
|
|
2019-07-24 21:54:46 +00:00
|
|
|
static void parse_dive_airpressure(char *line, struct membuffer *str, struct git_parser_state *state)
|
|
|
|
{ UNUSED(str); state->active_dive->surface_pressure = get_airpressure(line); }
|
2019-04-30 10:42:33 +00:00
|
|
|
|
2019-07-24 21:54:46 +00:00
|
|
|
static void parse_dive_duration(char *line, struct membuffer *str, struct git_parser_state *state)
|
|
|
|
{ UNUSED(str); state->active_dive->duration = get_duration(line); }
|
2014-03-09 19:19:41 +00:00
|
|
|
|
2019-07-24 21:54:46 +00:00
|
|
|
static void parse_dive_rating(char *line, struct membuffer *str, struct git_parser_state *state)
|
|
|
|
{ UNUSED(str); state->active_dive->rating = get_index(line); }
|
2014-03-09 19:19:41 +00:00
|
|
|
|
2019-07-24 21:54:46 +00:00
|
|
|
static void parse_dive_visibility(char *line, struct membuffer *str, struct git_parser_state *state)
|
|
|
|
{ UNUSED(str); state->active_dive->visibility = get_index(line); }
|
2014-03-09 19:19:41 +00:00
|
|
|
|
2019-11-29 05:08:14 +00:00
|
|
|
static void parse_dive_wavesize(char *line, struct membuffer *str, struct git_parser_state *state)
|
|
|
|
{ UNUSED(str); state->active_dive->wavesize = get_index(line); }
|
|
|
|
|
|
|
|
static void parse_dive_current(char *line, struct membuffer *str, struct git_parser_state *state)
|
|
|
|
{ UNUSED(str); state->active_dive->current = get_index(line); }
|
|
|
|
|
|
|
|
static void parse_dive_surge(char *line, struct membuffer *str, struct git_parser_state *state)
|
|
|
|
{ UNUSED(str); state->active_dive->surge = get_index(line); }
|
|
|
|
|
|
|
|
static void parse_dive_chill(char *line, struct membuffer *str, struct git_parser_state *state)
|
|
|
|
{ UNUSED(str); state->active_dive->chill = get_index(line); }
|
|
|
|
|
2019-11-19 17:16:45 +00:00
|
|
|
static void parse_dive_watersalinity(char *line, struct membuffer *str, struct git_parser_state *state)
|
|
|
|
{ UNUSED(str); state->active_dive->user_salinity = get_salinity(line); }
|
|
|
|
|
2019-07-24 21:54:46 +00:00
|
|
|
static void parse_dive_notrip(char *line, struct membuffer *str, struct git_parser_state *state)
|
2016-03-07 19:18:50 +00:00
|
|
|
{
|
2018-05-22 07:07:42 +00:00
|
|
|
UNUSED(str);
|
|
|
|
UNUSED(line);
|
2019-07-24 21:54:46 +00:00
|
|
|
state->active_dive->notrip = true;
|
2016-03-07 19:18:50 +00:00
|
|
|
}
|
2014-03-09 19:19:41 +00:00
|
|
|
|
2019-12-12 21:58:53 +00:00
|
|
|
static void parse_dive_invalid(char *line, struct membuffer *str, struct git_parser_state *state)
|
|
|
|
{
|
|
|
|
UNUSED(str);
|
|
|
|
UNUSED(line);
|
|
|
|
state->active_dive->invalid = true;
|
|
|
|
}
|
|
|
|
|
2019-07-24 21:54:46 +00:00
|
|
|
static void parse_site_description(char *line, struct membuffer *str, struct git_parser_state *state)
|
load-git: clean up string handling during parsing
We had some fairly obscure rules for how strings were parsed, and it
actually caused bugs when the same line had multiple strings in it.
That normally doesn't happen, and the cases where it was _supposed_ to
happen had special cases for it (divecomputer ID lines, and tag lines).
But by mistake, we had introduced a case of that for the event line
handling in commit b9174332d ("Read and write divemode changes (xml and
git)"), and nobody realized that the divemode string addition meant that
"oops, now it's corrupting the event name". An event line could look
like this:
event 40:00 type=8 divemode="OC" name="modechange"
where we now had both that "OC" and "modechange" strings, and the code
to pick the name just picked the first string. So we'd end up
effectively mis-parsing the above line as
event 40:00 type=8 divemode="OC" name="OC"
which is obviously wrong.
The dive mode didn't really need to be a string in the first place
(there is nothing to quote, and no spaces in it), but hey, here we are.
We can't just magially fix the existing broken saves.
So make it more straightforward to handle strings in the git format line
parser. We still stash the different decoded strings together in one
special memory buffer, but now the parser helpers automatically untangle
it as they traverse the key value pairs.
This is still overly subtle code, and it doesn't fix the cases where
we've saved the wrong data back. That comes later.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2020-02-14 19:49:28 +00:00
|
|
|
{ UNUSED(line); state->active_site->description = detach_cstring(str); }
|
2015-02-12 08:14:50 +00:00
|
|
|
|
2019-07-24 21:54:46 +00:00
|
|
|
static void parse_site_name(char *line, struct membuffer *str, struct git_parser_state *state)
|
load-git: clean up string handling during parsing
We had some fairly obscure rules for how strings were parsed, and it
actually caused bugs when the same line had multiple strings in it.
That normally doesn't happen, and the cases where it was _supposed_ to
happen had special cases for it (divecomputer ID lines, and tag lines).
But by mistake, we had introduced a case of that for the event line
handling in commit b9174332d ("Read and write divemode changes (xml and
git)"), and nobody realized that the divemode string addition meant that
"oops, now it's corrupting the event name". An event line could look
like this:
event 40:00 type=8 divemode="OC" name="modechange"
where we now had both that "OC" and "modechange" strings, and the code
to pick the name just picked the first string. So we'd end up
effectively mis-parsing the above line as
event 40:00 type=8 divemode="OC" name="OC"
which is obviously wrong.
The dive mode didn't really need to be a string in the first place
(there is nothing to quote, and no spaces in it), but hey, here we are.
We can't just magially fix the existing broken saves.
So make it more straightforward to handle strings in the git format line
parser. We still stash the different decoded strings together in one
special memory buffer, but now the parser helpers automatically untangle
it as they traverse the key value pairs.
This is still overly subtle code, and it doesn't fix the cases where
we've saved the wrong data back. That comes later.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2020-02-14 19:49:28 +00:00
|
|
|
{ UNUSED(line); state->active_site->name = detach_cstring(str); }
|
2015-02-12 08:14:50 +00:00
|
|
|
|
2019-07-24 21:54:46 +00:00
|
|
|
static void parse_site_notes(char *line, struct membuffer *str, struct git_parser_state *state)
|
load-git: clean up string handling during parsing
We had some fairly obscure rules for how strings were parsed, and it
actually caused bugs when the same line had multiple strings in it.
That normally doesn't happen, and the cases where it was _supposed_ to
happen had special cases for it (divecomputer ID lines, and tag lines).
But by mistake, we had introduced a case of that for the event line
handling in commit b9174332d ("Read and write divemode changes (xml and
git)"), and nobody realized that the divemode string addition meant that
"oops, now it's corrupting the event name". An event line could look
like this:
event 40:00 type=8 divemode="OC" name="modechange"
where we now had both that "OC" and "modechange" strings, and the code
to pick the name just picked the first string. So we'd end up
effectively mis-parsing the above line as
event 40:00 type=8 divemode="OC" name="OC"
which is obviously wrong.
The dive mode didn't really need to be a string in the first place
(there is nothing to quote, and no spaces in it), but hey, here we are.
We can't just magially fix the existing broken saves.
So make it more straightforward to handle strings in the git format line
parser. We still stash the different decoded strings together in one
special memory buffer, but now the parser helpers automatically untangle
it as they traverse the key value pairs.
This is still overly subtle code, and it doesn't fix the cases where
we've saved the wrong data back. That comes later.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2020-02-14 19:49:28 +00:00
|
|
|
{ UNUSED(line); state->active_site->notes = detach_cstring(str); }
|
2015-02-12 08:14:50 +00:00
|
|
|
|
2019-07-24 21:54:46 +00:00
|
|
|
static void parse_site_gps(char *line, struct membuffer *str, struct git_parser_state *state)
|
2015-02-12 08:14:50 +00:00
|
|
|
{
|
2018-05-22 07:07:42 +00:00
|
|
|
UNUSED(str);
|
2019-07-24 21:54:46 +00:00
|
|
|
parse_location(line, &state->active_site->location);
|
2015-02-12 08:14:50 +00:00
|
|
|
}
|
|
|
|
|
2019-07-24 21:54:46 +00:00
|
|
|
static void parse_site_geo(char *line, struct membuffer *str, struct git_parser_state *state)
|
2015-07-01 19:29:32 +00:00
|
|
|
{
|
2019-07-24 21:54:46 +00:00
|
|
|
if (state->active_site->taxonomy.category == NULL)
|
|
|
|
state->active_site->taxonomy.category = alloc_taxonomy();
|
|
|
|
int nr = state->active_site->taxonomy.nr;
|
2015-07-02 17:21:35 +00:00
|
|
|
if (nr < TC_NR_CATEGORIES) {
|
2019-07-24 21:54:46 +00:00
|
|
|
struct taxonomy *t = &state->active_site->taxonomy.category[nr];
|
load-git: clean up string handling during parsing
We had some fairly obscure rules for how strings were parsed, and it
actually caused bugs when the same line had multiple strings in it.
That normally doesn't happen, and the cases where it was _supposed_ to
happen had special cases for it (divecomputer ID lines, and tag lines).
But by mistake, we had introduced a case of that for the event line
handling in commit b9174332d ("Read and write divemode changes (xml and
git)"), and nobody realized that the divemode string addition meant that
"oops, now it's corrupting the event name". An event line could look
like this:
event 40:00 type=8 divemode="OC" name="modechange"
where we now had both that "OC" and "modechange" strings, and the code
to pick the name just picked the first string. So we'd end up
effectively mis-parsing the above line as
event 40:00 type=8 divemode="OC" name="OC"
which is obviously wrong.
The dive mode didn't really need to be a string in the first place
(there is nothing to quote, and no spaces in it), but hey, here we are.
We can't just magially fix the existing broken saves.
So make it more straightforward to handle strings in the git format line
parser. We still stash the different decoded strings together in one
special memory buffer, but now the parser helpers automatically untangle
it as they traverse the key value pairs.
This is still overly subtle code, and it doesn't fix the cases where
we've saved the wrong data back. That comes later.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2020-02-14 19:49:28 +00:00
|
|
|
t->value = detach_cstring(str);
|
2015-09-23 14:38:41 +00:00
|
|
|
sscanf(line, "cat %d origin %d \"", &t->category, (int *)&t->origin);
|
2019-07-24 21:54:46 +00:00
|
|
|
state->active_site->taxonomy.nr++;
|
2015-07-01 19:29:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
load-git: clean up string handling during parsing
We had some fairly obscure rules for how strings were parsed, and it
actually caused bugs when the same line had multiple strings in it.
That normally doesn't happen, and the cases where it was _supposed_ to
happen had special cases for it (divecomputer ID lines, and tag lines).
But by mistake, we had introduced a case of that for the event line
handling in commit b9174332d ("Read and write divemode changes (xml and
git)"), and nobody realized that the divemode string addition meant that
"oops, now it's corrupting the event name". An event line could look
like this:
event 40:00 type=8 divemode="OC" name="modechange"
where we now had both that "OC" and "modechange" strings, and the code
to pick the name just picked the first string. So we'd end up
effectively mis-parsing the above line as
event 40:00 type=8 divemode="OC" name="OC"
which is obviously wrong.
The dive mode didn't really need to be a string in the first place
(there is nothing to quote, and no spaces in it), but hey, here we are.
We can't just magially fix the existing broken saves.
So make it more straightforward to handle strings in the git format line
parser. We still stash the different decoded strings together in one
special memory buffer, but now the parser helpers automatically untangle
it as they traverse the key value pairs.
This is still overly subtle code, and it doesn't fix the cases where
we've saved the wrong data back. That comes later.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2020-02-14 19:49:28 +00:00
|
|
|
static char *remove_from_front(struct membuffer *str, int len)
|
|
|
|
{
|
|
|
|
char *prefix;
|
|
|
|
|
|
|
|
if (len >= str->len)
|
|
|
|
return detach_cstring(str);
|
|
|
|
|
|
|
|
/* memdup() - oh well */
|
|
|
|
prefix = malloc(len);
|
|
|
|
if (!prefix) {
|
|
|
|
report_error("git-load: out of memory");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
memcpy(prefix, str->buffer, len);
|
|
|
|
|
|
|
|
str->len -= len;
|
|
|
|
memmove(str->buffer, str->buffer+len, str->len);
|
|
|
|
return prefix;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *pop_cstring(struct membuffer *str, const char *err)
|
|
|
|
{
|
|
|
|
int len;
|
|
|
|
|
|
|
|
if (!str) {
|
|
|
|
report_error("git-load: string marker without any strings ('%s')", err);
|
2020-03-16 17:00:20 +00:00
|
|
|
return strdup("");
|
load-git: clean up string handling during parsing
We had some fairly obscure rules for how strings were parsed, and it
actually caused bugs when the same line had multiple strings in it.
That normally doesn't happen, and the cases where it was _supposed_ to
happen had special cases for it (divecomputer ID lines, and tag lines).
But by mistake, we had introduced a case of that for the event line
handling in commit b9174332d ("Read and write divemode changes (xml and
git)"), and nobody realized that the divemode string addition meant that
"oops, now it's corrupting the event name". An event line could look
like this:
event 40:00 type=8 divemode="OC" name="modechange"
where we now had both that "OC" and "modechange" strings, and the code
to pick the name just picked the first string. So we'd end up
effectively mis-parsing the above line as
event 40:00 type=8 divemode="OC" name="OC"
which is obviously wrong.
The dive mode didn't really need to be a string in the first place
(there is nothing to quote, and no spaces in it), but hey, here we are.
We can't just magially fix the existing broken saves.
So make it more straightforward to handle strings in the git format line
parser. We still stash the different decoded strings together in one
special memory buffer, but now the parser helpers automatically untangle
it as they traverse the key value pairs.
This is still overly subtle code, and it doesn't fix the cases where
we've saved the wrong data back. That comes later.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2020-02-14 19:49:28 +00:00
|
|
|
}
|
|
|
|
if (!str->len) {
|
|
|
|
report_error("git-load: string marker after running out of strings ('%s')", err);
|
2020-03-16 17:00:20 +00:00
|
|
|
return strdup("");
|
load-git: clean up string handling during parsing
We had some fairly obscure rules for how strings were parsed, and it
actually caused bugs when the same line had multiple strings in it.
That normally doesn't happen, and the cases where it was _supposed_ to
happen had special cases for it (divecomputer ID lines, and tag lines).
But by mistake, we had introduced a case of that for the event line
handling in commit b9174332d ("Read and write divemode changes (xml and
git)"), and nobody realized that the divemode string addition meant that
"oops, now it's corrupting the event name". An event line could look
like this:
event 40:00 type=8 divemode="OC" name="modechange"
where we now had both that "OC" and "modechange" strings, and the code
to pick the name just picked the first string. So we'd end up
effectively mis-parsing the above line as
event 40:00 type=8 divemode="OC" name="OC"
which is obviously wrong.
The dive mode didn't really need to be a string in the first place
(there is nothing to quote, and no spaces in it), but hey, here we are.
We can't just magially fix the existing broken saves.
So make it more straightforward to handle strings in the git format line
parser. We still stash the different decoded strings together in one
special memory buffer, but now the parser helpers automatically untangle
it as they traverse the key value pairs.
This is still overly subtle code, and it doesn't fix the cases where
we've saved the wrong data back. That comes later.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2020-02-14 19:49:28 +00:00
|
|
|
}
|
|
|
|
len = strlen(mb_cstring(str)) + 1;
|
|
|
|
return remove_from_front(str, len);
|
|
|
|
}
|
|
|
|
|
2014-03-09 21:45:20 +00:00
|
|
|
/* Parse key=val parts of samples and cylinders etc */
|
load-git: clean up string handling during parsing
We had some fairly obscure rules for how strings were parsed, and it
actually caused bugs when the same line had multiple strings in it.
That normally doesn't happen, and the cases where it was _supposed_ to
happen had special cases for it (divecomputer ID lines, and tag lines).
But by mistake, we had introduced a case of that for the event line
handling in commit b9174332d ("Read and write divemode changes (xml and
git)"), and nobody realized that the divemode string addition meant that
"oops, now it's corrupting the event name". An event line could look
like this:
event 40:00 type=8 divemode="OC" name="modechange"
where we now had both that "OC" and "modechange" strings, and the code
to pick the name just picked the first string. So we'd end up
effectively mis-parsing the above line as
event 40:00 type=8 divemode="OC" name="OC"
which is obviously wrong.
The dive mode didn't really need to be a string in the first place
(there is nothing to quote, and no spaces in it), but hey, here we are.
We can't just magially fix the existing broken saves.
So make it more straightforward to handle strings in the git format line
parser. We still stash the different decoded strings together in one
special memory buffer, but now the parser helpers automatically untangle
it as they traverse the key value pairs.
This is still overly subtle code, and it doesn't fix the cases where
we've saved the wrong data back. That comes later.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2020-02-14 19:49:28 +00:00
|
|
|
static char *parse_keyvalue_entry(void (*fn)(void *, const char *, const char *), void *fndata, char *line, struct membuffer *str)
|
2014-03-09 21:45:20 +00:00
|
|
|
{
|
|
|
|
char *key = line, *val, c;
|
|
|
|
|
|
|
|
while ((c = *line) != 0) {
|
|
|
|
if (isspace(c) || c == '=')
|
|
|
|
break;
|
|
|
|
line++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (c == '=')
|
|
|
|
*line++ = 0;
|
|
|
|
val = line;
|
|
|
|
|
load-git: clean up string handling during parsing
We had some fairly obscure rules for how strings were parsed, and it
actually caused bugs when the same line had multiple strings in it.
That normally doesn't happen, and the cases where it was _supposed_ to
happen had special cases for it (divecomputer ID lines, and tag lines).
But by mistake, we had introduced a case of that for the event line
handling in commit b9174332d ("Read and write divemode changes (xml and
git)"), and nobody realized that the divemode string addition meant that
"oops, now it's corrupting the event name". An event line could look
like this:
event 40:00 type=8 divemode="OC" name="modechange"
where we now had both that "OC" and "modechange" strings, and the code
to pick the name just picked the first string. So we'd end up
effectively mis-parsing the above line as
event 40:00 type=8 divemode="OC" name="OC"
which is obviously wrong.
The dive mode didn't really need to be a string in the first place
(there is nothing to quote, and no spaces in it), but hey, here we are.
We can't just magially fix the existing broken saves.
So make it more straightforward to handle strings in the git format line
parser. We still stash the different decoded strings together in one
special memory buffer, but now the parser helpers automatically untangle
it as they traverse the key value pairs.
This is still overly subtle code, and it doesn't fix the cases where
we've saved the wrong data back. That comes later.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2020-02-14 19:49:28 +00:00
|
|
|
/* Did we get a string? Take it from the list of strings */
|
|
|
|
if (*val == '"')
|
|
|
|
val = pop_cstring(str, key);
|
|
|
|
|
2014-03-09 21:45:20 +00:00
|
|
|
while ((c = *line) != 0) {
|
|
|
|
if (isspace(c))
|
|
|
|
break;
|
|
|
|
line++;
|
|
|
|
}
|
|
|
|
if (c)
|
|
|
|
*line++ = 0;
|
|
|
|
|
|
|
|
fn(fndata, key, val);
|
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void parse_cylinder_keyvalue(void *_cylinder, const char *key, const char *value)
|
|
|
|
{
|
|
|
|
cylinder_t *cylinder = _cylinder;
|
|
|
|
if (!strcmp(key, "vol")) {
|
|
|
|
cylinder->type.size = get_volume(value);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!strcmp(key, "workpressure")) {
|
|
|
|
cylinder->type.workingpressure = get_pressure(value);
|
|
|
|
return;
|
|
|
|
}
|
load-git: clean up string handling during parsing
We had some fairly obscure rules for how strings were parsed, and it
actually caused bugs when the same line had multiple strings in it.
That normally doesn't happen, and the cases where it was _supposed_ to
happen had special cases for it (divecomputer ID lines, and tag lines).
But by mistake, we had introduced a case of that for the event line
handling in commit b9174332d ("Read and write divemode changes (xml and
git)"), and nobody realized that the divemode string addition meant that
"oops, now it's corrupting the event name". An event line could look
like this:
event 40:00 type=8 divemode="OC" name="modechange"
where we now had both that "OC" and "modechange" strings, and the code
to pick the name just picked the first string. So we'd end up
effectively mis-parsing the above line as
event 40:00 type=8 divemode="OC" name="OC"
which is obviously wrong.
The dive mode didn't really need to be a string in the first place
(there is nothing to quote, and no spaces in it), but hey, here we are.
We can't just magially fix the existing broken saves.
So make it more straightforward to handle strings in the git format line
parser. We still stash the different decoded strings together in one
special memory buffer, but now the parser helpers automatically untangle
it as they traverse the key value pairs.
This is still overly subtle code, and it doesn't fix the cases where
we've saved the wrong data back. That comes later.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2020-02-14 19:49:28 +00:00
|
|
|
if (!strcmp(key, "description")) {
|
|
|
|
cylinder->type.description = value;
|
2014-03-09 21:45:20 +00:00
|
|
|
return;
|
load-git: clean up string handling during parsing
We had some fairly obscure rules for how strings were parsed, and it
actually caused bugs when the same line had multiple strings in it.
That normally doesn't happen, and the cases where it was _supposed_ to
happen had special cases for it (divecomputer ID lines, and tag lines).
But by mistake, we had introduced a case of that for the event line
handling in commit b9174332d ("Read and write divemode changes (xml and
git)"), and nobody realized that the divemode string addition meant that
"oops, now it's corrupting the event name". An event line could look
like this:
event 40:00 type=8 divemode="OC" name="modechange"
where we now had both that "OC" and "modechange" strings, and the code
to pick the name just picked the first string. So we'd end up
effectively mis-parsing the above line as
event 40:00 type=8 divemode="OC" name="OC"
which is obviously wrong.
The dive mode didn't really need to be a string in the first place
(there is nothing to quote, and no spaces in it), but hey, here we are.
We can't just magially fix the existing broken saves.
So make it more straightforward to handle strings in the git format line
parser. We still stash the different decoded strings together in one
special memory buffer, but now the parser helpers automatically untangle
it as they traverse the key value pairs.
This is still overly subtle code, and it doesn't fix the cases where
we've saved the wrong data back. That comes later.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2020-02-14 19:49:28 +00:00
|
|
|
}
|
2014-03-09 21:45:20 +00:00
|
|
|
if (!strcmp(key, "o2")) {
|
|
|
|
cylinder->gasmix.o2 = get_fraction(value);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!strcmp(key, "he")) {
|
|
|
|
cylinder->gasmix.he = get_fraction(value);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!strcmp(key, "start")) {
|
|
|
|
cylinder->start = get_pressure(value);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!strcmp(key, "end")) {
|
|
|
|
cylinder->end = get_pressure(value);
|
|
|
|
return;
|
|
|
|
}
|
2014-11-16 22:11:34 +00:00
|
|
|
if (!strcmp(key, "use")) {
|
2014-11-17 13:52:22 +00:00
|
|
|
cylinder->cylinder_use = cylinderuse_from_text(value);
|
|
|
|
return;
|
2014-11-16 22:11:34 +00:00
|
|
|
}
|
2017-11-27 17:20:21 +00:00
|
|
|
if (!strcmp(key, "depth")) {
|
|
|
|
cylinder->depth = get_depth(value);
|
|
|
|
return;
|
|
|
|
}
|
2017-12-20 13:37:03 +00:00
|
|
|
if ((*key == 'm') && strlen(value) == 0) {
|
2017-12-18 18:45:45 +00:00
|
|
|
/* found a bogus key/value pair in the cylinder, consisting
|
2017-12-20 13:37:03 +00:00
|
|
|
* of a lonely "m" or m<single quote> without value. This
|
|
|
|
* is caused by commit 46004c39e26 and fixed in 48d9c8eb6eb0 and
|
|
|
|
* b984fb98c38e4. See referenced commits for more info.
|
2017-12-18 18:45:45 +00:00
|
|
|
*
|
|
|
|
* Just ignore this key/value pair. No processing is broken
|
|
|
|
* due to this, as the git storage stores only metric SI type data.
|
|
|
|
* In fact, the m unit is superfluous anyway.
|
|
|
|
*/
|
|
|
|
return;
|
|
|
|
}
|
2014-03-09 21:45:20 +00:00
|
|
|
report_error("Unknown cylinder key/value pair (%s/%s)", key, value);
|
|
|
|
}
|
|
|
|
|
2019-07-24 21:54:46 +00:00
|
|
|
static void parse_dive_cylinder(char *line, struct membuffer *str, struct git_parser_state *state)
|
2014-03-09 21:45:20 +00:00
|
|
|
{
|
2020-01-07 03:00:20 +00:00
|
|
|
cylinder_t cylinder = empty_cylinder;
|
2014-03-09 21:45:20 +00:00
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
char c;
|
|
|
|
while (isspace(c = *line))
|
|
|
|
line++;
|
|
|
|
if (!c)
|
|
|
|
break;
|
load-git: clean up string handling during parsing
We had some fairly obscure rules for how strings were parsed, and it
actually caused bugs when the same line had multiple strings in it.
That normally doesn't happen, and the cases where it was _supposed_ to
happen had special cases for it (divecomputer ID lines, and tag lines).
But by mistake, we had introduced a case of that for the event line
handling in commit b9174332d ("Read and write divemode changes (xml and
git)"), and nobody realized that the divemode string addition meant that
"oops, now it's corrupting the event name". An event line could look
like this:
event 40:00 type=8 divemode="OC" name="modechange"
where we now had both that "OC" and "modechange" strings, and the code
to pick the name just picked the first string. So we'd end up
effectively mis-parsing the above line as
event 40:00 type=8 divemode="OC" name="OC"
which is obviously wrong.
The dive mode didn't really need to be a string in the first place
(there is nothing to quote, and no spaces in it), but hey, here we are.
We can't just magially fix the existing broken saves.
So make it more straightforward to handle strings in the git format line
parser. We still stash the different decoded strings together in one
special memory buffer, but now the parser helpers automatically untangle
it as they traverse the key value pairs.
This is still overly subtle code, and it doesn't fix the cases where
we've saved the wrong data back. That comes later.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2020-02-14 19:49:28 +00:00
|
|
|
line = parse_keyvalue_entry(parse_cylinder_keyvalue, &cylinder, line, str);
|
2014-03-09 21:45:20 +00:00
|
|
|
}
|
2019-08-04 16:44:57 +00:00
|
|
|
if (cylinder.cylinder_use == OXYGEN)
|
|
|
|
state->o2pressure_sensor = state->active_dive->cylinders.nr;
|
|
|
|
|
2020-04-28 12:50:40 +00:00
|
|
|
add_cylinder(&state->active_dive->cylinders, state->active_dive->cylinders.nr, cylinder);
|
2014-03-09 21:45:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void parse_weightsystem_keyvalue(void *_ws, const char *key, const char *value)
|
|
|
|
{
|
|
|
|
weightsystem_t *ws = _ws;
|
|
|
|
if (!strcmp(key, "weight")) {
|
|
|
|
ws->weight = get_weight(value);
|
|
|
|
return;
|
|
|
|
}
|
load-git: clean up string handling during parsing
We had some fairly obscure rules for how strings were parsed, and it
actually caused bugs when the same line had multiple strings in it.
That normally doesn't happen, and the cases where it was _supposed_ to
happen had special cases for it (divecomputer ID lines, and tag lines).
But by mistake, we had introduced a case of that for the event line
handling in commit b9174332d ("Read and write divemode changes (xml and
git)"), and nobody realized that the divemode string addition meant that
"oops, now it's corrupting the event name". An event line could look
like this:
event 40:00 type=8 divemode="OC" name="modechange"
where we now had both that "OC" and "modechange" strings, and the code
to pick the name just picked the first string. So we'd end up
effectively mis-parsing the above line as
event 40:00 type=8 divemode="OC" name="OC"
which is obviously wrong.
The dive mode didn't really need to be a string in the first place
(there is nothing to quote, and no spaces in it), but hey, here we are.
We can't just magially fix the existing broken saves.
So make it more straightforward to handle strings in the git format line
parser. We still stash the different decoded strings together in one
special memory buffer, but now the parser helpers automatically untangle
it as they traverse the key value pairs.
This is still overly subtle code, and it doesn't fix the cases where
we've saved the wrong data back. That comes later.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2020-02-14 19:49:28 +00:00
|
|
|
if (!strcmp(key, "description")) {
|
|
|
|
ws->description = value;
|
2014-03-09 21:45:20 +00:00
|
|
|
return;
|
load-git: clean up string handling during parsing
We had some fairly obscure rules for how strings were parsed, and it
actually caused bugs when the same line had multiple strings in it.
That normally doesn't happen, and the cases where it was _supposed_ to
happen had special cases for it (divecomputer ID lines, and tag lines).
But by mistake, we had introduced a case of that for the event line
handling in commit b9174332d ("Read and write divemode changes (xml and
git)"), and nobody realized that the divemode string addition meant that
"oops, now it's corrupting the event name". An event line could look
like this:
event 40:00 type=8 divemode="OC" name="modechange"
where we now had both that "OC" and "modechange" strings, and the code
to pick the name just picked the first string. So we'd end up
effectively mis-parsing the above line as
event 40:00 type=8 divemode="OC" name="OC"
which is obviously wrong.
The dive mode didn't really need to be a string in the first place
(there is nothing to quote, and no spaces in it), but hey, here we are.
We can't just magially fix the existing broken saves.
So make it more straightforward to handle strings in the git format line
parser. We still stash the different decoded strings together in one
special memory buffer, but now the parser helpers automatically untangle
it as they traverse the key value pairs.
This is still overly subtle code, and it doesn't fix the cases where
we've saved the wrong data back. That comes later.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2020-02-14 19:49:28 +00:00
|
|
|
}
|
2014-03-09 21:45:20 +00:00
|
|
|
report_error("Unknown weightsystem key/value pair (%s/%s)", key, value);
|
|
|
|
}
|
|
|
|
|
2019-07-24 21:54:46 +00:00
|
|
|
static void parse_dive_weightsystem(char *line, struct membuffer *str, struct git_parser_state *state)
|
2014-03-09 21:45:20 +00:00
|
|
|
{
|
2020-01-07 03:00:20 +00:00
|
|
|
weightsystem_t ws = empty_weightsystem;
|
2014-03-09 21:45:20 +00:00
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
char c;
|
|
|
|
while (isspace(c = *line))
|
|
|
|
line++;
|
|
|
|
if (!c)
|
|
|
|
break;
|
load-git: clean up string handling during parsing
We had some fairly obscure rules for how strings were parsed, and it
actually caused bugs when the same line had multiple strings in it.
That normally doesn't happen, and the cases where it was _supposed_ to
happen had special cases for it (divecomputer ID lines, and tag lines).
But by mistake, we had introduced a case of that for the event line
handling in commit b9174332d ("Read and write divemode changes (xml and
git)"), and nobody realized that the divemode string addition meant that
"oops, now it's corrupting the event name". An event line could look
like this:
event 40:00 type=8 divemode="OC" name="modechange"
where we now had both that "OC" and "modechange" strings, and the code
to pick the name just picked the first string. So we'd end up
effectively mis-parsing the above line as
event 40:00 type=8 divemode="OC" name="OC"
which is obviously wrong.
The dive mode didn't really need to be a string in the first place
(there is nothing to quote, and no spaces in it), but hey, here we are.
We can't just magially fix the existing broken saves.
So make it more straightforward to handle strings in the git format line
parser. We still stash the different decoded strings together in one
special memory buffer, but now the parser helpers automatically untangle
it as they traverse the key value pairs.
This is still overly subtle code, and it doesn't fix the cases where
we've saved the wrong data back. That comes later.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2020-02-14 19:49:28 +00:00
|
|
|
line = parse_keyvalue_entry(parse_weightsystem_keyvalue, &ws, line, str);
|
2014-03-09 21:45:20 +00:00
|
|
|
}
|
2019-06-26 15:21:03 +00:00
|
|
|
|
2019-07-24 21:54:46 +00:00
|
|
|
add_to_weightsystem_table(&state->active_dive->weightsystems, state->active_dive->weightsystems.nr, ws);
|
2014-03-09 21:45:20 +00:00
|
|
|
}
|
2014-03-09 19:19:41 +00:00
|
|
|
|
|
|
|
static int match_action(char *line, struct membuffer *str, void *data,
|
|
|
|
struct keyword_action *action, unsigned nr_action)
|
2014-03-08 23:59:39 +00:00
|
|
|
{
|
2014-03-09 19:19:41 +00:00
|
|
|
char *p = line, c;
|
|
|
|
unsigned low, high;
|
|
|
|
|
2014-11-22 13:13:13 +00:00
|
|
|
while ((c = *p) >= 'a' && c <= 'z') // skip over 1st word
|
|
|
|
p++; // Extract the second word from the line:
|
2014-03-09 19:19:41 +00:00
|
|
|
if (p == line)
|
|
|
|
return -1;
|
|
|
|
switch (c) {
|
2014-11-22 13:13:13 +00:00
|
|
|
case 0: // if 2nd word is C-terminated
|
2014-03-09 19:19:41 +00:00
|
|
|
break;
|
2014-11-22 13:13:13 +00:00
|
|
|
case ' ': // =end of 2nd word?
|
|
|
|
*p++ = 0; // then C-terminate that word
|
2014-03-09 19:19:41 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Standard binary search in a table */
|
|
|
|
low = 0;
|
|
|
|
high = nr_action;
|
|
|
|
while (low < high) {
|
2019-07-24 22:26:51 +00:00
|
|
|
unsigned mid = (low + high)/2;
|
2014-03-09 19:19:41 +00:00
|
|
|
struct keyword_action *a = action + mid;
|
|
|
|
int cmp = strcmp(line, a->keyword);
|
2014-11-22 13:13:13 +00:00
|
|
|
if (!cmp) { // attribute found:
|
|
|
|
a->fn(p, str, data); // Execute appropriate function,
|
|
|
|
return 0; // .. passing 2n word from above
|
|
|
|
} // (p) as a function argument.
|
2014-03-09 19:19:41 +00:00
|
|
|
if (cmp < 0)
|
|
|
|
high = mid;
|
|
|
|
else
|
2019-07-24 22:26:51 +00:00
|
|
|
low = mid + 1;
|
2014-03-09 19:19:41 +00:00
|
|
|
}
|
|
|
|
report_error("Unmatched action '%s'", line);
|
|
|
|
return -1;
|
2014-03-08 23:59:39 +00:00
|
|
|
}
|
|
|
|
|
2014-03-09 21:26:39 +00:00
|
|
|
/* FIXME! We should do the array thing here too. */
|
2014-03-09 21:45:20 +00:00
|
|
|
static void parse_sample_keyvalue(void *_sample, const char *key, const char *value)
|
2014-03-09 20:13:48 +00:00
|
|
|
{
|
2014-03-09 21:45:20 +00:00
|
|
|
struct sample *sample = _sample;
|
|
|
|
|
2014-03-09 21:26:39 +00:00
|
|
|
if (!strcmp(key, "sensor")) {
|
Start cleaning up sensor indexing for multiple sensors
This is a very timid start at making us actually use multiple sensors
without the magical special case for just CCR oxygen tracking.
It mainly does:
- turn the "sample->sensor" index into an array of two indexes, to
match the pressures themselves.
- get rid of dive->{oxygen_cylinder_index,diluent_cylinder_index},
since a CCR dive should now simply set the sample->sensor[] indices
correctly instead.
- in a couple of places, start actually looping over the sensors rather
than special-case the O2 case (although often the small "loops" are
just unrolled, since it's just two cases.
but in many cases we still end up only covering the zero sensor case,
because the CCR O2 sensor code coverage was fairly limited.
It's entirely possible (even likely) that this migth break some existing
case: it tries to be a fairly direct ("stupid") translation of the old
code, but unlike the preparatory patch this does actually does change
some semantics.
For example, right now the git loader code assumes that if the git save
data contains a o2pressure entry, it just hardcodes the O2 sensor index
to 1.
In fact, one issue is going to simply be that our file formats do not
have that multiple sensor format, but instead had very clearly encoded
things as being the CCR O2 pressure sensor.
But this is hopefully close to usable, and I will need feedback (and
maybe test cases) from people who have existing CCR dives with pressure
data.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2017-07-21 02:49:45 +00:00
|
|
|
sample->sensor[0] = atoi(value);
|
2014-03-09 21:26:39 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!strcmp(key, "ndl")) {
|
|
|
|
sample->ndl = get_duration(value);
|
|
|
|
return;
|
|
|
|
}
|
2014-07-09 20:13:36 +00:00
|
|
|
if (!strcmp(key, "tts")) {
|
|
|
|
sample->tts = get_duration(value);
|
|
|
|
return;
|
|
|
|
}
|
2014-03-09 21:26:39 +00:00
|
|
|
if (!strcmp(key, "in_deco")) {
|
|
|
|
sample->in_deco = atoi(value);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!strcmp(key, "stoptime")) {
|
|
|
|
sample->stoptime = get_duration(value);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!strcmp(key, "stopdepth")) {
|
|
|
|
sample->stopdepth = get_depth(value);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!strcmp(key, "cns")) {
|
|
|
|
sample->cns = atoi(value);
|
|
|
|
return;
|
|
|
|
}
|
2015-07-22 15:23:20 +00:00
|
|
|
|
|
|
|
if (!strcmp(key, "rbt")) {
|
|
|
|
sample->rbt = get_duration(value);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-09 21:26:39 +00:00
|
|
|
if (!strcmp(key, "po2")) {
|
|
|
|
pressure_t p = get_pressure(value);
|
2014-10-19 14:07:07 +00:00
|
|
|
sample->setpoint.mbar = p.mbar;
|
2014-03-09 21:26:39 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-11-22 13:13:13 +00:00
|
|
|
if (!strcmp(key, "sensor1")) {
|
|
|
|
pressure_t p = get_pressure(value);
|
|
|
|
sample->o2sensor[0].mbar = p.mbar;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!strcmp(key, "sensor2")) {
|
|
|
|
pressure_t p = get_pressure(value);
|
|
|
|
sample->o2sensor[1].mbar = p.mbar;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!strcmp(key, "sensor3")) {
|
|
|
|
pressure_t p = get_pressure(value);
|
|
|
|
sample->o2sensor[2].mbar = p.mbar;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!strcmp(key, "o2pressure")) {
|
|
|
|
pressure_t p = get_pressure(value);
|
2017-07-20 21:39:02 +00:00
|
|
|
sample->pressure[1].mbar = p.mbar;
|
2014-11-22 13:13:13 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-03-09 21:26:39 +00:00
|
|
|
if (!strcmp(key, "heartbeat")) {
|
|
|
|
sample->heartbeat = atoi(value);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!strcmp(key, "bearing")) {
|
2014-06-03 17:21:41 +00:00
|
|
|
sample->bearing.degrees = atoi(value);
|
2014-03-09 21:26:39 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-11-22 13:13:13 +00:00
|
|
|
|
2014-03-09 21:45:20 +00:00
|
|
|
report_error("Unexpected sample key/value pair (%s/%s)", key, value);
|
2014-03-09 20:13:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static char *parse_sample_unit(struct sample *sample, double val, char *unit)
|
|
|
|
{
|
Add support for loading and saving multiple pressure samples
This does both the XML and the git save format, because the changes
really are the same, even if the actual format differs in some details.
See how the two "save_samples()" routines both do the same basic setup,
for example.
This is fairly straightforward, with the possible exception of the odd
sensor = sample->sensor[0];
default in the git pressure loading code.
That line just means that if we do *not* have an explicit cylinder index
for the pressure reading, we will always end up filling in the new
pressure as the first pressure (because the cylinder index will match the
first sensor slot).
So that makes the "add_sample_pressure()" case always do the same thing it
used to do for the legacy case: fill in the first slot. The actual sensor
index may later change, since the legacy format has a "sensor=X" key value
pair that sets the sensor, but it will also use the first sensor slot,
making it all do exactly what it used to do.
And on the other hand, if we're loading new-style data with cylinder
pressure and sensor index together, we just end up using the new semantics
for add_sample_pressure(), which tries to keep the same slot for the same
sensor, but does the right thing if we already have other pressure values.
The XML code has no such issues at all, since it can't share the cases
anyway, and we need to have different node names for the different sensor
values and cannot just have multiple "pressure" entries. Have I mentioned
how much I despise XML lately?
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2017-07-26 02:10:03 +00:00
|
|
|
unsigned int sensor;
|
2014-03-09 20:13:48 +00:00
|
|
|
char *end = unit, c;
|
|
|
|
|
|
|
|
/* Skip over the unit */
|
|
|
|
while ((c = *end) != 0) {
|
|
|
|
if (isspace(c)) {
|
|
|
|
*end++ = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
end++;
|
|
|
|
}
|
|
|
|
|
2014-03-10 04:12:13 +00:00
|
|
|
/* The units are "°C", "m" or "bar", so let's just look at the first character */
|
Add support for loading and saving multiple pressure samples
This does both the XML and the git save format, because the changes
really are the same, even if the actual format differs in some details.
See how the two "save_samples()" routines both do the same basic setup,
for example.
This is fairly straightforward, with the possible exception of the odd
sensor = sample->sensor[0];
default in the git pressure loading code.
That line just means that if we do *not* have an explicit cylinder index
for the pressure reading, we will always end up filling in the new
pressure as the first pressure (because the cylinder index will match the
first sensor slot).
So that makes the "add_sample_pressure()" case always do the same thing it
used to do for the legacy case: fill in the first slot. The actual sensor
index may later change, since the legacy format has a "sensor=X" key value
pair that sets the sensor, but it will also use the first sensor slot,
making it all do exactly what it used to do.
And on the other hand, if we're loading new-style data with cylinder
pressure and sensor index together, we just end up using the new semantics
for add_sample_pressure(), which tries to keep the same slot for the same
sensor, but does the right thing if we already have other pressure values.
The XML code has no such issues at all, since it can't share the cases
anyway, and we need to have different node names for the different sensor
values and cannot just have multiple "pressure" entries. Have I mentioned
how much I despise XML lately?
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2017-07-26 02:10:03 +00:00
|
|
|
/* The cylinder pressure may also be of the form '123.0bar:4' to indicate sensor */
|
2014-03-09 20:13:48 +00:00
|
|
|
switch (*unit) {
|
|
|
|
case 'm':
|
2019-07-24 22:26:51 +00:00
|
|
|
sample->depth.mm = lrint(1000 * val);
|
2014-03-09 20:13:48 +00:00
|
|
|
break;
|
|
|
|
case 'b':
|
Add support for loading and saving multiple pressure samples
This does both the XML and the git save format, because the changes
really are the same, even if the actual format differs in some details.
See how the two "save_samples()" routines both do the same basic setup,
for example.
This is fairly straightforward, with the possible exception of the odd
sensor = sample->sensor[0];
default in the git pressure loading code.
That line just means that if we do *not* have an explicit cylinder index
for the pressure reading, we will always end up filling in the new
pressure as the first pressure (because the cylinder index will match the
first sensor slot).
So that makes the "add_sample_pressure()" case always do the same thing it
used to do for the legacy case: fill in the first slot. The actual sensor
index may later change, since the legacy format has a "sensor=X" key value
pair that sets the sensor, but it will also use the first sensor slot,
making it all do exactly what it used to do.
And on the other hand, if we're loading new-style data with cylinder
pressure and sensor index together, we just end up using the new semantics
for add_sample_pressure(), which tries to keep the same slot for the same
sensor, but does the right thing if we already have other pressure values.
The XML code has no such issues at all, since it can't share the cases
anyway, and we need to have different node names for the different sensor
values and cannot just have multiple "pressure" entries. Have I mentioned
how much I despise XML lately?
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2017-07-26 02:10:03 +00:00
|
|
|
sensor = sample->sensor[0];
|
2019-07-24 22:26:51 +00:00
|
|
|
if (end > unit + 4 && unit[3] == ':')
|
|
|
|
sensor = atoi(unit + 4);
|
|
|
|
add_sample_pressure(sample, sensor, lrint(1000 * val));
|
2014-03-09 20:13:48 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
sample->temperature.mkelvin = C_to_mkelvin(val);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return end;
|
|
|
|
}
|
|
|
|
|
2014-03-09 21:26:39 +00:00
|
|
|
/*
|
|
|
|
* By default the sample data does not change unless the
|
|
|
|
* save-file gives an explicit new value. So we copy the
|
|
|
|
* data from the previous sample if one exists, and then
|
|
|
|
* the parsing will update it as necessary.
|
|
|
|
*
|
|
|
|
* There are a few exceptions, like the sample pressure:
|
|
|
|
* missing sample pressure doesn't mean "same as last
|
|
|
|
* time", but "interpolate". We clear those ones
|
|
|
|
* explicitly.
|
Fix up o2 pressure sensor handling at load time
Because of how we traditionally did things, the "o2pressure" parsing
depends on implicitly setting the sensor index to the last cylinder that
was marked as being used for oxygen.
We also always defaulted the primary sensor (which is used for the
diluent tank for CCR) to cylinder 0, but that doesn't work when the
oxygen tank is cylinder 0.
This gets that right at file loading time, and unifies the xml and git
sample parsing to make them match. The new defaults are:
- unless anything else is explicitly specified, the primary sensor is
associated with the first tank, and the secondary sensor is
associated with the second tank
- if we're a CCR dive, and have an explicit oxygen tank, we associate
the secondary sensor with that oxygen cylinder. The primary sensor
will be switched over to the second cylinder if the oxygen cylinder
is the first one.
This may sound backwards, but matches our traditional behavior where
the O2 pressure was the secondary pressure.
This is definitely not pretty, but it gets our historical files working
right, and is at least reasonably sensible.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2017-07-21 20:37:34 +00:00
|
|
|
*
|
|
|
|
* NOTE! We default sensor use to 0, 1 respetively for
|
|
|
|
* the two sensors, but for CCR dives with explicit
|
|
|
|
* OXYGEN bottles we set the secondary sensor to that.
|
|
|
|
* Then the primary sensor will be either the first
|
|
|
|
* or the second cylinder depending on what isn't an
|
|
|
|
* oxygen cylinder.
|
2014-03-09 21:26:39 +00:00
|
|
|
*/
|
2019-07-24 21:54:46 +00:00
|
|
|
static struct sample *new_sample(struct git_parser_state *state)
|
2014-03-09 21:26:39 +00:00
|
|
|
{
|
2019-07-24 21:54:46 +00:00
|
|
|
struct sample *sample = prepare_sample(state->active_dc);
|
|
|
|
if (sample != state->active_dc->sample) {
|
|
|
|
memcpy(sample, sample - 1, sizeof(struct sample));
|
2017-07-20 21:39:02 +00:00
|
|
|
sample->pressure[0].mbar = 0;
|
Fix up o2 pressure sensor handling at load time
Because of how we traditionally did things, the "o2pressure" parsing
depends on implicitly setting the sensor index to the last cylinder that
was marked as being used for oxygen.
We also always defaulted the primary sensor (which is used for the
diluent tank for CCR) to cylinder 0, but that doesn't work when the
oxygen tank is cylinder 0.
This gets that right at file loading time, and unifies the xml and git
sample parsing to make them match. The new defaults are:
- unless anything else is explicitly specified, the primary sensor is
associated with the first tank, and the secondary sensor is
associated with the second tank
- if we're a CCR dive, and have an explicit oxygen tank, we associate
the secondary sensor with that oxygen cylinder. The primary sensor
will be switched over to the second cylinder if the oxygen cylinder
is the first one.
This may sound backwards, but matches our traditional behavior where
the O2 pressure was the secondary pressure.
This is definitely not pretty, but it gets our historical files working
right, and is at least reasonably sensible.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2017-07-21 20:37:34 +00:00
|
|
|
sample->pressure[1].mbar = 0;
|
|
|
|
} else {
|
2019-07-24 21:54:46 +00:00
|
|
|
sample->sensor[0] = !state->o2pressure_sensor;
|
|
|
|
sample->sensor[1] = state->o2pressure_sensor;
|
2014-03-09 21:26:39 +00:00
|
|
|
}
|
|
|
|
return sample;
|
|
|
|
}
|
|
|
|
|
2019-07-24 21:54:46 +00:00
|
|
|
static void sample_parser(char *line, struct git_parser_state *state)
|
2014-03-08 23:59:39 +00:00
|
|
|
{
|
2014-03-10 04:12:13 +00:00
|
|
|
int m, s = 0;
|
2019-07-24 21:54:46 +00:00
|
|
|
struct sample *sample = new_sample(state);
|
2014-03-09 20:13:48 +00:00
|
|
|
|
|
|
|
m = strtol(line, &line, 10);
|
|
|
|
if (*line == ':')
|
2019-07-24 22:26:51 +00:00
|
|
|
s = strtol(line + 1, &line, 10);
|
|
|
|
sample->time.seconds = m * 60 + s;
|
2014-03-09 20:13:48 +00:00
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
char c;
|
|
|
|
|
|
|
|
while (isspace(c = *line))
|
|
|
|
line++;
|
|
|
|
if (!c)
|
|
|
|
break;
|
|
|
|
/* Less common sample entries have a name */
|
|
|
|
if (c >= 'a' && c <= 'z') {
|
load-git: clean up string handling during parsing
We had some fairly obscure rules for how strings were parsed, and it
actually caused bugs when the same line had multiple strings in it.
That normally doesn't happen, and the cases where it was _supposed_ to
happen had special cases for it (divecomputer ID lines, and tag lines).
But by mistake, we had introduced a case of that for the event line
handling in commit b9174332d ("Read and write divemode changes (xml and
git)"), and nobody realized that the divemode string addition meant that
"oops, now it's corrupting the event name". An event line could look
like this:
event 40:00 type=8 divemode="OC" name="modechange"
where we now had both that "OC" and "modechange" strings, and the code
to pick the name just picked the first string. So we'd end up
effectively mis-parsing the above line as
event 40:00 type=8 divemode="OC" name="OC"
which is obviously wrong.
The dive mode didn't really need to be a string in the first place
(there is nothing to quote, and no spaces in it), but hey, here we are.
We can't just magially fix the existing broken saves.
So make it more straightforward to handle strings in the git format line
parser. We still stash the different decoded strings together in one
special memory buffer, but now the parser helpers automatically untangle
it as they traverse the key value pairs.
This is still overly subtle code, and it doesn't fix the cases where
we've saved the wrong data back. That comes later.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2020-02-14 19:49:28 +00:00
|
|
|
line = parse_keyvalue_entry(parse_sample_keyvalue, sample, line, NULL);
|
2014-03-09 20:13:48 +00:00
|
|
|
} else {
|
|
|
|
const char *end;
|
|
|
|
double val = ascii_strtod(line, &end);
|
|
|
|
if (end == line) {
|
|
|
|
report_error("Odd sample data: %s", line);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
line = (char *)end;
|
|
|
|
line = parse_sample_unit(sample, val, line);
|
|
|
|
}
|
|
|
|
}
|
2019-07-24 21:54:46 +00:00
|
|
|
finish_sample(state->active_dc);
|
2014-03-09 19:19:41 +00:00
|
|
|
}
|
|
|
|
|
2019-07-24 21:54:46 +00:00
|
|
|
static void parse_dc_airtemp(char *line, struct membuffer *str, struct git_parser_state *state)
|
|
|
|
{ UNUSED(str); state->active_dc->airtemp = get_temperature(line); }
|
2014-03-09 19:19:41 +00:00
|
|
|
|
2019-07-24 21:54:46 +00:00
|
|
|
static void parse_dc_date(char *line, struct membuffer *str, struct git_parser_state *state)
|
|
|
|
{ UNUSED(str); update_date(&state->active_dc->when, line); }
|
2014-03-09 19:19:41 +00:00
|
|
|
|
2019-07-24 21:54:46 +00:00
|
|
|
static void parse_dc_deviceid(char *line, struct membuffer *str, struct git_parser_state *state)
|
|
|
|
{ UNUSED(str); set_dc_deviceid(state->active_dc, get_hex(line)); }
|
2014-03-09 19:19:41 +00:00
|
|
|
|
2019-07-24 21:54:46 +00:00
|
|
|
static void parse_dc_diveid(char *line, struct membuffer *str, struct git_parser_state *state)
|
|
|
|
{ UNUSED(str); state->active_dc->diveid = get_hex(line); }
|
2014-03-09 19:19:41 +00:00
|
|
|
|
2019-07-24 21:54:46 +00:00
|
|
|
static void parse_dc_duration(char *line, struct membuffer *str, struct git_parser_state *state)
|
|
|
|
{ UNUSED(str); state->active_dc->duration = get_duration(line); }
|
2014-03-09 19:19:41 +00:00
|
|
|
|
2019-07-24 21:54:46 +00:00
|
|
|
static void parse_dc_dctype(char *line, struct membuffer *str, struct git_parser_state *state)
|
|
|
|
{ UNUSED(str); state->active_dc->divemode = get_dctype(line); }
|
2014-11-16 23:11:18 +00:00
|
|
|
|
2019-07-24 21:54:46 +00:00
|
|
|
static void parse_dc_lastmanualtime(char *line, struct membuffer *str, struct git_parser_state *state)
|
|
|
|
{ UNUSED(str); state->active_dc->last_manual_time = get_duration(line); }
|
Improve profile display in planner
This patch allows the planner to save the last manually-entered
dive planner point of a dive plan. When the plan has been saved
and re-opened for edit, the time of the last-entered dive planner
point is used to ensure that dive planning continues from the same
point in the profile as was when the original dive plan was saved.
Mechanism:
1) In dive.h, create a new dc attribute dc->last_manual_time
with data type of duration_t.
2) In diveplanner.c, ensure that the last manually-entered
dive planner point is saved in dc->last_manual_time.
3) In save-xml.c, create a new XML attribute for the <divecomputer>
element, named last-manual-time. For dive plans, the element would
now look like:
<divecomputer model='planned dive' last-manual-time='31:17 min'>
4) In parse-xml.c, insert code that recognises the last-manual-time
XML attribute, reads the time value and assigns this time to
dc->last_manual_time.
5) In diveplannermodel.cpp, method DiveplannerPointModel::loadfromdive,
insert code that sets the appropriate boolean value to dp->entered
by comparing newtime (i.e. time of dp) with dc->last_manual_time.
6) Diveplannermodel.cpp also accepts profile data from normal dives in
the dive log, whether hand-entered or loaded from dive computer. It
looks like the reduction of dive points for dives with >100 points
continues to work ok.
The result is that when a dive plan is saved with manually entered
points up to e.g. 10 minutes into the dive, it can be re-opened for edit
in the dive planner and the planner re-creates the plan with manually
entered points up to 10 minutes. The rest of the points are "soft"
points, shaped by the deco calculations of the planner.
Improvements: Improve code for profile display in dive planner
This responds to #1052.
Change load-git.c and save-git.c so that the last-manual-time is
also saved in the git-format dive log.
Several stylistic changes in text for consistent C source code.
Improvement of dive planner profile display:
Do some simplification of my alterations to diveplannermodel.cpp
Two small style changes in planner.c and diveplannermodel.cpp
as requested ny @neolit123
Signed-off-by: Willem Ferguson <willemferguson@zoology.up.ac.za>
2018-01-15 12:51:47 +00:00
|
|
|
|
2019-07-24 21:54:46 +00:00
|
|
|
static void parse_dc_maxdepth(char *line, struct membuffer *str, struct git_parser_state *state)
|
|
|
|
{ UNUSED(str); state->active_dc->maxdepth = get_depth(line); }
|
2014-03-09 19:19:41 +00:00
|
|
|
|
2019-07-24 21:54:46 +00:00
|
|
|
static void parse_dc_meandepth(char *line, struct membuffer *str, struct git_parser_state *state)
|
|
|
|
{ UNUSED(str); state->active_dc->meandepth = get_depth(line); }
|
2014-03-09 19:19:41 +00:00
|
|
|
|
2019-07-24 21:54:46 +00:00
|
|
|
static void parse_dc_model(char *line, struct membuffer *str, struct git_parser_state *state)
|
load-git: clean up string handling during parsing
We had some fairly obscure rules for how strings were parsed, and it
actually caused bugs when the same line had multiple strings in it.
That normally doesn't happen, and the cases where it was _supposed_ to
happen had special cases for it (divecomputer ID lines, and tag lines).
But by mistake, we had introduced a case of that for the event line
handling in commit b9174332d ("Read and write divemode changes (xml and
git)"), and nobody realized that the divemode string addition meant that
"oops, now it's corrupting the event name". An event line could look
like this:
event 40:00 type=8 divemode="OC" name="modechange"
where we now had both that "OC" and "modechange" strings, and the code
to pick the name just picked the first string. So we'd end up
effectively mis-parsing the above line as
event 40:00 type=8 divemode="OC" name="OC"
which is obviously wrong.
The dive mode didn't really need to be a string in the first place
(there is nothing to quote, and no spaces in it), but hey, here we are.
We can't just magially fix the existing broken saves.
So make it more straightforward to handle strings in the git format line
parser. We still stash the different decoded strings together in one
special memory buffer, but now the parser helpers automatically untangle
it as they traverse the key value pairs.
This is still overly subtle code, and it doesn't fix the cases where
we've saved the wrong data back. That comes later.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2020-02-14 19:49:28 +00:00
|
|
|
{ UNUSED(line); state->active_dc->model = detach_cstring(str); }
|
2014-03-09 19:19:41 +00:00
|
|
|
|
2019-07-24 21:54:46 +00:00
|
|
|
static void parse_dc_numberofoxygensensors(char *line, struct membuffer *str, struct git_parser_state *state)
|
|
|
|
{ UNUSED(str); state->active_dc->no_o2sensors = get_index(line); }
|
2014-11-22 13:13:13 +00:00
|
|
|
|
2019-07-24 21:54:46 +00:00
|
|
|
static void parse_dc_surfacepressure(char *line, struct membuffer *str, struct git_parser_state *state)
|
|
|
|
{ UNUSED(str); state->active_dc->surface_pressure = get_pressure(line); }
|
2014-03-09 19:19:41 +00:00
|
|
|
|
2019-07-24 21:54:46 +00:00
|
|
|
static void parse_dc_salinity(char *line, struct membuffer *str, struct git_parser_state *state)
|
|
|
|
{ UNUSED(str); state->active_dc->salinity = get_salinity(line); }
|
2014-03-10 03:55:29 +00:00
|
|
|
|
2019-07-24 21:54:46 +00:00
|
|
|
static void parse_dc_surfacetime(char *line, struct membuffer *str, struct git_parser_state *state)
|
|
|
|
{ UNUSED(str); state->active_dc->surfacetime = get_duration(line); }
|
2014-03-09 19:19:41 +00:00
|
|
|
|
2019-07-24 21:54:46 +00:00
|
|
|
static void parse_dc_time(char *line, struct membuffer *str, struct git_parser_state *state)
|
|
|
|
{ UNUSED(str); update_time(&state->active_dc->when, line); }
|
2014-03-09 19:19:41 +00:00
|
|
|
|
2019-07-24 21:54:46 +00:00
|
|
|
static void parse_dc_watertemp(char *line, struct membuffer *str, struct git_parser_state *state)
|
|
|
|
{ UNUSED(str); state->active_dc->watertemp = get_temperature(line); }
|
2014-03-09 19:19:41 +00:00
|
|
|
|
2018-04-07 12:56:37 +00:00
|
|
|
|
2019-07-24 21:17:26 +00:00
|
|
|
static int get_divemode(const char *divemodestring) {
|
2018-05-08 14:24:51 +00:00
|
|
|
for (int i = 0; i < NUM_DIVEMODE; i++) {
|
2018-04-07 12:56:37 +00:00
|
|
|
if (!strcmp(divemodestring, divemode_text[i]))
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
load-git: clean up string handling during parsing
We had some fairly obscure rules for how strings were parsed, and it
actually caused bugs when the same line had multiple strings in it.
That normally doesn't happen, and the cases where it was _supposed_ to
happen had special cases for it (divecomputer ID lines, and tag lines).
But by mistake, we had introduced a case of that for the event line
handling in commit b9174332d ("Read and write divemode changes (xml and
git)"), and nobody realized that the divemode string addition meant that
"oops, now it's corrupting the event name". An event line could look
like this:
event 40:00 type=8 divemode="OC" name="modechange"
where we now had both that "OC" and "modechange" strings, and the code
to pick the name just picked the first string. So we'd end up
effectively mis-parsing the above line as
event 40:00 type=8 divemode="OC" name="OC"
which is obviously wrong.
The dive mode didn't really need to be a string in the first place
(there is nothing to quote, and no spaces in it), but hey, here we are.
We can't just magially fix the existing broken saves.
So make it more straightforward to handle strings in the git format line
parser. We still stash the different decoded strings together in one
special memory buffer, but now the parser helpers automatically untangle
it as they traverse the key value pairs.
This is still overly subtle code, and it doesn't fix the cases where
we've saved the wrong data back. That comes later.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2020-02-14 19:49:28 +00:00
|
|
|
/*
|
|
|
|
* A 'struct event' has a variable-sized name allocation at the
|
|
|
|
* end. So when we parse the event data, we can't fill in the
|
|
|
|
* event directly, because we don't know how to allocate one
|
|
|
|
* before we have the size of the name.
|
|
|
|
*
|
|
|
|
* Thus this initial 'parse_event' with a separate name pointer.
|
|
|
|
*/
|
|
|
|
struct parse_event {
|
|
|
|
const char *name;
|
2020-02-14 20:08:43 +00:00
|
|
|
int has_divemode;
|
2020-04-12 10:40:12 +00:00
|
|
|
struct event ev;
|
load-git: clean up string handling during parsing
We had some fairly obscure rules for how strings were parsed, and it
actually caused bugs when the same line had multiple strings in it.
That normally doesn't happen, and the cases where it was _supposed_ to
happen had special cases for it (divecomputer ID lines, and tag lines).
But by mistake, we had introduced a case of that for the event line
handling in commit b9174332d ("Read and write divemode changes (xml and
git)"), and nobody realized that the divemode string addition meant that
"oops, now it's corrupting the event name". An event line could look
like this:
event 40:00 type=8 divemode="OC" name="modechange"
where we now had both that "OC" and "modechange" strings, and the code
to pick the name just picked the first string. So we'd end up
effectively mis-parsing the above line as
event 40:00 type=8 divemode="OC" name="OC"
which is obviously wrong.
The dive mode didn't really need to be a string in the first place
(there is nothing to quote, and no spaces in it), but hey, here we are.
We can't just magially fix the existing broken saves.
So make it more straightforward to handle strings in the git format line
parser. We still stash the different decoded strings together in one
special memory buffer, but now the parser helpers automatically untangle
it as they traverse the key value pairs.
This is still overly subtle code, and it doesn't fix the cases where
we've saved the wrong data back. That comes later.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2020-02-14 19:49:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static void parse_event_keyvalue(void *_parse, const char *key, const char *value)
|
Add event parsing to the git object tree loader
This makes us parse everything we save, and I can load my XML file, save
it as a git file, load that git file, save it as a new XML file, and the
end result is identical.
Well... *ALMOST* identical. We currently don't save the dive computer
nickname and serial/firmware information in the git repository, so that
does get lost in translation. But all the actual dive data is there.
NOTE! I have currently only worked with my own dive files. They are
reasonably complex and complete, and do have a lot of the interesting
cases covered (like multiple dive computers etc), but there's no CCR
information, and all the dives are in trips, so this does need more
testing. It's at the very least very close.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2014-03-09 22:32:42 +00:00
|
|
|
{
|
load-git: clean up string handling during parsing
We had some fairly obscure rules for how strings were parsed, and it
actually caused bugs when the same line had multiple strings in it.
That normally doesn't happen, and the cases where it was _supposed_ to
happen had special cases for it (divecomputer ID lines, and tag lines).
But by mistake, we had introduced a case of that for the event line
handling in commit b9174332d ("Read and write divemode changes (xml and
git)"), and nobody realized that the divemode string addition meant that
"oops, now it's corrupting the event name". An event line could look
like this:
event 40:00 type=8 divemode="OC" name="modechange"
where we now had both that "OC" and "modechange" strings, and the code
to pick the name just picked the first string. So we'd end up
effectively mis-parsing the above line as
event 40:00 type=8 divemode="OC" name="OC"
which is obviously wrong.
The dive mode didn't really need to be a string in the first place
(there is nothing to quote, and no spaces in it), but hey, here we are.
We can't just magially fix the existing broken saves.
So make it more straightforward to handle strings in the git format line
parser. We still stash the different decoded strings together in one
special memory buffer, but now the parser helpers automatically untangle
it as they traverse the key value pairs.
This is still overly subtle code, and it doesn't fix the cases where
we've saved the wrong data back. That comes later.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2020-02-14 19:49:28 +00:00
|
|
|
struct parse_event *parse = _parse;
|
Add event parsing to the git object tree loader
This makes us parse everything we save, and I can load my XML file, save
it as a git file, load that git file, save it as a new XML file, and the
end result is identical.
Well... *ALMOST* identical. We currently don't save the dive computer
nickname and serial/firmware information in the git repository, so that
does get lost in translation. But all the actual dive data is there.
NOTE! I have currently only worked with my own dive files. They are
reasonably complex and complete, and do have a lot of the interesting
cases covered (like multiple dive computers etc), but there's no CCR
information, and all the dives are in trips, so this does need more
testing. It's at the very least very close.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2014-03-09 22:32:42 +00:00
|
|
|
int val = atoi(value);
|
|
|
|
|
|
|
|
if (!strcmp(key, "type")) {
|
load-git: clean up string handling during parsing
We had some fairly obscure rules for how strings were parsed, and it
actually caused bugs when the same line had multiple strings in it.
That normally doesn't happen, and the cases where it was _supposed_ to
happen had special cases for it (divecomputer ID lines, and tag lines).
But by mistake, we had introduced a case of that for the event line
handling in commit b9174332d ("Read and write divemode changes (xml and
git)"), and nobody realized that the divemode string addition meant that
"oops, now it's corrupting the event name". An event line could look
like this:
event 40:00 type=8 divemode="OC" name="modechange"
where we now had both that "OC" and "modechange" strings, and the code
to pick the name just picked the first string. So we'd end up
effectively mis-parsing the above line as
event 40:00 type=8 divemode="OC" name="OC"
which is obviously wrong.
The dive mode didn't really need to be a string in the first place
(there is nothing to quote, and no spaces in it), but hey, here we are.
We can't just magially fix the existing broken saves.
So make it more straightforward to handle strings in the git format line
parser. We still stash the different decoded strings together in one
special memory buffer, but now the parser helpers automatically untangle
it as they traverse the key value pairs.
This is still overly subtle code, and it doesn't fix the cases where
we've saved the wrong data back. That comes later.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2020-02-14 19:49:28 +00:00
|
|
|
parse->ev.type = val;
|
Add event parsing to the git object tree loader
This makes us parse everything we save, and I can load my XML file, save
it as a git file, load that git file, save it as a new XML file, and the
end result is identical.
Well... *ALMOST* identical. We currently don't save the dive computer
nickname and serial/firmware information in the git repository, so that
does get lost in translation. But all the actual dive data is there.
NOTE! I have currently only worked with my own dive files. They are
reasonably complex and complete, and do have a lot of the interesting
cases covered (like multiple dive computers etc), but there's no CCR
information, and all the dives are in trips, so this does need more
testing. It's at the very least very close.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2014-03-09 22:32:42 +00:00
|
|
|
} else if (!strcmp(key, "flags")) {
|
load-git: clean up string handling during parsing
We had some fairly obscure rules for how strings were parsed, and it
actually caused bugs when the same line had multiple strings in it.
That normally doesn't happen, and the cases where it was _supposed_ to
happen had special cases for it (divecomputer ID lines, and tag lines).
But by mistake, we had introduced a case of that for the event line
handling in commit b9174332d ("Read and write divemode changes (xml and
git)"), and nobody realized that the divemode string addition meant that
"oops, now it's corrupting the event name". An event line could look
like this:
event 40:00 type=8 divemode="OC" name="modechange"
where we now had both that "OC" and "modechange" strings, and the code
to pick the name just picked the first string. So we'd end up
effectively mis-parsing the above line as
event 40:00 type=8 divemode="OC" name="OC"
which is obviously wrong.
The dive mode didn't really need to be a string in the first place
(there is nothing to quote, and no spaces in it), but hey, here we are.
We can't just magially fix the existing broken saves.
So make it more straightforward to handle strings in the git format line
parser. We still stash the different decoded strings together in one
special memory buffer, but now the parser helpers automatically untangle
it as they traverse the key value pairs.
This is still overly subtle code, and it doesn't fix the cases where
we've saved the wrong data back. That comes later.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2020-02-14 19:49:28 +00:00
|
|
|
parse->ev.flags = val;
|
Add event parsing to the git object tree loader
This makes us parse everything we save, and I can load my XML file, save
it as a git file, load that git file, save it as a new XML file, and the
end result is identical.
Well... *ALMOST* identical. We currently don't save the dive computer
nickname and serial/firmware information in the git repository, so that
does get lost in translation. But all the actual dive data is there.
NOTE! I have currently only worked with my own dive files. They are
reasonably complex and complete, and do have a lot of the interesting
cases covered (like multiple dive computers etc), but there's no CCR
information, and all the dives are in trips, so this does need more
testing. It's at the very least very close.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2014-03-09 22:32:42 +00:00
|
|
|
} else if (!strcmp(key, "value")) {
|
load-git: clean up string handling during parsing
We had some fairly obscure rules for how strings were parsed, and it
actually caused bugs when the same line had multiple strings in it.
That normally doesn't happen, and the cases where it was _supposed_ to
happen had special cases for it (divecomputer ID lines, and tag lines).
But by mistake, we had introduced a case of that for the event line
handling in commit b9174332d ("Read and write divemode changes (xml and
git)"), and nobody realized that the divemode string addition meant that
"oops, now it's corrupting the event name". An event line could look
like this:
event 40:00 type=8 divemode="OC" name="modechange"
where we now had both that "OC" and "modechange" strings, and the code
to pick the name just picked the first string. So we'd end up
effectively mis-parsing the above line as
event 40:00 type=8 divemode="OC" name="OC"
which is obviously wrong.
The dive mode didn't really need to be a string in the first place
(there is nothing to quote, and no spaces in it), but hey, here we are.
We can't just magially fix the existing broken saves.
So make it more straightforward to handle strings in the git format line
parser. We still stash the different decoded strings together in one
special memory buffer, but now the parser helpers automatically untangle
it as they traverse the key value pairs.
This is still overly subtle code, and it doesn't fix the cases where
we've saved the wrong data back. That comes later.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2020-02-14 19:49:28 +00:00
|
|
|
parse->ev.value = val;
|
Add event parsing to the git object tree loader
This makes us parse everything we save, and I can load my XML file, save
it as a git file, load that git file, save it as a new XML file, and the
end result is identical.
Well... *ALMOST* identical. We currently don't save the dive computer
nickname and serial/firmware information in the git repository, so that
does get lost in translation. But all the actual dive data is there.
NOTE! I have currently only worked with my own dive files. They are
reasonably complex and complete, and do have a lot of the interesting
cases covered (like multiple dive computers etc), but there's no CCR
information, and all the dives are in trips, so this does need more
testing. It's at the very least very close.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2014-03-09 22:32:42 +00:00
|
|
|
} else if (!strcmp(key, "name")) {
|
load-git: clean up string handling during parsing
We had some fairly obscure rules for how strings were parsed, and it
actually caused bugs when the same line had multiple strings in it.
That normally doesn't happen, and the cases where it was _supposed_ to
happen had special cases for it (divecomputer ID lines, and tag lines).
But by mistake, we had introduced a case of that for the event line
handling in commit b9174332d ("Read and write divemode changes (xml and
git)"), and nobody realized that the divemode string addition meant that
"oops, now it's corrupting the event name". An event line could look
like this:
event 40:00 type=8 divemode="OC" name="modechange"
where we now had both that "OC" and "modechange" strings, and the code
to pick the name just picked the first string. So we'd end up
effectively mis-parsing the above line as
event 40:00 type=8 divemode="OC" name="OC"
which is obviously wrong.
The dive mode didn't really need to be a string in the first place
(there is nothing to quote, and no spaces in it), but hey, here we are.
We can't just magially fix the existing broken saves.
So make it more straightforward to handle strings in the git format line
parser. We still stash the different decoded strings together in one
special memory buffer, but now the parser helpers automatically untangle
it as they traverse the key value pairs.
This is still overly subtle code, and it doesn't fix the cases where
we've saved the wrong data back. That comes later.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2020-02-14 19:49:28 +00:00
|
|
|
parse->name = value;
|
2018-04-07 12:56:37 +00:00
|
|
|
} else if (!strcmp(key,"divemode")) {
|
load-git: clean up string handling during parsing
We had some fairly obscure rules for how strings were parsed, and it
actually caused bugs when the same line had multiple strings in it.
That normally doesn't happen, and the cases where it was _supposed_ to
happen had special cases for it (divecomputer ID lines, and tag lines).
But by mistake, we had introduced a case of that for the event line
handling in commit b9174332d ("Read and write divemode changes (xml and
git)"), and nobody realized that the divemode string addition meant that
"oops, now it's corrupting the event name". An event line could look
like this:
event 40:00 type=8 divemode="OC" name="modechange"
where we now had both that "OC" and "modechange" strings, and the code
to pick the name just picked the first string. So we'd end up
effectively mis-parsing the above line as
event 40:00 type=8 divemode="OC" name="OC"
which is obviously wrong.
The dive mode didn't really need to be a string in the first place
(there is nothing to quote, and no spaces in it), but hey, here we are.
We can't just magially fix the existing broken saves.
So make it more straightforward to handle strings in the git format line
parser. We still stash the different decoded strings together in one
special memory buffer, but now the parser helpers automatically untangle
it as they traverse the key value pairs.
This is still overly subtle code, and it doesn't fix the cases where
we've saved the wrong data back. That comes later.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2020-02-14 19:49:28 +00:00
|
|
|
parse->ev.value = get_divemode(value);
|
2020-02-14 20:08:43 +00:00
|
|
|
parse->has_divemode = 1;
|
2014-08-17 18:26:21 +00:00
|
|
|
} else if (!strcmp(key, "cylinder")) {
|
|
|
|
/* NOTE! We add one here as a marker that "yes, we got a cylinder index" */
|
load-git: clean up string handling during parsing
We had some fairly obscure rules for how strings were parsed, and it
actually caused bugs when the same line had multiple strings in it.
That normally doesn't happen, and the cases where it was _supposed_ to
happen had special cases for it (divecomputer ID lines, and tag lines).
But by mistake, we had introduced a case of that for the event line
handling in commit b9174332d ("Read and write divemode changes (xml and
git)"), and nobody realized that the divemode string addition meant that
"oops, now it's corrupting the event name". An event line could look
like this:
event 40:00 type=8 divemode="OC" name="modechange"
where we now had both that "OC" and "modechange" strings, and the code
to pick the name just picked the first string. So we'd end up
effectively mis-parsing the above line as
event 40:00 type=8 divemode="OC" name="OC"
which is obviously wrong.
The dive mode didn't really need to be a string in the first place
(there is nothing to quote, and no spaces in it), but hey, here we are.
We can't just magially fix the existing broken saves.
So make it more straightforward to handle strings in the git format line
parser. We still stash the different decoded strings together in one
special memory buffer, but now the parser helpers automatically untangle
it as they traverse the key value pairs.
This is still overly subtle code, and it doesn't fix the cases where
we've saved the wrong data back. That comes later.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2020-02-14 19:49:28 +00:00
|
|
|
parse->ev.gas.index = 1 + get_index(value);
|
2014-08-17 18:26:21 +00:00
|
|
|
} else if (!strcmp(key, "o2")) {
|
load-git: clean up string handling during parsing
We had some fairly obscure rules for how strings were parsed, and it
actually caused bugs when the same line had multiple strings in it.
That normally doesn't happen, and the cases where it was _supposed_ to
happen had special cases for it (divecomputer ID lines, and tag lines).
But by mistake, we had introduced a case of that for the event line
handling in commit b9174332d ("Read and write divemode changes (xml and
git)"), and nobody realized that the divemode string addition meant that
"oops, now it's corrupting the event name". An event line could look
like this:
event 40:00 type=8 divemode="OC" name="modechange"
where we now had both that "OC" and "modechange" strings, and the code
to pick the name just picked the first string. So we'd end up
effectively mis-parsing the above line as
event 40:00 type=8 divemode="OC" name="OC"
which is obviously wrong.
The dive mode didn't really need to be a string in the first place
(there is nothing to quote, and no spaces in it), but hey, here we are.
We can't just magially fix the existing broken saves.
So make it more straightforward to handle strings in the git format line
parser. We still stash the different decoded strings together in one
special memory buffer, but now the parser helpers automatically untangle
it as they traverse the key value pairs.
This is still overly subtle code, and it doesn't fix the cases where
we've saved the wrong data back. That comes later.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2020-02-14 19:49:28 +00:00
|
|
|
parse->ev.gas.mix.o2 = get_fraction(value);
|
2014-08-17 18:26:21 +00:00
|
|
|
} else if (!strcmp(key, "he")) {
|
load-git: clean up string handling during parsing
We had some fairly obscure rules for how strings were parsed, and it
actually caused bugs when the same line had multiple strings in it.
That normally doesn't happen, and the cases where it was _supposed_ to
happen had special cases for it (divecomputer ID lines, and tag lines).
But by mistake, we had introduced a case of that for the event line
handling in commit b9174332d ("Read and write divemode changes (xml and
git)"), and nobody realized that the divemode string addition meant that
"oops, now it's corrupting the event name". An event line could look
like this:
event 40:00 type=8 divemode="OC" name="modechange"
where we now had both that "OC" and "modechange" strings, and the code
to pick the name just picked the first string. So we'd end up
effectively mis-parsing the above line as
event 40:00 type=8 divemode="OC" name="OC"
which is obviously wrong.
The dive mode didn't really need to be a string in the first place
(there is nothing to quote, and no spaces in it), but hey, here we are.
We can't just magially fix the existing broken saves.
So make it more straightforward to handle strings in the git format line
parser. We still stash the different decoded strings together in one
special memory buffer, but now the parser helpers automatically untangle
it as they traverse the key value pairs.
This is still overly subtle code, and it doesn't fix the cases where
we've saved the wrong data back. That comes later.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2020-02-14 19:49:28 +00:00
|
|
|
parse->ev.gas.mix.he = get_fraction(value);
|
Add event parsing to the git object tree loader
This makes us parse everything we save, and I can load my XML file, save
it as a git file, load that git file, save it as a new XML file, and the
end result is identical.
Well... *ALMOST* identical. We currently don't save the dive computer
nickname and serial/firmware information in the git repository, so that
does get lost in translation. But all the actual dive data is there.
NOTE! I have currently only worked with my own dive files. They are
reasonably complex and complete, and do have a lot of the interesting
cases covered (like multiple dive computers etc), but there's no CCR
information, and all the dives are in trips, so this does need more
testing. It's at the very least very close.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2014-03-09 22:32:42 +00:00
|
|
|
} else
|
|
|
|
report_error("Unexpected event key/value pair (%s/%s)", key, value);
|
|
|
|
}
|
|
|
|
|
2014-11-06 19:23:34 +00:00
|
|
|
/* keyvalue "key" "value"
|
|
|
|
* so we have two strings (possibly empty) in the membuffer, separated by a '\0' */
|
2019-07-24 21:54:46 +00:00
|
|
|
static void parse_dc_keyvalue(char *line, struct membuffer *str, struct git_parser_state *state)
|
2014-11-06 19:23:34 +00:00
|
|
|
{
|
|
|
|
const char *key, *value;
|
|
|
|
|
|
|
|
// Let's make sure we have two strings...
|
|
|
|
int string_counter = 0;
|
|
|
|
while(*line) {
|
|
|
|
if (*line == '"')
|
|
|
|
string_counter++;
|
|
|
|
line++;
|
|
|
|
}
|
|
|
|
if (string_counter != 2)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// stupidly the second string in the membuffer isn't NUL terminated;
|
|
|
|
// asking for a cstring fixes that; interestingly enough, given that there are two
|
|
|
|
// strings in the mb, the next command at the same time assigns a pointer to the
|
|
|
|
// first string to 'key' and NUL terminates the second string (which then goes to 'value')
|
|
|
|
key = mb_cstring(str);
|
|
|
|
value = key + strlen(key) + 1;
|
2019-07-24 21:54:46 +00:00
|
|
|
add_extra_data(state->active_dc, key, value);
|
2014-11-06 19:23:34 +00:00
|
|
|
}
|
|
|
|
|
2019-07-24 21:54:46 +00:00
|
|
|
static void parse_dc_event(char *line, struct membuffer *str, struct git_parser_state *state)
|
Add event parsing to the git object tree loader
This makes us parse everything we save, and I can load my XML file, save
it as a git file, load that git file, save it as a new XML file, and the
end result is identical.
Well... *ALMOST* identical. We currently don't save the dive computer
nickname and serial/firmware information in the git repository, so that
does get lost in translation. But all the actual dive data is there.
NOTE! I have currently only worked with my own dive files. They are
reasonably complex and complete, and do have a lot of the interesting
cases covered (like multiple dive computers etc), but there's no CCR
information, and all the dives are in trips, so this does need more
testing. It's at the very least very close.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2014-03-09 22:32:42 +00:00
|
|
|
{
|
2014-03-10 04:12:13 +00:00
|
|
|
int m, s = 0;
|
load-git: clean up string handling during parsing
We had some fairly obscure rules for how strings were parsed, and it
actually caused bugs when the same line had multiple strings in it.
That normally doesn't happen, and the cases where it was _supposed_ to
happen had special cases for it (divecomputer ID lines, and tag lines).
But by mistake, we had introduced a case of that for the event line
handling in commit b9174332d ("Read and write divemode changes (xml and
git)"), and nobody realized that the divemode string addition meant that
"oops, now it's corrupting the event name". An event line could look
like this:
event 40:00 type=8 divemode="OC" name="modechange"
where we now had both that "OC" and "modechange" strings, and the code
to pick the name just picked the first string. So we'd end up
effectively mis-parsing the above line as
event 40:00 type=8 divemode="OC" name="OC"
which is obviously wrong.
The dive mode didn't really need to be a string in the first place
(there is nothing to quote, and no spaces in it), but hey, here we are.
We can't just magially fix the existing broken saves.
So make it more straightforward to handle strings in the git format line
parser. We still stash the different decoded strings together in one
special memory buffer, but now the parser helpers automatically untangle
it as they traverse the key value pairs.
This is still overly subtle code, and it doesn't fix the cases where
we've saved the wrong data back. That comes later.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2020-02-14 19:49:28 +00:00
|
|
|
struct parse_event p = { NULL, };
|
|
|
|
struct event *ev;
|
Add event parsing to the git object tree loader
This makes us parse everything we save, and I can load my XML file, save
it as a git file, load that git file, save it as a new XML file, and the
end result is identical.
Well... *ALMOST* identical. We currently don't save the dive computer
nickname and serial/firmware information in the git repository, so that
does get lost in translation. But all the actual dive data is there.
NOTE! I have currently only worked with my own dive files. They are
reasonably complex and complete, and do have a lot of the interesting
cases covered (like multiple dive computers etc), but there's no CCR
information, and all the dives are in trips, so this does need more
testing. It's at the very least very close.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2014-03-09 22:32:42 +00:00
|
|
|
|
|
|
|
m = strtol(line, &line, 10);
|
|
|
|
if (*line == ':')
|
2019-07-24 22:26:51 +00:00
|
|
|
s = strtol(line + 1, &line, 10);
|
load-git: clean up string handling during parsing
We had some fairly obscure rules for how strings were parsed, and it
actually caused bugs when the same line had multiple strings in it.
That normally doesn't happen, and the cases where it was _supposed_ to
happen had special cases for it (divecomputer ID lines, and tag lines).
But by mistake, we had introduced a case of that for the event line
handling in commit b9174332d ("Read and write divemode changes (xml and
git)"), and nobody realized that the divemode string addition meant that
"oops, now it's corrupting the event name". An event line could look
like this:
event 40:00 type=8 divemode="OC" name="modechange"
where we now had both that "OC" and "modechange" strings, and the code
to pick the name just picked the first string. So we'd end up
effectively mis-parsing the above line as
event 40:00 type=8 divemode="OC" name="OC"
which is obviously wrong.
The dive mode didn't really need to be a string in the first place
(there is nothing to quote, and no spaces in it), but hey, here we are.
We can't just magially fix the existing broken saves.
So make it more straightforward to handle strings in the git format line
parser. We still stash the different decoded strings together in one
special memory buffer, but now the parser helpers automatically untangle
it as they traverse the key value pairs.
This is still overly subtle code, and it doesn't fix the cases where
we've saved the wrong data back. That comes later.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2020-02-14 19:49:28 +00:00
|
|
|
p.ev.time.seconds = m * 60 + s;
|
Add event parsing to the git object tree loader
This makes us parse everything we save, and I can load my XML file, save
it as a git file, load that git file, save it as a new XML file, and the
end result is identical.
Well... *ALMOST* identical. We currently don't save the dive computer
nickname and serial/firmware information in the git repository, so that
does get lost in translation. But all the actual dive data is there.
NOTE! I have currently only worked with my own dive files. They are
reasonably complex and complete, and do have a lot of the interesting
cases covered (like multiple dive computers etc), but there's no CCR
information, and all the dives are in trips, so this does need more
testing. It's at the very least very close.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2014-03-09 22:32:42 +00:00
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
char c;
|
|
|
|
while (isspace(c = *line))
|
|
|
|
line++;
|
|
|
|
if (!c)
|
|
|
|
break;
|
load-git: clean up string handling during parsing
We had some fairly obscure rules for how strings were parsed, and it
actually caused bugs when the same line had multiple strings in it.
That normally doesn't happen, and the cases where it was _supposed_ to
happen had special cases for it (divecomputer ID lines, and tag lines).
But by mistake, we had introduced a case of that for the event line
handling in commit b9174332d ("Read and write divemode changes (xml and
git)"), and nobody realized that the divemode string addition meant that
"oops, now it's corrupting the event name". An event line could look
like this:
event 40:00 type=8 divemode="OC" name="modechange"
where we now had both that "OC" and "modechange" strings, and the code
to pick the name just picked the first string. So we'd end up
effectively mis-parsing the above line as
event 40:00 type=8 divemode="OC" name="OC"
which is obviously wrong.
The dive mode didn't really need to be a string in the first place
(there is nothing to quote, and no spaces in it), but hey, here we are.
We can't just magially fix the existing broken saves.
So make it more straightforward to handle strings in the git format line
parser. We still stash the different decoded strings together in one
special memory buffer, but now the parser helpers automatically untangle
it as they traverse the key value pairs.
This is still overly subtle code, and it doesn't fix the cases where
we've saved the wrong data back. That comes later.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2020-02-14 19:49:28 +00:00
|
|
|
line = parse_keyvalue_entry(parse_event_keyvalue, &p, line, str);
|
Add event parsing to the git object tree loader
This makes us parse everything we save, and I can load my XML file, save
it as a git file, load that git file, save it as a new XML file, and the
end result is identical.
Well... *ALMOST* identical. We currently don't save the dive computer
nickname and serial/firmware information in the git repository, so that
does get lost in translation. But all the actual dive data is there.
NOTE! I have currently only worked with my own dive files. They are
reasonably complex and complete, and do have a lot of the interesting
cases covered (like multiple dive computers etc), but there's no CCR
information, and all the dives are in trips, so this does need more
testing. It's at the very least very close.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2014-03-09 22:32:42 +00:00
|
|
|
}
|
|
|
|
|
2020-02-14 20:08:43 +00:00
|
|
|
/* Only modechange events should have a divemode - fix up any corrupted names */
|
|
|
|
if (p.has_divemode && strcmp(p.name, "modechange"))
|
|
|
|
p.name = "modechange";
|
|
|
|
|
load-git: clean up string handling during parsing
We had some fairly obscure rules for how strings were parsed, and it
actually caused bugs when the same line had multiple strings in it.
That normally doesn't happen, and the cases where it was _supposed_ to
happen had special cases for it (divecomputer ID lines, and tag lines).
But by mistake, we had introduced a case of that for the event line
handling in commit b9174332d ("Read and write divemode changes (xml and
git)"), and nobody realized that the divemode string addition meant that
"oops, now it's corrupting the event name". An event line could look
like this:
event 40:00 type=8 divemode="OC" name="modechange"
where we now had both that "OC" and "modechange" strings, and the code
to pick the name just picked the first string. So we'd end up
effectively mis-parsing the above line as
event 40:00 type=8 divemode="OC" name="OC"
which is obviously wrong.
The dive mode didn't really need to be a string in the first place
(there is nothing to quote, and no spaces in it), but hey, here we are.
We can't just magially fix the existing broken saves.
So make it more straightforward to handle strings in the git format line
parser. We still stash the different decoded strings together in one
special memory buffer, but now the parser helpers automatically untangle
it as they traverse the key value pairs.
This is still overly subtle code, and it doesn't fix the cases where
we've saved the wrong data back. That comes later.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2020-02-14 19:49:28 +00:00
|
|
|
ev = add_event(state->active_dc, p.ev.time.seconds, p.ev.type, p.ev.flags, p.ev.value, p.name);
|
2016-01-30 10:57:19 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Older logs might mark the dive to be CCR by having an "SP change" event at time 0:00.
|
|
|
|
* Better to mark them being CCR on import so no need for special treatments elsewhere on
|
|
|
|
* the code.
|
|
|
|
*/
|
load-git: clean up string handling during parsing
We had some fairly obscure rules for how strings were parsed, and it
actually caused bugs when the same line had multiple strings in it.
That normally doesn't happen, and the cases where it was _supposed_ to
happen had special cases for it (divecomputer ID lines, and tag lines).
But by mistake, we had introduced a case of that for the event line
handling in commit b9174332d ("Read and write divemode changes (xml and
git)"), and nobody realized that the divemode string addition meant that
"oops, now it's corrupting the event name". An event line could look
like this:
event 40:00 type=8 divemode="OC" name="modechange"
where we now had both that "OC" and "modechange" strings, and the code
to pick the name just picked the first string. So we'd end up
effectively mis-parsing the above line as
event 40:00 type=8 divemode="OC" name="OC"
which is obviously wrong.
The dive mode didn't really need to be a string in the first place
(there is nothing to quote, and no spaces in it), but hey, here we are.
We can't just magially fix the existing broken saves.
So make it more straightforward to handle strings in the git format line
parser. We still stash the different decoded strings together in one
special memory buffer, but now the parser helpers automatically untangle
it as they traverse the key value pairs.
This is still overly subtle code, and it doesn't fix the cases where
we've saved the wrong data back. That comes later.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2020-02-14 19:49:28 +00:00
|
|
|
if (ev && p.ev.time.seconds == 0 && p.ev.type == SAMPLE_EVENT_PO2 && p.ev.value && state->active_dc->divemode==OC)
|
2019-07-24 21:54:46 +00:00
|
|
|
state->active_dc->divemode = CCR;
|
2016-01-30 10:57:19 +00:00
|
|
|
|
2014-08-17 18:26:21 +00:00
|
|
|
if (ev && event_is_gaschange(ev)) {
|
|
|
|
/*
|
|
|
|
* We subtract one here because "0" is "no index",
|
|
|
|
* and the parsing will add one for actual cylinder
|
|
|
|
* index data (see parse_event_keyvalue)
|
|
|
|
*/
|
load-git: clean up string handling during parsing
We had some fairly obscure rules for how strings were parsed, and it
actually caused bugs when the same line had multiple strings in it.
That normally doesn't happen, and the cases where it was _supposed_ to
happen had special cases for it (divecomputer ID lines, and tag lines).
But by mistake, we had introduced a case of that for the event line
handling in commit b9174332d ("Read and write divemode changes (xml and
git)"), and nobody realized that the divemode string addition meant that
"oops, now it's corrupting the event name". An event line could look
like this:
event 40:00 type=8 divemode="OC" name="modechange"
where we now had both that "OC" and "modechange" strings, and the code
to pick the name just picked the first string. So we'd end up
effectively mis-parsing the above line as
event 40:00 type=8 divemode="OC" name="OC"
which is obviously wrong.
The dive mode didn't really need to be a string in the first place
(there is nothing to quote, and no spaces in it), but hey, here we are.
We can't just magially fix the existing broken saves.
So make it more straightforward to handle strings in the git format line
parser. We still stash the different decoded strings together in one
special memory buffer, but now the parser helpers automatically untangle
it as they traverse the key value pairs.
This is still overly subtle code, and it doesn't fix the cases where
we've saved the wrong data back. That comes later.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2020-02-14 19:49:28 +00:00
|
|
|
ev->gas.index = p.ev.gas.index-1;
|
|
|
|
if (p.ev.gas.mix.o2.permille || p.ev.gas.mix.he.permille)
|
|
|
|
ev->gas.mix = p.ev.gas.mix;
|
2014-08-17 18:26:21 +00:00
|
|
|
}
|
Add event parsing to the git object tree loader
This makes us parse everything we save, and I can load my XML file, save
it as a git file, load that git file, save it as a new XML file, and the
end result is identical.
Well... *ALMOST* identical. We currently don't save the dive computer
nickname and serial/firmware information in the git repository, so that
does get lost in translation. But all the actual dive data is there.
NOTE! I have currently only worked with my own dive files. They are
reasonably complex and complete, and do have a lot of the interesting
cases covered (like multiple dive computers etc), but there's no CCR
information, and all the dives are in trips, so this does need more
testing. It's at the very least very close.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2014-03-09 22:32:42 +00:00
|
|
|
}
|
2014-03-09 19:19:41 +00:00
|
|
|
|
2018-11-20 11:07:33 +00:00
|
|
|
/* Not needed anymore - trip date calculated implicitly from first dive */
|
2019-07-24 21:54:46 +00:00
|
|
|
static void parse_trip_date(char *line, struct membuffer *str, struct git_parser_state *state)
|
|
|
|
{ UNUSED(line); UNUSED(str); UNUSED(state); }
|
2018-11-20 11:07:33 +00:00
|
|
|
|
|
|
|
/* Not needed anymore - trip date calculated implicitly from first dive */
|
2019-07-24 21:54:46 +00:00
|
|
|
static void parse_trip_time(char *line, struct membuffer *str, struct git_parser_state *state)
|
|
|
|
{ UNUSED(line); UNUSED(str); UNUSED(state); }
|
2018-11-20 11:07:33 +00:00
|
|
|
|
2019-07-24 21:54:46 +00:00
|
|
|
static void parse_trip_location(char *line, struct membuffer *str, struct git_parser_state *state)
|
load-git: clean up string handling during parsing
We had some fairly obscure rules for how strings were parsed, and it
actually caused bugs when the same line had multiple strings in it.
That normally doesn't happen, and the cases where it was _supposed_ to
happen had special cases for it (divecomputer ID lines, and tag lines).
But by mistake, we had introduced a case of that for the event line
handling in commit b9174332d ("Read and write divemode changes (xml and
git)"), and nobody realized that the divemode string addition meant that
"oops, now it's corrupting the event name". An event line could look
like this:
event 40:00 type=8 divemode="OC" name="modechange"
where we now had both that "OC" and "modechange" strings, and the code
to pick the name just picked the first string. So we'd end up
effectively mis-parsing the above line as
event 40:00 type=8 divemode="OC" name="OC"
which is obviously wrong.
The dive mode didn't really need to be a string in the first place
(there is nothing to quote, and no spaces in it), but hey, here we are.
We can't just magially fix the existing broken saves.
So make it more straightforward to handle strings in the git format line
parser. We still stash the different decoded strings together in one
special memory buffer, but now the parser helpers automatically untangle
it as they traverse the key value pairs.
This is still overly subtle code, and it doesn't fix the cases where
we've saved the wrong data back. That comes later.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2020-02-14 19:49:28 +00:00
|
|
|
{ UNUSED(line); state->active_trip->location = detach_cstring(str); }
|
2014-03-09 19:19:41 +00:00
|
|
|
|
2019-07-24 21:54:46 +00:00
|
|
|
static void parse_trip_notes(char *line, struct membuffer *str, struct git_parser_state *state)
|
load-git: clean up string handling during parsing
We had some fairly obscure rules for how strings were parsed, and it
actually caused bugs when the same line had multiple strings in it.
That normally doesn't happen, and the cases where it was _supposed_ to
happen had special cases for it (divecomputer ID lines, and tag lines).
But by mistake, we had introduced a case of that for the event line
handling in commit b9174332d ("Read and write divemode changes (xml and
git)"), and nobody realized that the divemode string addition meant that
"oops, now it's corrupting the event name". An event line could look
like this:
event 40:00 type=8 divemode="OC" name="modechange"
where we now had both that "OC" and "modechange" strings, and the code
to pick the name just picked the first string. So we'd end up
effectively mis-parsing the above line as
event 40:00 type=8 divemode="OC" name="OC"
which is obviously wrong.
The dive mode didn't really need to be a string in the first place
(there is nothing to quote, and no spaces in it), but hey, here we are.
We can't just magially fix the existing broken saves.
So make it more straightforward to handle strings in the git format line
parser. We still stash the different decoded strings together in one
special memory buffer, but now the parser helpers automatically untangle
it as they traverse the key value pairs.
This is still overly subtle code, and it doesn't fix the cases where
we've saved the wrong data back. That comes later.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2020-02-14 19:49:28 +00:00
|
|
|
{ UNUSED(line); state->active_trip->notes = detach_cstring(str); }
|
2014-03-09 19:19:41 +00:00
|
|
|
|
2019-07-24 21:54:46 +00:00
|
|
|
static void parse_settings_autogroup(char *line, struct membuffer *str, struct git_parser_state *_unused)
|
2016-03-07 19:18:50 +00:00
|
|
|
{
|
2018-05-22 07:07:42 +00:00
|
|
|
UNUSED(line);
|
|
|
|
UNUSED(str);
|
|
|
|
UNUSED(_unused);
|
2017-12-29 10:49:56 +00:00
|
|
|
set_autogroup(true);
|
2016-03-07 19:18:50 +00:00
|
|
|
}
|
2014-03-10 01:31:36 +00:00
|
|
|
|
2019-07-24 21:54:46 +00:00
|
|
|
static void parse_settings_units(char *line, struct membuffer *str, struct git_parser_state *unused)
|
2015-06-17 03:28:42 +00:00
|
|
|
{
|
2018-05-22 07:07:42 +00:00
|
|
|
UNUSED(str);
|
|
|
|
UNUSED(unused);
|
2015-06-17 03:28:42 +00:00
|
|
|
if (line)
|
|
|
|
set_informational_units(line);
|
|
|
|
}
|
|
|
|
|
2019-07-24 21:54:46 +00:00
|
|
|
static void parse_settings_userid(char *line, struct membuffer *str, struct git_parser_state *_unused)
|
2018-09-11 12:39:25 +00:00
|
|
|
/* Keep this despite removal of the webservice as there are legacy logbook around
|
|
|
|
* that still have this defined.
|
|
|
|
*/
|
2014-04-11 06:17:35 +00:00
|
|
|
{
|
2018-05-22 07:07:42 +00:00
|
|
|
UNUSED(str);
|
|
|
|
UNUSED(_unused);
|
2018-09-11 12:39:25 +00:00
|
|
|
UNUSED(line);
|
2014-04-11 06:17:35 +00:00
|
|
|
}
|
|
|
|
|
2019-07-24 21:54:46 +00:00
|
|
|
static void parse_settings_prefs(char *line, struct membuffer *str, struct git_parser_state *unused)
|
2017-02-04 09:13:58 +00:00
|
|
|
{
|
2018-05-22 07:07:42 +00:00
|
|
|
UNUSED(str);
|
|
|
|
UNUSED(unused);
|
2017-02-04 09:13:58 +00:00
|
|
|
if (line)
|
|
|
|
set_git_prefs(line);
|
|
|
|
}
|
|
|
|
|
2014-03-10 01:31:36 +00:00
|
|
|
/*
|
|
|
|
* Our versioning is a joke right now, but this is more of an example of what we
|
|
|
|
* *can* do some day. And if we do change the version, this warning will show if
|
|
|
|
* you read with a version of subsurface that doesn't know about it.
|
2015-06-20 13:45:12 +00:00
|
|
|
* We MUST keep this in sync with the XML version (so we can report a consistent
|
|
|
|
* minimum datafile version)
|
2014-03-10 01:31:36 +00:00
|
|
|
*/
|
2019-07-24 21:54:46 +00:00
|
|
|
static void parse_settings_version(char *line, struct membuffer *str, struct git_parser_state *_unused)
|
2014-03-10 01:31:36 +00:00
|
|
|
{
|
2018-05-22 07:07:42 +00:00
|
|
|
UNUSED(str);
|
|
|
|
UNUSED(_unused);
|
2014-03-10 01:31:36 +00:00
|
|
|
int version = atoi(line);
|
2015-06-20 13:45:12 +00:00
|
|
|
report_datafile_version(version);
|
|
|
|
if (version > DATAFORMAT_VERSION)
|
|
|
|
report_error("Git save file version %d is newer than version %d I know about", version, DATAFORMAT_VERSION);
|
2014-03-10 01:31:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* The string in the membuffer is the version string of subsurface that saved things, just FYI */
|
2019-07-24 21:54:46 +00:00
|
|
|
static void parse_settings_subsurface(char *line, struct membuffer *str, struct git_parser_state *_unused)
|
2016-03-07 19:18:50 +00:00
|
|
|
{
|
2018-05-22 07:07:42 +00:00
|
|
|
UNUSED(line);
|
|
|
|
UNUSED(str);
|
|
|
|
UNUSED(_unused);
|
2016-03-07 19:18:50 +00:00
|
|
|
}
|
2014-03-10 01:31:36 +00:00
|
|
|
|
|
|
|
struct divecomputerid {
|
|
|
|
const char *model;
|
|
|
|
const char *nickname;
|
|
|
|
const char *firmware;
|
|
|
|
const char *serial;
|
|
|
|
unsigned int deviceid;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void parse_divecomputerid_keyvalue(void *_cid, const char *key, const char *value)
|
|
|
|
{
|
|
|
|
struct divecomputerid *cid = _cid;
|
|
|
|
|
|
|
|
if (!strcmp(key, "deviceid")) {
|
|
|
|
cid->deviceid = get_hex(value);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!strcmp(key, "serial")) {
|
|
|
|
cid->serial = value;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!strcmp(key, "firmware")) {
|
|
|
|
cid->firmware = value;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!strcmp(key, "nickname")) {
|
|
|
|
cid->nickname = value;
|
|
|
|
return;
|
|
|
|
}
|
2017-02-20 07:56:15 +00:00
|
|
|
report_error("Unknown divecomputerid key/value pair (%s/%s)", key, value);
|
2014-03-10 01:31:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The 'divecomputerid' is a bit harder to parse than some other things, because
|
|
|
|
* it can have multiple strings (but see the tag parsing for another example of
|
|
|
|
* that) in addition to the non-string entries.
|
|
|
|
*/
|
2019-07-24 21:54:46 +00:00
|
|
|
static void parse_settings_divecomputerid(char *line, struct membuffer *str, struct git_parser_state *_unused)
|
2014-03-10 01:31:36 +00:00
|
|
|
{
|
2018-05-22 07:07:42 +00:00
|
|
|
UNUSED(_unused);
|
load-git: clean up string handling during parsing
We had some fairly obscure rules for how strings were parsed, and it
actually caused bugs when the same line had multiple strings in it.
That normally doesn't happen, and the cases where it was _supposed_ to
happen had special cases for it (divecomputer ID lines, and tag lines).
But by mistake, we had introduced a case of that for the event line
handling in commit b9174332d ("Read and write divemode changes (xml and
git)"), and nobody realized that the divemode string addition meant that
"oops, now it's corrupting the event name". An event line could look
like this:
event 40:00 type=8 divemode="OC" name="modechange"
where we now had both that "OC" and "modechange" strings, and the code
to pick the name just picked the first string. So we'd end up
effectively mis-parsing the above line as
event 40:00 type=8 divemode="OC" name="OC"
which is obviously wrong.
The dive mode didn't really need to be a string in the first place
(there is nothing to quote, and no spaces in it), but hey, here we are.
We can't just magially fix the existing broken saves.
So make it more straightforward to handle strings in the git format line
parser. We still stash the different decoded strings together in one
special memory buffer, but now the parser helpers automatically untangle
it as they traverse the key value pairs.
This is still overly subtle code, and it doesn't fix the cases where
we've saved the wrong data back. That comes later.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2020-02-14 19:49:28 +00:00
|
|
|
struct divecomputerid id = { pop_cstring(str, line) };
|
2014-03-10 01:31:36 +00:00
|
|
|
|
|
|
|
/* Skip the '"' that stood for the model string */
|
|
|
|
line++;
|
|
|
|
|
load-git: clean up string handling during parsing
We had some fairly obscure rules for how strings were parsed, and it
actually caused bugs when the same line had multiple strings in it.
That normally doesn't happen, and the cases where it was _supposed_ to
happen had special cases for it (divecomputer ID lines, and tag lines).
But by mistake, we had introduced a case of that for the event line
handling in commit b9174332d ("Read and write divemode changes (xml and
git)"), and nobody realized that the divemode string addition meant that
"oops, now it's corrupting the event name". An event line could look
like this:
event 40:00 type=8 divemode="OC" name="modechange"
where we now had both that "OC" and "modechange" strings, and the code
to pick the name just picked the first string. So we'd end up
effectively mis-parsing the above line as
event 40:00 type=8 divemode="OC" name="OC"
which is obviously wrong.
The dive mode didn't really need to be a string in the first place
(there is nothing to quote, and no spaces in it), but hey, here we are.
We can't just magially fix the existing broken saves.
So make it more straightforward to handle strings in the git format line
parser. We still stash the different decoded strings together in one
special memory buffer, but now the parser helpers automatically untangle
it as they traverse the key value pairs.
This is still overly subtle code, and it doesn't fix the cases where
we've saved the wrong data back. That comes later.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2020-02-14 19:49:28 +00:00
|
|
|
/* Parse the rest of the entries */
|
2014-03-10 01:31:36 +00:00
|
|
|
for (;;) {
|
|
|
|
char c;
|
|
|
|
while (isspace(c = *line))
|
|
|
|
line++;
|
|
|
|
if (!c)
|
|
|
|
break;
|
load-git: clean up string handling during parsing
We had some fairly obscure rules for how strings were parsed, and it
actually caused bugs when the same line had multiple strings in it.
That normally doesn't happen, and the cases where it was _supposed_ to
happen had special cases for it (divecomputer ID lines, and tag lines).
But by mistake, we had introduced a case of that for the event line
handling in commit b9174332d ("Read and write divemode changes (xml and
git)"), and nobody realized that the divemode string addition meant that
"oops, now it's corrupting the event name". An event line could look
like this:
event 40:00 type=8 divemode="OC" name="modechange"
where we now had both that "OC" and "modechange" strings, and the code
to pick the name just picked the first string. So we'd end up
effectively mis-parsing the above line as
event 40:00 type=8 divemode="OC" name="OC"
which is obviously wrong.
The dive mode didn't really need to be a string in the first place
(there is nothing to quote, and no spaces in it), but hey, here we are.
We can't just magially fix the existing broken saves.
So make it more straightforward to handle strings in the git format line
parser. We still stash the different decoded strings together in one
special memory buffer, but now the parser helpers automatically untangle
it as they traverse the key value pairs.
This is still overly subtle code, and it doesn't fix the cases where
we've saved the wrong data back. That comes later.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2020-02-14 19:49:28 +00:00
|
|
|
line = parse_keyvalue_entry(parse_divecomputerid_keyvalue, &id, line, str);
|
2014-03-10 01:31:36 +00:00
|
|
|
}
|
|
|
|
create_device_node(id.model, id.deviceid, id.serial, id.firmware, id.nickname);
|
|
|
|
}
|
|
|
|
|
2019-07-24 21:54:46 +00:00
|
|
|
static void parse_picture_filename(char *line, struct membuffer *str, struct git_parser_state *state)
|
2014-06-29 18:21:52 +00:00
|
|
|
{
|
2018-05-22 07:07:42 +00:00
|
|
|
UNUSED(line);
|
load-git: clean up string handling during parsing
We had some fairly obscure rules for how strings were parsed, and it
actually caused bugs when the same line had multiple strings in it.
That normally doesn't happen, and the cases where it was _supposed_ to
happen had special cases for it (divecomputer ID lines, and tag lines).
But by mistake, we had introduced a case of that for the event line
handling in commit b9174332d ("Read and write divemode changes (xml and
git)"), and nobody realized that the divemode string addition meant that
"oops, now it's corrupting the event name". An event line could look
like this:
event 40:00 type=8 divemode="OC" name="modechange"
where we now had both that "OC" and "modechange" strings, and the code
to pick the name just picked the first string. So we'd end up
effectively mis-parsing the above line as
event 40:00 type=8 divemode="OC" name="OC"
which is obviously wrong.
The dive mode didn't really need to be a string in the first place
(there is nothing to quote, and no spaces in it), but hey, here we are.
We can't just magially fix the existing broken saves.
So make it more straightforward to handle strings in the git format line
parser. We still stash the different decoded strings together in one
special memory buffer, but now the parser helpers automatically untangle
it as they traverse the key value pairs.
This is still overly subtle code, and it doesn't fix the cases where
we've saved the wrong data back. That comes later.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2020-02-14 19:49:28 +00:00
|
|
|
state->active_pic->filename = detach_cstring(str);
|
2014-06-29 18:21:52 +00:00
|
|
|
}
|
|
|
|
|
2019-07-24 21:54:46 +00:00
|
|
|
static void parse_picture_gps(char *line, struct membuffer *str, struct git_parser_state *state)
|
2014-06-29 18:21:52 +00:00
|
|
|
{
|
2018-05-22 07:07:42 +00:00
|
|
|
UNUSED(str);
|
2019-07-24 21:54:46 +00:00
|
|
|
parse_location(line, &state->active_pic->location);
|
2014-06-29 18:21:52 +00:00
|
|
|
}
|
|
|
|
|
2019-07-24 21:54:46 +00:00
|
|
|
static void parse_picture_hash(char *line, struct membuffer *str, struct git_parser_state *state)
|
2018-07-07 14:35:45 +00:00
|
|
|
{
|
|
|
|
// we no longer use hashes to identify pictures, but we shouldn't
|
2019-07-24 21:54:46 +00:00
|
|
|
// remove this parser lest users get an ugly red warning when
|
2018-07-07 14:35:45 +00:00
|
|
|
// opening old git repos
|
|
|
|
UNUSED(line);
|
2019-07-24 21:54:46 +00:00
|
|
|
UNUSED(state);
|
2018-07-07 14:35:45 +00:00
|
|
|
UNUSED(str);
|
|
|
|
}
|
|
|
|
|
2014-03-09 19:19:41 +00:00
|
|
|
/* These need to be sorted! */
|
|
|
|
struct keyword_action dc_action[] = {
|
|
|
|
#undef D
|
|
|
|
#define D(x) { #x, parse_dc_ ## x }
|
2014-11-16 23:11:18 +00:00
|
|
|
D(airtemp), D(date), D(dctype), D(deviceid), D(diveid), D(duration),
|
Improve profile display in planner
This patch allows the planner to save the last manually-entered
dive planner point of a dive plan. When the plan has been saved
and re-opened for edit, the time of the last-entered dive planner
point is used to ensure that dive planning continues from the same
point in the profile as was when the original dive plan was saved.
Mechanism:
1) In dive.h, create a new dc attribute dc->last_manual_time
with data type of duration_t.
2) In diveplanner.c, ensure that the last manually-entered
dive planner point is saved in dc->last_manual_time.
3) In save-xml.c, create a new XML attribute for the <divecomputer>
element, named last-manual-time. For dive plans, the element would
now look like:
<divecomputer model='planned dive' last-manual-time='31:17 min'>
4) In parse-xml.c, insert code that recognises the last-manual-time
XML attribute, reads the time value and assigns this time to
dc->last_manual_time.
5) In diveplannermodel.cpp, method DiveplannerPointModel::loadfromdive,
insert code that sets the appropriate boolean value to dp->entered
by comparing newtime (i.e. time of dp) with dc->last_manual_time.
6) Diveplannermodel.cpp also accepts profile data from normal dives in
the dive log, whether hand-entered or loaded from dive computer. It
looks like the reduction of dive points for dives with >100 points
continues to work ok.
The result is that when a dive plan is saved with manually entered
points up to e.g. 10 minutes into the dive, it can be re-opened for edit
in the dive planner and the planner re-creates the plan with manually
entered points up to 10 minutes. The rest of the points are "soft"
points, shaped by the deco calculations of the planner.
Improvements: Improve code for profile display in dive planner
This responds to #1052.
Change load-git.c and save-git.c so that the last-manual-time is
also saved in the git-format dive log.
Several stylistic changes in text for consistent C source code.
Improvement of dive planner profile display:
Do some simplification of my alterations to diveplannermodel.cpp
Two small style changes in planner.c and diveplannermodel.cpp
as requested ny @neolit123
Signed-off-by: Willem Ferguson <willemferguson@zoology.up.ac.za>
2018-01-15 12:51:47 +00:00
|
|
|
D(event), D(keyvalue), D(lastmanualtime), D(maxdepth), D(meandepth), D(model), D(numberofoxygensensors),
|
2014-11-22 13:13:13 +00:00
|
|
|
D(salinity), D(surfacepressure), D(surfacetime), D(time), D(watertemp)
|
2014-03-09 19:19:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Sample lines start with a space or a number */
|
2019-07-24 21:54:46 +00:00
|
|
|
static void divecomputer_parser(char *line, struct membuffer *str, struct git_parser_state *state)
|
2014-03-09 19:19:41 +00:00
|
|
|
{
|
|
|
|
char c = *line;
|
|
|
|
if (c < 'a' || c > 'z')
|
2019-07-24 21:54:46 +00:00
|
|
|
sample_parser(line, state);
|
|
|
|
match_action(line, str, state, dc_action, ARRAY_SIZE(dc_action));
|
2014-03-09 19:19:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* These need to be sorted! */
|
|
|
|
struct keyword_action dive_action[] = {
|
|
|
|
#undef D
|
|
|
|
#define D(x) { #x, parse_dive_ ## x }
|
2019-11-29 05:08:14 +00:00
|
|
|
D(airpressure), D(airtemp), D(buddy), D(chill), D(current), D(cylinder), D(divemaster), D(divesiteid), D(duration),
|
2019-12-12 21:58:53 +00:00
|
|
|
D(gps), D(invalid), D(location), D(notes), D(notrip), D(rating), D(suit), D(surge),
|
2019-11-19 17:16:45 +00:00
|
|
|
D(tags), D(visibility), D(watersalinity), D(watertemp), D(wavesize), D(weightsystem)
|
2014-03-09 19:19:41 +00:00
|
|
|
};
|
|
|
|
|
2019-07-24 21:54:46 +00:00
|
|
|
static void dive_parser(char *line, struct membuffer *str, struct git_parser_state *state)
|
2014-03-09 19:19:41 +00:00
|
|
|
{
|
2019-07-24 21:54:46 +00:00
|
|
|
match_action(line, str, state, dive_action, ARRAY_SIZE(dive_action));
|
2014-03-09 19:19:41 +00:00
|
|
|
}
|
|
|
|
|
2015-02-12 08:14:50 +00:00
|
|
|
/* These need to be sorted! */
|
|
|
|
struct keyword_action site_action[] = {
|
|
|
|
#undef D
|
|
|
|
#define D(x) { #x, parse_site_ ## x }
|
2017-10-03 06:03:44 +00:00
|
|
|
D(description), D(geo), D(gps), D(name), D(notes)
|
2015-02-12 08:14:50 +00:00
|
|
|
};
|
|
|
|
|
2019-07-24 21:54:46 +00:00
|
|
|
static void site_parser(char *line, struct membuffer *str, struct git_parser_state *state)
|
2015-02-12 08:14:50 +00:00
|
|
|
{
|
2019-07-24 21:54:46 +00:00
|
|
|
match_action(line, str, state, site_action, ARRAY_SIZE(site_action));
|
2015-02-12 08:14:50 +00:00
|
|
|
}
|
|
|
|
|
2014-03-09 19:19:41 +00:00
|
|
|
/* These need to be sorted! */
|
|
|
|
struct keyword_action trip_action[] = {
|
|
|
|
#undef D
|
|
|
|
#define D(x) { #x, parse_trip_ ## x }
|
2018-11-20 11:07:33 +00:00
|
|
|
D(date), D(location), D(notes), D(time),
|
2014-03-09 19:19:41 +00:00
|
|
|
};
|
|
|
|
|
2019-07-24 21:54:46 +00:00
|
|
|
static void trip_parser(char *line, struct membuffer *str, struct git_parser_state *state)
|
2014-03-09 19:19:41 +00:00
|
|
|
{
|
2019-07-24 21:54:46 +00:00
|
|
|
match_action(line, str, state, trip_action, ARRAY_SIZE(trip_action));
|
2014-03-08 23:59:39 +00:00
|
|
|
}
|
|
|
|
|
2014-03-10 01:31:36 +00:00
|
|
|
/* These need to be sorted! */
|
|
|
|
static struct keyword_action settings_action[] = {
|
|
|
|
#undef D
|
|
|
|
#define D(x) { #x, parse_settings_ ## x }
|
2017-02-04 16:55:25 +00:00
|
|
|
D(autogroup), D(divecomputerid), D(prefs), D(subsurface), D(units), D(userid), D(version)
|
2014-03-10 01:31:36 +00:00
|
|
|
};
|
|
|
|
|
2019-07-24 21:54:46 +00:00
|
|
|
static void settings_parser(char *line, struct membuffer *str, struct git_parser_state *state)
|
2014-03-10 01:31:36 +00:00
|
|
|
{
|
2019-07-24 21:54:46 +00:00
|
|
|
match_action(line, str, state, settings_action, ARRAY_SIZE(settings_action));
|
2014-03-10 01:31:36 +00:00
|
|
|
}
|
|
|
|
|
2014-06-29 18:21:52 +00:00
|
|
|
/* These need to be sorted! */
|
|
|
|
static struct keyword_action picture_action[] = {
|
|
|
|
#undef D
|
|
|
|
#define D(x) { #x, parse_picture_ ## x }
|
2018-07-07 14:35:45 +00:00
|
|
|
D(filename), D(gps), D(hash)
|
2014-06-29 18:21:52 +00:00
|
|
|
};
|
|
|
|
|
2019-07-24 21:54:46 +00:00
|
|
|
static void picture_parser(char *line, struct membuffer *str, struct git_parser_state *state)
|
2014-06-29 18:21:52 +00:00
|
|
|
{
|
2019-07-24 21:54:46 +00:00
|
|
|
match_action(line, str, state, picture_action, ARRAY_SIZE(picture_action));
|
2014-06-29 18:21:52 +00:00
|
|
|
}
|
|
|
|
|
2014-03-08 23:59:39 +00:00
|
|
|
/*
|
|
|
|
* We have a very simple line-based interface, with the small
|
|
|
|
* complication that lines can have strings in the middle, and
|
|
|
|
* a string can be multiple lines.
|
|
|
|
*
|
|
|
|
* The UTF-8 string escaping is *very* simple, though:
|
|
|
|
*
|
|
|
|
* - a string starts and ends with double quotes (")
|
|
|
|
*
|
|
|
|
* - inside the string we escape:
|
|
|
|
* (a) double quotes with '\"'
|
|
|
|
* (b) backslash (\) with '\\'
|
|
|
|
*
|
|
|
|
* - additionally, for human readability, we escape
|
|
|
|
* newlines with '\n\t', with the exception that
|
|
|
|
* consecutive newlines are left unescaped (so an
|
|
|
|
* empty line doesn't become a line with just a tab
|
|
|
|
* on it).
|
|
|
|
*
|
|
|
|
* Also, while the UTF-8 string can have arbitrarily
|
|
|
|
* long lines, the non-string parts of the lines are
|
|
|
|
* never long, so we can use a small temporary buffer
|
|
|
|
* on stack for that part.
|
|
|
|
*
|
|
|
|
* Also, note that if a line has one or more strings
|
|
|
|
* in it:
|
|
|
|
*
|
|
|
|
* - each string will be represented as a single '"'
|
|
|
|
* character in the output.
|
|
|
|
*
|
|
|
|
* - all string will exist in the same 'membuffer',
|
|
|
|
* separated by NUL characters (that cannot exist
|
|
|
|
* in a string, not even quoted).
|
|
|
|
*/
|
|
|
|
static const char *parse_one_string(const char *buf, const char *end, struct membuffer *b)
|
|
|
|
{
|
|
|
|
const char *p = buf;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We turn multiple strings one one line (think dive tags) into one
|
|
|
|
* membuffer that has NUL characters in between strings.
|
|
|
|
*/
|
|
|
|
if (b->len)
|
|
|
|
put_bytes(b, "", 1);
|
|
|
|
|
|
|
|
while (p < end) {
|
|
|
|
char replace;
|
|
|
|
|
|
|
|
switch (*p++) {
|
|
|
|
default:
|
|
|
|
continue;
|
|
|
|
case '\n':
|
|
|
|
if (p < end && *p == '\t') {
|
|
|
|
replace = '\n';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
case '\\':
|
|
|
|
if (p < end) {
|
|
|
|
replace = *p;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
case '"':
|
|
|
|
replace = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
put_bytes(b, buf, p - buf - 1);
|
|
|
|
if (!replace)
|
|
|
|
break;
|
|
|
|
put_bytes(b, &replace, 1);
|
|
|
|
buf = ++p;
|
|
|
|
}
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2019-07-24 21:54:46 +00:00
|
|
|
typedef void (line_fn_t)(char *, struct membuffer *, struct git_parser_state *);
|
2014-03-10 04:06:18 +00:00
|
|
|
#define MAXLINE 500
|
2019-07-24 21:54:46 +00:00
|
|
|
static unsigned parse_one_line(const char *buf, unsigned size, line_fn_t *fn, struct git_parser_state *state, struct membuffer *b)
|
2014-03-08 23:59:39 +00:00
|
|
|
{
|
|
|
|
const char *end = buf + size;
|
|
|
|
const char *p = buf;
|
2019-07-24 22:26:51 +00:00
|
|
|
char line[MAXLINE + 1];
|
2014-03-08 23:59:39 +00:00
|
|
|
int off = 0;
|
|
|
|
|
|
|
|
while (p < end) {
|
|
|
|
char c = *p++;
|
|
|
|
if (c == '\n')
|
|
|
|
break;
|
|
|
|
line[off] = c;
|
|
|
|
off++;
|
|
|
|
if (off > MAXLINE)
|
|
|
|
off = MAXLINE;
|
|
|
|
if (c == '"')
|
|
|
|
p = parse_one_string(p, end, b);
|
|
|
|
}
|
|
|
|
line[off] = 0;
|
2019-07-24 21:54:46 +00:00
|
|
|
fn(line, b, state);
|
2014-03-08 23:59:39 +00:00
|
|
|
return p - buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We keep on re-using the membuffer that we use for
|
|
|
|
* strings, but the callback function can "steal" it by
|
|
|
|
* saving its value and just clear the original.
|
|
|
|
*/
|
2019-07-24 21:54:46 +00:00
|
|
|
static void for_each_line(git_blob *blob, line_fn_t *fn, struct git_parser_state *state)
|
2014-03-08 23:59:39 +00:00
|
|
|
{
|
|
|
|
const char *content = git_blob_rawcontent(blob);
|
|
|
|
unsigned int size = git_blob_rawsize(blob);
|
|
|
|
struct membuffer str = { 0 };
|
|
|
|
|
|
|
|
while (size) {
|
2019-07-24 21:54:46 +00:00
|
|
|
unsigned int n = parse_one_line(content, size, fn, state, &str);
|
2014-03-08 23:59:39 +00:00
|
|
|
content += n;
|
|
|
|
size -= n;
|
|
|
|
|
|
|
|
/* Re-use the allocation, but forget the data */
|
|
|
|
str.len = 0;
|
|
|
|
}
|
|
|
|
free_buffer(&str);
|
|
|
|
}
|
|
|
|
|
2014-03-07 03:27:28 +00:00
|
|
|
#define GIT_WALK_OK 0
|
|
|
|
#define GIT_WALK_SKIP 1
|
|
|
|
|
2019-07-24 21:54:46 +00:00
|
|
|
static void finish_active_trip(struct git_parser_state *state)
|
git-load: Add trips to the trip list on loading
We don't actually much use the trip list any more, and it's possible we
should simply get rid of it. I hadn't added the trips to the trip list
when loading them, and everything worked fine.
Well, *almost* everything worked fine.
There is one use of the list of trips, and that's the "clear the trip
index for each trip before saving them". That literally seems to be the
only non-debug use of this list, but when we didn't add the trips to the
list, the trip index never got cleared before saving trips.
And even that is unnoticeable for the *first* save event, because the
trip index will have been clear before that.
But on the *second* save event, if the trip index doesn't get cleared
before saving, the saving code will look at the index, say "Hey, I
already saved this" and skip the trip.
So if you loaded the trips from a git repository, and then saved things,
everything worked fine. But it you saved things a *second* time,
nothing would get saved at all, because all the trips were marked as
saved already.
Anyway, I think the real solution is to get rid of the pointless trip
list, and just use "for_each_dive()" to find all the trips, since that
list clearly is just more pain than gain. But in the meantime, this
makes the git loading add the trips properly to the list.
Signed-off-by: Linus "oops" Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2014-03-11 21:48:03 +00:00
|
|
|
{
|
2019-07-24 21:54:46 +00:00
|
|
|
dive_trip_t *trip = state->active_trip;
|
git-load: Add trips to the trip list on loading
We don't actually much use the trip list any more, and it's possible we
should simply get rid of it. I hadn't added the trips to the trip list
when loading them, and everything worked fine.
Well, *almost* everything worked fine.
There is one use of the list of trips, and that's the "clear the trip
index for each trip before saving them". That literally seems to be the
only non-debug use of this list, but when we didn't add the trips to the
list, the trip index never got cleared before saving trips.
And even that is unnoticeable for the *first* save event, because the
trip index will have been clear before that.
But on the *second* save event, if the trip index doesn't get cleared
before saving, the saving code will look at the index, say "Hey, I
already saved this" and skip the trip.
So if you loaded the trips from a git repository, and then saved things,
everything worked fine. But it you saved things a *second* time,
nothing would get saved at all, because all the trips were marked as
saved already.
Anyway, I think the real solution is to get rid of the pointless trip
list, and just use "for_each_dive()" to find all the trips, since that
list clearly is just more pain than gain. But in the meantime, this
makes the git loading add the trips properly to the list.
Signed-off-by: Linus "oops" Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2014-03-11 21:48:03 +00:00
|
|
|
|
|
|
|
if (trip) {
|
2019-07-24 21:54:46 +00:00
|
|
|
state->active_trip = NULL;
|
2020-03-17 21:49:29 +00:00
|
|
|
insert_trip(trip, state->trips);
|
git-load: Add trips to the trip list on loading
We don't actually much use the trip list any more, and it's possible we
should simply get rid of it. I hadn't added the trips to the trip list
when loading them, and everything worked fine.
Well, *almost* everything worked fine.
There is one use of the list of trips, and that's the "clear the trip
index for each trip before saving them". That literally seems to be the
only non-debug use of this list, but when we didn't add the trips to the
list, the trip index never got cleared before saving trips.
And even that is unnoticeable for the *first* save event, because the
trip index will have been clear before that.
But on the *second* save event, if the trip index doesn't get cleared
before saving, the saving code will look at the index, say "Hey, I
already saved this" and skip the trip.
So if you loaded the trips from a git repository, and then saved things,
everything worked fine. But it you saved things a *second* time,
nothing would get saved at all, because all the trips were marked as
saved already.
Anyway, I think the real solution is to get rid of the pointless trip
list, and just use "for_each_dive()" to find all the trips, since that
list clearly is just more pain than gain. But in the meantime, this
makes the git loading add the trips properly to the list.
Signed-off-by: Linus "oops" Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2014-03-11 21:48:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-24 21:54:46 +00:00
|
|
|
static void finish_active_dive(struct git_parser_state *state)
|
git-load: Add trips to the trip list on loading
We don't actually much use the trip list any more, and it's possible we
should simply get rid of it. I hadn't added the trips to the trip list
when loading them, and everything worked fine.
Well, *almost* everything worked fine.
There is one use of the list of trips, and that's the "clear the trip
index for each trip before saving them". That literally seems to be the
only non-debug use of this list, but when we didn't add the trips to the
list, the trip index never got cleared before saving trips.
And even that is unnoticeable for the *first* save event, because the
trip index will have been clear before that.
But on the *second* save event, if the trip index doesn't get cleared
before saving, the saving code will look at the index, say "Hey, I
already saved this" and skip the trip.
So if you loaded the trips from a git repository, and then saved things,
everything worked fine. But it you saved things a *second* time,
nothing would get saved at all, because all the trips were marked as
saved already.
Anyway, I think the real solution is to get rid of the pointless trip
list, and just use "for_each_dive()" to find all the trips, since that
list clearly is just more pain than gain. But in the meantime, this
makes the git loading add the trips properly to the list.
Signed-off-by: Linus "oops" Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2014-03-11 21:48:03 +00:00
|
|
|
{
|
2019-07-24 21:54:46 +00:00
|
|
|
struct dive *dive = state->active_dive;
|
git-load: Add trips to the trip list on loading
We don't actually much use the trip list any more, and it's possible we
should simply get rid of it. I hadn't added the trips to the trip list
when loading them, and everything worked fine.
Well, *almost* everything worked fine.
There is one use of the list of trips, and that's the "clear the trip
index for each trip before saving them". That literally seems to be the
only non-debug use of this list, but when we didn't add the trips to the
list, the trip index never got cleared before saving trips.
And even that is unnoticeable for the *first* save event, because the
trip index will have been clear before that.
But on the *second* save event, if the trip index doesn't get cleared
before saving, the saving code will look at the index, say "Hey, I
already saved this" and skip the trip.
So if you loaded the trips from a git repository, and then saved things,
everything worked fine. But it you saved things a *second* time,
nothing would get saved at all, because all the trips were marked as
saved already.
Anyway, I think the real solution is to get rid of the pointless trip
list, and just use "for_each_dive()" to find all the trips, since that
list clearly is just more pain than gain. But in the meantime, this
makes the git loading add the trips properly to the list.
Signed-off-by: Linus "oops" Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2014-03-11 21:48:03 +00:00
|
|
|
|
|
|
|
if (dive) {
|
2019-07-24 21:54:46 +00:00
|
|
|
state->active_dive = NULL;
|
2020-03-17 21:49:29 +00:00
|
|
|
record_dive_to_table(dive, state->table);
|
git-load: Add trips to the trip list on loading
We don't actually much use the trip list any more, and it's possible we
should simply get rid of it. I hadn't added the trips to the trip list
when loading them, and everything worked fine.
Well, *almost* everything worked fine.
There is one use of the list of trips, and that's the "clear the trip
index for each trip before saving them". That literally seems to be the
only non-debug use of this list, but when we didn't add the trips to the
list, the trip index never got cleared before saving trips.
And even that is unnoticeable for the *first* save event, because the
trip index will have been clear before that.
But on the *second* save event, if the trip index doesn't get cleared
before saving, the saving code will look at the index, say "Hey, I
already saved this" and skip the trip.
So if you loaded the trips from a git repository, and then saved things,
everything worked fine. But it you saved things a *second* time,
nothing would get saved at all, because all the trips were marked as
saved already.
Anyway, I think the real solution is to get rid of the pointless trip
list, and just use "for_each_dive()" to find all the trips, since that
list clearly is just more pain than gain. But in the meantime, this
makes the git loading add the trips properly to the list.
Signed-off-by: Linus "oops" Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2014-03-11 21:48:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-24 21:54:46 +00:00
|
|
|
static void create_new_dive(timestamp_t when, struct git_parser_state *state)
|
2014-03-07 03:27:28 +00:00
|
|
|
{
|
2019-07-24 21:54:46 +00:00
|
|
|
state->active_dive = alloc_dive();
|
2014-03-07 03:27:28 +00:00
|
|
|
|
|
|
|
/* We'll fill in more data from the dive file */
|
2019-07-24 21:54:46 +00:00
|
|
|
state->active_dive->when = when;
|
2014-03-07 03:27:28 +00:00
|
|
|
|
2019-07-24 21:54:46 +00:00
|
|
|
if (state->active_trip)
|
|
|
|
add_dive_to_trip(state->active_dive, state->active_trip);
|
2014-03-07 03:27:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool validate_date(int yyyy, int mm, int dd)
|
|
|
|
{
|
2016-04-28 22:13:30 +00:00
|
|
|
return yyyy > 1930 && yyyy < 3000 &&
|
2014-03-07 03:27:28 +00:00
|
|
|
mm > 0 && mm < 13 &&
|
|
|
|
dd > 0 && dd < 32;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool validate_time(int h, int m, int s)
|
|
|
|
{
|
|
|
|
return h >= 0 && h < 24 &&
|
|
|
|
m >= 0 && m < 60 &&
|
|
|
|
s >=0 && s <= 60;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Dive trip directory, name is 'nn-alphabetic[~hex]'
|
|
|
|
*/
|
2019-07-24 21:54:46 +00:00
|
|
|
static int dive_trip_directory(const char *root, const char *name, struct git_parser_state *state)
|
2014-03-07 03:27:28 +00:00
|
|
|
{
|
|
|
|
int yyyy = -1, mm = -1, dd = -1;
|
|
|
|
|
|
|
|
if (sscanf(root, "%d/%d", &yyyy, &mm) != 2)
|
|
|
|
return GIT_WALK_SKIP;
|
|
|
|
dd = atoi(name);
|
|
|
|
if (!validate_date(yyyy, mm, dd))
|
|
|
|
return GIT_WALK_SKIP;
|
2019-07-24 21:54:46 +00:00
|
|
|
finish_active_trip(state);
|
|
|
|
state->active_trip = alloc_trip();
|
2014-03-07 03:27:28 +00:00
|
|
|
return GIT_WALK_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2015-06-19 18:45:24 +00:00
|
|
|
* Dive directory, name is [[yyyy-]mm-]nn-ddd-hh:mm:ss[~hex] in older git repositories
|
|
|
|
* but [[yyyy-]mm-]nn-ddd-hh=mm=ss[~hex] in newer repos as ':' is an illegal character for Windows files
|
2014-03-07 03:27:28 +00:00
|
|
|
* and 'timeoff' points to what should be the time part of
|
|
|
|
* the name (the first digit of the hour).
|
|
|
|
*
|
|
|
|
* The root path will be of the form yyyy/mm[/tripdir],
|
|
|
|
*/
|
2019-07-24 21:54:46 +00:00
|
|
|
static int dive_directory(const char *root, const git_tree_entry *entry, const char *name, int timeoff, struct git_parser_state *state)
|
2014-03-07 03:27:28 +00:00
|
|
|
{
|
|
|
|
int yyyy = -1, mm = -1, dd = -1;
|
|
|
|
int h, m, s;
|
2014-05-12 07:53:28 +00:00
|
|
|
int mday_off, month_off, year_off;
|
2014-03-07 03:27:28 +00:00
|
|
|
struct tm tm;
|
|
|
|
|
2014-05-12 07:53:28 +00:00
|
|
|
/* Skip the '-' before the time */
|
|
|
|
mday_off = timeoff;
|
|
|
|
if (!mday_off || name[--mday_off] != '-')
|
2014-03-07 03:27:28 +00:00
|
|
|
return GIT_WALK_SKIP;
|
2014-05-12 07:53:28 +00:00
|
|
|
/* Skip the day name */
|
|
|
|
while (mday_off > 0 && name[--mday_off] != '-')
|
|
|
|
/* nothing */;
|
|
|
|
|
|
|
|
mday_off = mday_off - 2;
|
|
|
|
month_off = mday_off - 3;
|
|
|
|
year_off = month_off - 5;
|
|
|
|
if (mday_off < 0)
|
2014-03-07 03:27:28 +00:00
|
|
|
return GIT_WALK_SKIP;
|
|
|
|
|
2015-06-19 18:45:24 +00:00
|
|
|
/* Get the time of day -- parse both time formats so we can read old repos when not on Windows */
|
2019-07-24 22:26:51 +00:00
|
|
|
if (sscanf(name + timeoff, "%d:%d:%d", &h, &m, &s) != 3 && sscanf(name + timeoff, "%d=%d=%d", &h, &m, &s) != 3)
|
2014-03-07 03:27:28 +00:00
|
|
|
return GIT_WALK_SKIP;
|
|
|
|
if (!validate_time(h, m, s))
|
|
|
|
return GIT_WALK_SKIP;
|
|
|
|
|
git dive loading: actually insert the dives into the dive table
The biggest part of this commit is the comment about the woeful state of
the "git_tree_walk()" interface - the interface is not really very good
for seeing any recursive state, since it just walks the tree pretty much
linearly.
But the only real recursive state we care about is the trip, and in all
normal situations the "trip this dive is in" is the same thing as "what
was the last trip directory we traversed", so a linear walk works fine.
The one exception is if a dive isn't in a trip at all, in which case
"last trip directory" obviously isn't what we want.
But rather than do our own tree walking by hand (and just passing the
trip information in the natural recursive manner when traversing the
tree), we hack around it by just looking at the path to the dive.
That one-liner trivial hack has now generated about 20 lines of
explanation of it.
ANYWAY. With this, we parse the dive and trip hierarchy properly, and
instead of just printing out the data, we might as well insert the dives
and trips into the subsurface data structures.
Note: the only data we have about the dive and trip right now is what is
visible in the directory structure, since we don't look at the actual
dive file at all (not even the name of it, which contains the dive
number). So the end result will be just a sea of empty dives and the
trips they are contained in. The dives have a date and time, and the
trip has a date, though.
So this is *not* useful for actually saving and loading data, but the
data we do load is easily visualized inside subsurface, so as I'm
starting to add real dive data parsing code, it will all be much more
visually satisfying.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2014-03-08 16:35:23 +00:00
|
|
|
/*
|
|
|
|
* Using the "git_tree_walk()" interface is simple, but
|
|
|
|
* it kind of sucks as an interface because there is
|
|
|
|
* no sane way to pass the hierarchy to the callbacks.
|
|
|
|
* The "payload" is a fixed one-time thing: we'd like
|
|
|
|
* the "current trip" to be passed down to the dives
|
|
|
|
* that get parsed under that trip, but we can't.
|
|
|
|
*
|
|
|
|
* So "active_trip" is not the trip that is in the hierarchy
|
|
|
|
* _above_ us, it's just the trip that was _before_ us. But
|
|
|
|
* if a dive is not in a trip at all, we can't tell.
|
|
|
|
*
|
|
|
|
* We could just do a better walker that passes the
|
|
|
|
* return value around, but we hack around this by
|
|
|
|
* instead looking at the one hierarchical piece of
|
|
|
|
* data we have: the pathname to the current entry.
|
|
|
|
*
|
|
|
|
* This is pretty hacky. The magic '8' is the length
|
|
|
|
* of a pathname of the form 'yyyy/mm/'.
|
|
|
|
*/
|
|
|
|
if (strlen(root) == 8)
|
2019-07-24 21:54:46 +00:00
|
|
|
finish_active_trip(state);
|
git dive loading: actually insert the dives into the dive table
The biggest part of this commit is the comment about the woeful state of
the "git_tree_walk()" interface - the interface is not really very good
for seeing any recursive state, since it just walks the tree pretty much
linearly.
But the only real recursive state we care about is the trip, and in all
normal situations the "trip this dive is in" is the same thing as "what
was the last trip directory we traversed", so a linear walk works fine.
The one exception is if a dive isn't in a trip at all, in which case
"last trip directory" obviously isn't what we want.
But rather than do our own tree walking by hand (and just passing the
trip information in the natural recursive manner when traversing the
tree), we hack around it by just looking at the path to the dive.
That one-liner trivial hack has now generated about 20 lines of
explanation of it.
ANYWAY. With this, we parse the dive and trip hierarchy properly, and
instead of just printing out the data, we might as well insert the dives
and trips into the subsurface data structures.
Note: the only data we have about the dive and trip right now is what is
visible in the directory structure, since we don't look at the actual
dive file at all (not even the name of it, which contains the dive
number). So the end result will be just a sea of empty dives and the
trips they are contained in. The dives have a date and time, and the
trip has a date, though.
So this is *not* useful for actually saving and loading data, but the
data we do load is easily visualized inside subsurface, so as I'm
starting to add real dive data parsing code, it will all be much more
visually satisfying.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2014-03-08 16:35:23 +00:00
|
|
|
|
2014-03-07 03:27:28 +00:00
|
|
|
/*
|
|
|
|
* Get the date. The day of the month is in the dive directory
|
|
|
|
* name, the year and month might be in the path leading up
|
|
|
|
* to it.
|
|
|
|
*/
|
|
|
|
dd = atoi(name + mday_off);
|
|
|
|
if (year_off < 0) {
|
|
|
|
if (sscanf(root, "%d/%d", &yyyy, &mm) != 2)
|
|
|
|
return GIT_WALK_SKIP;
|
|
|
|
} else
|
|
|
|
yyyy = atoi(name + year_off);
|
|
|
|
if (month_off >= 0)
|
|
|
|
mm = atoi(name + month_off);
|
|
|
|
|
|
|
|
if (!validate_date(yyyy, mm, dd))
|
|
|
|
return GIT_WALK_SKIP;
|
|
|
|
|
|
|
|
/* Ok, close enough. We've gotten sufficient information */
|
|
|
|
memset(&tm, 0, sizeof(tm));
|
|
|
|
tm.tm_hour = h;
|
|
|
|
tm.tm_min = m;
|
|
|
|
tm.tm_sec = s;
|
2016-04-28 22:13:30 +00:00
|
|
|
tm.tm_year = yyyy;
|
2014-03-07 03:27:28 +00:00
|
|
|
tm.tm_mon = mm-1;
|
|
|
|
tm.tm_mday = dd;
|
|
|
|
|
2019-07-24 21:54:46 +00:00
|
|
|
finish_active_dive(state);
|
|
|
|
create_new_dive(utc_mktime(&tm), state);
|
|
|
|
memcpy(state->active_dive->git_id, git_tree_entry_id(entry)->id, 20);
|
2014-03-07 03:27:28 +00:00
|
|
|
return GIT_WALK_OK;
|
|
|
|
}
|
|
|
|
|
2019-07-24 21:54:46 +00:00
|
|
|
static int picture_directory(const char *root, const char *name, struct git_parser_state *state)
|
2014-06-29 18:21:52 +00:00
|
|
|
{
|
2018-05-22 07:07:42 +00:00
|
|
|
UNUSED(root);
|
|
|
|
UNUSED(name);
|
2019-07-24 21:54:46 +00:00
|
|
|
if (!state->active_dive)
|
2014-06-29 18:21:52 +00:00
|
|
|
return GIT_WALK_SKIP;
|
|
|
|
return GIT_WALK_OK;
|
|
|
|
}
|
|
|
|
|
2014-03-07 03:27:28 +00:00
|
|
|
/*
|
|
|
|
* Return the length of the string without the unique part.
|
|
|
|
*/
|
|
|
|
static int nonunique_length(const char *str)
|
|
|
|
{
|
|
|
|
int len = 0;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
char c = *str++;
|
|
|
|
if (!c || c == '~')
|
|
|
|
return len;
|
|
|
|
len++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* When hitting a directory node, we have a couple of cases:
|
|
|
|
*
|
|
|
|
* - It's just a date entry - all numeric (either year or month):
|
|
|
|
*
|
|
|
|
* [yyyy|mm]
|
|
|
|
*
|
|
|
|
* We don't do anything with these, we just traverse into them.
|
|
|
|
* The numeric data will show up as part of the full path when
|
|
|
|
* we hit more interesting entries.
|
|
|
|
*
|
|
|
|
* - It's a trip directory. The name will be of the form
|
|
|
|
*
|
|
|
|
* nn-alphabetic[~hex]
|
|
|
|
*
|
|
|
|
* where 'nn' is the day of the month (year and month will be
|
|
|
|
* encoded in the path leading up to this).
|
|
|
|
*
|
|
|
|
* - It's a dive directory. The name will be of the form
|
|
|
|
*
|
2015-06-21 13:12:33 +00:00
|
|
|
* [[yyyy-]mm-]nn-ddd-hh=mm=ss[~hex]
|
|
|
|
*
|
|
|
|
* (older versions had this as [[yyyy-]mm-]nn-ddd-hh:mm:ss[~hex]
|
|
|
|
* but that faile on Windows)
|
2014-03-07 03:27:28 +00:00
|
|
|
*
|
|
|
|
* which describes the date and time of a dive (yyyy and mm
|
|
|
|
* are optional, and may be encoded in the path leading up to
|
|
|
|
* the dive).
|
|
|
|
*
|
2014-06-29 18:21:52 +00:00
|
|
|
* - It is a per-dive picture directory ("Pictures")
|
|
|
|
*
|
2014-03-07 03:27:28 +00:00
|
|
|
* - It's some random non-dive-data directory.
|
|
|
|
*
|
2015-06-21 13:12:33 +00:00
|
|
|
* If it doesn't match the above patterns, we'll ignore them
|
|
|
|
* for dive loading purposes, and not even recurse into them.
|
2014-03-07 03:27:28 +00:00
|
|
|
*/
|
2019-07-24 21:54:46 +00:00
|
|
|
static int walk_tree_directory(const char *root, const git_tree_entry *entry, struct git_parser_state *state)
|
2014-03-07 03:27:28 +00:00
|
|
|
{
|
|
|
|
const char *name = git_tree_entry_name(entry);
|
|
|
|
int digits = 0, len;
|
|
|
|
char c;
|
|
|
|
|
2014-06-29 18:21:52 +00:00
|
|
|
if (!strcmp(name, "Pictures"))
|
2019-07-24 21:54:46 +00:00
|
|
|
return picture_directory(root, name, state);
|
2014-06-29 18:21:52 +00:00
|
|
|
|
2015-02-12 08:14:50 +00:00
|
|
|
if (!strcmp(name, "01-Divesites"))
|
|
|
|
return GIT_WALK_OK;
|
|
|
|
|
2014-03-07 03:27:28 +00:00
|
|
|
while (isdigit(c = name[digits]))
|
|
|
|
digits++;
|
|
|
|
|
|
|
|
/* Doesn't start with two or four digits? Skip */
|
|
|
|
if (digits != 4 && digits != 2)
|
|
|
|
return GIT_WALK_SKIP;
|
|
|
|
|
|
|
|
/* Only digits? Do nothing, but recurse into it */
|
|
|
|
if (!c)
|
|
|
|
return GIT_WALK_OK;
|
|
|
|
|
|
|
|
/* All valid cases need to have a slash following */
|
|
|
|
if (c != '-')
|
|
|
|
return GIT_WALK_SKIP;
|
|
|
|
|
|
|
|
/* Do a quick check for a common dive case */
|
|
|
|
len = nonunique_length(name);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We know the len is at least 3, because we had at least
|
|
|
|
* two digits and a dash
|
|
|
|
*/
|
2015-06-19 18:45:24 +00:00
|
|
|
if (name[len-3] == ':' || name[len-3] == '=')
|
2019-07-24 21:54:46 +00:00
|
|
|
return dive_directory(root, entry, name, len-8, state);
|
2014-03-07 03:27:28 +00:00
|
|
|
|
|
|
|
if (digits != 2)
|
|
|
|
return GIT_WALK_SKIP;
|
|
|
|
|
2019-07-24 21:54:46 +00:00
|
|
|
return dive_trip_directory(root, name, state);
|
2014-03-07 03:27:28 +00:00
|
|
|
}
|
|
|
|
|
2019-07-24 21:17:26 +00:00
|
|
|
static git_blob *git_tree_entry_blob(git_repository *repo, const git_tree_entry *entry)
|
2014-03-07 03:27:28 +00:00
|
|
|
{
|
2014-03-08 21:11:50 +00:00
|
|
|
const git_oid *id = git_tree_entry_id(entry);
|
|
|
|
git_blob *blob;
|
|
|
|
|
|
|
|
if (git_blob_lookup(&blob, repo, id))
|
|
|
|
return NULL;
|
|
|
|
return blob;
|
|
|
|
}
|
|
|
|
|
2014-03-09 19:19:41 +00:00
|
|
|
static struct divecomputer *create_new_dc(struct dive *dive)
|
|
|
|
{
|
|
|
|
struct divecomputer *dc = &dive->dc;
|
|
|
|
|
|
|
|
while (dc->next)
|
|
|
|
dc = dc->next;
|
|
|
|
/* Did we already fill that in? */
|
|
|
|
if (dc->samples || dc->model || dc->when) {
|
|
|
|
struct divecomputer *newdc = calloc(1, sizeof(*newdc));
|
2014-05-14 06:07:58 +00:00
|
|
|
if (!newdc)
|
|
|
|
return NULL;
|
|
|
|
dc->next = newdc;
|
|
|
|
dc = newdc;
|
2014-03-09 19:19:41 +00:00
|
|
|
}
|
2014-05-14 06:07:58 +00:00
|
|
|
dc->when = dive->when;
|
|
|
|
dc->duration = dive->duration;
|
2014-03-09 19:19:41 +00:00
|
|
|
return dc;
|
|
|
|
}
|
|
|
|
|
2014-03-08 21:11:50 +00:00
|
|
|
/*
|
|
|
|
* We should *really* try to delay the dive computer data parsing
|
|
|
|
* until necessary, in order to reduce load-time. The parsing is
|
|
|
|
* cheap, but the loading of the git blob into memory can be pretty
|
|
|
|
* costly.
|
|
|
|
*/
|
2019-07-24 21:54:46 +00:00
|
|
|
static int parse_divecomputer_entry(struct git_parser_state *state, const git_tree_entry *entry, const char *suffix)
|
2014-03-08 21:11:50 +00:00
|
|
|
{
|
2018-05-22 07:07:42 +00:00
|
|
|
UNUSED(suffix);
|
2019-07-24 21:54:46 +00:00
|
|
|
git_blob *blob = git_tree_entry_blob(state->repo, entry);
|
2014-03-09 19:19:41 +00:00
|
|
|
|
2014-03-08 21:11:50 +00:00
|
|
|
if (!blob)
|
|
|
|
return report_error("Unable to read divecomputer file");
|
2014-03-09 19:19:41 +00:00
|
|
|
|
2019-07-24 21:54:46 +00:00
|
|
|
state->active_dc = create_new_dc(state->active_dive);
|
|
|
|
for_each_line(blob, divecomputer_parser, state);
|
2014-03-08 21:11:50 +00:00
|
|
|
git_blob_free(blob);
|
2019-07-24 21:54:46 +00:00
|
|
|
state->active_dc = NULL;
|
2014-03-08 21:11:50 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-04-03 22:31:59 +00:00
|
|
|
/*
|
|
|
|
* NOTE! The "git_id" for the dive is the hash for the whole dive directory.
|
|
|
|
* As such, it covers not just the dive, but the divecomputers and the
|
|
|
|
* pictures too. So if any of the dive computers change, the dive cache
|
|
|
|
* has to be invalidated too.
|
|
|
|
*/
|
2019-07-24 21:54:46 +00:00
|
|
|
static int parse_dive_entry(struct git_parser_state *state, const git_tree_entry *entry, const char *suffix)
|
2014-03-08 21:11:50 +00:00
|
|
|
{
|
2019-07-24 21:54:46 +00:00
|
|
|
struct dive *dive = state->active_dive;
|
|
|
|
git_blob *blob = git_tree_entry_blob(state->repo, entry);
|
2014-03-08 21:11:50 +00:00
|
|
|
if (!blob)
|
|
|
|
return report_error("Unable to read dive file");
|
|
|
|
if (*suffix)
|
2019-07-24 22:26:51 +00:00
|
|
|
dive->number = atoi(suffix + 1);
|
2019-07-24 21:54:46 +00:00
|
|
|
clear_weightsystem_table(&state->active_dive->weightsystems);
|
|
|
|
state->o2pressure_sensor = 1;
|
|
|
|
for_each_line(blob, dive_parser, state);
|
2014-03-08 21:11:50 +00:00
|
|
|
git_blob_free(blob);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-07-24 21:54:46 +00:00
|
|
|
static int parse_site_entry(struct git_parser_state *state, const git_tree_entry *entry, const char *suffix)
|
2015-02-12 08:14:50 +00:00
|
|
|
{
|
|
|
|
if (*suffix == '\0')
|
|
|
|
return report_error("Dive site without uuid");
|
2015-08-24 17:37:18 +00:00
|
|
|
uint32_t uuid = strtoul(suffix, NULL, 16);
|
2019-07-24 21:54:46 +00:00
|
|
|
state->active_site = alloc_or_get_dive_site(uuid, &dive_site_table);
|
|
|
|
git_blob *blob = git_tree_entry_blob(state->repo, entry);
|
2015-02-12 08:14:50 +00:00
|
|
|
if (!blob)
|
|
|
|
return report_error("Unable to read dive site file");
|
2019-07-24 21:54:46 +00:00
|
|
|
for_each_line(blob, site_parser, state);
|
|
|
|
state->active_site = NULL;
|
2015-02-12 08:14:50 +00:00
|
|
|
git_blob_free(blob);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-07-24 21:54:46 +00:00
|
|
|
static int parse_trip_entry(struct git_parser_state *state, const git_tree_entry *entry)
|
2014-03-08 21:11:50 +00:00
|
|
|
{
|
2019-07-24 21:54:46 +00:00
|
|
|
git_blob *blob = git_tree_entry_blob(state->repo, entry);
|
2014-03-08 21:11:50 +00:00
|
|
|
if (!blob)
|
|
|
|
return report_error("Unable to read trip file");
|
2019-07-24 21:54:46 +00:00
|
|
|
for_each_line(blob, trip_parser, state);
|
2014-03-08 21:11:50 +00:00
|
|
|
git_blob_free(blob);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-07-24 21:54:46 +00:00
|
|
|
static int parse_settings_entry(struct git_parser_state *state, const git_tree_entry *entry)
|
2014-03-10 01:31:36 +00:00
|
|
|
{
|
2019-07-24 21:54:46 +00:00
|
|
|
git_blob *blob = git_tree_entry_blob(state->repo, entry);
|
2014-03-10 01:31:36 +00:00
|
|
|
if (!blob)
|
|
|
|
return report_error("Unable to read settings file");
|
|
|
|
for_each_line(blob, settings_parser, NULL);
|
|
|
|
git_blob_free(blob);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-07-24 21:54:46 +00:00
|
|
|
static int parse_picture_entry(struct git_parser_state *state, const git_tree_entry *entry, const char *name)
|
2014-06-29 18:21:52 +00:00
|
|
|
{
|
|
|
|
git_blob *blob;
|
|
|
|
int hh, mm, ss, offset;
|
|
|
|
char sign;
|
|
|
|
|
|
|
|
/*
|
2015-06-21 13:12:33 +00:00
|
|
|
* The format of the picture name files is just the offset within
|
|
|
|
* the dive in form [[+-]hh=mm=ss (previously [[+-]hh:mm:ss, but
|
|
|
|
* that didn't work on Windows), possibly followed by a hash to
|
|
|
|
* make the filename unique (which we can just ignore).
|
2014-06-29 18:21:52 +00:00
|
|
|
*/
|
2015-06-21 13:12:33 +00:00
|
|
|
if (sscanf(name, "%c%d:%d:%d", &sign, &hh, &mm, &ss) != 4 &&
|
|
|
|
sscanf(name, "%c%d=%d=%d", &sign, &hh, &mm, &ss) != 4)
|
2014-06-29 18:21:52 +00:00
|
|
|
return report_error("Unknown file name %s", name);
|
2019-07-24 22:26:51 +00:00
|
|
|
offset = ss + 60 * (mm + 60 * hh);
|
2014-06-29 18:21:52 +00:00
|
|
|
if (sign == '-')
|
|
|
|
offset = -offset;
|
|
|
|
|
2019-07-24 21:54:46 +00:00
|
|
|
blob = git_tree_entry_blob(state->repo, entry);
|
2014-06-29 18:21:52 +00:00
|
|
|
if (!blob)
|
2015-06-21 13:12:33 +00:00
|
|
|
return report_error("Unable to read picture file");
|
2014-06-29 18:21:52 +00:00
|
|
|
|
2019-07-24 21:54:46 +00:00
|
|
|
state->active_pic = alloc_picture();
|
|
|
|
state->active_pic->offset.seconds = offset;
|
2014-06-29 18:21:52 +00:00
|
|
|
|
2019-07-24 21:54:46 +00:00
|
|
|
for_each_line(blob, picture_parser, state);
|
|
|
|
dive_add_picture(state->active_dive, state->active_pic);
|
2014-06-29 18:21:52 +00:00
|
|
|
git_blob_free(blob);
|
2019-07-24 21:54:46 +00:00
|
|
|
state->active_pic = NULL;
|
2014-06-29 18:21:52 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-07-24 21:54:46 +00:00
|
|
|
static int walk_tree_file(const char *root, const git_tree_entry *entry, struct git_parser_state *state)
|
2014-03-08 21:11:50 +00:00
|
|
|
{
|
2019-07-24 21:54:46 +00:00
|
|
|
struct dive *dive = state->active_dive;
|
|
|
|
dive_trip_t *trip = state->active_trip;
|
2014-03-08 21:11:50 +00:00
|
|
|
const char *name = git_tree_entry_name(entry);
|
2015-09-20 17:11:09 +00:00
|
|
|
if (verbose > 1)
|
2020-04-17 20:02:25 +00:00
|
|
|
SSRF_INFO("git load handling file %s\n", name);
|
2014-06-29 18:21:52 +00:00
|
|
|
switch (*name) {
|
|
|
|
case '-': case '+':
|
|
|
|
if (dive)
|
2019-07-24 21:54:46 +00:00
|
|
|
return parse_picture_entry(state, entry, name);
|
2014-06-29 18:21:52 +00:00
|
|
|
break;
|
|
|
|
case 'D':
|
|
|
|
if (dive && !strncmp(name, "Divecomputer", 12))
|
2019-07-24 21:54:46 +00:00
|
|
|
return parse_divecomputer_entry(state, entry, name + 12);
|
2014-06-29 18:21:52 +00:00
|
|
|
if (dive && !strncmp(name, "Dive", 4))
|
2019-07-24 21:54:46 +00:00
|
|
|
return parse_dive_entry(state, entry, name + 4);
|
2014-06-29 18:21:52 +00:00
|
|
|
break;
|
2015-02-12 08:14:50 +00:00
|
|
|
case 'S':
|
|
|
|
if (!strncmp(name, "Site", 4))
|
2019-07-24 21:54:46 +00:00
|
|
|
return parse_site_entry(state, entry, name + 5);
|
2015-03-23 01:53:16 +00:00
|
|
|
break;
|
2014-06-29 18:21:52 +00:00
|
|
|
case '0':
|
|
|
|
if (trip && !strcmp(name, "00-Trip"))
|
2019-07-24 21:54:46 +00:00
|
|
|
return parse_trip_entry(state, entry);
|
2014-06-29 18:21:52 +00:00
|
|
|
if (!strcmp(name, "00-Subsurface"))
|
2019-07-24 21:54:46 +00:00
|
|
|
return parse_settings_entry(state, entry);
|
2014-06-29 18:21:52 +00:00
|
|
|
break;
|
|
|
|
}
|
2014-03-08 21:11:50 +00:00
|
|
|
report_error("Unknown file %s%s (%p %p)", root, name, dive, trip);
|
|
|
|
return GIT_WALK_SKIP;
|
2014-03-07 03:27:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int walk_tree_cb(const char *root, const git_tree_entry *entry, void *payload)
|
|
|
|
{
|
2019-07-24 21:54:46 +00:00
|
|
|
struct git_parser_state *state = payload;
|
2014-03-07 03:27:28 +00:00
|
|
|
git_filemode_t mode = git_tree_entry_filemode(entry);
|
|
|
|
|
|
|
|
if (mode == GIT_FILEMODE_TREE)
|
2019-07-24 21:54:46 +00:00
|
|
|
return walk_tree_directory(root, entry, state);
|
2014-03-07 03:27:28 +00:00
|
|
|
|
2019-07-24 21:54:46 +00:00
|
|
|
walk_tree_file(root, entry, state);
|
2014-03-08 21:11:50 +00:00
|
|
|
/* Ignore failed blob loads */
|
|
|
|
return GIT_WALK_OK;
|
2014-03-07 03:27:28 +00:00
|
|
|
}
|
|
|
|
|
2019-07-24 21:54:46 +00:00
|
|
|
static int load_dives_from_tree(git_repository *repo, git_tree *tree, struct git_parser_state *state)
|
2014-03-07 03:27:28 +00:00
|
|
|
{
|
2019-07-24 21:54:46 +00:00
|
|
|
git_tree_walk(tree, GIT_TREEWALK_PRE, walk_tree_cb, state);
|
2014-03-07 03:27:28 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-03-13 22:42:45 +00:00
|
|
|
void clear_git_id(void)
|
|
|
|
{
|
2020-04-04 10:22:50 +00:00
|
|
|
free((void *)saved_git_id);
|
2014-03-13 22:42:45 +00:00
|
|
|
saved_git_id = NULL;
|
|
|
|
}
|
|
|
|
|
2019-07-25 05:25:54 +00:00
|
|
|
void set_git_id(const struct git_oid *id)
|
2014-03-13 22:42:45 +00:00
|
|
|
{
|
2020-04-04 10:22:50 +00:00
|
|
|
char git_id_buffer[GIT_OID_HEXSZ + 1];
|
2014-03-13 22:42:45 +00:00
|
|
|
|
|
|
|
git_oid_tostr(git_id_buffer, sizeof(git_id_buffer), id);
|
2020-04-04 10:22:50 +00:00
|
|
|
free((void *)saved_git_id);
|
|
|
|
saved_git_id = strdup(git_id_buffer);
|
2014-03-13 22:42:45 +00:00
|
|
|
}
|
|
|
|
|
2015-12-27 17:56:27 +00:00
|
|
|
static int find_commit(git_repository *repo, const char *branch, git_commit **commit_p)
|
2014-03-07 03:27:28 +00:00
|
|
|
{
|
git access: allow arbitrary revision specifiers on reading
Commit 13e2210d75bb ("Allow remote branch names when reading a git
object tree") made it possible to read (but not write) remote branches,
which is very convenient when you just want to look at somebody elses
dives in a shared repository.
However, it was really quite stupidly done - both overly complicated,
and overly restrictive.
It's much better and simpler to just allow general git revision
specifications, which includes branches (both remote and local) as a
simple case, but also allows general git revision expressions. So you
can tag things, and use a tag-name instead. Or you can say that you
want to look at the previous save, by using the "branchname^" syntax.
Or, you can use the git reflog, and do things like
subsurface ~/scuba/[linus@{two.days.ago}]
to see the dives that your repository contained two days ago.
Obviously, you will not be able to save to this kind of ref-spec (and I
really will have to make error handling work better), but for browsing
state it's quite useful.
And in git terms, this is actually simpler than the "lets try to first
see if we have a local branch of that name, and then if we have a remote
one", as shown by the fact that this removes more lines than it adds.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2014-03-14 16:51:19 +00:00
|
|
|
git_object *object;
|
2014-03-07 03:27:28 +00:00
|
|
|
|
git access: allow arbitrary revision specifiers on reading
Commit 13e2210d75bb ("Allow remote branch names when reading a git
object tree") made it possible to read (but not write) remote branches,
which is very convenient when you just want to look at somebody elses
dives in a shared repository.
However, it was really quite stupidly done - both overly complicated,
and overly restrictive.
It's much better and simpler to just allow general git revision
specifications, which includes branches (both remote and local) as a
simple case, but also allows general git revision expressions. So you
can tag things, and use a tag-name instead. Or you can say that you
want to look at the previous save, by using the "branchname^" syntax.
Or, you can use the git reflog, and do things like
subsurface ~/scuba/[linus@{two.days.ago}]
to see the dives that your repository contained two days ago.
Obviously, you will not be able to save to this kind of ref-spec (and I
really will have to make error handling work better), but for browsing
state it's quite useful.
And in git terms, this is actually simpler than the "lets try to first
see if we have a local branch of that name, and then if we have a remote
one", as shown by the fact that this removes more lines than it adds.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2014-03-14 16:51:19 +00:00
|
|
|
if (git_revparse_single(&object, repo, branch))
|
|
|
|
return report_error("Unable to look up revision '%s'", branch);
|
2015-12-27 17:56:27 +00:00
|
|
|
if (git_object_peel((git_object **)commit_p, object, GIT_OBJ_COMMIT))
|
git access: allow arbitrary revision specifiers on reading
Commit 13e2210d75bb ("Allow remote branch names when reading a git
object tree") made it possible to read (but not write) remote branches,
which is very convenient when you just want to look at somebody elses
dives in a shared repository.
However, it was really quite stupidly done - both overly complicated,
and overly restrictive.
It's much better and simpler to just allow general git revision
specifications, which includes branches (both remote and local) as a
simple case, but also allows general git revision expressions. So you
can tag things, and use a tag-name instead. Or you can say that you
want to look at the previous save, by using the "branchname^" syntax.
Or, you can use the git reflog, and do things like
subsurface ~/scuba/[linus@{two.days.ago}]
to see the dives that your repository contained two days ago.
Obviously, you will not be able to save to this kind of ref-spec (and I
really will have to make error handling work better), but for browsing
state it's quite useful.
And in git terms, this is actually simpler than the "lets try to first
see if we have a local branch of that name, and then if we have a remote
one", as shown by the fact that this removes more lines than it adds.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2014-03-14 16:51:19 +00:00
|
|
|
return report_error("Revision '%s' is not a valid commit", branch);
|
2015-12-27 17:56:27 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-07-24 21:54:46 +00:00
|
|
|
static int do_git_load(git_repository *repo, const char *branch, struct git_parser_state *state)
|
2015-12-27 17:56:27 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
git_commit *commit;
|
|
|
|
git_tree *tree;
|
|
|
|
|
|
|
|
ret = find_commit(repo, branch, &commit);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2014-03-13 22:42:45 +00:00
|
|
|
if (git_commit_tree(&tree, commit))
|
|
|
|
return report_error("Could not look up tree of commit in branch '%s'", branch);
|
2017-06-18 06:50:22 +00:00
|
|
|
git_storage_update_progress(translate("gettextFromC", "Load dives from local cache"));
|
2019-07-24 21:54:46 +00:00
|
|
|
ret = load_dives_from_tree(repo, tree, state);
|
2017-06-18 06:50:22 +00:00
|
|
|
if (!ret) {
|
2014-03-13 22:42:45 +00:00
|
|
|
set_git_id(git_commit_id(commit));
|
2017-06-18 06:50:22 +00:00
|
|
|
git_storage_update_progress(translate("gettextFromC", "Successfully opened dive data"));
|
|
|
|
}
|
2014-03-13 22:42:45 +00:00
|
|
|
git_object_free((git_object *)tree);
|
2017-06-18 06:50:22 +00:00
|
|
|
|
2014-03-07 03:27:28 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-12-27 17:56:27 +00:00
|
|
|
const char *get_sha(git_repository *repo, const char *branch)
|
|
|
|
{
|
2019-07-24 22:26:51 +00:00
|
|
|
static char git_id_buffer[GIT_OID_HEXSZ + 1];
|
2015-12-27 17:56:27 +00:00
|
|
|
git_commit *commit;
|
|
|
|
if (find_commit(repo, branch, &commit))
|
|
|
|
return NULL;
|
|
|
|
git_oid_tostr(git_id_buffer, sizeof(git_id_buffer), (const git_oid *)commit);
|
|
|
|
return git_id_buffer;
|
|
|
|
}
|
|
|
|
|
2014-03-12 21:12:58 +00:00
|
|
|
/*
|
|
|
|
* Like git_save_dives(), this silently returns a negative
|
|
|
|
* value if it's not a git repository at all (so that you
|
|
|
|
* can try to load it some other way.
|
|
|
|
*
|
|
|
|
* If it is a git repository, we return zero for success,
|
|
|
|
* or report an error and return 1 if the load failed.
|
|
|
|
*/
|
2020-03-17 21:49:29 +00:00
|
|
|
int git_load_dives(struct git_repository *repo, const char *branch, struct dive_table *table, struct trip_table *trips, struct dive_site_table *sites)
|
2014-03-07 03:27:28 +00:00
|
|
|
{
|
2014-03-15 00:55:07 +00:00
|
|
|
int ret;
|
2019-07-24 21:54:46 +00:00
|
|
|
struct git_parser_state state = { 0 };
|
|
|
|
state.repo = repo;
|
2020-03-17 21:49:29 +00:00
|
|
|
state.table = table;
|
|
|
|
state.trips = trips;
|
|
|
|
state.sites = sites;
|
2014-03-15 00:55:07 +00:00
|
|
|
|
|
|
|
if (repo == dummy_git_repository)
|
|
|
|
return report_error("Unable to open git repository at '%s'", branch);
|
2019-07-24 21:54:46 +00:00
|
|
|
ret = do_git_load(repo, branch, &state);
|
2014-03-07 03:27:28 +00:00
|
|
|
git_repository_free(repo);
|
2014-03-12 21:12:58 +00:00
|
|
|
free((void *)branch);
|
2019-07-24 21:54:46 +00:00
|
|
|
finish_active_dive(&state);
|
|
|
|
finish_active_trip(&state);
|
2014-03-07 03:27:28 +00:00
|
|
|
return ret;
|
|
|
|
}
|