From 08e84cb8fccee751140f8d4097a9c7d8fd374d01 Mon Sep 17 00:00:00 2001 From: Jef Driesen Date: Fri, 31 Oct 2008 14:02:39 +0000 Subject: [PATCH] Added a base class for the parsers. --- src/Makefile.am | 2 + src/libdivecomputer.symbols | 5 ++ src/parser-private.h | 58 ++++++++++++++++++++++ src/parser.c | 84 ++++++++++++++++++++++++++++++++ src/parser.h | 96 +++++++++++++++++++++++++++++++++++++ 5 files changed, 245 insertions(+) create mode 100644 src/parser-private.h create mode 100644 src/parser.c create mode 100644 src/parser.h diff --git a/src/Makefile.am b/src/Makefile.am index 1b2be11..ced0d8b 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -6,6 +6,7 @@ libdivecomputerdir = $(includedir)/libdivecomputer libdivecomputer_HEADERS = \ utils.h \ device.h \ + parser.h \ suunto.h \ suunto_eon.h \ suunto_vyper.h \ @@ -33,6 +34,7 @@ libdivecomputer_la_LDFLAGS = \ libdivecomputer_la_SOURCES = \ device.h device-private.h device.c \ + parser.h parser-private.h parser.c \ suunto.h \ suunto_common.h suunto_common.c \ suunto_eon.h suunto_eon.c \ diff --git a/src/libdivecomputer.symbols b/src/libdivecomputer.symbols index e593b82..271261f 100644 --- a/src/libdivecomputer.symbols +++ b/src/libdivecomputer.symbols @@ -1,3 +1,8 @@ +parser_get_type +parser_set_data +parser_samples_foreach +parser_destroy + device_close device_dump device_foreach diff --git a/src/parser-private.h b/src/parser-private.h new file mode 100644 index 0000000..ce87721 --- /dev/null +++ b/src/parser-private.h @@ -0,0 +1,58 @@ +/* + * libdivecomputer + * + * Copyright (C) 2008 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 PARSER_PRIVATE_H +#define PARSER_PRIVATE_H + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +#include "parser.h" + +struct parser_t; +struct parser_backend_t; + +typedef struct parser_backend_t parser_backend_t; + +struct parser_t { + const parser_backend_t *backend; + const unsigned char *data; + unsigned int size; +}; + +struct parser_backend_t { + parser_type_t type; + + parser_status_t (*set_data) (parser_t *parser, const unsigned char *data, unsigned int size); + + parser_status_t (*samples_foreach) (parser_t *parser, sample_callback_t callback, void *userdata); + + parser_status_t (*destroy) (parser_t *parser); +}; + +void +parser_init (parser_t *parser, const parser_backend_t *backend); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ +#endif /* PARSER_PRIVATE_H */ diff --git a/src/parser.c b/src/parser.c new file mode 100644 index 0000000..747ebb7 --- /dev/null +++ b/src/parser.c @@ -0,0 +1,84 @@ +/* + * libdivecomputer + * + * Copyright (C) 2008 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 "parser-private.h" + +#define NULL 0 + +void +parser_init (parser_t *parser, const parser_backend_t *backend) +{ + parser->backend = backend; + parser->data = NULL; + parser->size = 0; +} + + +parser_type_t +parser_get_type (parser_t *parser) +{ + if (parser == NULL) + return PARSER_TYPE_NULL; + + return parser->backend->type; +} + + +parser_status_t +parser_set_data (parser_t *parser, const unsigned char *data, unsigned int size) +{ + if (parser == NULL) + return PARSER_STATUS_UNSUPPORTED; + + if (parser->backend->set_data == NULL) + return PARSER_STATUS_UNSUPPORTED; + + parser->data = data; + parser->size = size; + + return parser->backend->set_data (parser, data, size); +} + + +parser_status_t +parser_samples_foreach (parser_t *parser, sample_callback_t callback, void *userdata) +{ + if (parser == NULL) + return PARSER_STATUS_UNSUPPORTED; + + if (parser->backend->samples_foreach == NULL) + return PARSER_STATUS_UNSUPPORTED; + + return parser->backend->samples_foreach (parser, callback, userdata); +} + + +parser_status_t +parser_destroy (parser_t *parser) +{ + if (parser == NULL) + return PARSER_STATUS_SUCCESS; + + if (parser->backend->destroy == NULL) + return PARSER_STATUS_UNSUPPORTED; + + return parser->backend->destroy (parser); +} diff --git a/src/parser.h b/src/parser.h new file mode 100644 index 0000000..0eb81de --- /dev/null +++ b/src/parser.h @@ -0,0 +1,96 @@ +/* + * libdivecomputer + * + * Copyright (C) 2008 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 PARSER_H +#define PARSER_H + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +typedef enum parser_type_t { + PARSER_TYPE_NULL = 0 +} parser_type_t; + +typedef enum parser_status_t { + PARSER_STATUS_SUCCESS = 0, + PARSER_STATUS_UNSUPPORTED = -1, + PARSER_STATUS_TYPE_MISMATCH = -2, + PARSER_STATUS_ERROR = -3, + PARSER_STATUS_MEMORY = -7 +} parser_status_t; + +typedef enum parser_sample_type_t { + SAMPLE_TYPE_TIME, + SAMPLE_TYPE_DEPTH, + SAMPLE_TYPE_PRESSURE, + SAMPLE_TYPE_TEMPERATURE, + SAMPLE_TYPE_EVENT, + SAMPLE_TYPE_RBT, + SAMPLE_TYPE_HEARTBEAT +} parser_sample_type_t; + +typedef enum parser_sample_event_t { + SAMPLE_EVENT_NONE +} parser_sample_event_t; + +typedef enum parser_sample_flags_t { + SAMPLE_FLAGS_NONE +} parser_sample_flags_t; + +typedef union parser_sample_value_t { + unsigned int time; + double depth; + struct { + unsigned int tank; + double value; + } pressure; + double temperature; + struct { + unsigned int type; + unsigned int time; + unsigned int flags; + unsigned int value; + } event; + unsigned int rbt; + unsigned int heartbeat; +} parser_sample_value_t; + +typedef struct parser_t parser_t; + +typedef void (*sample_callback_t) (parser_sample_type_t type, parser_sample_value_t value, void *userdata); + +parser_type_t +parser_get_type (parser_t *device); + +parser_status_t +parser_set_data (parser_t *parser, const unsigned char *data, unsigned int size); + +parser_status_t +parser_samples_foreach (parser_t *parser, sample_callback_t callback, void *userdata); + +parser_status_t +parser_destroy (parser_t *parser); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ +#endif /* PARSER_H */