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>
|
|
|
|
|
|
|
|
#include "dive.h"
|
|
|
|
#include "device.h"
|
|
|
|
#include "membuffer.h"
|
|
|
|
|
2014-03-13 22:42:45 +00:00
|
|
|
const char *saved_git_id = NULL;
|
|
|
|
|
2014-03-09 19:19:41 +00:00
|
|
|
struct keyword_action {
|
|
|
|
const char *keyword;
|
|
|
|
void (*fn)(char *, struct membuffer *, void *);
|
|
|
|
};
|
|
|
|
#define ARRAY_SIZE(array) (sizeof(array)/sizeof(array[0]))
|
|
|
|
|
|
|
|
extern degrees_t parse_degrees(char *buf, char **end);
|
|
|
|
static void parse_dive_gps(char *line, struct membuffer *str, void *_dive)
|
2014-03-08 23:59:39 +00:00
|
|
|
{
|
2014-03-09 19:19:41 +00:00
|
|
|
struct dive *dive = _dive;
|
|
|
|
|
|
|
|
dive->latitude = parse_degrees(line, &line);
|
|
|
|
dive->longitude = parse_degrees(line, &line);
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *get_utf8(struct membuffer *b)
|
|
|
|
{
|
|
|
|
int len = b->len;
|
|
|
|
char *res;
|
|
|
|
|
|
|
|
if (!len)
|
|
|
|
return NULL;
|
|
|
|
res = malloc(len+1);
|
|
|
|
if (res) {
|
|
|
|
memcpy(res, b->buffer, len);
|
|
|
|
res[len] = 0;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
d.mm = rint(1000*ascii_strtod(line, NULL));
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
2014-03-09 21:45:20 +00:00
|
|
|
static volume_t get_volume(const char *line)
|
|
|
|
{
|
|
|
|
volume_t v;
|
|
|
|
v.mliter = rint(1000*ascii_strtod(line, NULL));
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
static weight_t get_weight(const char *line)
|
|
|
|
{
|
|
|
|
weight_t w;
|
|
|
|
w.grams = rint(1000*ascii_strtod(line, NULL));
|
|
|
|
return w;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
p.mbar = rint(1000*ascii_strtod(line, NULL));
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2014-03-10 03:55:29 +00:00
|
|
|
static int get_salinity(const char *line)
|
|
|
|
{
|
|
|
|
return rint(10*ascii_strtod(line, NULL));
|
|
|
|
}
|
|
|
|
|
2014-03-09 21:45:20 +00:00
|
|
|
static fraction_t get_fraction(const char *line)
|
|
|
|
{
|
|
|
|
fraction_t f;
|
|
|
|
f.permille = rint(10*ascii_strtod(line, NULL));
|
|
|
|
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);
|
|
|
|
tm.tm_year = yyyy - 1900;
|
|
|
|
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);
|
|
|
|
d.seconds = m*60+s;
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
2014-11-16 23:11:18 +00:00
|
|
|
static enum dive_comp_type get_dctype(const char *line)
|
|
|
|
{
|
|
|
|
for (enum dive_comp_type i = 0; i < NUM_DC_TYPE; i++) {
|
|
|
|
if (strcmp(line, dctype_text[i]) == 0)
|
|
|
|
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-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); }
|
|
|
|
|
|
|
|
static void parse_dive_location(char *line, struct membuffer *str, void *_dive)
|
|
|
|
{ struct dive *dive = _dive; dive->location = get_utf8(str); }
|
|
|
|
|
|
|
|
static void parse_dive_divemaster(char *line, struct membuffer *str, void *_dive)
|
|
|
|
{ struct dive *dive = _dive; dive->divemaster = get_utf8(str); }
|
|
|
|
|
|
|
|
static void parse_dive_buddy(char *line, struct membuffer *str, void *_dive)
|
|
|
|
{ struct dive *dive = _dive; dive->buddy = get_utf8(str); }
|
|
|
|
|
|
|
|
static void parse_dive_suit(char *line, struct membuffer *str, void *_dive)
|
|
|
|
{ struct dive *dive = _dive; dive->suit = get_utf8(str); }
|
|
|
|
|
|
|
|
static void parse_dive_notes(char *line, struct membuffer *str, void *_dive)
|
|
|
|
{ struct dive *dive = _dive; dive->notes = get_utf8(str); }
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We can have multiple tags in the membuffer. They are separated by
|
|
|
|
* NUL bytes.
|
|
|
|
*/
|
|
|
|
static void parse_dive_tags(char *line, struct membuffer *str, void *_dive)
|
|
|
|
{
|
|
|
|
struct dive *dive = _dive;
|
|
|
|
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)
|
Get rid of crazy empty tag_list element at the start
So this is totally unrelated to the git repository format, except for
the fact that I noticed it while writing the git saving code.
The subsurface divetag list handling is being stupid, and has a
initial dummy entry at the head of the list for no good reason.
I say "no good reason", because there *is* a reason for it: it allows
code to avoid the special case of empty list and adding entries to
before the first entry etc etc. But that reason is a really *bad*
reason, because it's valid only because people don't understand basic
list manipulation and pointers to pointers.
So get rid of the dummy element, and do things right instead - by
passing a *pointer* to the list, instead of the list. And then when
traversing the list and looking for a place to insert things, don't go
to the next entry - just update the "pointer to pointer" to point to
the address of the next entry. Each entry in a C linked list is no
different than the list itself, so you can use the pointer to the
pointer to the next entry as a pointer to the list.
This is a pet peeve of mine. The real beauty of pointers can never be
understood unless you understand the indirection they allow. People
who grew up with Pascal and were corrupted by that mindset are
mentally stunted. Niklaus Wirth has a lot to answer for!
But never fear. You too can overcome that mental limitation, it just
needs some brain exercise. Reading this patch may help. In particular,
contemplate the new "taglist_add_divetag()".
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2014-03-10 17:18:13 +00:00
|
|
|
taglist_add_tag(&dive->tag_list, tag);
|
2014-03-09 19:19:41 +00:00
|
|
|
len -= taglen;
|
|
|
|
if (!len)
|
|
|
|
return;
|
|
|
|
tag += taglen+1;
|
|
|
|
len--;
|
|
|
|
}
|
2014-03-08 23:59:39 +00:00
|
|
|
}
|
|
|
|
|
2014-03-09 19:19:41 +00:00
|
|
|
static void parse_dive_airtemp(char *line, struct membuffer *str, void *_dive)
|
|
|
|
{ struct dive *dive = _dive; dive->airtemp = get_temperature(line); }
|
|
|
|
|
|
|
|
static void parse_dive_watertemp(char *line, struct membuffer *str, void *_dive)
|
|
|
|
{ struct dive *dive = _dive; dive->watertemp = get_temperature(line); }
|
|
|
|
|
|
|
|
static void parse_dive_duration(char *line, struct membuffer *str, void *_dive)
|
|
|
|
{ struct dive *dive = _dive; dive->duration = get_duration(line); }
|
|
|
|
|
|
|
|
static void parse_dive_rating(char *line, struct membuffer *str, void *_dive)
|
|
|
|
{ struct dive *dive = _dive; dive->rating = get_index(line); }
|
|
|
|
|
|
|
|
static void parse_dive_visibility(char *line, struct membuffer *str, void *_dive)
|
|
|
|
{ struct dive *dive = _dive; dive->visibility = get_index(line); }
|
|
|
|
|
|
|
|
static void parse_dive_notrip(char *line, struct membuffer *str, void *_dive)
|
|
|
|
{ struct dive *dive = _dive; dive->tripflag = NO_TRIP; }
|
|
|
|
|
2014-03-09 21:45:20 +00:00
|
|
|
/* 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)
|
|
|
|
{
|
|
|
|
char *key = line, *val, c;
|
|
|
|
|
|
|
|
while ((c = *line) != 0) {
|
|
|
|
if (isspace(c) || c == '=')
|
|
|
|
break;
|
|
|
|
line++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (c == '=')
|
|
|
|
*line++ = 0;
|
|
|
|
val = line;
|
|
|
|
|
|
|
|
while ((c = *line) != 0) {
|
|
|
|
if (isspace(c))
|
|
|
|
break;
|
|
|
|
line++;
|
|
|
|
}
|
|
|
|
if (c)
|
|
|
|
*line++ = 0;
|
|
|
|
|
|
|
|
fn(fndata, key, val);
|
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int cylinder_index, weightsystem_index;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
/* This is handled by the "get_utf8()" */
|
|
|
|
if (!strcmp(key, "description"))
|
|
|
|
return;
|
|
|
|
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
|
|
|
}
|
2014-03-09 21:45:20 +00:00
|
|
|
report_error("Unknown cylinder key/value pair (%s/%s)", key, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void parse_dive_cylinder(char *line, struct membuffer *str, void *_dive)
|
|
|
|
{
|
|
|
|
struct dive *dive = _dive;
|
|
|
|
cylinder_t *cylinder = dive->cylinder + cylinder_index;
|
|
|
|
|
|
|
|
cylinder_index++;
|
|
|
|
cylinder->type.description = get_utf8(str);
|
|
|
|
for (;;) {
|
|
|
|
char c;
|
|
|
|
while (isspace(c = *line))
|
|
|
|
line++;
|
|
|
|
if (!c)
|
|
|
|
break;
|
|
|
|
line = parse_keyvalue_entry(parse_cylinder_keyvalue, cylinder, line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
/* This is handled by the "get_utf8()" */
|
|
|
|
if (!strcmp(key, "description"))
|
|
|
|
return;
|
|
|
|
report_error("Unknown weightsystem key/value pair (%s/%s)", key, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void parse_dive_weightsystem(char *line, struct membuffer *str, void *_dive)
|
|
|
|
{
|
|
|
|
struct dive *dive = _dive;
|
|
|
|
weightsystem_t *ws = dive->weightsystem + weightsystem_index;
|
|
|
|
|
|
|
|
weightsystem_index++;
|
|
|
|
ws->description = get_utf8(str);
|
|
|
|
for (;;) {
|
|
|
|
char c;
|
|
|
|
while (isspace(c = *line))
|
|
|
|
line++;
|
|
|
|
if (!c)
|
|
|
|
break;
|
|
|
|
line = parse_keyvalue_entry(parse_weightsystem_keyvalue, ws, line);
|
|
|
|
}
|
|
|
|
}
|
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;
|
|
|
|
|
|
|
|
while ((c = *p) >= 'a' && c <= 'z')
|
|
|
|
p++;
|
|
|
|
if (p == line)
|
|
|
|
return -1;
|
|
|
|
switch (c) {
|
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
case ' ':
|
|
|
|
*p++ = 0;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Standard binary search in a table */
|
|
|
|
low = 0;
|
|
|
|
high = nr_action;
|
|
|
|
while (low < high) {
|
|
|
|
unsigned mid = (low+high)/2;
|
|
|
|
struct keyword_action *a = action + mid;
|
|
|
|
int cmp = strcmp(line, a->keyword);
|
|
|
|
if (!cmp) {
|
|
|
|
a->fn(p, str, data);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (cmp < 0)
|
|
|
|
high = mid;
|
|
|
|
else
|
|
|
|
low = mid+1;
|
|
|
|
}
|
|
|
|
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")) {
|
|
|
|
sample->sensor = atoi(value);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
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-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)
|
|
|
|
{
|
|
|
|
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 */
|
2014-03-09 20:13:48 +00:00
|
|
|
switch (*unit) {
|
|
|
|
case 'm':
|
|
|
|
sample->depth.mm = rint(1000*val);
|
|
|
|
break;
|
|
|
|
case 'b':
|
|
|
|
sample->cylinderpressure.mbar = rint(1000*val);
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
static struct sample *new_sample(struct divecomputer *dc)
|
|
|
|
{
|
|
|
|
struct sample *sample = prepare_sample(dc);
|
|
|
|
if (sample != dc->sample) {
|
|
|
|
memcpy(sample, sample-1, sizeof(struct sample));
|
|
|
|
sample->cylinderpressure.mbar = 0;
|
|
|
|
}
|
|
|
|
return sample;
|
|
|
|
}
|
|
|
|
|
2014-03-09 19:19:41 +00:00
|
|
|
static void sample_parser(char *line, struct divecomputer *dc)
|
2014-03-08 23:59:39 +00:00
|
|
|
{
|
2014-03-10 04:12:13 +00:00
|
|
|
int m, s = 0;
|
2014-03-09 21:26:39 +00:00
|
|
|
struct sample *sample = new_sample(dc);
|
2014-03-09 20:13:48 +00:00
|
|
|
|
|
|
|
m = strtol(line, &line, 10);
|
|
|
|
if (*line == ':')
|
|
|
|
s = strtol(line+1, &line, 10);
|
|
|
|
sample->time.seconds = m*60+s;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
char c;
|
|
|
|
|
|
|
|
while (isspace(c = *line))
|
|
|
|
line++;
|
|
|
|
if (!c)
|
|
|
|
break;
|
|
|
|
/* Less common sample entries have a name */
|
|
|
|
if (c >= 'a' && c <= 'z') {
|
2014-03-09 21:45:20 +00:00
|
|
|
line = parse_keyvalue_entry(parse_sample_keyvalue, sample, line);
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
finish_sample(dc);
|
2014-03-09 19:19:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void parse_dc_airtemp(char *line, struct membuffer *str, void *_dc)
|
|
|
|
{ struct divecomputer *dc = _dc; dc->airtemp = get_temperature(line); }
|
|
|
|
|
|
|
|
static void parse_dc_date(char *line, struct membuffer *str, void *_dc)
|
|
|
|
{ struct divecomputer *dc = _dc; update_date(&dc->when, line); }
|
|
|
|
|
|
|
|
static void parse_dc_deviceid(char *line, struct membuffer *str, void *_dc)
|
|
|
|
{ struct divecomputer *dc = _dc; dc->deviceid = get_hex(line); }
|
|
|
|
|
|
|
|
static void parse_dc_diveid(char *line, struct membuffer *str, void *_dc)
|
|
|
|
{ struct divecomputer *dc = _dc; dc->diveid = get_hex(line); }
|
|
|
|
|
|
|
|
static void parse_dc_duration(char *line, struct membuffer *str, void *_dc)
|
|
|
|
{ struct divecomputer *dc = _dc; dc->duration = get_duration(line); }
|
|
|
|
|
2014-11-16 23:11:18 +00:00
|
|
|
static void parse_dc_dctype(char *line, struct membuffer *str, void *_dc)
|
|
|
|
{ struct divecomputer *dc = _dc; dc->dctype = get_dctype(line); }
|
|
|
|
|
2014-03-09 19:19:41 +00:00
|
|
|
static void parse_dc_maxdepth(char *line, struct membuffer *str, void *_dc)
|
|
|
|
{ struct divecomputer *dc = _dc; dc->maxdepth = get_depth(line); }
|
|
|
|
|
|
|
|
static void parse_dc_meandepth(char *line, struct membuffer *str, void *_dc)
|
|
|
|
{ struct divecomputer *dc = _dc; dc->meandepth = get_depth(line); }
|
|
|
|
|
|
|
|
static void parse_dc_model(char *line, struct membuffer *str, void *_dc)
|
|
|
|
{ struct divecomputer *dc = _dc; dc->model = get_utf8(str); }
|
|
|
|
|
|
|
|
static void parse_dc_surfacepressure(char *line, struct membuffer *str, void *_dc)
|
|
|
|
{ struct divecomputer *dc = _dc; dc->surface_pressure = get_pressure(line); }
|
|
|
|
|
2014-03-10 03:55:29 +00:00
|
|
|
static void parse_dc_salinity(char *line, struct membuffer *str, void *_dc)
|
|
|
|
{ struct divecomputer *dc = _dc; dc->salinity = get_salinity(line); }
|
|
|
|
|
2014-03-09 19:19:41 +00:00
|
|
|
static void parse_dc_surfacetime(char *line, struct membuffer *str, void *_dc)
|
|
|
|
{ struct divecomputer *dc = _dc; dc->surfacetime = get_duration(line); }
|
|
|
|
|
|
|
|
static void parse_dc_time(char *line, struct membuffer *str, void *_dc)
|
|
|
|
{ struct divecomputer *dc = _dc; update_time(&dc->when, line); }
|
|
|
|
|
|
|
|
static void parse_dc_watertemp(char *line, struct membuffer *str, void *_dc)
|
|
|
|
{ struct divecomputer *dc = _dc; dc->watertemp = get_temperature(line); }
|
|
|
|
|
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
|
|
|
static void parse_event_keyvalue(void *_event, const char *key, const char *value)
|
|
|
|
{
|
|
|
|
struct event *event = _event;
|
|
|
|
int val = atoi(value);
|
|
|
|
|
|
|
|
if (!strcmp(key, "type")) {
|
|
|
|
event->type = val;
|
|
|
|
} else if (!strcmp(key, "flags")) {
|
|
|
|
event->flags = val;
|
|
|
|
} else if (!strcmp(key, "value")) {
|
|
|
|
event->value = val;
|
|
|
|
} else if (!strcmp(key, "name")) {
|
|
|
|
/* We get the name from the string handling */
|
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" */
|
|
|
|
event->gas.index = 1+get_index(value);
|
|
|
|
} else if (!strcmp(key, "o2")) {
|
|
|
|
event->gas.mix.o2 = get_fraction(value);
|
|
|
|
} else if (!strcmp(key, "he")) {
|
|
|
|
event->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' */
|
|
|
|
static void parse_dc_keyvalue(char *line, struct membuffer *str, void *_dc)
|
|
|
|
{
|
|
|
|
const char *key, *value;
|
|
|
|
struct divecomputer *dc = _dc;
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
add_extra_data(dc, key, value);
|
|
|
|
}
|
|
|
|
|
2014-03-09 19:19:41 +00:00
|
|
|
static void parse_dc_event(char *line, struct membuffer *str, void *_dc)
|
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;
|
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
|
|
|
const char *name;
|
|
|
|
struct divecomputer *dc = _dc;
|
2014-08-17 18:26:21 +00:00
|
|
|
struct event event = { 0 }, *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 == ':')
|
|
|
|
s = strtol(line+1, &line, 10);
|
|
|
|
event.time.seconds = m*60+s;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
char c;
|
|
|
|
while (isspace(c = *line))
|
|
|
|
line++;
|
|
|
|
if (!c)
|
|
|
|
break;
|
|
|
|
line = parse_keyvalue_entry(parse_event_keyvalue, &event, line);
|
|
|
|
}
|
|
|
|
|
|
|
|
name = "";
|
|
|
|
if (str->len)
|
|
|
|
name = mb_cstring(str);
|
2014-08-17 18:26:21 +00:00
|
|
|
ev = add_event(dc, event.time.seconds, event.type, event.flags, event.value, name);
|
|
|
|
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)
|
|
|
|
*/
|
|
|
|
ev->gas.index = event.gas.index-1;
|
|
|
|
if (event.gas.mix.o2.permille || event.gas.mix.he.permille)
|
|
|
|
ev->gas.mix = event.gas.mix;
|
|
|
|
}
|
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
|
|
|
|
|
|
|
static void parse_trip_date(char *line, struct membuffer *str, void *_trip)
|
|
|
|
{ dive_trip_t *trip = _trip; update_date(&trip->when, line); }
|
|
|
|
|
|
|
|
static void parse_trip_time(char *line, struct membuffer *str, void *_trip)
|
|
|
|
{ dive_trip_t *trip = _trip; update_time(&trip->when, line); }
|
|
|
|
|
|
|
|
static void parse_trip_location(char *line, struct membuffer *str, void *_trip)
|
|
|
|
{ dive_trip_t *trip = _trip; trip->location = get_utf8(str); }
|
|
|
|
|
|
|
|
static void parse_trip_notes(char *line, struct membuffer *str, void *_trip)
|
|
|
|
{ dive_trip_t *trip = _trip; trip->notes = get_utf8(str); }
|
|
|
|
|
2014-03-10 01:31:36 +00:00
|
|
|
static void parse_settings_autogroup(char *line, struct membuffer *str, void *_unused)
|
|
|
|
{ set_autogroup(1); }
|
|
|
|
|
2014-04-11 06:17:35 +00:00
|
|
|
static void parse_settings_userid(char *line, struct membuffer *str, void *_unused)
|
|
|
|
{
|
|
|
|
if (line) {
|
|
|
|
set_save_userid_local(true);
|
|
|
|
set_userid(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.
|
|
|
|
*/
|
|
|
|
#define VERSION 2
|
|
|
|
static void parse_settings_version(char *line, struct membuffer *str, void *_unused)
|
|
|
|
{
|
|
|
|
int version = atoi(line);
|
|
|
|
if (version > VERSION)
|
|
|
|
report_error("Git save file version %d is newer than version %d I know about", version, VERSION);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The string in the membuffer is the version string of subsurface that saved things, just FYI */
|
|
|
|
static void parse_settings_subsurface(char *line, struct membuffer *str, void *_unused)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
struct divecomputerid {
|
|
|
|
const char *model;
|
|
|
|
const char *nickname;
|
|
|
|
const char *firmware;
|
|
|
|
const char *serial;
|
|
|
|
const char *cstr;
|
|
|
|
unsigned int deviceid;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void parse_divecomputerid_keyvalue(void *_cid, const char *key, const char *value)
|
|
|
|
{
|
|
|
|
struct divecomputerid *cid = _cid;
|
|
|
|
|
|
|
|
if (*value == '"') {
|
|
|
|
value = cid->cstr;
|
|
|
|
cid->cstr += strlen(cid->cstr)+1;
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
report_error("Unknow divecomputerid key/value pair (%s/%s)", key, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* We keep the "next" string in "id.cstr" and update it as we use it.
|
|
|
|
*/
|
|
|
|
static void parse_settings_divecomputerid(char *line, struct membuffer *str, void *_unused)
|
|
|
|
{
|
|
|
|
struct divecomputerid id = { mb_cstring(str) };
|
|
|
|
|
|
|
|
id.cstr = id.model + strlen(id.model) + 1;
|
|
|
|
|
|
|
|
/* Skip the '"' that stood for the model string */
|
|
|
|
line++;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
char c;
|
|
|
|
while (isspace(c = *line))
|
|
|
|
line++;
|
|
|
|
if (!c)
|
|
|
|
break;
|
|
|
|
line = parse_keyvalue_entry(parse_divecomputerid_keyvalue, &id, line);
|
|
|
|
}
|
|
|
|
create_device_node(id.model, id.deviceid, id.serial, id.firmware, id.nickname);
|
|
|
|
}
|
|
|
|
|
2014-06-29 18:21:52 +00:00
|
|
|
static void parse_picture_filename(char *line, struct membuffer *str, void *_pic)
|
|
|
|
{
|
|
|
|
struct picture *pic = _pic;
|
|
|
|
pic->filename = get_utf8(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void parse_picture_gps(char *line, struct membuffer *str, void *_pic)
|
|
|
|
{
|
|
|
|
struct picture *pic = _pic;
|
|
|
|
|
|
|
|
pic->latitude = parse_degrees(line, &line);
|
|
|
|
pic->longitude = parse_degrees(line, &line);
|
|
|
|
}
|
|
|
|
|
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),
|
2014-11-06 19:23:34 +00:00
|
|
|
D(event), D(keyvalue), D(maxdepth), D(meandepth), D(model), D(salinity),
|
2014-03-09 19:19:41 +00:00
|
|
|
D(surfacepressure), D(surfacetime), D(time), D(watertemp),
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Sample lines start with a space or a number */
|
|
|
|
static void divecomputer_parser(char *line, struct membuffer *str, void *_dc)
|
|
|
|
{
|
|
|
|
char c = *line;
|
|
|
|
if (c < 'a' || c > 'z')
|
|
|
|
sample_parser(line, _dc);
|
|
|
|
match_action(line, str, _dc, dc_action, ARRAY_SIZE(dc_action));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* These need to be sorted! */
|
|
|
|
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(gps), D(location), D(notes), D(notrip), D(rating), D(suit),
|
|
|
|
D(tags), D(visibility), D(watertemp), D(weightsystem)
|
|
|
|
};
|
|
|
|
|
|
|
|
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 trip_action[] = {
|
|
|
|
#undef D
|
|
|
|
#define D(x) { #x, parse_trip_ ## x }
|
|
|
|
D(date), D(location), D(notes), D(time),
|
|
|
|
};
|
|
|
|
|
|
|
|
static void trip_parser(char *line, struct membuffer *str, void *_trip)
|
|
|
|
{
|
|
|
|
match_action(line, str, _trip, 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 }
|
2014-04-11 06:17:35 +00:00
|
|
|
D(autogroup), D(divecomputerid), D(subsurface), D(userid), D(version),
|
2014-03-10 01:31:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static void settings_parser(char *line, struct membuffer *str, void *_unused)
|
|
|
|
{
|
|
|
|
match_action(line, str, NULL, settings_action, ARRAY_SIZE(settings_action));
|
|
|
|
}
|
|
|
|
|
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 }
|
|
|
|
D(filename), D(gps)
|
|
|
|
};
|
|
|
|
|
|
|
|
static void picture_parser(char *line, struct membuffer *str, void *_pic)
|
|
|
|
{
|
|
|
|
match_action(line, str, _pic, picture_action, ARRAY_SIZE(picture_action));
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-03-09 19:19:41 +00:00
|
|
|
typedef void (line_fn_t)(char *, struct membuffer *, void *);
|
2014-03-10 04:06:18 +00:00
|
|
|
#define MAXLINE 500
|
2014-03-08 23:59:39 +00:00
|
|
|
static unsigned parse_one_line(const char *buf, unsigned size, line_fn_t *fn, void *fndata, struct membuffer *b)
|
|
|
|
{
|
|
|
|
const char *end = buf + size;
|
|
|
|
const char *p = buf;
|
|
|
|
char line[MAXLINE+1];
|
|
|
|
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;
|
|
|
|
fn(line, b, fndata);
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
static void for_each_line(git_blob *blob, line_fn_t *fn, void *fndata)
|
|
|
|
{
|
|
|
|
const char *content = git_blob_rawcontent(blob);
|
|
|
|
unsigned int size = git_blob_rawsize(blob);
|
|
|
|
struct membuffer str = { 0 };
|
|
|
|
|
|
|
|
while (size) {
|
|
|
|
unsigned int n = parse_one_line(content, size, fn, fndata, &str);
|
|
|
|
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
|
|
|
|
|
2014-03-09 19:19:41 +00:00
|
|
|
static struct divecomputer *active_dc;
|
2014-03-07 03:27:28 +00:00
|
|
|
static struct dive *active_dive;
|
|
|
|
static dive_trip_t *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
|
|
|
static void finish_active_trip(void)
|
|
|
|
{
|
|
|
|
dive_trip_t *trip = active_trip;
|
|
|
|
|
|
|
|
if (trip) {
|
|
|
|
active_trip = NULL;
|
|
|
|
insert_trip(&trip);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void finish_active_dive(void)
|
|
|
|
{
|
|
|
|
struct dive *dive = active_dive;
|
|
|
|
|
|
|
|
if (dive) {
|
|
|
|
active_dive = NULL;
|
|
|
|
record_dive(dive);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-07 03:27:28 +00:00
|
|
|
static struct dive *create_new_dive(timestamp_t when)
|
|
|
|
{
|
|
|
|
struct dive *dive = alloc_dive();
|
|
|
|
|
|
|
|
/* We'll fill in more data from the dive file */
|
|
|
|
dive->when = when;
|
|
|
|
|
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
|
|
|
if (active_trip)
|
|
|
|
add_dive_to_trip(dive, active_trip);
|
2014-03-07 03:27:28 +00:00
|
|
|
return dive;
|
|
|
|
}
|
|
|
|
|
|
|
|
static dive_trip_t *create_new_trip(int yyyy, int mm, int dd)
|
|
|
|
{
|
|
|
|
dive_trip_t *trip = calloc(1, sizeof(dive_trip_t));
|
|
|
|
struct tm tm = { 0 };
|
|
|
|
|
|
|
|
/* We'll fill in the real data from the trip descriptor file */
|
|
|
|
tm.tm_year = yyyy;
|
|
|
|
tm.tm_mon = mm-1;
|
|
|
|
tm.tm_mday = dd;
|
|
|
|
trip->when = utc_mktime(&tm);
|
|
|
|
|
|
|
|
return trip;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool validate_date(int yyyy, int mm, int dd)
|
|
|
|
{
|
|
|
|
return yyyy > 1970 && yyyy < 3000 &&
|
|
|
|
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]'
|
|
|
|
*/
|
|
|
|
static int dive_trip_directory(const char *root, const char *name)
|
|
|
|
{
|
|
|
|
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;
|
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
|
|
|
finish_active_trip();
|
2014-03-07 03:27:28 +00:00
|
|
|
active_trip = create_new_trip(yyyy, mm, dd);
|
|
|
|
return GIT_WALK_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Dive directory, name is [[yyyy-]mm-]nn-ddd-hh:mm:ss[~hex],
|
|
|
|
* 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],
|
|
|
|
*/
|
|
|
|
static int dive_directory(const char *root, const char *name, int timeoff)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
|
|
|
/* Get the time of day */
|
|
|
|
if (sscanf(name+timeoff, "%d:%d:%d", &h, &m, &s) != 3)
|
|
|
|
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)
|
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
|
|
|
finish_active_trip();
|
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;
|
|
|
|
tm.tm_year = yyyy - 1900;
|
|
|
|
tm.tm_mon = mm-1;
|
|
|
|
tm.tm_mday = dd;
|
|
|
|
|
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
|
|
|
finish_active_dive();
|
2014-03-07 03:27:28 +00:00
|
|
|
active_dive = create_new_dive(utc_mktime(&tm));
|
|
|
|
return GIT_WALK_OK;
|
|
|
|
}
|
|
|
|
|
2014-06-29 18:21:52 +00:00
|
|
|
static int picture_directory(const char *root, const char *name)
|
|
|
|
{
|
|
|
|
if (!active_dive)
|
|
|
|
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
|
|
|
|
*
|
|
|
|
* [[yyyy-]mm-]nn-ddd-hh:mm:ss[~hex]
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* Subsurface doesn't create these yet, but maybe we'll encode
|
|
|
|
* pictures etc. If it doesn't match the above patterns, we'll
|
|
|
|
* ignore them for dive loading purposes, and not even recurse
|
|
|
|
* into them.
|
|
|
|
*/
|
|
|
|
static int walk_tree_directory(const char *root, const git_tree_entry *entry)
|
|
|
|
{
|
|
|
|
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"))
|
|
|
|
return picture_directory(root, name);
|
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
if (name[len-3] == ':')
|
|
|
|
return dive_directory(root, name, len-8);
|
|
|
|
|
|
|
|
if (digits != 2)
|
|
|
|
return GIT_WALK_SKIP;
|
|
|
|
|
|
|
|
return dive_trip_directory(root, name);
|
|
|
|
}
|
|
|
|
|
2014-03-08 21:11:50 +00:00
|
|
|
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.
|
|
|
|
*/
|
|
|
|
static int parse_divecomputer_entry(git_repository *repo, const git_tree_entry *entry, const char *suffix)
|
|
|
|
{
|
|
|
|
git_blob *blob = git_tree_entry_blob(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
|
|
|
|
|
|
|
active_dc = create_new_dc(active_dive);
|
|
|
|
for_each_line(blob, divecomputer_parser, active_dc);
|
2014-03-08 21:11:50 +00:00
|
|
|
git_blob_free(blob);
|
2014-03-09 19:19:41 +00:00
|
|
|
active_dc = NULL;
|
2014-03-08 21:11:50 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int parse_dive_entry(git_repository *repo, const git_tree_entry *entry, const char *suffix)
|
|
|
|
{
|
|
|
|
struct dive *dive = active_dive;
|
|
|
|
git_blob *blob = git_tree_entry_blob(repo, entry);
|
|
|
|
if (!blob)
|
|
|
|
return report_error("Unable to read dive file");
|
|
|
|
if (*suffix)
|
|
|
|
dive->number = atoi(suffix+1);
|
2014-03-09 21:45:20 +00:00
|
|
|
cylinder_index = weightsystem_index = 0;
|
2014-03-08 23:59:39 +00:00
|
|
|
for_each_line(blob, dive_parser, active_dive);
|
2014-03-08 21:11:50 +00:00
|
|
|
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);
|
|
|
|
if (!blob)
|
|
|
|
return report_error("Unable to read trip file");
|
2014-03-08 23:59:39 +00:00
|
|
|
for_each_line(blob, trip_parser, active_trip);
|
2014-03-08 21:11:50 +00:00
|
|
|
git_blob_free(blob);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-03-10 01:31:36 +00:00
|
|
|
static int parse_settings_entry(git_repository *repo, const git_tree_entry *entry)
|
|
|
|
{
|
|
|
|
git_blob *blob = git_tree_entry_blob(repo, entry);
|
|
|
|
if (!blob)
|
|
|
|
return report_error("Unable to read settings file");
|
2014-04-11 06:17:35 +00:00
|
|
|
set_save_userid_local(false);
|
|
|
|
set_userid("");
|
2014-03-10 01:31:36 +00:00
|
|
|
for_each_line(blob, settings_parser, NULL);
|
|
|
|
git_blob_free(blob);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-06-29 18:21:52 +00:00
|
|
|
static int parse_picture_entry(git_repository *repo, const git_tree_entry *entry, const char *name)
|
|
|
|
{
|
|
|
|
git_blob *blob;
|
|
|
|
struct picture *pic;
|
|
|
|
int hh, mm, ss, offset;
|
|
|
|
char sign;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The format of the picture name files is just the offset
|
|
|
|
* within the dive in form [[+-]hh:mm:ss, possibly followed
|
|
|
|
* by a hash to make the filename unique (which we can just
|
|
|
|
* ignore).
|
|
|
|
*/
|
|
|
|
if (sscanf(name, "%c%d:%d:%d", &sign, &hh, &mm, &ss) != 4)
|
|
|
|
return report_error("Unknown file name %s", name);
|
|
|
|
offset = ss + 60*(mm + 60*hh);
|
|
|
|
if (sign == '-')
|
|
|
|
offset = -offset;
|
|
|
|
|
|
|
|
blob = git_tree_entry_blob(repo, entry);
|
|
|
|
if (!blob)
|
|
|
|
return report_error("Unable to read trip file");
|
|
|
|
|
|
|
|
pic = alloc_picture();
|
|
|
|
pic->offset.seconds = offset;
|
|
|
|
dive_add_picture(active_dive, pic);
|
|
|
|
|
|
|
|
for_each_line(blob, picture_parser, pic);
|
|
|
|
git_blob_free(blob);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-03-08 21:11:50 +00:00
|
|
|
static int walk_tree_file(const char *root, const git_tree_entry *entry, git_repository *repo)
|
|
|
|
{
|
|
|
|
struct dive *dive = active_dive;
|
|
|
|
dive_trip_t *trip = active_trip;
|
|
|
|
const char *name = git_tree_entry_name(entry);
|
|
|
|
|
2014-06-29 18:21:52 +00:00
|
|
|
switch (*name) {
|
|
|
|
/* Picture file? They are saved as time offsets in the dive */
|
|
|
|
case '-': case '+':
|
|
|
|
if (dive)
|
|
|
|
return parse_picture_entry(repo, entry, name);
|
|
|
|
break;
|
|
|
|
case 'D':
|
|
|
|
if (dive && !strncmp(name, "Divecomputer", 12))
|
|
|
|
return parse_divecomputer_entry(repo, entry, name+12);
|
|
|
|
if (dive && !strncmp(name, "Dive", 4))
|
|
|
|
return parse_dive_entry(repo, entry, name+4);
|
|
|
|
break;
|
|
|
|
case '0':
|
|
|
|
if (trip && !strcmp(name, "00-Trip"))
|
|
|
|
return parse_trip_entry(repo, entry);
|
|
|
|
if (!strcmp(name, "00-Subsurface"))
|
|
|
|
return parse_settings_entry(repo, entry);
|
|
|
|
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)
|
|
|
|
{
|
2014-03-08 21:11:50 +00:00
|
|
|
git_repository *repo = payload;
|
2014-03-07 03:27:28 +00:00
|
|
|
git_filemode_t mode = git_tree_entry_filemode(entry);
|
|
|
|
|
|
|
|
if (mode == GIT_FILEMODE_TREE)
|
|
|
|
return walk_tree_directory(root, entry);
|
|
|
|
|
2014-03-08 21:11:50 +00:00
|
|
|
walk_tree_file(root, entry, repo);
|
|
|
|
/* Ignore failed blob loads */
|
|
|
|
return GIT_WALK_OK;
|
2014-03-07 03:27:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int load_dives_from_tree(git_repository *repo, git_tree *tree)
|
|
|
|
{
|
2014-03-08 21:11:50 +00:00
|
|
|
git_tree_walk(tree, GIT_TREEWALK_PRE, walk_tree_cb, repo);
|
2014-03-07 03:27:28 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-03-13 22:42:45 +00:00
|
|
|
void clear_git_id(void)
|
|
|
|
{
|
|
|
|
saved_git_id = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_git_id(const struct git_oid * id)
|
|
|
|
{
|
|
|
|
static char git_id_buffer[GIT_OID_HEXSZ+1];
|
|
|
|
|
|
|
|
git_oid_tostr(git_id_buffer, sizeof(git_id_buffer), id);
|
|
|
|
saved_git_id = git_id_buffer;
|
|
|
|
}
|
|
|
|
|
2014-03-07 03:27:28 +00:00
|
|
|
static int do_git_load(git_repository *repo, const char *branch)
|
|
|
|
{
|
|
|
|
int ret;
|
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-13 22:42:45 +00:00
|
|
|
git_commit *commit;
|
|
|
|
git_tree *tree;
|
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);
|
|
|
|
if (git_object_peel((git_object **)&commit, object, GIT_OBJ_COMMIT))
|
|
|
|
return report_error("Revision '%s' is not a valid commit", branch);
|
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);
|
|
|
|
ret = load_dives_from_tree(repo, tree);
|
|
|
|
if (!ret)
|
|
|
|
set_git_id(git_commit_id(commit));
|
|
|
|
git_object_free((git_object *)tree);
|
2014-03-07 03:27:28 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
int git_load_dives(struct git_repository *repo, const char *branch)
|
2014-03-07 03:27:28 +00:00
|
|
|
{
|
2014-03-15 00:55:07 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (repo == dummy_git_repository)
|
|
|
|
return report_error("Unable to open git repository at '%s'", branch);
|
|
|
|
ret = do_git_load(repo, branch);
|
2014-03-07 03:27:28 +00:00
|
|
|
git_repository_free(repo);
|
2014-03-12 21:12:58 +00:00
|
|
|
free((void *)branch);
|
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
|
|
|
finish_active_dive();
|
|
|
|
finish_active_trip();
|
2014-03-07 03:27:28 +00:00
|
|
|
return ret;
|
|
|
|
}
|