From e35f0a3ff45dde9114bba1c896b64c226c065a78 Mon Sep 17 00:00:00 2001 From: Jef Driesen Date: Sat, 6 Feb 2016 15:35:08 +0100 Subject: [PATCH] Add support for the xml output format. The XML output format exports all dives to a single xml file. --- examples/Makefile.am | 1 + examples/output.h | 3 + examples/output_xml.c | 365 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 369 insertions(+) create mode 100644 examples/output_xml.c diff --git a/examples/Makefile.am b/examples/Makefile.am index a05af08..3ae96d5 100644 --- a/examples/Makefile.am +++ b/examples/Makefile.am @@ -20,5 +20,6 @@ dctool_SOURCES = \ output.h \ output-private.h \ output.c \ + output_xml.c \ utils.h \ utils.c diff --git a/examples/output.h b/examples/output.h index b11675f..b123689 100644 --- a/examples/output.h +++ b/examples/output.h @@ -31,6 +31,9 @@ extern "C" { typedef struct dctool_output_t dctool_output_t; +dctool_output_t * +dctool_xml_output_new (const char *filename); + dc_status_t dctool_output_write (dctool_output_t *output, dc_parser_t *parser, const unsigned char data[], unsigned int size, const unsigned char fingerprint[], unsigned int fsize); diff --git a/examples/output_xml.c b/examples/output_xml.c new file mode 100644 index 0000000..a36e62a --- /dev/null +++ b/examples/output_xml.c @@ -0,0 +1,365 @@ +/* + * libdivecomputer + * + * Copyright (C) 2016 Jef Driesen + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301 USA + */ + +#include +#include +#include + +#include "output-private.h" +#include "utils.h" + +static dc_status_t dctool_xml_output_write (dctool_output_t *output, dc_parser_t *parser, const unsigned char data[], unsigned int size, const unsigned char fingerprint[], unsigned int fsize); +static dc_status_t dctool_xml_output_free (dctool_output_t *output); + +typedef struct dctool_xml_output_t { + dctool_output_t base; + FILE *ostream; +} dctool_xml_output_t; + +static const dctool_output_vtable_t xml_vtable = { + sizeof(dctool_xml_output_t), /* size */ + dctool_xml_output_write, /* write */ + dctool_xml_output_free, /* free */ +}; + +typedef struct sample_data_t { + FILE *ostream; + unsigned int nsamples; +} sample_data_t; + +static void +sample_cb (dc_sample_type_t type, dc_sample_value_t value, void *userdata) +{ + static const char *events[] = { + "none", "deco", "rbt", "ascent", "ceiling", "workload", "transmitter", + "violation", "bookmark", "surface", "safety stop", "gaschange", + "safety stop (voluntary)", "safety stop (mandatory)", "deepstop", + "ceiling (safety stop)", "floor", "divetime", "maxdepth", + "OLF", "PO2", "airtime", "rgbm", "heading", "tissue level warning", + "gaschange2"}; + static const char *decostop[] = { + "ndl", "safety", "deco", "deep"}; + + sample_data_t *sampledata = (sample_data_t *) userdata; + + switch (type) { + case DC_SAMPLE_TIME: + if (sampledata->nsamples++) + fprintf (sampledata->ostream, "\n"); + fprintf (sampledata->ostream, "\n"); + fprintf (sampledata->ostream, " \n", value.time / 60, value.time % 60); + break; + case DC_SAMPLE_DEPTH: + fprintf (sampledata->ostream, " %.2f\n", value.depth); + break; + case DC_SAMPLE_PRESSURE: + fprintf (sampledata->ostream, " %.2f\n", value.pressure.tank, value.pressure.value); + break; + case DC_SAMPLE_TEMPERATURE: + fprintf (sampledata->ostream, " %.2f\n", value.temperature); + break; + case DC_SAMPLE_EVENT: + if (value.event.type != SAMPLE_EVENT_GASCHANGE && value.event.type != SAMPLE_EVENT_GASCHANGE2) { + fprintf (sampledata->ostream, " %s\n", + value.event.type, value.event.time, value.event.flags, value.event.value, events[value.event.type]); + } + break; + case DC_SAMPLE_RBT: + fprintf (sampledata->ostream, " %u\n", value.rbt); + break; + case DC_SAMPLE_HEARTBEAT: + fprintf (sampledata->ostream, " %u\n", value.heartbeat); + break; + case DC_SAMPLE_BEARING: + fprintf (sampledata->ostream, " %u\n", value.bearing); + break; + case DC_SAMPLE_VENDOR: + fprintf (sampledata->ostream, " ", value.vendor.type, value.vendor.size); + for (unsigned int i = 0; i < value.vendor.size; ++i) + fprintf (sampledata->ostream, "%02X", ((unsigned char *) value.vendor.data)[i]); + fprintf (sampledata->ostream, "\n"); + break; + case DC_SAMPLE_SETPOINT: + fprintf (sampledata->ostream, " %.2f\n", value.setpoint); + break; + case DC_SAMPLE_PPO2: + fprintf (sampledata->ostream, " %.2f\n", value.ppo2); + break; + case DC_SAMPLE_CNS: + fprintf (sampledata->ostream, " %.1f\n", value.cns * 100.0); + break; + case DC_SAMPLE_DECO: + fprintf (sampledata->ostream, " %s\n", + value.deco.time, value.deco.depth, decostop[value.deco.type]); + break; + case DC_SAMPLE_GASMIX: + fprintf (sampledata->ostream, " %u\n", value.gasmix); + break; + default: + break; + } +} + +dctool_output_t * +dctool_xml_output_new (const char *filename) +{ + dctool_xml_output_t *output = NULL; + + if (filename == NULL) + goto error_exit; + + // Allocate memory. + output = (dctool_xml_output_t *) dctool_output_allocate (&xml_vtable); + if (output == NULL) { + goto error_exit; + } + + // Open the output file. + output->ostream = fopen (filename, "w"); + if (output->ostream == NULL) { + goto error_free; + } + + fprintf (output->ostream, "\n"); + + return (dctool_output_t *) output; + +error_free: + dctool_output_deallocate ((dctool_output_t *) output); +error_exit: + return NULL; +} + +static dc_status_t +dctool_xml_output_write (dctool_output_t *abstract, dc_parser_t *parser, const unsigned char data[], unsigned int size, const unsigned char fingerprint[], unsigned int fsize) +{ + dctool_xml_output_t *output = (dctool_xml_output_t *) abstract; + dc_status_t status = DC_STATUS_SUCCESS; + + fprintf (output->ostream, "\n%u\n%u\n", abstract->number, size); + + if (fingerprint) { + fprintf (output->ostream, ""); + for (unsigned int i = 0; i < fsize; ++i) + fprintf (output->ostream, "%02X", fingerprint[i]); + fprintf (output->ostream, "\n"); + } + + // Parse the datetime. + message ("Parsing the datetime.\n"); + dc_datetime_t dt = {0}; + status = dc_parser_get_datetime (parser, &dt); + if (status != DC_STATUS_SUCCESS && status != DC_STATUS_UNSUPPORTED) { + ERROR ("Error parsing the datetime."); + goto cleanup; + } + + fprintf (output->ostream, "%04i-%02i-%02i %02i:%02i:%02i\n", + dt.year, dt.month, dt.day, + dt.hour, dt.minute, dt.second); + + // Parse the divetime. + message ("Parsing the divetime.\n"); + unsigned int divetime = 0; + status = dc_parser_get_field (parser, DC_FIELD_DIVETIME, 0, &divetime); + if (status != DC_STATUS_SUCCESS && status != DC_STATUS_UNSUPPORTED) { + ERROR ("Error parsing the divetime."); + goto cleanup; + } + + fprintf (output->ostream, "%02u:%02u\n", + divetime / 60, divetime % 60); + + // Parse the maxdepth. + message ("Parsing the maxdepth.\n"); + double maxdepth = 0.0; + status = dc_parser_get_field (parser, DC_FIELD_MAXDEPTH, 0, &maxdepth); + if (status != DC_STATUS_SUCCESS && status != DC_STATUS_UNSUPPORTED) { + ERROR ("Error parsing the maxdepth."); + goto cleanup; + } + + fprintf (output->ostream, "%.2f\n", + maxdepth); + + // Parse the temperature. + message ("Parsing the temperature.\n"); + for (unsigned int i = 0; i < 3; ++i) { + dc_field_type_t fields[] = {DC_FIELD_TEMPERATURE_SURFACE, + DC_FIELD_TEMPERATURE_MINIMUM, + DC_FIELD_TEMPERATURE_MAXIMUM}; + const char *names[] = {"surface", "minimum", "maximum"}; + + double temperature = 0.0; + status = dc_parser_get_field (parser, fields[i], 0, &temperature); + if (status != DC_STATUS_SUCCESS && status != DC_STATUS_UNSUPPORTED) { + ERROR ("Error parsing the temperature."); + goto cleanup; + } + + if (status != DC_STATUS_UNSUPPORTED) { + fprintf (output->ostream, "%.1f\n", + names[i], temperature); + } + } + + // Parse the gas mixes. + message ("Parsing the gas mixes.\n"); + unsigned int ngases = 0; + status = dc_parser_get_field (parser, DC_FIELD_GASMIX_COUNT, 0, &ngases); + if (status != DC_STATUS_SUCCESS && status != DC_STATUS_UNSUPPORTED) { + ERROR ("Error parsing the gas mix count."); + goto cleanup; + } + + for (unsigned int i = 0; i < ngases; ++i) { + dc_gasmix_t gasmix = {0}; + status = dc_parser_get_field (parser, DC_FIELD_GASMIX, i, &gasmix); + if (status != DC_STATUS_SUCCESS && status != DC_STATUS_UNSUPPORTED) { + ERROR ("Error parsing the gas mix."); + goto cleanup; + } + + fprintf (output->ostream, + "\n" + " %.1f\n" + " %.1f\n" + " %.1f\n" + "\n", + gasmix.helium * 100.0, + gasmix.oxygen * 100.0, + gasmix.nitrogen * 100.0); + } + + // Parse the tanks. + message ("Parsing the tanks.\n"); + unsigned int ntanks = 0; + status = dc_parser_get_field (parser, DC_FIELD_TANK_COUNT, 0, &ntanks); + if (status != DC_STATUS_SUCCESS && status != DC_STATUS_UNSUPPORTED) { + ERROR ("Error parsing the tank count."); + goto cleanup; + } + + for (unsigned int i = 0; i < ntanks; ++i) { + const char *names[] = {"none", "metric", "imperial"}; + + dc_tank_t tank = {0}; + status = dc_parser_get_field (parser, DC_FIELD_TANK, i, &tank); + if (status != DC_STATUS_SUCCESS && status != DC_STATUS_UNSUPPORTED) { + ERROR ("Error parsing the tank."); + goto cleanup; + } + + fprintf (output->ostream, "\n"); + if (tank.gasmix != DC_GASMIX_UNKNOWN) { + fprintf (output->ostream, + " %u\n", + tank.gasmix); + } + if (tank.type != DC_TANKVOLUME_NONE) { + fprintf (output->ostream, + " %s\n" + " %.1f\n" + " %.2f\n", + names[tank.type], tank.volume, tank.workpressure); + } + fprintf (output->ostream, + " %.2f\n" + " %.2f\n" + "\n", + tank.beginpressure, tank.endpressure); + } + + // Parse the dive mode. + message ("Parsing the dive mode.\n"); + dc_divemode_t divemode = DC_DIVEMODE_OC; + status = dc_parser_get_field (parser, DC_FIELD_DIVEMODE, 0, &divemode); + if (status != DC_STATUS_SUCCESS && status != DC_STATUS_UNSUPPORTED) { + ERROR ("Error parsing the dive mode."); + goto cleanup; + } + + if (status != DC_STATUS_UNSUPPORTED) { + const char *names[] = {"freedive", "gauge", "oc", "cc"}; + fprintf (output->ostream, "%s\n", + names[divemode]); + } + + // Parse the salinity. + message ("Parsing the salinity.\n"); + dc_salinity_t salinity = {DC_WATER_FRESH, 0.0}; + status = dc_parser_get_field (parser, DC_FIELD_SALINITY, 0, &salinity); + if (status != DC_STATUS_SUCCESS && status != DC_STATUS_UNSUPPORTED) { + ERROR ("Error parsing the salinity."); + goto cleanup; + } + + if (status != DC_STATUS_UNSUPPORTED) { + fprintf (output->ostream, "%.1f\n", + salinity.type, salinity.density); + } + + // Parse the atmospheric pressure. + message ("Parsing the atmospheric pressure.\n"); + double atmospheric = 0.0; + status = dc_parser_get_field (parser, DC_FIELD_ATMOSPHERIC, 0, &atmospheric); + if (status != DC_STATUS_SUCCESS && status != DC_STATUS_UNSUPPORTED) { + ERROR ("Error parsing the atmospheric pressure."); + goto cleanup; + } + + if (status != DC_STATUS_UNSUPPORTED) { + fprintf (output->ostream, "%.5f\n", + atmospheric); + } + + // Initialize the sample data. + sample_data_t sampledata = {0}; + sampledata.nsamples = 0; + sampledata.ostream = output->ostream; + + // Parse the sample data. + message ("Parsing the sample data.\n"); + status = dc_parser_samples_foreach (parser, sample_cb, &sampledata); + if (status != DC_STATUS_SUCCESS) { + ERROR ("Error parsing the sample data."); + goto cleanup; + } + + if (sampledata.nsamples) + fprintf (output->ostream, "\n"); + fprintf (output->ostream, "\n"); + +cleanup: + return status; +} + +static dc_status_t +dctool_xml_output_free (dctool_output_t *abstract) +{ + dctool_xml_output_t *output = (dctool_xml_output_t *) abstract; + + fprintf (output->ostream, "\n"); + + fclose (output->ostream); + + return DC_STATUS_SUCCESS; +}