Added a base class for the parsers.

This commit is contained in:
Jef Driesen 2008-10-31 14:02:39 +00:00
parent 596af5a17b
commit 08e84cb8fc
5 changed files with 245 additions and 0 deletions

View File

@ -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 \

View File

@ -1,3 +1,8 @@
parser_get_type
parser_set_data
parser_samples_foreach
parser_destroy
device_close
device_dump
device_foreach

58
src/parser-private.h Normal file
View File

@ -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 */

84
src/parser.c Normal file
View File

@ -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);
}

96
src/parser.h Normal file
View File

@ -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 */