1 /* tinyproxy - A fast light-weight HTTP proxy
2 * Copyright (C) 2002 Robert James Kaes <rjkaes@users.sourceforge.net>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 /* A vector implementation. The vector can be of an arbitrary length, and
20 * the data for each entry is an lump of data (the size is stored in the
30 * These structures are the storage for the "vector". Entries are
31 * stored in struct vectorentry_s (the data and the length), and the
32 * "vector" structure is implemented as a linked-list. The struct
33 * vector_s stores a pointer to the first vector (vector[0]) and a
34 * count of the number of entries (or how long the vector is.)
36 struct vectorentry_s {
40 struct vectorentry_s *next;
45 struct vectorentry_s *head;
46 struct vectorentry_s *tail;
50 * Create an vector. The vector initially has no elements and no
51 * storage has been allocated for the entries.
53 * A NULL is returned if memory could not be allocated for the
56 vector_t vector_create (void)
60 vector = (vector_t) safemalloc (sizeof (struct vector_s));
64 vector->num_entries = 0;
65 vector->head = vector->tail = NULL;
71 * Deletes an vector. All the entries when this function is run.
73 * Returns: 0 on success
74 * negative if a NULL vector is supplied
76 int vector_delete (vector_t vector)
78 struct vectorentry_s *ptr, *next;
98 * Appends an entry into the vector. The entry is an arbitrary
99 * collection of bytes of _len_ octets. The data is copied into the
100 * vector, so the original data must be freed to avoid a memory leak.
101 * The "data" must be non-NULL and the "len" must be greater than zero.
102 * "pos" is either 0 to prepend the data, or 1 to append the data.
104 * Returns: 0 on success
105 * negative number if there are errors
114 vector_insert (vector_t vector,
119 struct vectorentry_s *entry;
121 if (!vector || !data || len <= 0 ||
122 (pos != INSERT_PREPEND && pos != INSERT_APPEND))
126 (struct vectorentry_s *) safemalloc (sizeof (struct vectorentry_s));
130 entry->data = safemalloc (len);
136 memcpy (entry->data, data, len);
140 /* If there is no head or tail, create them */
141 if (!vector->head && !vector->tail)
142 vector->head = vector->tail = entry;
143 else if (pos == INSERT_PREPEND) {
144 /* prepend the entry */
145 entry->next = vector->head;
146 vector->head = entry;
148 /* append the entry */
149 vector->tail->next = entry;
150 vector->tail = entry;
153 vector->num_entries++;
159 * The following two function are used to make the API clearer. As you
160 * can see they simply call the vector_insert() function with appropriate
163 int vector_append (vector_t vector, void *data, size_t len)
165 return vector_insert (vector, data, len, INSERT_APPEND);
168 int vector_prepend (vector_t vector, void *data, size_t len)
170 return vector_insert (vector, data, len, INSERT_PREPEND);
174 * A pointer to the data at position "pos" (zero based) is returned.
175 * If the vector is out of bound, data is set to NULL.
177 * Returns: negative upon an error
178 * length of data if position is valid
180 void *vector_getentry (vector_t vector, size_t pos, size_t * size)
182 struct vectorentry_s *ptr;
185 if (!vector || pos >= vector->num_entries)
203 * Returns the number of entries (or the length) of the vector.
205 * Returns: negative if vector is not valid
206 * positive length of vector otherwise
208 ssize_t vector_length (vector_t vector)
213 return vector->num_entries;