diff options
Diffstat (limited to 'src/vector.c')
-rw-r--r-- | src/vector.c | 192 |
1 files changed, 89 insertions, 103 deletions
diff --git a/src/vector.c b/src/vector.c index 77f3955..a80eb4e 100644 --- a/src/vector.c +++ b/src/vector.c @@ -33,19 +33,17 @@ * vector_s stores a pointer to the first vector (vector[0]) and a * count of the number of entries (or how long the vector is.) */ -struct vectorentry_s -{ - void *data; - size_t len; +struct vectorentry_s { + void *data; + size_t len; - struct vectorentry_s *next; + struct vectorentry_s *next; }; -struct vector_s -{ - size_t num_entries; - struct vectorentry_s *head; - struct vectorentry_s *tail; +struct vector_s { + size_t num_entries; + struct vectorentry_s *head; + struct vectorentry_s *tail; }; /* @@ -55,19 +53,18 @@ struct vector_s * A NULL is returned if memory could not be allocated for the * vector. */ -vector_t -vector_create (void) +vector_t vector_create (void) { - vector_t vector; + vector_t vector; - vector = (vector_t)safemalloc (sizeof (struct vector_s)); - if (!vector) - return NULL; + vector = (vector_t) safemalloc (sizeof (struct vector_s)); + if (!vector) + return NULL; - vector->num_entries = 0; - vector->head = vector->tail = NULL; + vector->num_entries = 0; + vector->head = vector->tail = NULL; - return vector; + return vector; } /* @@ -76,27 +73,25 @@ vector_create (void) * Returns: 0 on success * negative if a NULL vector is supplied */ -int -vector_delete (vector_t vector) +int vector_delete (vector_t vector) { - struct vectorentry_s *ptr, *next; + struct vectorentry_s *ptr, *next; - if (!vector) - return -EINVAL; + if (!vector) + return -EINVAL; - ptr = vector->head; - while (ptr) - { - next = ptr->next; - safefree (ptr->data); - safefree (ptr); + ptr = vector->head; + while (ptr) { + next = ptr->next; + safefree (ptr->data); + safefree (ptr); - ptr = next; - } + ptr = next; + } - safefree (vector); + safefree (vector); - return 0; + return 0; } /* @@ -112,49 +107,45 @@ vector_delete (vector_t vector) #define INSERT_PREPEND 0 #define INSERT_APPEND 1 -static int -vector_insert (vector_t vector, void *data, size_t len, int pos) +static int vector_insert (vector_t vector, void *data, size_t len, int pos) { - struct vectorentry_s *entry; - - if (!vector || !data || len <= 0 || - (pos != INSERT_PREPEND && pos != INSERT_APPEND)) - return -EINVAL; - - entry = (struct vectorentry_s *)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; + struct vectorentry_s *entry; + + if (!vector || !data || len <= 0 || + (pos != INSERT_PREPEND && pos != INSERT_APPEND)) + return -EINVAL; + + entry = + (struct vectorentry_s *) 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; } /* @@ -162,16 +153,14 @@ vector_insert (vector_t vector, void *data, size_t len, int pos) * can see they simply call the vector_insert() function with appropriate * arguments. */ -int -vector_append (vector_t vector, void *data, size_t len) +int vector_append (vector_t vector, void *data, size_t len) { - return vector_insert (vector, data, len, INSERT_APPEND); + return vector_insert (vector, data, len, INSERT_APPEND); } -int -vector_prepend (vector_t vector, void *data, size_t len) +int vector_prepend (vector_t vector, void *data, size_t len) { - return vector_insert (vector, data, len, INSERT_PREPEND); + return vector_insert (vector, data, len, INSERT_PREPEND); } /* @@ -181,28 +170,26 @@ vector_prepend (vector_t vector, void *data, size_t len) * Returns: negative upon an error * length of data if position is valid */ -void * -vector_getentry (vector_t vector, size_t pos, size_t * size) +void *vector_getentry (vector_t vector, size_t pos, size_t * size) { - struct vectorentry_s *ptr; - size_t loc; + struct vectorentry_s *ptr; + size_t loc; - if (!vector || pos >= vector->num_entries) - return NULL; + if (!vector || pos >= vector->num_entries) + return NULL; - loc = 0; - ptr = vector->head; + loc = 0; + ptr = vector->head; - while (loc != pos) - { - ptr = ptr->next; - loc++; - } + while (loc != pos) { + ptr = ptr->next; + loc++; + } - if (size) - *size = ptr->len; + if (size) + *size = ptr->len; - return ptr->data; + return ptr->data; } /* @@ -211,11 +198,10 @@ vector_getentry (vector_t vector, size_t pos, size_t * size) * Returns: negative if vector is not valid * positive length of vector otherwise */ -ssize_t -vector_length (vector_t vector) +ssize_t vector_length (vector_t vector) { - if (!vector) - return -EINVAL; + if (!vector) + return -EINVAL; - return vector->num_entries; + return vector->num_entries; } |