From 195702046c9f2a1d6a1445c2735a634227838043 Mon Sep 17 00:00:00 2001 From: Jef Driesen Date: Sat, 6 Feb 2016 15:27:40 +0100 Subject: [PATCH] Add a new abstract output interface. The new output interface provides the necessary infrastructure to add support for multiple output formats. Due to the abstract interface, each new format will require only minimal changes in the application itself. --- examples/Makefile.am | 3 ++ examples/output-private.h | 58 ++++++++++++++++++++++++++++ examples/output.c | 79 +++++++++++++++++++++++++++++++++++++++ examples/output.h | 43 +++++++++++++++++++++ 4 files changed, 183 insertions(+) create mode 100644 examples/output-private.h create mode 100644 examples/output.c create mode 100644 examples/output.h diff --git a/examples/Makefile.am b/examples/Makefile.am index cb13b64..a05af08 100644 --- a/examples/Makefile.am +++ b/examples/Makefile.am @@ -17,5 +17,8 @@ dctool_SOURCES = \ dctool_read.c \ dctool_write.c \ dctool_fwupdate.c \ + output.h \ + output-private.h \ + output.c \ utils.h \ utils.c diff --git a/examples/output-private.h b/examples/output-private.h new file mode 100644 index 0000000..db8cb06 --- /dev/null +++ b/examples/output-private.h @@ -0,0 +1,58 @@ +/* + * 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 + */ + +#ifndef DCTOOL_OUTPUT_PRIVATE_H +#define DCTOOL_OUTPUT_PRIVATE_H + +#include +#include + +#include "output.h" + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +typedef struct dctool_output_vtable_t dctool_output_vtable_t; + +struct dctool_output_t { + const dctool_output_vtable_t *vtable; + unsigned int number; +}; + +struct dctool_output_vtable_t { + size_t size; + + dc_status_t (*write) (dctool_output_t *output, dc_parser_t *parser, const unsigned char data[], unsigned int size, const unsigned char fingerprint[], unsigned int fsize); + + dc_status_t (*free) (dctool_output_t *output); +}; + +dctool_output_t * +dctool_output_allocate (const dctool_output_vtable_t *vtable); + +void +dctool_output_deallocate (dctool_output_t *output); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ +#endif /* DCTOOL_OUTPUT_PRIVATE_H */ diff --git a/examples/output.c b/examples/output.c new file mode 100644 index 0000000..510c929 --- /dev/null +++ b/examples/output.c @@ -0,0 +1,79 @@ +/* + * 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 "output-private.h" + +dctool_output_t * +dctool_output_allocate (const dctool_output_vtable_t *vtable) +{ + dctool_output_t *output = NULL; + + assert(vtable != NULL); + assert(vtable->size >= sizeof(dctool_output_t)); + + // Allocate memory. + output = (dctool_output_t *) malloc (vtable->size); + if (output == NULL) { + return output; + } + + output->vtable = vtable; + output->number = 0; + + return output; +} + +void +dctool_output_deallocate (dctool_output_t *output) +{ + free (output); +} + +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) +{ + if (output == NULL || output->vtable->write == NULL) + return DC_STATUS_SUCCESS; + + output->number++; + + return output->vtable->write (output, parser, data, size, fingerprint, fsize); +} + +dc_status_t +dctool_output_free (dctool_output_t *output) +{ + dc_status_t status = DC_STATUS_SUCCESS; + + if (output == NULL) + return DC_STATUS_SUCCESS; + + if (output->vtable->free) { + status = output->vtable->free (output); + } + + dctool_output_deallocate (output); + + return status; +} diff --git a/examples/output.h b/examples/output.h new file mode 100644 index 0000000..b11675f --- /dev/null +++ b/examples/output.h @@ -0,0 +1,43 @@ +/* + * 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 + */ + +#ifndef DCTOOL_OUTPUT_H +#define DCTOOL_OUTPUT_H + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +typedef struct dctool_output_t dctool_output_t; + +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); + +dc_status_t +dctool_output_free (dctool_output_t *output); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ +#endif /* DCTOOL_OUTPUT_H */