2014-05-23 18:49:49 +00:00
|
|
|
#include "save-html.h"
|
|
|
|
#include "gettext.h"
|
2014-06-08 04:56:54 +00:00
|
|
|
#include "stdio.h"
|
|
|
|
|
2014-06-08 13:09:24 +00:00
|
|
|
void write_attribute(struct membuffer *b, const char *att_name, const char *value)
|
|
|
|
{
|
|
|
|
if (!value)
|
|
|
|
value = "--";
|
|
|
|
put_format(b, "\"%s\":\"", att_name);
|
|
|
|
put_HTML_quoted(b, value);
|
|
|
|
put_string(b, "\",");
|
|
|
|
}
|
|
|
|
|
2014-06-24 18:40:06 +00:00
|
|
|
void put_HTML_bookmarks(struct membuffer *b, struct dive *dive)
|
|
|
|
{
|
|
|
|
struct event *ev = dive->dc.events;
|
|
|
|
put_string(b, "\"events\":[");
|
|
|
|
while (ev) {
|
|
|
|
put_format(b, "{\"name\":\"%s\",", ev->name);
|
2014-07-04 11:37:08 +00:00
|
|
|
put_format(b, "\"time\":\"%d\",},", ev->time.seconds);
|
2014-06-24 18:40:06 +00:00
|
|
|
ev = ev->next;
|
|
|
|
}
|
|
|
|
put_string(b, "],");
|
|
|
|
}
|
|
|
|
|
2014-06-08 13:09:24 +00:00
|
|
|
static void put_cylinder_HTML(struct membuffer *b, struct dive *dive)
|
|
|
|
{
|
|
|
|
int i, nr;
|
|
|
|
|
|
|
|
nr = nr_cylinders(dive);
|
|
|
|
|
|
|
|
put_string(b, "\"Cylinders\":[");
|
|
|
|
|
|
|
|
for (i = 0; i < nr; i++) {
|
|
|
|
cylinder_t *cylinder = dive->cylinder + i;
|
|
|
|
put_string(b, "{");
|
|
|
|
write_attribute(b, "Type", cylinder->type.description);
|
|
|
|
if (cylinder->type.size.mliter) {
|
|
|
|
put_milli(b, "\"Size\":\"", cylinder->type.size.mliter, " l\",");
|
|
|
|
} else {
|
|
|
|
write_attribute(b, "Size", "--");
|
|
|
|
}
|
|
|
|
put_pressure(b, cylinder->type.workingpressure, "\"WPressure\":\"", " bar\",");
|
2014-06-28 15:14:29 +00:00
|
|
|
|
|
|
|
if (cylinder->start.mbar) {
|
|
|
|
put_milli(b, "\"EPressure\":\"", cylinder->start.mbar, " bar\",");
|
|
|
|
} else {
|
|
|
|
write_attribute(b, "EPressure", "--");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cylinder->end.mbar) {
|
|
|
|
put_milli(b, "\"SPressure\":\"", cylinder->end.mbar, " bar\",");
|
|
|
|
} else {
|
|
|
|
write_attribute(b, "SPressure", "--");
|
|
|
|
}
|
2014-06-08 13:09:24 +00:00
|
|
|
|
|
|
|
if (cylinder->gasmix.o2.permille) {
|
2014-06-28 15:14:29 +00:00
|
|
|
put_format(b, "\"O2\":\"%u.%u%%\",", FRACTION(cylinder->gasmix.o2.permille, 10));
|
2014-06-08 13:09:24 +00:00
|
|
|
} else {
|
2014-06-28 15:14:29 +00:00
|
|
|
write_attribute(b, "O2", "--");
|
2014-06-08 13:09:24 +00:00
|
|
|
}
|
|
|
|
put_string(b, "},");
|
|
|
|
}
|
|
|
|
|
|
|
|
put_string(b, "],");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-06-08 04:56:54 +00:00
|
|
|
void put_HTML_samples(struct membuffer *b, struct dive *dive)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
put_format(b, "\"maxdepth\":%d,", dive->dc.maxdepth.mm);
|
|
|
|
put_format(b, "\"duration\":%d,", dive->dc.duration.seconds);
|
|
|
|
put_string(b, "\"samples\":\[");
|
|
|
|
struct sample *s = dive->dc.sample;
|
|
|
|
for (i = 0; i < dive->dc.samples; i++) {
|
2014-06-25 21:44:40 +00:00
|
|
|
put_format(b, "[%d,%d,%d,%d],", s->time.seconds, s->depth.mm, s->cylinderpressure.mbar, s->temperature.mkelvin);
|
2014-06-08 04:56:54 +00:00
|
|
|
s++;
|
|
|
|
}
|
|
|
|
put_string(b, "],");
|
|
|
|
}
|
2014-05-23 18:49:49 +00:00
|
|
|
|
|
|
|
void put_HTML_date(struct membuffer *b, struct dive *dive, const char *pre, const char *post)
|
|
|
|
{
|
|
|
|
struct tm tm;
|
|
|
|
utc_mkdate(dive->when, &tm);
|
|
|
|
put_format(b, "%s%04u-%02u-%02u%s", pre, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, post);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void put_HTML_notes(struct membuffer *b, struct dive *dive, const char *pre, const char *post)
|
|
|
|
{
|
|
|
|
if (dive->notes) {
|
2014-06-02 17:10:54 +00:00
|
|
|
put_string(b, pre);
|
|
|
|
put_HTML_quoted(b, dive->notes);
|
|
|
|
put_string(b, post);
|
2014-05-23 18:49:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void put_HTML_time(struct membuffer *b, struct dive *dive, const char *pre, const char *post)
|
|
|
|
{
|
|
|
|
struct tm tm;
|
|
|
|
utc_mkdate(dive->when, &tm);
|
|
|
|
put_format(b, "%s%02u:%02u:%02u%s", pre, tm.tm_hour, tm.tm_min, tm.tm_sec, post);
|
|
|
|
}
|
|
|
|
|
|
|
|
void put_HTML_airtemp(struct membuffer *b, struct dive *dive, const char *pre, const char *post)
|
|
|
|
{
|
|
|
|
const char *unit;
|
|
|
|
double value;
|
|
|
|
|
2014-05-27 21:06:56 +00:00
|
|
|
if (!dive->airtemp.mkelvin) {
|
|
|
|
put_format(b, "%s--%s", pre, post);
|
|
|
|
return;
|
|
|
|
}
|
2014-05-23 18:49:49 +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
|
|
|
}
|
|
|
|
|
|
|
|
void put_HTML_watertemp(struct membuffer *b, struct dive *dive, const char *pre, const char *post)
|
|
|
|
{
|
|
|
|
const char *unit;
|
|
|
|
double value;
|
|
|
|
|
2014-05-27 21:06:56 +00:00
|
|
|
if (!dive->watertemp.mkelvin) {
|
|
|
|
put_format(b, "%s--%s", pre, post);
|
|
|
|
return;
|
|
|
|
}
|
2014-05-23 18:49:49 +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
|
|
|
}
|
|
|
|
|
|
|
|
void put_HTML_tags(struct membuffer *b, struct dive *dive, const char *pre, const char *post)
|
|
|
|
{
|
|
|
|
put_string(b, pre);
|
2014-05-28 17:52:05 +00:00
|
|
|
put_string(b, "[");
|
2014-05-23 18:49:49 +00:00
|
|
|
struct tag_entry *tag = dive->tag_list;
|
2014-05-28 11:43:57 +00:00
|
|
|
|
|
|
|
if (!tag)
|
2014-05-28 17:52:05 +00:00
|
|
|
put_string(b, "\"--\",");
|
2014-05-28 11:43:57 +00:00
|
|
|
|
2014-05-28 17:52:05 +00:00
|
|
|
while (tag) {
|
2014-06-02 17:10:54 +00:00
|
|
|
put_string(b, "\"");
|
|
|
|
put_HTML_quoted(b, tag->tag->name);
|
|
|
|
put_string(b, "\",");
|
2014-05-23 18:49:49 +00:00
|
|
|
tag = tag->next;
|
|
|
|
}
|
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 */
|
|
|
|
void write_one_dive(struct membuffer *b, struct dive *dive, int *dive_no, const 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);
|
|
|
|
put_format(b, "\"subsurface_number\":%d,", dive->number);
|
|
|
|
put_HTML_date(b, dive, "\"date\":\"", "\",");
|
|
|
|
put_HTML_time(b, dive, "\"time\":\"", "\",");
|
2014-05-28 11:43:57 +00:00
|
|
|
write_attribute(b, "location", dive->location);
|
2014-05-27 21:06:56 +00:00
|
|
|
put_format(b, "\"rating\":%d,", dive->rating);
|
|
|
|
put_format(b, "\"visibility\":%d,", dive->visibility);
|
|
|
|
put_string(b, "\"temperature\":{");
|
|
|
|
put_HTML_airtemp(b, dive, "\"air\":\"", "\",");
|
|
|
|
put_HTML_watertemp(b, dive, "\"water\":\"", "\",");
|
|
|
|
put_string(b, " },");
|
2014-05-28 11:43:57 +00:00
|
|
|
write_attribute(b, "buddy", dive->buddy);
|
|
|
|
write_attribute(b, "divemaster", dive->divemaster);
|
|
|
|
write_attribute(b, "suit", dive->suit);
|
2014-05-28 17:52:05 +00:00
|
|
|
put_HTML_tags(b, dive, "\"tags\":", ",");
|
2014-05-29 14:30:33 +00:00
|
|
|
put_HTML_notes(b, dive, "\"notes\":\"", "\",");
|
2014-06-24 13:01:49 +00:00
|
|
|
if (!list_only) {
|
|
|
|
put_cylinder_HTML(b, dive);
|
|
|
|
put_HTML_samples(b, dive);
|
2014-06-24 18:40:06 +00:00
|
|
|
put_HTML_bookmarks(b, dive);
|
2014-06-24 13:01:49 +00:00
|
|
|
}
|
2014-05-27 21:06:56 +00:00
|
|
|
put_string(b, "},\n");
|
|
|
|
(*dive_no)++;
|
|
|
|
}
|
|
|
|
|
2014-06-24 13:01:49 +00:00
|
|
|
void write_no_trip(struct membuffer *b, int *dive_no, const bool list_only)
|
2014-05-27 21:06:56 +00:00
|
|
|
{
|
|
|
|
int i;
|
2014-05-23 18:49:49 +00:00
|
|
|
struct dive *dive;
|
|
|
|
|
2014-05-27 21:06:56 +00:00
|
|
|
put_format(b, "{");
|
|
|
|
put_format(b, "\"name\":\"Other\",");
|
|
|
|
put_format(b, "\"dives\":[");
|
|
|
|
|
2014-05-29 14:30:33 +00:00
|
|
|
for_each_dive (i, dive) {
|
2014-05-27 21:06:56 +00:00
|
|
|
if (!dive->divetrip)
|
2014-06-24 13:01:49 +00:00
|
|
|
write_one_dive(b, dive, dive_no, list_only);
|
2014-05-27 21:06:56 +00:00
|
|
|
}
|
|
|
|
put_format(b, "]},\n\n");
|
|
|
|
}
|
|
|
|
|
2014-06-24 13:01:49 +00:00
|
|
|
void write_trip(struct membuffer *b, dive_trip_t *trip, int *dive_no, const bool list_only)
|
2014-05-27 21:06:56 +00:00
|
|
|
{
|
|
|
|
struct dive *dive;
|
|
|
|
|
|
|
|
put_format(b, "{");
|
2014-05-29 14:30:33 +00:00
|
|
|
put_format(b, "\"name\":\"%s\",", trip->location);
|
2014-05-27 21:06:56 +00:00
|
|
|
put_format(b, "\"dives\":[");
|
|
|
|
|
2014-05-29 14:30:33 +00:00
|
|
|
for (dive = trip->dives; dive != NULL; dive = dive->next) {
|
2014-06-24 13:01:49 +00:00
|
|
|
write_one_dive(b, dive, dive_no, list_only);
|
2014-05-27 21:06:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
put_format(b, "]},\n\n");
|
|
|
|
}
|
|
|
|
|
2014-06-24 13:01:49 +00:00
|
|
|
void write_trips(struct membuffer *b, bool selected_only, const bool list_only)
|
2014-05-27 21:06:56 +00:00
|
|
|
{
|
|
|
|
int i, dive_no = 0;
|
|
|
|
struct dive *dive;
|
|
|
|
dive_trip_t *trip;
|
|
|
|
|
|
|
|
for (trip = dive_trip_list; trip != NULL; trip = trip->next)
|
|
|
|
trip->index = 0;
|
|
|
|
|
|
|
|
if (selected_only) {
|
|
|
|
put_format(b, "{");
|
|
|
|
put_format(b, "\"name\":\"Other\",");
|
|
|
|
put_format(b, "\"dives\":[");
|
|
|
|
|
2014-05-29 14:30:33 +00:00
|
|
|
for_each_dive (i, dive) {
|
2014-05-23 18:49:49 +00:00
|
|
|
if (!dive->selected)
|
|
|
|
continue;
|
2014-06-24 13:01:49 +00:00
|
|
|
write_one_dive(b, dive, &dive_no, list_only);
|
2014-05-23 18:49:49 +00:00
|
|
|
}
|
2014-05-27 21:06:56 +00:00
|
|
|
put_format(b, "]},\n\n");
|
|
|
|
} else {
|
|
|
|
|
2014-05-29 14:30:33 +00:00
|
|
|
for_each_dive (i, dive) {
|
2014-05-27 21:06:56 +00:00
|
|
|
trip = dive->divetrip;
|
|
|
|
|
|
|
|
/*Continue if the dive have no trips or we have seen this trip before*/
|
|
|
|
if (!trip || trip->index)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* We haven't seen this trip before - save it and all dives */
|
|
|
|
trip->index = 1;
|
2014-06-24 13:01:49 +00:00
|
|
|
write_trip(b, trip, &dive_no, list_only);
|
2014-05-27 21:06:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*Save all remaining trips into Others*/
|
2014-06-24 13:01:49 +00:00
|
|
|
write_no_trip(b, &dive_no, list_only);
|
2014-05-23 18:49:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-24 13:01:49 +00:00
|
|
|
void export_list(struct membuffer *b, 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-06-24 13:01:49 +00:00
|
|
|
write_trips(b, selected_only, list_only);
|
2014-05-23 18:49:49 +00:00
|
|
|
put_string(b, "]");
|
|
|
|
}
|
|
|
|
|
2014-06-24 13:01:49 +00:00
|
|
|
void export_HTML(const char *file_name, const bool selected_only, const bool list_only)
|
2014-05-23 18:49:49 +00:00
|
|
|
{
|
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
struct membuffer buf = { 0 };
|
2014-06-24 13:01:49 +00:00
|
|
|
export_list(&buf, 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+");
|
2014-05-23 18:49:49 +00:00
|
|
|
if (!f)
|
2014-06-08 04:35:20 +00:00
|
|
|
report_error(translate("gettextFromC", "Can't open file %s"), file_name);
|
2014-05-23 18:49:49 +00:00
|
|
|
|
|
|
|
flush_buffer(&buf, f); /*check for writing errors? */
|
|
|
|
free_buffer(&buf);
|
|
|
|
fclose(f);
|
|
|
|
}
|