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.
This commit is contained in:
parent
4765f7bc50
commit
195702046c
@ -17,5 +17,8 @@ dctool_SOURCES = \
|
|||||||
dctool_read.c \
|
dctool_read.c \
|
||||||
dctool_write.c \
|
dctool_write.c \
|
||||||
dctool_fwupdate.c \
|
dctool_fwupdate.c \
|
||||||
|
output.h \
|
||||||
|
output-private.h \
|
||||||
|
output.c \
|
||||||
utils.h \
|
utils.h \
|
||||||
utils.c
|
utils.c
|
||||||
|
|||||||
58
examples/output-private.h
Normal file
58
examples/output-private.h
Normal file
@ -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 <libdivecomputer/common.h>
|
||||||
|
#include <libdivecomputer/parser.h>
|
||||||
|
|
||||||
|
#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 */
|
||||||
79
examples/output.c
Normal file
79
examples/output.c
Normal file
@ -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 <stdlib.h>
|
||||||
|
#include <assert.h>
|
||||||
|
|
||||||
|
#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;
|
||||||
|
}
|
||||||
43
examples/output.h
Normal file
43
examples/output.h
Normal file
@ -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 <libdivecomputer/common.h>
|
||||||
|
#include <libdivecomputer/parser.h>
|
||||||
|
|
||||||
|
#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 */
|
||||||
Loading…
x
Reference in New Issue
Block a user