diff options
Diffstat (limited to 'src/vector.c')
-rw-r--r-- | src/vector.c | 184 |
1 files changed, 96 insertions, 88 deletions
diff --git a/src/vector.c b/src/vector.c index e9ca586..a68fddc 100644 --- a/src/vector.c +++ b/src/vector.c @@ -33,17 +33,19 @@ * 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; }; /* @@ -54,18 +56,18 @@ struct vector_s { * vector. */ vector_t -vector_create(void) +vector_create (void) { - vector_t vector; + vector_t vector; - vector = safemalloc(sizeof(struct vector_s)); - if (!vector) - return NULL; + vector = 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; } /* @@ -75,25 +77,26 @@ vector_create(void) * negative if a NULL vector is supplied */ int -vector_delete(vector_t vector) +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; } /* @@ -110,44 +113,48 @@ vector_delete(vector_t vector) #define INSERT_APPEND 1 static int -vector_insert(vector_t vector, void *data, ssize_t len, int pos) +vector_insert (vector_t vector, void *data, ssize_t len, int pos) { - 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; + 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; } /* @@ -156,15 +163,15 @@ vector_insert(vector_t vector, void *data, ssize_t len, int pos) * arguments. */ int -vector_append(vector_t vector, void *data, ssize_t len) +vector_append (vector_t vector, void *data, ssize_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, ssize_t len) +vector_prepend (vector_t vector, void *data, ssize_t len) { - return vector_insert(vector, data, len, INSERT_PREPEND); + return vector_insert (vector, data, len, INSERT_PREPEND); } /* @@ -175,26 +182,27 @@ vector_prepend(vector_t vector, void *data, ssize_t len) * length of data if position is valid */ void * -vector_getentry(vector_t vector, size_t pos, size_t * size) +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; } /* @@ -204,10 +212,10 @@ vector_getentry(vector_t vector, size_t pos, size_t * size) * positive length of vector otherwise */ ssize_t -vector_length(vector_t vector) +vector_length (vector_t vector) { - if (!vector) - return -EINVAL; + if (!vector) + return -EINVAL; - return vector->num_entries; + return vector->num_entries; } |