Save and load dive sites in git format

Update the version to 3.

Continue to read version 2 files and create dive sites on the fly.

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Dirk Hohndel 2015-02-12 00:14:50 -08:00
parent 4b15b9dfe9
commit 05948530e9
2 changed files with 80 additions and 6 deletions

View file

@ -160,6 +160,9 @@ static void parse_dive_suit(char *line, struct membuffer *str, void *_dive)
static void parse_dive_notes(char *line, struct membuffer *str, void *_dive)
{ struct dive *dive = _dive; dive->notes = get_utf8(str); }
static void parse_dive_divesiteid(char *line, struct membuffer *str, void *_dive)
{ struct dive *dive = _dive; dive->dive_site_uuid = get_hex(line); }
/*
* We can have multiple tags in the membuffer. They are separated by
* NUL bytes.
@ -205,6 +208,24 @@ static void parse_dive_visibility(char *line, struct membuffer *str, void *_dive
static void parse_dive_notrip(char *line, struct membuffer *str, void *_dive)
{ struct dive *dive = _dive; dive->tripflag = NO_TRIP; }
static void parse_site_description(char *line, struct membuffer *str, void *_ds)
{ struct dive_site *ds = _ds; ds->description = strdup(mb_cstring(str)); }
static void parse_site_name(char *line, struct membuffer *str, void *_ds)
{ struct dive_site *ds = _ds; ds->name = strdup(mb_cstring(str)); }
static void parse_site_notes(char *line, struct membuffer *str, void *_ds)
{ struct dive_site *ds = _ds; ds->notes = strdup(mb_cstring(str)); }
extern degrees_t parse_degrees(char *buf, char **end);
static void parse_site_gps(char *line, struct membuffer *str, void *_ds)
{
struct dive_site *ds = _ds;
ds->latitude = parse_degrees(line, &line);
ds->longitude = parse_degrees(line, &line);
}
/* Parse key=val parts of samples and cylinders etc */
static char *parse_keyvalue_entry(void (*fn)(void *, const char *, const char *), void *fndata, char *line)
{
@ -672,7 +693,7 @@ static void parse_settings_userid(char *line, struct membuffer *str, void *_unus
* *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.
*/
#define VERSION 2
#define VERSION 3
static void parse_settings_version(char *line, struct membuffer *str, void *_unused)
{
int version = atoi(line);
@ -783,7 +804,7 @@ static void divecomputer_parser(char *line, struct membuffer *str, void *_dc)
struct keyword_action dive_action[] = {
#undef D
#define D(x) { #x, parse_dive_ ## x }
D(airtemp), D(buddy), D(cylinder), D(divemaster), D(duration),
D(airtemp), D(buddy), D(cylinder), D(divemaster), D(divesiteid), D(duration),
D(gps), D(location), D(notes), D(notrip), D(rating), D(suit),
D(tags), D(visibility), D(watertemp), D(weightsystem)
};
@ -793,6 +814,18 @@ static void dive_parser(char *line, struct membuffer *str, void *_dive)
match_action(line, str, _dive, dive_action, ARRAY_SIZE(dive_action));
}
/* These need to be sorted! */
struct keyword_action site_action[] = {
#undef D
#define D(x) { #x, parse_site_ ## x }
D(description), D(gps), D(name), D(notes)
};
static void site_parser(char *line, struct membuffer *str, void *_ds)
{
match_action(line, str, _ds, site_action, ARRAY_SIZE(site_action));
}
/* These need to be sorted! */
struct keyword_action trip_action[] = {
#undef D
@ -1190,6 +1223,9 @@ static int walk_tree_directory(const char *root, const git_tree_entry *entry)
if (!strcmp(name, "Pictures"))
return picture_directory(root, name);
if (!strcmp(name, "01-Divesites"))
return GIT_WALK_OK;
while (isdigit(c = name[digits]))
digits++;
@ -1284,6 +1320,20 @@ static int parse_dive_entry(git_repository *repo, const git_tree_entry *entry, c
return 0;
}
static int parse_site_entry(git_repository *repo, const git_tree_entry *entry, const char *suffix)
{
if (*suffix == '\0')
return report_error("Dive site without uuid");
struct dive_site *ds = alloc_dive_site();
ds->uuid = strtol(suffix, NULL, 16);
git_blob *blob = git_tree_entry_blob(repo, entry);
if (!blob)
return report_error("Unable to read dive site file");
for_each_line(blob, site_parser, ds);
git_blob_free(blob);
return 0;
}
static int parse_trip_entry(git_repository *repo, const git_tree_entry *entry)
{
git_blob *blob = git_tree_entry_blob(repo, entry);
@ -1343,7 +1393,6 @@ static int walk_tree_file(const char *root, const git_tree_entry *entry, git_rep
struct dive *dive = active_dive;
dive_trip_t *trip = active_trip;
const char *name = git_tree_entry_name(entry);
switch (*name) {
/* Picture file? They are saved as time offsets in the dive */
case '-': case '+':
@ -1356,6 +1405,9 @@ static int walk_tree_file(const char *root, const git_tree_entry *entry, git_rep
if (dive && !strncmp(name, "Dive", 4))
return parse_dive_entry(repo, entry, name+4);
break;
case 'S':
if (!strncmp(name, "Site", 4))
return parse_site_entry(repo, entry, name + 5);
case '0':
if (trip && !strcmp(name, "00-Trip"))
return parse_trip_entry(repo, entry);

View file

@ -104,8 +104,6 @@ static void show_utf8(struct membuffer *b, const char *prefix, const char *value
static void save_overview(struct membuffer *b, struct dive *dive)
{
show_gps(b, dive->latitude, dive->longitude);
show_utf8(b, "location ", dive->location, "\n");
show_utf8(b, "divemaster ", dive->divemaster, "\n");
show_utf8(b, "buddy ", dive->buddy, "\n");
show_utf8(b, "suit ", dive->suit, "\n");
@ -390,6 +388,7 @@ static void create_dive_buffer(struct dive *dive, struct membuffer *b)
SAVE("visibility", visibility);
cond_put_format(dive->tripflag == NO_TRIP, b, "notrip\n");
save_tags(b, dive->tag_list);
cond_put_format(dive->dive_site_uuid, b, "divesiteid %8x\n", dive->dive_site_uuid);
save_overview(b, dive);
save_cylinder_info(b, dive);
@ -804,7 +803,7 @@ static void save_one_device(void *_b, const char *model, uint32_t deviceid,
put_string(b, "\n");
}
#define VERSION 2
#define VERSION 3
static void save_settings(git_repository *repo, struct dir *tree)
{
@ -818,6 +817,27 @@ static void save_settings(git_repository *repo, struct dir *tree)
blob_insert(repo, tree, &b, "00-Subsurface");
}
static void save_divesites(git_repository *repo, struct dir *tree)
{
struct dir *subdir;
struct membuffer dirname = { 0 };
put_format(&dirname, "01-Divesites");
subdir = new_directory(repo, tree, &dirname);
for (int i = 0; i < dive_site_table.nr; i++) {
struct membuffer b = { 0 };
struct dive_site *ds = get_dive_site(i);
int size = sizeof("Site-012345678");
char name[size];
snprintf(name, size, "Site-%8x", ds->uuid);
show_utf8(&b, "name ", ds->name, "\n");
show_utf8(&b, "description ", ds->description, "\n");
show_utf8(&b, "notes ", ds->notes, "\n");
show_gps(&b, ds->latitude, ds->longitude);
blob_insert(repo, subdir, &b, name);
}
}
static int create_git_tree(git_repository *repo, struct dir *root, bool select_only)
{
int i;
@ -826,6 +846,8 @@ static int create_git_tree(git_repository *repo, struct dir *root, bool select_o
save_settings(repo, root);
save_divesites(repo, root);
for (trip = dive_trip_list; trip != NULL; trip = trip->next)
trip->index = 0;