2005-08-15 11:54:31 +08:00
|
|
|
/* $Id: vector.c,v 1.13 2005-08-15 03:54:31 rjkaes Exp $
|
2002-04-08 05:29:23 +08:00
|
|
|
*
|
2002-05-24 12:45:32 +08:00
|
|
|
* A vector implementation. The vector can be of an arbitrary length, and
|
2002-04-08 05:29:23 +08:00
|
|
|
* the data for each entry is an lump of data (the size is stored in the
|
|
|
|
* vector.)
|
|
|
|
*
|
2005-07-13 01:39:44 +08:00
|
|
|
* Copyright (C) 2002 Robert James Kaes (rjkaes@users.sourceforge.net)
|
2002-04-08 05:29:23 +08:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program 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 General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
2002-04-19 01:57:20 +08:00
|
|
|
#include "tinyproxy.h"
|
2002-04-08 05:29:23 +08:00
|
|
|
|
2002-05-24 02:20:27 +08:00
|
|
|
#include "heap.h"
|
2002-04-08 05:29:23 +08:00
|
|
|
#include "vector.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* These structures are the storage for the "vector". Entries are
|
|
|
|
* stored in struct vectorentry_s (the data and the length), and the
|
|
|
|
* "vector" structure is implemented as a linked-list. The struct
|
|
|
|
* vector_s stores a pointer to the first vector (vector[0]) and a
|
2002-05-24 12:45:32 +08:00
|
|
|
* count of the number of entries (or how long the vector is.)
|
2002-04-08 05:29:23 +08:00
|
|
|
*/
|
|
|
|
struct vectorentry_s {
|
2005-08-15 11:54:31 +08:00
|
|
|
void *data;
|
|
|
|
size_t len;
|
2002-04-08 05:29:23 +08:00
|
|
|
|
2005-08-15 11:54:31 +08:00
|
|
|
struct vectorentry_s *next;
|
2002-04-08 05:29:23 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct vector_s {
|
2005-08-15 11:54:31 +08:00
|
|
|
size_t num_entries;
|
|
|
|
struct vectorentry_s *head;
|
|
|
|
struct vectorentry_s *tail;
|
2002-04-08 05:29:23 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create an vector. The vector initially has no elements and no
|
|
|
|
* storage has been allocated for the entries.
|
|
|
|
*
|
|
|
|
* A NULL is returned if memory could not be allocated for the
|
|
|
|
* vector.
|
|
|
|
*/
|
|
|
|
vector_t
|
|
|
|
vector_create(void)
|
|
|
|
{
|
2005-08-15 11:54:31 +08:00
|
|
|
vector_t vector;
|
2002-04-08 05:29:23 +08:00
|
|
|
|
2005-08-15 11:54:31 +08:00
|
|
|
vector = safemalloc(sizeof(struct vector_s));
|
|
|
|
if (!vector)
|
|
|
|
return NULL;
|
2002-04-08 05:29:23 +08:00
|
|
|
|
2005-08-15 11:54:31 +08:00
|
|
|
vector->num_entries = 0;
|
|
|
|
vector->head = vector->tail = NULL;
|
|
|
|
|
|
|
|
return vector;
|
2002-04-08 05:29:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2002-05-24 12:45:32 +08:00
|
|
|
* Deletes an vector. All the entries when this function is run.
|
2002-04-08 05:29:23 +08:00
|
|
|
*
|
|
|
|
* Returns: 0 on success
|
|
|
|
* negative if a NULL vector is supplied
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
vector_delete(vector_t vector)
|
|
|
|
{
|
2005-08-15 11:54:31 +08:00
|
|
|
struct vectorentry_s *ptr, *next;
|
|
|
|
|
|
|
|
if (!vector)
|
|
|
|
return -EINVAL;
|
2002-04-08 05:29:23 +08:00
|
|
|
|
2005-08-15 11:54:31 +08:00
|
|
|
ptr = vector->head;
|
|
|
|
while (ptr) {
|
|
|
|
next = ptr->next;
|
|
|
|
safefree(ptr->data);
|
|
|
|
safefree(ptr);
|
2002-04-08 05:29:23 +08:00
|
|
|
|
2005-08-15 11:54:31 +08:00
|
|
|
ptr = next;
|
|
|
|
}
|
2002-04-08 05:29:23 +08:00
|
|
|
|
2005-08-15 11:54:31 +08:00
|
|
|
safefree(vector);
|
2002-04-08 05:29:23 +08:00
|
|
|
|
2005-08-15 11:54:31 +08:00
|
|
|
return 0;
|
2002-04-08 05:29:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2003-05-30 04:47:52 +08:00
|
|
|
* Appends an entry into the vector. The entry is an arbitrary
|
2002-04-08 05:29:23 +08:00
|
|
|
* collection of bytes of _len_ octets. The data is copied into the
|
|
|
|
* vector, so the original data must be freed to avoid a memory leak.
|
|
|
|
* The "data" must be non-NULL and the "len" must be greater than zero.
|
2003-05-30 05:07:22 +08:00
|
|
|
* "pos" is either 0 to prepend the data, or 1 to append the data.
|
2002-04-08 05:29:23 +08:00
|
|
|
*
|
|
|
|
* Returns: 0 on success
|
|
|
|
* negative number if there are errors
|
|
|
|
*/
|
2003-05-30 05:07:22 +08:00
|
|
|
#define INSERT_PREPEND 0
|
|
|
|
#define INSERT_APPEND 1
|
|
|
|
|
|
|
|
static int
|
|
|
|
vector_insert(vector_t vector, void *data, ssize_t len, int pos)
|
2002-04-08 05:29:23 +08:00
|
|
|
{
|
2005-08-15 11:54:31 +08:00
|
|
|
struct vectorentry_s *entry;
|
|
|
|
|
|
|
|
if (!vector || !data || len <= 0 ||
|
|
|
|
(pos != INSERT_PREPEND && pos != INSERT_APPEND))
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
entry = safemalloc(sizeof(struct vectorentry_s));
|
|
|
|
if (!entry)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
entry->data = safemalloc(len);
|
|
|
|
if (!entry->data) {
|
|
|
|
safefree(entry);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(entry->data, data, len);
|
|
|
|
entry->len = len;
|
|
|
|
entry->next = NULL;
|
|
|
|
|
|
|
|
/* If there is no head or tail, create them */
|
|
|
|
if (!vector->head && !vector->tail)
|
|
|
|
vector->head = vector->tail = entry;
|
|
|
|
else if (pos == 0) {
|
|
|
|
/* prepend the entry */
|
|
|
|
entry->next = vector->head;
|
|
|
|
vector->head = entry;
|
|
|
|
} else {
|
|
|
|
/* append the entry */
|
|
|
|
vector->tail->next = entry;
|
|
|
|
vector->tail = entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
vector->num_entries++;
|
|
|
|
|
|
|
|
return 0;
|
2002-04-08 05:29:23 +08:00
|
|
|
}
|
|
|
|
|
2003-05-30 05:07:22 +08:00
|
|
|
/*
|
|
|
|
* The following two function are used to make the API clearer. As you
|
|
|
|
* can see they simply call the vector_insert() function with appropriate
|
|
|
|
* arguments.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
vector_append(vector_t vector, void *data, ssize_t len)
|
|
|
|
{
|
2005-08-15 11:54:31 +08:00
|
|
|
return vector_insert(vector, data, len, INSERT_APPEND);
|
2003-05-30 05:07:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
vector_prepend(vector_t vector, void *data, ssize_t len)
|
|
|
|
{
|
2005-08-15 11:54:31 +08:00
|
|
|
return vector_insert(vector, data, len, INSERT_PREPEND);
|
2003-05-30 05:07:22 +08:00
|
|
|
}
|
|
|
|
|
2002-04-08 05:29:23 +08:00
|
|
|
/*
|
2003-05-31 00:21:48 +08:00
|
|
|
* A pointer to the data at position "pos" (zero based) is returned.
|
|
|
|
* If the vector is out of bound, data is set to NULL.
|
2002-04-08 05:29:23 +08:00
|
|
|
*
|
|
|
|
* Returns: negative upon an error
|
|
|
|
* length of data if position is valid
|
|
|
|
*/
|
2003-05-31 00:21:48 +08:00
|
|
|
void *
|
2005-08-15 11:54:31 +08:00
|
|
|
vector_getentry(vector_t vector, size_t pos, size_t * size)
|
2002-04-08 05:29:23 +08:00
|
|
|
{
|
2005-08-15 11:54:31 +08:00
|
|
|
struct vectorentry_s *ptr;
|
|
|
|
size_t loc;
|
2002-04-08 05:29:23 +08:00
|
|
|
|
2005-08-15 11:54:31 +08:00
|
|
|
if (!vector || pos < 0 || pos >= vector->num_entries)
|
|
|
|
return NULL;
|
2002-04-08 05:29:23 +08:00
|
|
|
|
2005-08-15 11:54:31 +08:00
|
|
|
loc = 0;
|
|
|
|
ptr = vector->head;
|
2002-04-08 05:29:23 +08:00
|
|
|
|
2005-08-15 11:54:31 +08:00
|
|
|
while (loc != pos) {
|
|
|
|
ptr = ptr->next;
|
|
|
|
loc++;
|
|
|
|
}
|
2002-04-08 05:29:23 +08:00
|
|
|
|
2005-08-15 11:54:31 +08:00
|
|
|
if (size)
|
|
|
|
*size = ptr->len;
|
2003-05-31 00:21:48 +08:00
|
|
|
|
2005-08-15 11:54:31 +08:00
|
|
|
return ptr->data;
|
2002-04-08 05:29:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2002-05-24 12:45:32 +08:00
|
|
|
* Returns the number of entries (or the length) of the vector.
|
2002-04-08 05:29:23 +08:00
|
|
|
*
|
|
|
|
* Returns: negative if vector is not valid
|
|
|
|
* positive length of vector otherwise
|
|
|
|
*/
|
|
|
|
ssize_t
|
|
|
|
vector_length(vector_t vector)
|
|
|
|
{
|
2005-08-15 11:54:31 +08:00
|
|
|
if (!vector)
|
|
|
|
return -EINVAL;
|
2002-04-08 05:29:23 +08:00
|
|
|
|
2005-08-15 11:54:31 +08:00
|
|
|
return vector->num_entries;
|
2002-04-08 05:29:23 +08:00
|
|
|
}
|