subrascasse

Sub Rascasse: an iPhone dive log for Subsurface cloud storage, with a pure-Swift git client (GPL-2.0)
git clone https://git.schurger.org/subrascasse.git
Log | Files | Refs | README | LICENSE

iterator.c (2181B)


      1 /*
      2  * libdivecomputer
      3  *
      4  * Copyright (C) 2012 Jef Driesen
      5  *
      6  * This library is free software; you can redistribute it and/or
      7  * modify it under the terms of the GNU Lesser General Public
      8  * License as published by the Free Software Foundation; either
      9  * version 2.1 of the License, or (at your option) any later version.
     10  *
     11  * This library is distributed in the hope that it will be useful,
     12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     14  * Lesser General Public License for more details.
     15  *
     16  * You should have received a copy of the GNU Lesser General Public
     17  * License along with this library; if not, write to the Free Software
     18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
     19  * MA 02110-1301 USA
     20  */
     21 
     22 #include <stddef.h>
     23 #include <stdlib.h>
     24 #include <assert.h>
     25 
     26 #include "context-private.h"
     27 #include "iterator-private.h"
     28 
     29 dc_iterator_t *
     30 dc_iterator_allocate (dc_context_t *context, const dc_iterator_vtable_t *vtable)
     31 {
     32 	dc_iterator_t *iterator = NULL;
     33 
     34 	assert(vtable != NULL);
     35 	assert(vtable->size >= sizeof(dc_iterator_t));
     36 
     37 	// Allocate memory.
     38 	iterator = (dc_iterator_t *) malloc (vtable->size);
     39 	if (iterator == NULL) {
     40 		ERROR (context, "Failed to allocate memory.");
     41 		return iterator;
     42 	}
     43 
     44 	iterator->vtable = vtable;
     45 	iterator->context = context;
     46 
     47 	return iterator;
     48 }
     49 
     50 void
     51 dc_iterator_deallocate (dc_iterator_t *iterator)
     52 {
     53 	free (iterator);
     54 }
     55 
     56 int
     57 dc_iterator_isinstance (dc_iterator_t *iterator, const dc_iterator_vtable_t *vtable)
     58 {
     59 	if (iterator == NULL)
     60 		return 0;
     61 
     62 	return iterator->vtable == vtable;
     63 }
     64 
     65 dc_status_t
     66 dc_iterator_next (dc_iterator_t *iterator, void *item)
     67 {
     68 	if (iterator == NULL || iterator->vtable->next == NULL)
     69 		return DC_STATUS_UNSUPPORTED;
     70 
     71 	if (item == NULL)
     72 		return DC_STATUS_INVALIDARGS;
     73 
     74 	return iterator->vtable->next (iterator, item);
     75 }
     76 
     77 dc_status_t
     78 dc_iterator_free (dc_iterator_t *iterator)
     79 {
     80 	dc_status_t status = DC_STATUS_SUCCESS;
     81 
     82 	if (iterator == NULL)
     83 		return DC_STATUS_SUCCESS;
     84 
     85 	if (iterator->vtable->free) {
     86 		status = iterator->vtable->free (iterator);
     87 	}
     88 
     89 	dc_iterator_deallocate (iterator);
     90 
     91 	return status;
     92 }