From e8b9e881714b62e57c35320b3d58beb8b0914de8 Mon Sep 17 00:00:00 2001 From: Jef Driesen Date: Sat, 6 Feb 2016 15:58:23 +0100 Subject: [PATCH] Integrate the new output formats. The existing output code is removed and replaced with the new XML and RAW output formats. The desired output format can be selected with a new command-line option. The XML format remains the default output format. --- examples/dctool_download.c | 380 +++++++------------------------------ 1 file changed, 70 insertions(+), 310 deletions(-) diff --git a/examples/dctool_download.c b/examples/dctool_download.c index 61c4127..4baa333 100644 --- a/examples/dctool_download.c +++ b/examples/dctool_download.c @@ -38,6 +38,7 @@ #include "dctool.h" #include "common.h" +#include "output.h" #include "utils.h" typedef struct event_data_t { @@ -46,305 +47,18 @@ typedef struct event_data_t { } event_data_t; typedef struct dive_data_t { - FILE* ostream; dc_device_t *device; dc_buffer_t **fingerprint; unsigned int number; + dctool_output_t *output; } dive_data_t; -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; - } -} - -static dc_status_t -doparse (FILE *ostream, dc_device_t *device, const unsigned char data[], unsigned int size) -{ - dc_status_t rc = DC_STATUS_SUCCESS; - dc_parser_t *parser = NULL; - - // Create the parser. - message ("Creating the parser.\n"); - rc = dc_parser_new (&parser, device); - if (rc != DC_STATUS_SUCCESS) { - ERROR ("Error creating the parser."); - goto cleanup; - } - - // Register the data. - message ("Registering the data.\n"); - rc = dc_parser_set_data (parser, data, size); - if (rc != DC_STATUS_SUCCESS) { - ERROR ("Error registering the data."); - goto cleanup; - } - - // Parse the datetime. - message ("Parsing the datetime.\n"); - dc_datetime_t dt = {0}; - rc = dc_parser_get_datetime (parser, &dt); - if (rc != DC_STATUS_SUCCESS && rc != DC_STATUS_UNSUPPORTED) { - ERROR ("Error parsing the datetime."); - goto cleanup; - } - - fprintf (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; - rc = dc_parser_get_field (parser, DC_FIELD_DIVETIME, 0, &divetime); - if (rc != DC_STATUS_SUCCESS && rc != DC_STATUS_UNSUPPORTED) { - ERROR ("Error parsing the divetime."); - goto cleanup; - } - - fprintf (ostream, "%02u:%02u\n", - divetime / 60, divetime % 60); - - // Parse the maxdepth. - message ("Parsing the maxdepth.\n"); - double maxdepth = 0.0; - rc = dc_parser_get_field (parser, DC_FIELD_MAXDEPTH, 0, &maxdepth); - if (rc != DC_STATUS_SUCCESS && rc != DC_STATUS_UNSUPPORTED) { - ERROR ("Error parsing the maxdepth."); - goto cleanup; - } - - fprintf (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; - rc = dc_parser_get_field (parser, fields[i], 0, &temperature); - if (rc != DC_STATUS_SUCCESS && rc != DC_STATUS_UNSUPPORTED) { - ERROR ("Error parsing the temperature."); - goto cleanup; - } - - if (rc != DC_STATUS_UNSUPPORTED) { - fprintf (ostream, "%.1f\n", - names[i], temperature); - } - } - - // Parse the gas mixes. - message ("Parsing the gas mixes.\n"); - unsigned int ngases = 0; - rc = dc_parser_get_field (parser, DC_FIELD_GASMIX_COUNT, 0, &ngases); - if (rc != DC_STATUS_SUCCESS && rc != 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}; - rc = dc_parser_get_field (parser, DC_FIELD_GASMIX, i, &gasmix); - if (rc != DC_STATUS_SUCCESS && rc != DC_STATUS_UNSUPPORTED) { - ERROR ("Error parsing the gas mix."); - goto cleanup; - } - - fprintf (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; - rc = dc_parser_get_field (parser, DC_FIELD_TANK_COUNT, 0, &ntanks); - if (rc != DC_STATUS_SUCCESS && rc != 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}; - rc = dc_parser_get_field (parser, DC_FIELD_TANK, i, &tank); - if (rc != DC_STATUS_SUCCESS && rc != DC_STATUS_UNSUPPORTED) { - ERROR ("Error parsing the tank."); - goto cleanup; - } - - fprintf (ostream, "\n"); - if (tank.gasmix != DC_GASMIX_UNKNOWN) { - fprintf (ostream, - " %u\n", - tank.gasmix); - } - if (tank.type != DC_TANKVOLUME_NONE) { - fprintf (ostream, - " %s\n" - " %.1f\n" - " %.2f\n", - names[tank.type], tank.volume, tank.workpressure); - } - fprintf (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; - rc = dc_parser_get_field (parser, DC_FIELD_DIVEMODE, 0, &divemode); - if (rc != DC_STATUS_SUCCESS && rc != DC_STATUS_UNSUPPORTED) { - ERROR ("Error parsing the dive mode."); - goto cleanup; - } - - if (rc != DC_STATUS_UNSUPPORTED) { - const char *names[] = {"freedive", "gauge", "oc", "cc"}; - fprintf (ostream, "%s\n", - names[divemode]); - } - - // Parse the salinity. - message ("Parsing the salinity.\n"); - dc_salinity_t salinity = {DC_WATER_FRESH, 0.0}; - rc = dc_parser_get_field (parser, DC_FIELD_SALINITY, 0, &salinity); - if (rc != DC_STATUS_SUCCESS && rc != DC_STATUS_UNSUPPORTED) { - ERROR ("Error parsing the salinity."); - goto cleanup; - } - - if (rc != DC_STATUS_UNSUPPORTED) { - fprintf (ostream, "%.1f\n", - salinity.type, salinity.density); - } - - // Parse the atmospheric pressure. - message ("Parsing the atmospheric pressure.\n"); - double atmospheric = 0.0; - rc = dc_parser_get_field (parser, DC_FIELD_ATMOSPHERIC, 0, &atmospheric); - if (rc != DC_STATUS_SUCCESS && rc != DC_STATUS_UNSUPPORTED) { - ERROR ("Error parsing the atmospheric pressure."); - goto cleanup; - } - - if (rc != DC_STATUS_UNSUPPORTED) { - fprintf (ostream, "%.5f\n", - atmospheric); - } - - // Initialize the sample data. - sample_data_t sampledata = {0}; - sampledata.nsamples = 0; - sampledata.ostream = ostream; - - // Parse the sample data. - message ("Parsing the sample data.\n"); - rc = dc_parser_samples_foreach (parser, sample_cb, &sampledata); - if (rc != DC_STATUS_SUCCESS) { - ERROR ("Error parsing the sample data."); - goto cleanup; - } - - if (sampledata.nsamples) - fprintf (ostream, "\n"); - -cleanup: - dc_parser_destroy (parser); - return rc; -} - static int dive_cb (const unsigned char *data, unsigned int size, const unsigned char *fingerprint, unsigned int fsize, void *userdata) { dive_data_t *divedata = (dive_data_t *) userdata; + dc_status_t rc = DC_STATUS_SUCCESS; + dc_parser_t *parser = NULL; divedata->number++; @@ -362,15 +76,32 @@ dive_cb (const unsigned char *data, unsigned int size, const unsigned char *fing *divedata->fingerprint = fp; } - fprintf (divedata->ostream, "\n%u\n%u\n", divedata->number, size); - for (unsigned int i = 0; i < fsize; ++i) - fprintf (divedata->ostream, "%02X", fingerprint[i]); - fprintf (divedata->ostream, "\n"); + // Create the parser. + message ("Creating the parser.\n"); + rc = dc_parser_new (&parser, divedata->device); + if (rc != DC_STATUS_SUCCESS) { + ERROR ("Error creating the parser."); + goto cleanup; + } - doparse (divedata->ostream, divedata->device, data, size); + // Register the data. + message ("Registering the data.\n"); + rc = dc_parser_set_data (parser, data, size); + if (rc != DC_STATUS_SUCCESS) { + ERROR ("Error registering the data."); + goto cleanup; + } - fprintf (divedata->ostream, "\n"); + // Parse the dive data. + message ("Parsing the dive data.\n"); + rc = dctool_output_write (divedata->output, parser, data, size, fingerprint, fsize); + if (rc != DC_STATUS_SUCCESS) { + ERROR ("Error parsing the dive data."); + goto cleanup; + } +cleanup: + dc_parser_destroy (parser); return 1; } @@ -421,7 +152,7 @@ event_cb (dc_device_t *device, dc_event_type_t event, const void *data, void *us } static dc_status_t -download (dc_context_t *context, dc_descriptor_t *descriptor, const char *devname, const char *cachedir, dc_buffer_t *fingerprint, FILE *ostream) +download (dc_context_t *context, dc_descriptor_t *descriptor, const char *devname, const char *cachedir, dc_buffer_t *fingerprint, dctool_output_t *output) { dc_status_t rc = DC_STATUS_SUCCESS; dc_device_t *device = NULL; @@ -476,11 +207,9 @@ download (dc_context_t *context, dc_descriptor_t *descriptor, const char *devnam // Initialize the dive data. dive_data_t divedata = {0}; divedata.device = device; - divedata.ostream = ostream; divedata.fingerprint = &ofingerprint; divedata.number = 0; - - fprintf (ostream, "\n"); + divedata.output = output; // Download the dives. message ("Downloading the dives.\n"); @@ -490,8 +219,6 @@ download (dc_context_t *context, dc_descriptor_t *descriptor, const char *devnam goto cleanup; } - fprintf (ostream, "\n"); - // Store the fingerprint data. if (cachedir && ofingerprint) { char filename[1024] = {0}; @@ -518,23 +245,25 @@ dctool_download_run (int argc, char *argv[], dc_context_t *context, dc_descripto int exitcode = EXIT_SUCCESS; dc_status_t status = DC_STATUS_SUCCESS; dc_buffer_t *fingerprint = NULL; - FILE *ostream = NULL; + dctool_output_t *output = NULL; // Default option values. unsigned int help = 0; const char *fphex = NULL; const char *filename = NULL; const char *cachedir = NULL; + const char *format = "xml"; // Parse the command-line options. int opt = 0; - const char *optstring = "ho:p:c:"; + const char *optstring = "ho:p:c:f:"; #ifdef HAVE_GETOPT_LONG struct option options[] = { {"help", no_argument, 0, 'h'}, {"output", required_argument, 0, 'o'}, {"fingerprint", required_argument, 0, 'p'}, {"cache", required_argument, 0, 'c'}, + {"format", required_argument, 0, 'f'}, {0, 0, 0, 0 } }; while ((opt = getopt_long (argc, argv, optstring, options, NULL)) != -1) { @@ -554,6 +283,9 @@ dctool_download_run (int argc, char *argv[], dc_context_t *context, dc_descripto case 'c': cachedir = optarg; break; + case 'f': + format = optarg; + break; default: return EXIT_FAILURE; } @@ -571,16 +303,24 @@ dctool_download_run (int argc, char *argv[], dc_context_t *context, dc_descripto // Convert the fingerprint to binary. fingerprint = dctool_convert_hex2bin (fphex); - // Open the output file. - ostream = fopen (filename, "w"); - if (ostream == NULL) { - message ("Failed to open the output file.\n"); + // Create the output. + if (strcasecmp(format, "raw") == 0) { + output = dctool_raw_output_new (filename); + } else if (strcasecmp(format, "xml") == 0) { + output = dctool_xml_output_new (filename); + } else { + message ("Unknown output format: %s\n", format); + exitcode = EXIT_FAILURE; + goto cleanup; + } + if (output == NULL) { + message ("Failed to create the output.\n"); exitcode = EXIT_FAILURE; goto cleanup; } // Download the dives. - status = download (context, descriptor, argv[0], cachedir, fingerprint, ostream); + status = download (context, descriptor, argv[0], cachedir, fingerprint, output); if (status != DC_STATUS_SUCCESS) { message ("ERROR: %s\n", dctool_errmsg (status)); exitcode = EXIT_FAILURE; @@ -588,7 +328,7 @@ dctool_download_run (int argc, char *argv[], dc_context_t *context, dc_descripto } cleanup: - if (ostream) fclose (ostream); + dctool_output_free (output); dc_buffer_free (fingerprint); return exitcode; } @@ -607,10 +347,30 @@ const dctool_command_t dctool_download = { " -o, --output Output filename\n" " -p, --fingerprint Fingerprint data (hexadecimal)\n" " -c, --cache Cache directory\n" + " -f, --format Output format\n" #else " -h Show help message\n" " -o Output filename\n" " -p Fingerprint data (hexadecimal)\n" " -c Cache directory\n" + " -f Output format\n" #endif + "\n" + "Supported output formats:\n" + "\n" + " XML (default)\n" + "\n" + " All dives are exported to a single xml file.\n" + "\n" + " RAW\n" + "\n" + " Each dive is exported to a raw (binary) file. To output multiple\n" + " files, the filename is interpreted as a template and should\n" + " contain one or more placeholders.\n" + "\n" + "Supported template placeholders:\n" + "\n" + " %f Fingerprint (hexadecimal format)\n" + " %n Number (4 digits)\n" + " %t Timestamp (basic ISO 8601 date/time format)\n" };