(vector_getentry): Changed the API to return the data pointer and have

the length returned in a argument variable pointer.  This should be a
more natural way of using the function.
This commit is contained in:
Robert James Kaes 2003-05-30 16:21:48 +00:00
parent c3eaebd1c5
commit 1955dcd47b
2 changed files with 19 additions and 18 deletions

View File

@ -1,4 +1,4 @@
/* $Id: vector.c,v 1.8 2003-05-29 21:07:22 rjkaes Exp $ /* $Id: vector.c,v 1.9 2003-05-30 16:21:48 rjkaes Exp $
* *
* A vector implementation. The vector can be of an arbitrary length, and * A vector implementation. The vector can be of an arbitrary length, and
* the data for each entry is an lump of data (the size is stored in the * the data for each entry is an lump of data (the size is stored in the
@ -168,23 +168,20 @@ vector_prepend(vector_t vector, void *data, ssize_t len)
} }
/* /*
* A pointer to the data at position "pos" (zero based) is returned in the * A pointer to the data at position "pos" (zero based) is returned.
* "data" pointer. If the vector is out of bound, data is set to NULL. * If the vector is out of bound, data is set to NULL.
* *
* Returns: negative upon an error * Returns: negative upon an error
* length of data if position is valid * length of data if position is valid
*/ */
ssize_t void *
vector_getentry(vector_t vector, size_t pos, void **data) vector_getentry(vector_t vector, size_t pos, size_t* size)
{ {
struct vectorentry_s *ptr; struct vectorentry_s *ptr;
size_t loc; size_t loc;
if (!vector || !data) if (!vector || pos < 0 || pos >= vector->num_entries)
return -EINVAL; return NULL;
if (pos < 0 || pos >= vector->num_entries)
return -ERANGE;
loc = 0; loc = 0;
ptr = vector->head; ptr = vector->head;
@ -194,8 +191,10 @@ vector_getentry(vector_t vector, size_t pos, void **data)
loc++; loc++;
} }
*data = ptr->data; if (size)
return ptr->len; *size = ptr->len;
return ptr->data;
} }
/* /*

View File

@ -1,4 +1,4 @@
/* $Id: vector.h,v 1.3 2003-05-29 21:07:22 rjkaes Exp $ /* $Id: vector.h,v 1.4 2003-05-30 16:21:47 rjkaes Exp $
* *
* A vector implementation. The vector can be of an arbritrary length, and * A vector implementation. The vector can be of an arbritrary length, and
* the data for each entry is an lump of data (the size is stored in the * the data for each entry is an lump of data (the size is stored in the
@ -55,8 +55,8 @@ extern int vector_append(vector_t vector, void *data, ssize_t len);
extern int vector_prepend(vector_t vector, void *data, ssize_t len); extern int vector_prepend(vector_t vector, void *data, ssize_t len);
/* /*
* A pointer to the data at position "pos" (zero based) is returned in the * A pointer to the data at position "pos" (zero based) is returned and the
* "data" pointer. If the vector is out of bound, data is set to NULL. * size pointer contains the length of the data stored.
* *
* The pointer points to the actual data in the vector, so you have * The pointer points to the actual data in the vector, so you have
* the power to modify the data, but do it responsibly since the * the power to modify the data, but do it responsibly since the
@ -65,10 +65,12 @@ extern int vector_prepend(vector_t vector, void *data, ssize_t len);
* likely mess up the "length" parameter of the data.) However, DON'T * likely mess up the "length" parameter of the data.) However, DON'T
* try to realloc or free the data; doing so will break the vector. * try to realloc or free the data; doing so will break the vector.
* *
* Returns: negative upon an error * If "size" is NULL the size of the data is not returned.
* length of data if position is valid *
* Returns: NULL on error
* valid pointer to data
*/ */
extern ssize_t vector_getentry(vector_t vector, size_t pos, void **data); extern void* vector_getentry(vector_t vector, size_t pos, size_t* size);
/* /*
* Returns the number of enteries (or the length) of the vector. * Returns the number of enteries (or the length) of the vector.