2017-04-27 18:18:03 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2017-03-11 20:08:31 +00:00
|
|
|
#ifdef __clang__
|
2016-03-09 18:19:22 +00:00
|
|
|
// Clang has a bug on zero-initialization of C structs.
|
|
|
|
#pragma clang diagnostic ignored "-Wmissing-field-initializers"
|
2017-03-11 20:08:31 +00:00
|
|
|
#endif
|
2016-03-09 18:19:22 +00:00
|
|
|
|
2014-05-23 18:49:49 +00:00
|
|
|
#include "save-html.h"
|
2020-05-01 11:43:52 +00:00
|
|
|
#include "dive.h"
|
core: introduce divelog structure
The parser API was very annoying, as a number of tables
to-be-filled were passed in as pointers. The goal of this
commit is to collect all these tables in a single struct.
This should make it (more or less) clear what is actually
written into the divelog files.
Moreover, it should now be rather easy to search for
instances, where the global logfile is accessed (and it
turns out that there are many!).
The divelog struct does not contain the tables as substructs,
but only collects pointers. The idea is that the "divelog.h"
file can be included without all the other files describing
the numerous tables.
To make it easier to use from C++ parts of the code, the
struct implements a constructor and a destructor. Sadly,
we can't use smart pointers, since the pointers are accessed
from C code. Therfore the constructor and destructor are
quite complex.
The whole commit is large, but was mostly an automatic
conversion.
One oddity of note: the divelog structure also contains
the "autogroup" flag, since that is saved in the divelog.
This actually fixes a bug: Before, when importing dives
from a different log, the autogroup flag was overwritten.
This was probably not intended and does not happen anymore.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2022-11-08 20:31:08 +00:00
|
|
|
#include "divelog.h"
|
2018-02-24 22:28:13 +00:00
|
|
|
#include "qthelper.h"
|
2014-05-23 18:49:49 +00:00
|
|
|
#include "gettext.h"
|
2019-03-04 22:20:29 +00:00
|
|
|
#include "divesite.h"
|
2019-08-05 17:41:15 +00:00
|
|
|
#include "errorhelper.h"
|
2020-10-25 08:14:16 +00:00
|
|
|
#include "event.h"
|
2019-08-05 18:07:10 +00:00
|
|
|
#include "file.h"
|
2020-04-10 07:42:14 +00:00
|
|
|
#include "picture.h"
|
2020-10-25 12:28:55 +00:00
|
|
|
#include "sample.h"
|
2019-05-30 16:29:36 +00:00
|
|
|
#include "tag.h"
|
2020-05-01 12:07:59 +00:00
|
|
|
#include "subsurface-time.h"
|
2019-05-31 14:09:14 +00:00
|
|
|
#include "trip.h"
|
2020-10-25 13:10:52 +00:00
|
|
|
|
2019-03-04 22:20:29 +00:00
|
|
|
#include <stdio.h>
|
2020-10-25 13:10:52 +00:00
|
|
|
#include <string.h>
|
2024-05-30 13:00:28 +00:00
|
|
|
#include <QFile>
|
2014-06-08 04:56:54 +00:00
|
|
|
|
2020-05-29 06:45:38 +00:00
|
|
|
static void write_attribute(struct membuffer *b, const char *att_name, const char *value, const char *separator)
|
2014-06-08 13:09:24 +00:00
|
|
|
{
|
|
|
|
if (!value)
|
|
|
|
value = "--";
|
|
|
|
put_format(b, "\"%s\":\"", att_name);
|
|
|
|
put_HTML_quoted(b, value);
|
2014-08-27 17:19:20 +00:00
|
|
|
put_format(b, "\"%s", separator);
|
2014-06-08 13:09:24 +00:00
|
|
|
}
|
|
|
|
|
2024-05-30 13:00:28 +00:00
|
|
|
static void copy_image_and_overwrite(const std::string &cfileName, const std::string &path, const std::string &cnewName)
|
|
|
|
{
|
|
|
|
QString fileName = QString::fromStdString(cfileName);
|
|
|
|
std::string newName = path + cnewName;
|
|
|
|
QFile file(QString::fromStdString(newName));
|
|
|
|
if (file.exists())
|
|
|
|
file.remove();
|
|
|
|
if (!QFile::copy(fileName, QString::fromStdString(newName)))
|
|
|
|
report_info("copy of %s to %s failed", cfileName.c_str(), newName.c_str());
|
|
|
|
}
|
|
|
|
|
2024-06-07 08:25:09 +00:00
|
|
|
static void save_photos(struct membuffer *b, const char *photos_dir, const struct dive &dive)
|
2014-07-13 21:36:35 +00:00
|
|
|
{
|
2024-06-07 08:25:09 +00:00
|
|
|
if (dive.pictures.empty())
|
2014-08-27 17:19:20 +00:00
|
|
|
return;
|
|
|
|
|
2024-03-10 15:35:53 +00:00
|
|
|
const char *separator = "\"photos\":[";
|
2024-06-07 08:25:09 +00:00
|
|
|
for (auto &picture: dive.pictures) {
|
2014-08-27 17:19:20 +00:00
|
|
|
put_string(b, separator);
|
|
|
|
separator = ", ";
|
2024-03-10 15:41:56 +00:00
|
|
|
std::string fname = get_file_name(local_file_path(picture));
|
2018-08-23 00:05:32 +00:00
|
|
|
put_string(b, "{\"filename\":\"");
|
2024-03-10 15:41:56 +00:00
|
|
|
put_quoted(b, fname.c_str(), 1, 0);
|
2018-08-23 00:05:32 +00:00
|
|
|
put_string(b, "\"}");
|
2024-03-10 15:41:56 +00:00
|
|
|
copy_image_and_overwrite(local_file_path(picture), photos_dir, fname.c_str());
|
2020-04-11 15:41:56 +00:00
|
|
|
}
|
2014-07-13 21:36:35 +00:00
|
|
|
put_string(b, "],");
|
|
|
|
}
|
|
|
|
|
2024-06-07 08:25:09 +00:00
|
|
|
static void write_divecomputers(struct membuffer *b, const struct dive &dive)
|
2014-08-23 19:22:24 +00:00
|
|
|
{
|
|
|
|
put_string(b, "\"divecomputers\":[");
|
2024-03-10 15:35:53 +00:00
|
|
|
const char *separator = "";
|
2024-06-07 08:25:09 +00:00
|
|
|
for (auto &dc: dive.dcs) {
|
2014-08-27 17:19:20 +00:00
|
|
|
put_string(b, separator);
|
|
|
|
separator = ", ";
|
2014-08-23 19:22:24 +00:00
|
|
|
put_format(b, "{");
|
2024-05-27 15:09:48 +00:00
|
|
|
write_attribute(b, "model", dc.model.c_str(), ", ");
|
|
|
|
if (dc.deviceid)
|
|
|
|
put_format(b, "\"deviceid\":\"%08x\", ", dc.deviceid);
|
2014-08-27 17:19:20 +00:00
|
|
|
else
|
|
|
|
put_string(b, "\"deviceid\":\"--\", ");
|
2024-05-27 15:09:48 +00:00
|
|
|
if (dc.diveid)
|
|
|
|
put_format(b, "\"diveid\":\"%08x\" ", dc.diveid);
|
2014-08-27 17:19:20 +00:00
|
|
|
else
|
|
|
|
put_string(b, "\"diveid\":\"--\" ");
|
|
|
|
put_format(b, "}");
|
2014-08-23 19:22:24 +00:00
|
|
|
}
|
|
|
|
put_string(b, "],");
|
|
|
|
}
|
|
|
|
|
2024-06-07 08:25:09 +00:00
|
|
|
static void write_dive_status(struct membuffer *b, const struct dive &dive)
|
2014-07-11 01:26:21 +00:00
|
|
|
{
|
2024-06-07 08:25:09 +00:00
|
|
|
put_format(b, "\"sac\":\"%d\",", dive.sac);
|
|
|
|
put_format(b, "\"otu\":\"%d\",", dive.otu);
|
|
|
|
put_format(b, "\"cns\":\"%d\",", dive.cns);
|
2014-07-11 01:26:21 +00:00
|
|
|
}
|
|
|
|
|
2024-06-07 08:25:09 +00:00
|
|
|
static void put_HTML_bookmarks(struct membuffer *b, const struct dive &dive)
|
2014-06-24 18:40:06 +00:00
|
|
|
{
|
2024-03-10 15:35:53 +00:00
|
|
|
const char *separator = "\"events\":[";
|
2024-06-07 08:25:09 +00:00
|
|
|
for (const auto &ev: dive.dcs[0].events) {
|
2014-08-27 17:19:20 +00:00
|
|
|
put_string(b, separator);
|
|
|
|
separator = ", ";
|
2018-08-23 00:05:32 +00:00
|
|
|
put_string(b, "{\"name\":\"");
|
2024-05-25 06:16:57 +00:00
|
|
|
put_quoted(b, ev.name.c_str(), 1, 0);
|
2018-08-23 00:05:32 +00:00
|
|
|
put_string(b, "\",");
|
2024-05-25 06:16:57 +00:00
|
|
|
put_format(b, "\"value\":\"%d\",", ev.value);
|
|
|
|
put_format(b, "\"type\":\"%d\",", ev.type);
|
|
|
|
put_format(b, "\"time\":\"%d\"}", ev.time.seconds);
|
|
|
|
}
|
2014-06-24 18:40:06 +00:00
|
|
|
put_string(b, "],");
|
|
|
|
}
|
|
|
|
|
2024-06-07 08:25:09 +00:00
|
|
|
static void put_weightsystem_HTML(struct membuffer *b, const struct dive &dive)
|
2014-07-27 19:38:50 +00:00
|
|
|
{
|
|
|
|
put_string(b, "\"Weights\":[");
|
|
|
|
|
2024-03-10 15:35:53 +00:00
|
|
|
const char *separator = "";
|
2014-08-27 17:19:20 +00:00
|
|
|
|
2024-06-07 08:25:09 +00:00
|
|
|
for (auto &ws: dive.weightsystems) {
|
2019-06-26 15:21:03 +00:00
|
|
|
int grams = ws.weight.grams;
|
2014-07-27 19:38:50 +00:00
|
|
|
|
2014-08-27 17:19:20 +00:00
|
|
|
put_string(b, separator);
|
|
|
|
separator = ", ";
|
2014-07-27 19:38:50 +00:00
|
|
|
put_string(b, "{");
|
2014-10-19 17:30:11 +00:00
|
|
|
put_HTML_weight_units(b, grams, "\"weight\":\"", "\",");
|
2024-05-29 05:03:03 +00:00
|
|
|
write_attribute(b, "description", ws.description.c_str(), " ");
|
2014-08-27 17:19:20 +00:00
|
|
|
put_string(b, "}");
|
2014-07-27 19:38:50 +00:00
|
|
|
}
|
|
|
|
put_string(b, "],");
|
|
|
|
}
|
|
|
|
|
2024-06-07 08:25:09 +00:00
|
|
|
static void put_cylinder_HTML(struct membuffer *b, const struct dive &dive)
|
2014-06-08 13:09:24 +00:00
|
|
|
{
|
2024-03-10 15:35:53 +00:00
|
|
|
const char *separator = "\"Cylinders\":[";
|
2014-06-08 13:09:24 +00:00
|
|
|
|
2024-06-07 08:25:09 +00:00
|
|
|
if (dive.cylinders.empty())
|
2014-08-27 17:19:20 +00:00
|
|
|
put_string(b, separator);
|
2014-06-08 13:09:24 +00:00
|
|
|
|
2024-06-07 08:25:09 +00:00
|
|
|
for (auto &cyl: dive.cylinders) {
|
2014-08-27 17:19:20 +00:00
|
|
|
put_format(b, "%s{", separator);
|
|
|
|
separator = ", ";
|
2024-05-28 19:31:11 +00:00
|
|
|
write_attribute(b, "Type", cyl.type.description.c_str(), ", ");
|
|
|
|
if (cyl.type.size.mliter) {
|
|
|
|
int volume = cyl.type.size.mliter;
|
|
|
|
if (prefs.units.volume == units::CUFT && cyl.type.workingpressure.mbar)
|
|
|
|
volume = lrint(volume * bar_to_atm(cyl.type.workingpressure.mbar / 1000.0));
|
2015-06-18 00:25:44 +00:00
|
|
|
put_HTML_volume_units(b, volume, "\"Size\":\"", " \", ");
|
2014-06-08 13:09:24 +00:00
|
|
|
} else {
|
2014-08-27 17:19:20 +00:00
|
|
|
write_attribute(b, "Size", "--", ", ");
|
2014-06-08 13:09:24 +00:00
|
|
|
}
|
2024-05-28 19:31:11 +00:00
|
|
|
put_HTML_pressure_units(b, cyl.type.workingpressure, "\"WPressure\":\"", " \", ");
|
2014-06-28 15:14:29 +00:00
|
|
|
|
2024-05-28 19:31:11 +00:00
|
|
|
if (cyl.start.mbar) {
|
|
|
|
put_HTML_pressure_units(b, cyl.start, "\"SPressure\":\"", " \", ");
|
2014-06-28 15:14:29 +00:00
|
|
|
} else {
|
2014-08-27 17:19:20 +00:00
|
|
|
write_attribute(b, "SPressure", "--", ", ");
|
2014-06-28 15:14:29 +00:00
|
|
|
}
|
|
|
|
|
2024-05-28 19:31:11 +00:00
|
|
|
if (cyl.end.mbar) {
|
|
|
|
put_HTML_pressure_units(b, cyl.end, "\"EPressure\":\"", " \", ");
|
2014-06-28 15:14:29 +00:00
|
|
|
} else {
|
2014-08-27 17:19:20 +00:00
|
|
|
write_attribute(b, "EPressure", "--", ", ");
|
2014-06-28 15:14:29 +00:00
|
|
|
}
|
2014-06-08 13:09:24 +00:00
|
|
|
|
2024-05-28 19:31:11 +00:00
|
|
|
if (cyl.gasmix.o2.permille) {
|
|
|
|
put_format(b, "\"O2\":\"%u.%u%%\",", FRACTION_TUPLE(cyl.gasmix.o2.permille, 10));
|
|
|
|
put_format(b, "\"He\":\"%u.%u%%\"", FRACTION_TUPLE(cyl.gasmix.he.permille, 10));
|
2014-06-08 13:09:24 +00:00
|
|
|
} else {
|
2014-08-27 17:19:20 +00:00
|
|
|
write_attribute(b, "O2", "Air", "");
|
2014-06-08 13:09:24 +00:00
|
|
|
}
|
2015-03-09 20:33:14 +00:00
|
|
|
|
2014-08-27 17:19:20 +00:00
|
|
|
put_string(b, "}");
|
2014-06-08 13:09:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
put_string(b, "],");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-06-07 08:25:09 +00:00
|
|
|
static void put_HTML_samples(struct membuffer *b, const struct dive &dive)
|
2014-06-08 04:56:54 +00:00
|
|
|
{
|
2024-06-07 08:25:09 +00:00
|
|
|
put_format(b, "\"maxdepth\":%d,", dive.dcs[0].maxdepth.mm);
|
|
|
|
put_format(b, "\"duration\":%d,", dive.dcs[0].duration.seconds);
|
2014-08-27 17:19:20 +00:00
|
|
|
|
2024-06-07 08:25:09 +00:00
|
|
|
if (dive.dcs[0].samples.empty())
|
2014-08-27 17:19:20 +00:00
|
|
|
return;
|
|
|
|
|
2024-03-10 15:35:53 +00:00
|
|
|
const char *separator = "\"samples\":[";
|
2024-06-07 08:25:09 +00:00
|
|
|
for (auto &s: dive.dcs[0].samples) {
|
2024-05-19 10:38:38 +00:00
|
|
|
put_format(b, "%s[%d,%d,%d,%d]", separator, s.time.seconds, s.depth.mm, s.pressure[0].mbar, s.temperature.mkelvin);
|
2014-08-27 17:19:20 +00:00
|
|
|
separator = ", ";
|
2014-06-08 04:56:54 +00:00
|
|
|
}
|
|
|
|
put_string(b, "],");
|
|
|
|
}
|
2014-05-23 18:49:49 +00:00
|
|
|
|
2024-06-07 08:25:09 +00:00
|
|
|
static void put_HTML_coordinates(struct membuffer *b, const struct dive &dive)
|
2014-08-14 03:32:47 +00:00
|
|
|
{
|
2024-06-07 08:25:09 +00:00
|
|
|
struct dive_site *ds = get_dive_site_for_dive(&dive);
|
2015-02-13 03:01:27 +00:00
|
|
|
if (!ds)
|
|
|
|
return;
|
2018-10-20 18:12:15 +00:00
|
|
|
degrees_t latitude = ds->location.lat;
|
|
|
|
degrees_t longitude = ds->location.lon;
|
2014-08-14 03:32:47 +00:00
|
|
|
|
|
|
|
//don't put coordinates if in (0,0)
|
|
|
|
if (!latitude.udeg && !longitude.udeg)
|
|
|
|
return;
|
|
|
|
|
|
|
|
put_string(b, "\"coordinates\":{");
|
|
|
|
put_degrees(b, latitude, "\"lat\":\"", "\",");
|
2014-08-27 17:19:20 +00:00
|
|
|
put_degrees(b, longitude, "\"lon\":\"", "\"");
|
2014-08-14 03:32:47 +00:00
|
|
|
put_string(b, "},");
|
|
|
|
}
|
|
|
|
|
2024-06-07 08:25:09 +00:00
|
|
|
void put_HTML_date(struct membuffer *b, const struct dive &dive, const char *pre, const char *post)
|
2014-05-23 18:49:49 +00:00
|
|
|
{
|
|
|
|
struct tm tm;
|
2024-06-07 08:25:09 +00:00
|
|
|
utc_mkdate(dive.when, &tm);
|
2016-04-28 22:13:30 +00:00
|
|
|
put_format(b, "%s%04u-%02u-%02u%s", pre, tm.tm_year, tm.tm_mon + 1, tm.tm_mday, post);
|
2014-05-23 18:49:49 +00:00
|
|
|
}
|
|
|
|
|
2014-06-02 17:10:54 +00:00
|
|
|
void put_HTML_quoted(struct membuffer *b, const char *text)
|
2014-05-23 18:49:49 +00:00
|
|
|
{
|
2014-06-02 17:10:54 +00:00
|
|
|
int is_html = 1, is_attribute = 1;
|
|
|
|
put_quoted(b, text, is_attribute, is_html);
|
2014-05-23 18:49:49 +00:00
|
|
|
}
|
|
|
|
|
2024-06-07 08:25:09 +00:00
|
|
|
void put_HTML_notes(struct membuffer *b, const struct dive &dive, const char *pre, const char *post)
|
2014-05-23 18:49:49 +00:00
|
|
|
{
|
2014-07-18 23:49:30 +00:00
|
|
|
put_string(b, pre);
|
2024-06-07 08:25:09 +00:00
|
|
|
if (!dive.notes.empty())
|
|
|
|
put_HTML_quoted(b, dive.notes.c_str());
|
2024-05-29 18:40:18 +00:00
|
|
|
else
|
2014-07-18 23:49:30 +00:00
|
|
|
put_string(b, "--");
|
|
|
|
put_string(b, post);
|
2014-05-23 18:49:49 +00:00
|
|
|
}
|
|
|
|
|
2014-10-19 17:30:11 +00:00
|
|
|
void put_HTML_pressure_units(struct membuffer *b, pressure_t pressure, const char *pre, const char *post)
|
|
|
|
{
|
|
|
|
const char *unit;
|
|
|
|
double value;
|
|
|
|
|
|
|
|
if (!pressure.mbar) {
|
|
|
|
put_format(b, "%s%s", pre, post);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
value = get_pressure_units(pressure.mbar, &unit);
|
|
|
|
put_format(b, "%s%.1f %s%s", pre, value, unit, post);
|
|
|
|
}
|
|
|
|
|
|
|
|
void put_HTML_volume_units(struct membuffer *b, unsigned int ml, const char *pre, const char *post)
|
|
|
|
{
|
|
|
|
const char *unit;
|
|
|
|
double value;
|
|
|
|
int frac;
|
|
|
|
|
|
|
|
value = get_volume_units(ml, &frac, &unit);
|
|
|
|
put_format(b, "%s%.1f %s%s", pre, value, unit, post);
|
|
|
|
}
|
|
|
|
|
|
|
|
void put_HTML_weight_units(struct membuffer *b, unsigned int grams, const char *pre, const char *post)
|
|
|
|
{
|
|
|
|
const char *unit;
|
|
|
|
double value;
|
|
|
|
int frac;
|
|
|
|
|
|
|
|
value = get_weight_units(grams, &frac, &unit);
|
|
|
|
put_format(b, "%s%.1f %s%s", pre, value, unit, post);
|
|
|
|
}
|
|
|
|
|
2024-06-07 08:25:09 +00:00
|
|
|
void put_HTML_time(struct membuffer *b, const struct dive &dive, const char *pre, const char *post)
|
2014-05-23 18:49:49 +00:00
|
|
|
{
|
|
|
|
struct tm tm;
|
2024-06-07 08:25:09 +00:00
|
|
|
utc_mkdate(dive.when, &tm);
|
2014-05-23 18:49:49 +00:00
|
|
|
put_format(b, "%s%02u:%02u:%02u%s", pre, tm.tm_hour, tm.tm_min, tm.tm_sec, post);
|
|
|
|
}
|
|
|
|
|
2024-06-07 08:25:09 +00:00
|
|
|
void put_HTML_depth(struct membuffer *b, const struct dive &dive, const char *pre, const char *post)
|
2016-01-10 10:12:39 +00:00
|
|
|
{
|
|
|
|
const char *unit;
|
|
|
|
double value;
|
2018-09-02 19:21:29 +00:00
|
|
|
const struct units *units_p = get_units();
|
2016-01-10 10:12:39 +00:00
|
|
|
|
2024-06-07 08:25:09 +00:00
|
|
|
if (!dive.maxdepth.mm) {
|
2016-01-10 10:12:39 +00:00
|
|
|
put_format(b, "%s--%s", pre, post);
|
|
|
|
return;
|
|
|
|
}
|
2024-06-07 08:25:09 +00:00
|
|
|
value = get_depth_units(dive.maxdepth.mm, NULL, &unit);
|
2016-01-10 10:12:39 +00:00
|
|
|
|
|
|
|
switch (units_p->length) {
|
2024-03-10 15:35:53 +00:00
|
|
|
case units::METERS:
|
2016-01-10 10:12:39 +00:00
|
|
|
default:
|
|
|
|
put_format(b, "%s%.1f %s%s", pre, value, unit, post);
|
|
|
|
break;
|
2024-03-10 15:35:53 +00:00
|
|
|
case units::FEET:
|
2016-01-10 10:12:39 +00:00
|
|
|
put_format(b, "%s%.0f %s%s", pre, value, unit, post);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-07 08:25:09 +00:00
|
|
|
void put_HTML_airtemp(struct membuffer *b, const struct dive &dive, const char *pre, const char *post)
|
2014-05-23 18:49:49 +00:00
|
|
|
{
|
|
|
|
const char *unit;
|
|
|
|
double value;
|
|
|
|
|
2024-06-07 08:25:09 +00:00
|
|
|
if (!dive.airtemp.mkelvin) {
|
2014-05-27 21:06:56 +00:00
|
|
|
put_format(b, "%s--%s", pre, post);
|
|
|
|
return;
|
|
|
|
}
|
2024-06-07 08:25:09 +00:00
|
|
|
value = get_temp_units(dive.airtemp.mkelvin, &unit);
|
2014-05-27 21:06:56 +00:00
|
|
|
put_format(b, "%s%.1f %s%s", pre, value, unit, post);
|
2014-05-23 18:49:49 +00:00
|
|
|
}
|
|
|
|
|
2024-06-07 08:25:09 +00:00
|
|
|
void put_HTML_watertemp(struct membuffer *b, const struct dive &dive, const char *pre, const char *post)
|
2014-05-23 18:49:49 +00:00
|
|
|
{
|
|
|
|
const char *unit;
|
|
|
|
double value;
|
|
|
|
|
2024-06-07 08:25:09 +00:00
|
|
|
if (!dive.watertemp.mkelvin) {
|
2014-05-27 21:06:56 +00:00
|
|
|
put_format(b, "%s--%s", pre, post);
|
|
|
|
return;
|
|
|
|
}
|
2024-06-07 08:25:09 +00:00
|
|
|
value = get_temp_units(dive.watertemp.mkelvin, &unit);
|
2014-05-27 21:06:56 +00:00
|
|
|
put_format(b, "%s%.1f %s%s", pre, value, unit, post);
|
2014-05-23 18:49:49 +00:00
|
|
|
}
|
|
|
|
|
2024-06-07 08:25:09 +00:00
|
|
|
static void put_HTML_tags(struct membuffer *b, const struct dive &dive, const char *pre, const char *post)
|
2014-05-23 18:49:49 +00:00
|
|
|
{
|
|
|
|
put_string(b, pre);
|
2014-05-28 11:43:57 +00:00
|
|
|
|
2024-06-07 08:25:09 +00:00
|
|
|
if (dive.tags.empty())
|
2014-08-27 17:19:20 +00:00
|
|
|
put_string(b, "[\"--\"");
|
2014-05-28 11:43:57 +00:00
|
|
|
|
2024-03-10 15:35:53 +00:00
|
|
|
const char *separator = "[";
|
2024-06-07 08:25:09 +00:00
|
|
|
for (const divetag *tag: dive.tags) {
|
2014-08-27 17:19:20 +00:00
|
|
|
put_format(b, "%s\"", separator);
|
|
|
|
separator = ", ";
|
2024-05-29 15:57:48 +00:00
|
|
|
put_HTML_quoted(b, tag->name.c_str());
|
2014-08-27 17:19:20 +00:00
|
|
|
put_string(b, "\"");
|
2014-05-23 18:49:49 +00:00
|
|
|
}
|
2014-05-28 17:52:05 +00:00
|
|
|
put_string(b, "]");
|
2014-05-23 18:49:49 +00:00
|
|
|
put_string(b, post);
|
|
|
|
}
|
|
|
|
|
2014-06-24 13:01:49 +00:00
|
|
|
/* if exporting list_only mode, we neglect exporting the samples, bookmarks and cylinders */
|
2024-06-07 08:25:09 +00:00
|
|
|
static void write_one_dive(struct membuffer *b, const struct dive &dive, const char *photos_dir, int *dive_no, bool list_only)
|
2014-05-23 18:49:49 +00:00
|
|
|
{
|
2014-05-27 21:06:56 +00:00
|
|
|
put_string(b, "{");
|
|
|
|
put_format(b, "\"number\":%d,", *dive_no);
|
2024-06-07 08:25:09 +00:00
|
|
|
put_format(b, "\"subsurface_number\":%d,", dive.number);
|
2014-05-27 21:06:56 +00:00
|
|
|
put_HTML_date(b, dive, "\"date\":\"", "\",");
|
|
|
|
put_HTML_time(b, dive, "\"time\":\"", "\",");
|
2024-06-07 08:25:09 +00:00
|
|
|
write_attribute(b, "location", get_dive_location(&dive).c_str(), ", ");
|
2014-08-14 03:32:47 +00:00
|
|
|
put_HTML_coordinates(b, dive);
|
2024-06-07 08:25:09 +00:00
|
|
|
put_format(b, "\"rating\":%d,", dive.rating);
|
|
|
|
put_format(b, "\"visibility\":%d,", dive.visibility);
|
|
|
|
put_format(b, "\"current\":%d,", dive.current);
|
|
|
|
put_format(b, "\"wavesize\":%d,", dive.wavesize);
|
|
|
|
put_format(b, "\"surge\":%d,", dive.surge);
|
|
|
|
put_format(b, "\"chill\":%d,", dive.chill);
|
2014-08-07 14:11:42 +00:00
|
|
|
put_format(b, "\"dive_duration\":\"%u:%02u min\",",
|
2024-06-07 08:25:09 +00:00
|
|
|
FRACTION_TUPLE(dive.duration.seconds, 60));
|
2014-05-27 21:06:56 +00:00
|
|
|
put_string(b, "\"temperature\":{");
|
|
|
|
put_HTML_airtemp(b, dive, "\"air\":\"", "\",");
|
2014-08-27 17:19:20 +00:00
|
|
|
put_HTML_watertemp(b, dive, "\"water\":\"", "\"");
|
2014-05-27 21:06:56 +00:00
|
|
|
put_string(b, " },");
|
2024-06-07 08:25:09 +00:00
|
|
|
write_attribute(b, "buddy", dive.buddy.c_str(), ", ");
|
|
|
|
write_attribute(b, "diveguide", dive.diveguide.c_str(), ", ");
|
|
|
|
write_attribute(b, "suit", dive.suit.c_str(), ", ");
|
2014-05-28 17:52:05 +00:00
|
|
|
put_HTML_tags(b, dive, "\"tags\":", ",");
|
2014-06-24 13:01:49 +00:00
|
|
|
if (!list_only) {
|
|
|
|
put_cylinder_HTML(b, dive);
|
2014-07-27 19:38:50 +00:00
|
|
|
put_weightsystem_HTML(b, dive);
|
2014-06-24 13:01:49 +00:00
|
|
|
put_HTML_samples(b, dive);
|
2014-06-24 18:40:06 +00:00
|
|
|
put_HTML_bookmarks(b, dive);
|
2014-07-16 12:38:20 +00:00
|
|
|
write_dive_status(b, dive);
|
2015-03-13 13:36:52 +00:00
|
|
|
if (photos_dir && strcmp(photos_dir, ""))
|
2014-09-21 14:11:58 +00:00
|
|
|
save_photos(b, photos_dir, dive);
|
2014-08-23 19:22:24 +00:00
|
|
|
write_divecomputers(b, dive);
|
2014-06-24 13:01:49 +00:00
|
|
|
}
|
2014-08-27 17:19:20 +00:00
|
|
|
put_HTML_notes(b, dive, "\"notes\":\"", "\"");
|
|
|
|
put_string(b, "}\n");
|
2014-05-27 21:06:56 +00:00
|
|
|
(*dive_no)++;
|
|
|
|
}
|
|
|
|
|
2020-05-29 06:45:38 +00:00
|
|
|
static void write_no_trip(struct membuffer *b, int *dive_no, bool selected_only, const char *photos_dir, const bool list_only, char *sep)
|
2014-05-27 21:06:56 +00:00
|
|
|
{
|
2024-03-10 15:35:53 +00:00
|
|
|
const char *separator = "";
|
2014-08-15 05:07:17 +00:00
|
|
|
bool found_sel_dive = 0;
|
2014-05-27 21:06:56 +00:00
|
|
|
|
2024-06-07 08:25:09 +00:00
|
|
|
for (auto &dive: divelog.dives) {
|
2014-08-14 02:30:36 +00:00
|
|
|
// write dive if it doesn't belong to any trip and the dive is selected
|
|
|
|
// or we are in exporting all dives mode.
|
2014-08-15 05:07:17 +00:00
|
|
|
if (!dive->divetrip && (dive->selected || !selected_only)) {
|
|
|
|
if (!found_sel_dive) {
|
2014-08-27 17:19:20 +00:00
|
|
|
put_format(b, "%c{", *sep);
|
|
|
|
(*sep) = ',';
|
2014-08-15 05:07:17 +00:00
|
|
|
put_format(b, "\"name\":\"Other\",");
|
|
|
|
put_format(b, "\"dives\":[");
|
|
|
|
found_sel_dive = 1;
|
|
|
|
}
|
2014-08-27 17:19:20 +00:00
|
|
|
put_string(b, separator);
|
|
|
|
separator = ", ";
|
2024-06-07 08:25:09 +00:00
|
|
|
write_one_dive(b, *dive, photos_dir, dive_no, list_only);
|
2014-08-15 05:07:17 +00:00
|
|
|
}
|
2014-05-27 21:06:56 +00:00
|
|
|
}
|
2014-08-15 05:07:17 +00:00
|
|
|
if (found_sel_dive)
|
2014-08-27 17:19:20 +00:00
|
|
|
put_format(b, "]}\n\n");
|
2014-05-27 21:06:56 +00:00
|
|
|
}
|
|
|
|
|
2024-05-31 15:15:47 +00:00
|
|
|
static void write_trip(struct membuffer *b, dive_trip *trip, int *dive_no, bool selected_only, const char *photos_dir, const bool list_only, char *sep)
|
2014-05-27 21:06:56 +00:00
|
|
|
{
|
2024-03-10 15:35:53 +00:00
|
|
|
const char *separator = "";
|
2014-08-14 02:30:36 +00:00
|
|
|
bool found_sel_dive = 0;
|
2014-05-27 21:06:56 +00:00
|
|
|
|
2024-06-02 15:06:18 +00:00
|
|
|
for (auto dive: trip->dives) {
|
2014-08-14 02:30:36 +00:00
|
|
|
if (!dive->selected && selected_only)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// save trip if found at least one selected dive.
|
|
|
|
if (!found_sel_dive) {
|
|
|
|
found_sel_dive = 1;
|
2014-08-27 17:19:20 +00:00
|
|
|
put_format(b, "%c {", *sep);
|
|
|
|
(*sep) = ',';
|
2024-05-31 15:15:47 +00:00
|
|
|
write_attribute(b, "name", trip->location.c_str(), ", ");
|
2014-08-14 02:30:36 +00:00
|
|
|
put_format(b, "\"dives\":[");
|
|
|
|
}
|
2014-08-27 17:19:20 +00:00
|
|
|
put_string(b, separator);
|
|
|
|
separator = ", ";
|
2024-06-07 08:25:09 +00:00
|
|
|
write_one_dive(b, *dive, photos_dir, dive_no, list_only);
|
2014-05-27 21:06:56 +00:00
|
|
|
}
|
|
|
|
|
2014-08-14 02:30:36 +00:00
|
|
|
// close the trip object if contain dives.
|
|
|
|
if (found_sel_dive)
|
2014-08-27 17:19:20 +00:00
|
|
|
put_format(b, "]}\n\n");
|
2014-05-27 21:06:56 +00:00
|
|
|
}
|
|
|
|
|
2020-05-29 06:45:38 +00:00
|
|
|
static void write_trips(struct membuffer *b, const char *photos_dir, bool selected_only, const bool list_only)
|
2014-05-27 21:06:56 +00:00
|
|
|
{
|
2024-06-07 08:25:09 +00:00
|
|
|
int dive_no = 0;
|
2014-08-27 17:19:20 +00:00
|
|
|
char sep_ = ' ';
|
|
|
|
char *sep = &sep_;
|
2014-05-27 21:06:56 +00:00
|
|
|
|
2024-06-01 20:05:57 +00:00
|
|
|
for (auto &trip: *divelog.trips)
|
|
|
|
trip->saved = 0;
|
2014-05-27 21:06:56 +00:00
|
|
|
|
2024-06-07 08:25:09 +00:00
|
|
|
for (auto &dive: divelog.dives) {
|
2024-06-01 20:05:57 +00:00
|
|
|
dive_trip *trip = dive->divetrip;
|
2014-05-27 21:06:56 +00:00
|
|
|
|
2014-08-14 02:30:36 +00:00
|
|
|
/*Continue if the dive have no trips or we have seen this trip before*/
|
2018-07-18 06:01:25 +00:00
|
|
|
if (!trip || trip->saved)
|
2014-08-14 02:30:36 +00:00
|
|
|
continue;
|
2014-05-27 21:06:56 +00:00
|
|
|
|
2014-08-14 02:30:36 +00:00
|
|
|
/* We haven't seen this trip before - save it and all dives */
|
2018-07-18 06:01:25 +00:00
|
|
|
trip->saved = 1;
|
2014-08-27 17:19:20 +00:00
|
|
|
write_trip(b, trip, &dive_no, selected_only, photos_dir, list_only, sep);
|
2014-05-23 18:49:49 +00:00
|
|
|
}
|
2014-08-14 02:30:36 +00:00
|
|
|
|
|
|
|
/*Save all remaining trips into Others*/
|
2014-08-27 17:19:20 +00:00
|
|
|
write_no_trip(b, &dive_no, selected_only, photos_dir, list_only, sep);
|
2014-05-23 18:49:49 +00:00
|
|
|
}
|
|
|
|
|
2014-07-13 21:36:35 +00:00
|
|
|
void export_list(struct membuffer *b, const char *photos_dir, bool selected_only, const bool list_only)
|
2014-05-29 14:30:33 +00:00
|
|
|
{
|
2014-05-27 21:06:56 +00:00
|
|
|
put_string(b, "trips=[");
|
2014-07-13 21:36:35 +00:00
|
|
|
write_trips(b, photos_dir, selected_only, list_only);
|
2014-05-23 18:49:49 +00:00
|
|
|
put_string(b, "]");
|
|
|
|
}
|
|
|
|
|
2014-07-13 21:36:35 +00:00
|
|
|
void export_HTML(const char *file_name, const char *photos_dir, const bool selected_only, const bool list_only)
|
2014-05-23 18:49:49 +00:00
|
|
|
{
|
|
|
|
FILE *f;
|
|
|
|
|
2024-05-05 04:47:28 +00:00
|
|
|
membuffer buf;
|
2014-07-13 21:36:35 +00:00
|
|
|
export_list(&buf, photos_dir, selected_only, list_only);
|
2014-05-23 18:49:49 +00:00
|
|
|
|
2014-06-08 04:35:20 +00:00
|
|
|
f = subsurface_fopen(file_name, "w+");
|
2015-06-18 19:23:58 +00:00
|
|
|
if (!f) {
|
2014-06-08 04:35:20 +00:00
|
|
|
report_error(translate("gettextFromC", "Can't open file %s"), file_name);
|
2015-06-18 19:23:58 +00:00
|
|
|
} else {
|
|
|
|
flush_buffer(&buf, f); /*check for writing errors? */
|
|
|
|
fclose(f);
|
|
|
|
}
|
2014-05-23 18:49:49 +00:00
|
|
|
}
|
2014-08-10 07:12:29 +00:00
|
|
|
|
2014-08-10 15:29:25 +00:00
|
|
|
void export_translation(const char *file_name)
|
|
|
|
{
|
2014-08-10 07:12:29 +00:00
|
|
|
FILE *f;
|
|
|
|
|
2024-05-05 04:47:28 +00:00
|
|
|
membuffer buf;
|
2014-08-10 07:12:29 +00:00
|
|
|
|
|
|
|
//export translated words here
|
2024-03-10 15:41:56 +00:00
|
|
|
put_format(&buf, "translate={");
|
2014-08-10 07:12:29 +00:00
|
|
|
|
|
|
|
//Dive list view
|
2024-03-10 15:41:56 +00:00
|
|
|
write_attribute(&buf, "Number", translate("gettextFromC", "Number"), ", ");
|
|
|
|
write_attribute(&buf, "Date", translate("gettextFromC", "Date"), ", ");
|
|
|
|
write_attribute(&buf, "Time", translate("gettextFromC", "Time"), ", ");
|
|
|
|
write_attribute(&buf, "Location", translate("gettextFromC", "Location"), ", ");
|
|
|
|
write_attribute(&buf, "Air_Temp", translate("gettextFromC", "Air temp."), ", ");
|
|
|
|
write_attribute(&buf, "Water_Temp", translate("gettextFromC", "Water temp."), ", ");
|
|
|
|
write_attribute(&buf, "dives", translate("gettextFromC", "Dives"), ", ");
|
|
|
|
write_attribute(&buf, "Expand_All", translate("gettextFromC", "Expand all"), ", ");
|
|
|
|
write_attribute(&buf, "Collapse_All", translate("gettextFromC", "Collapse all"), ", ");
|
|
|
|
write_attribute(&buf, "trips", translate("gettextFromC", "Trips"), ", ");
|
|
|
|
write_attribute(&buf, "Statistics", translate("gettextFromC", "Statistics"), ", ");
|
|
|
|
write_attribute(&buf, "Advanced_Search", translate("gettextFromC", "Advanced search"), ", ");
|
2014-08-10 07:12:29 +00:00
|
|
|
|
|
|
|
//Dive expanded view
|
2019-12-03 06:58:09 +00:00
|
|
|
|
2024-03-10 15:41:56 +00:00
|
|
|
write_attribute(&buf, "Rating", translate("gettextFromC", "Rating"), ", ");
|
|
|
|
write_attribute(&buf, "WaveSize", translate("gettextFromC", "WaveSize"), ", ");
|
|
|
|
write_attribute(&buf, "Visibility", translate("gettextFromC", "Visibility"), ", ");
|
|
|
|
write_attribute(&buf, "Current", translate("gettextFromC", "Current"), ", ");
|
|
|
|
write_attribute(&buf, "Surge", translate("gettextFromC", "Surge"), ", ");
|
|
|
|
write_attribute(&buf, "Chill", translate("gettextFromC", "Chill"), ", ");
|
|
|
|
write_attribute(&buf, "Duration", translate("gettextFromC", "Duration"), ", ");
|
|
|
|
write_attribute(&buf, "DiveGuide", translate("gettextFromC", "Diveguide"), ", ");
|
|
|
|
write_attribute(&buf, "DiveMaster", translate("gettextFromC", "Divemaster"), ", ");
|
|
|
|
write_attribute(&buf, "Buddy", translate("gettextFromC", "Buddy"), ", ");
|
|
|
|
write_attribute(&buf, "Suit", translate("gettextFromC", "Suit"), ", ");
|
|
|
|
write_attribute(&buf, "Tags", translate("gettextFromC", "Tags"), ", ");
|
|
|
|
write_attribute(&buf, "Notes", translate("gettextFromC", "Notes"), ", ");
|
|
|
|
write_attribute(&buf, "Show_more_details", translate("gettextFromC", "Show more details"), ", ");
|
2014-08-10 07:12:29 +00:00
|
|
|
|
|
|
|
//Yearly statistics view
|
2024-03-10 15:41:56 +00:00
|
|
|
write_attribute(&buf, "Yearly_statistics", translate("gettextFromC", "Yearly statistics"), ", ");
|
|
|
|
write_attribute(&buf, "Year", translate("gettextFromC", "Year"), ", ");
|
|
|
|
write_attribute(&buf, "Total_Time", translate("gettextFromC", "Total time"), ", ");
|
|
|
|
write_attribute(&buf, "Average_Time", translate("gettextFromC", "Average time"), ", ");
|
|
|
|
write_attribute(&buf, "Shortest_Time", translate("gettextFromC", "Shortest time"), ", ");
|
|
|
|
write_attribute(&buf, "Longest_Time", translate("gettextFromC", "Longest time"), ", ");
|
|
|
|
write_attribute(&buf, "Average_Depth", translate("gettextFromC", "Average depth"), ", ");
|
|
|
|
write_attribute(&buf, "Min_Depth", translate("gettextFromC", "Min. depth"), ", ");
|
|
|
|
write_attribute(&buf, "Max_Depth", translate("gettextFromC", "Max. depth"), ", ");
|
|
|
|
write_attribute(&buf, "Average_SAC", translate("gettextFromC", "Average SAC"), ", ");
|
|
|
|
write_attribute(&buf, "Min_SAC", translate("gettextFromC", "Min. SAC"), ", ");
|
|
|
|
write_attribute(&buf, "Max_SAC", translate("gettextFromC", "Max. SAC"), ", ");
|
|
|
|
write_attribute(&buf, "Average_Temp", translate("gettextFromC", "Average temp."), ", ");
|
|
|
|
write_attribute(&buf, "Min_Temp", translate("gettextFromC", "Min. temp."), ", ");
|
|
|
|
write_attribute(&buf, "Max_Temp", translate("gettextFromC", "Max. temp."), ", ");
|
|
|
|
write_attribute(&buf, "Back_to_List", translate("gettextFromC", "Back to list"), ", ");
|
2014-08-10 07:12:29 +00:00
|
|
|
|
|
|
|
//dive detailed view
|
2024-03-10 15:41:56 +00:00
|
|
|
write_attribute(&buf, "Dive_No", translate("gettextFromC", "Dive #"), ", ");
|
|
|
|
write_attribute(&buf, "Dive_profile", translate("gettextFromC", "Dive profile"), ", ");
|
|
|
|
write_attribute(&buf, "Dive_information", translate("gettextFromC", "Dive information"), ", ");
|
|
|
|
write_attribute(&buf, "Dive_equipment", translate("gettextFromC", "Dive equipment"), ", ");
|
|
|
|
write_attribute(&buf, "Type", translate("gettextFromC", "Type"), ", ");
|
|
|
|
write_attribute(&buf, "Size", translate("gettextFromC", "Size"), ", ");
|
|
|
|
write_attribute(&buf, "Work_Pressure", translate("gettextFromC", "Work pressure"), ", ");
|
|
|
|
write_attribute(&buf, "Start_Pressure", translate("gettextFromC", "Start pressure"), ", ");
|
|
|
|
write_attribute(&buf, "End_Pressure", translate("gettextFromC", "End pressure"), ", ");
|
|
|
|
write_attribute(&buf, "Gas", translate("gettextFromC", "Gas"), ", ");
|
|
|
|
write_attribute(&buf, "Weight", translate("gettextFromC", "Weight"), ", ");
|
|
|
|
write_attribute(&buf, "Type", translate("gettextFromC", "Type"), ", ");
|
|
|
|
write_attribute(&buf, "Events", translate("gettextFromC", "Events"), ", ");
|
|
|
|
write_attribute(&buf, "Name", translate("gettextFromC", "Name"), ", ");
|
|
|
|
write_attribute(&buf, "Value", translate("gettextFromC", "Value"), ", ");
|
|
|
|
write_attribute(&buf, "Coordinates", translate("gettextFromC", "Coordinates"), ", ");
|
|
|
|
write_attribute(&buf, "Dive_Status", translate("gettextFromC", "Dive status"), " ");
|
|
|
|
|
|
|
|
put_format(&buf, "}");
|
2014-08-10 07:12:29 +00:00
|
|
|
|
|
|
|
f = subsurface_fopen(file_name, "w+");
|
2015-06-18 19:23:58 +00:00
|
|
|
if (!f) {
|
2014-08-10 07:12:29 +00:00
|
|
|
report_error(translate("gettextFromC", "Can't open file %s"), file_name);
|
2015-06-18 19:23:58 +00:00
|
|
|
} else {
|
|
|
|
flush_buffer(&buf, f); /*check for writing errors? */
|
|
|
|
fclose(f);
|
|
|
|
}
|
2014-08-10 07:12:29 +00:00
|
|
|
}
|