Add a generic iterator interface.

This commit is contained in:
Jef Driesen 2012-05-19 22:05:57 +02:00
parent c821d40144
commit fbe712fc8f
7 changed files with 145 additions and 0 deletions

View File

@ -4,6 +4,7 @@ libdivecomputer_HEADERS = \
common.h \ common.h \
utils.h \ utils.h \
buffer.h \ buffer.h \
iterator.h \
device.h \ device.h \
parser.h \ parser.h \
datetime.h \ datetime.h \

View File

@ -28,6 +28,7 @@ extern "C" {
typedef enum dc_status_t { typedef enum dc_status_t {
DC_STATUS_SUCCESS = 0, DC_STATUS_SUCCESS = 0,
DC_STATUS_DONE = 1,
DC_STATUS_UNSUPPORTED = -1, DC_STATUS_UNSUPPORTED = -1,
DC_STATUS_INVALIDARGS = -2, DC_STATUS_INVALIDARGS = -2,
DC_STATUS_NOMEMORY = -3, DC_STATUS_NOMEMORY = -3,

View File

@ -0,0 +1,42 @@
/*
* libdivecomputer
*
* Copyright (C) 2012 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 DC_ITERATOR_H
#define DC_ITERATOR_H
#include "common.h"
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
typedef struct dc_iterator_t dc_iterator_t;
dc_status_t
dc_iterator_next (dc_iterator_t *iterator, void *item);
dc_status_t
dc_iterator_free (dc_iterator_t *iterator);
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* DC_ITERATOR_H */

View File

@ -11,6 +11,7 @@ libdivecomputer_la_LDFLAGS = \
libdivecomputer_la_SOURCES = \ libdivecomputer_la_SOURCES = \
version.c \ version.c \
iterator-private.h iterator.c \
device-private.h device.c \ device-private.h device.c \
parser-private.h parser.c \ parser-private.h parser.c \
datetime.c \ datetime.c \

45
src/iterator-private.h Normal file
View File

@ -0,0 +1,45 @@
/*
* libdivecomputer
*
* Copyright (C) 2012 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 DC_ITERATOR_PRIVATE_H
#define DC_ITERATOR_PRIVATE_H
#include <libdivecomputer/iterator.h>
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
typedef struct dc_iterator_vtable_t dc_iterator_vtable_t;
struct dc_iterator_t {
const dc_iterator_vtable_t *vtable;
};
struct dc_iterator_vtable_t {
dc_status_t (*free) (dc_iterator_t *iterator);
dc_status_t (*next) (dc_iterator_t *iterator, void *item);
};
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* DC_ITERATOR_PRIVATE_H */

52
src/iterator.c Normal file
View File

@ -0,0 +1,52 @@
/*
* libdivecomputer
*
* Copyright (C) 2012 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 <stddef.h>
#include <stdlib.h>
#include "iterator-private.h"
dc_status_t
dc_iterator_next (dc_iterator_t *iterator, void *item)
{
if (iterator == NULL)
return DC_STATUS_UNSUPPORTED;
if (iterator->vtable->next == NULL)
return DC_STATUS_UNSUPPORTED;
if (item == NULL)
return DC_STATUS_INVALIDARGS;
return iterator->vtable->next (iterator, item);
}
dc_status_t
dc_iterator_free (dc_iterator_t *iterator)
{
if (iterator == NULL)
return DC_STATUS_SUCCESS;
if (iterator->vtable->free == NULL)
return DC_STATUS_UNSUPPORTED;
return iterator->vtable->free (iterator);
}

View File

@ -17,6 +17,9 @@ dc_datetime_localtime
dc_datetime_gmtime dc_datetime_gmtime
dc_datetime_mktime dc_datetime_mktime
dc_iterator_next
dc_iterator_free
dc_parser_get_type dc_parser_get_type
dc_parser_set_data dc_parser_set_data
dc_parser_get_datetime dc_parser_get_datetime