diff options
Diffstat (limited to 'src/utils.c')
| -rw-r--r-- | src/utils.c | 92 |
1 files changed, 0 insertions, 92 deletions
diff --git a/src/utils.c b/src/utils.c deleted file mode 100644 index 826bfbe..0000000 --- a/src/utils.c +++ /dev/null | |||
| @@ -1,92 +0,0 @@ | |||
| 1 | /* | ||
| 2 | * utils.c | ||
| 3 | * | ||
| 4 | * Copyright (C) 2009 Hector Martin <hector@marcansoft.com> | ||
| 5 | * Copyright (C) 2009 Nikias Bassen <nikias@gmx.li> | ||
| 6 | * Copyright (c) 2013 Federico Mena Quintero | ||
| 7 | * | ||
| 8 | * This library is free software; you can redistribute it and/or modify | ||
| 9 | * it under the terms of the GNU Lesser General Public License as | ||
| 10 | * published by the Free Software Foundation, either version 2.1 of the | ||
| 11 | * License, or (at your option) any later version. | ||
| 12 | * | ||
| 13 | * This library is distributed in the hope that it will be useful, | ||
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 16 | * GNU General Public License for more details. | ||
| 17 | * | ||
| 18 | * You should have received a copy of the GNU Lesser General Public | ||
| 19 | * License along with this program; if not, write to the Free Software | ||
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 21 | */ | ||
| 22 | |||
| 23 | #ifdef HAVE_CONFIG_H | ||
| 24 | #include <config.h> | ||
| 25 | #endif | ||
| 26 | |||
| 27 | #include <stdlib.h> | ||
| 28 | #include <string.h> | ||
| 29 | #include <stdio.h> | ||
| 30 | #include <errno.h> | ||
| 31 | |||
| 32 | #include "utils.h" | ||
| 33 | |||
| 34 | #define CAPACITY_STEP 8 | ||
| 35 | |||
| 36 | void collection_init(struct collection *col) | ||
| 37 | { | ||
| 38 | col->list = malloc(sizeof(void *) * CAPACITY_STEP); | ||
| 39 | memset(col->list, 0, sizeof(void *) * CAPACITY_STEP); | ||
| 40 | col->capacity = CAPACITY_STEP; | ||
| 41 | } | ||
| 42 | |||
| 43 | void collection_free(struct collection *col) | ||
| 44 | { | ||
| 45 | free(col->list); | ||
| 46 | col->list = NULL; | ||
| 47 | col->capacity = 0; | ||
| 48 | } | ||
| 49 | |||
| 50 | void collection_add(struct collection *col, void *element) | ||
| 51 | { | ||
| 52 | int i; | ||
| 53 | for(i=0; i<col->capacity; i++) { | ||
| 54 | if(!col->list[i]) { | ||
| 55 | col->list[i] = element; | ||
| 56 | return; | ||
| 57 | } | ||
| 58 | } | ||
| 59 | col->list = realloc(col->list, sizeof(void*) * (col->capacity + CAPACITY_STEP)); | ||
| 60 | memset(&col->list[col->capacity], 0, sizeof(void *) * CAPACITY_STEP); | ||
| 61 | col->list[col->capacity] = element; | ||
| 62 | col->capacity += CAPACITY_STEP; | ||
| 63 | } | ||
| 64 | |||
| 65 | void collection_remove(struct collection *col, void *element) | ||
| 66 | { | ||
| 67 | int i; | ||
| 68 | for(i=0; i<col->capacity; i++) { | ||
| 69 | if(col->list[i] == element) { | ||
| 70 | col->list[i] = NULL; | ||
| 71 | return; | ||
| 72 | } | ||
| 73 | } | ||
| 74 | } | ||
| 75 | |||
| 76 | int collection_count(struct collection *col) | ||
| 77 | { | ||
| 78 | int i, cnt = 0; | ||
| 79 | for(i=0; i<col->capacity; i++) { | ||
| 80 | if(col->list[i]) | ||
| 81 | cnt++; | ||
| 82 | } | ||
| 83 | return cnt; | ||
| 84 | } | ||
| 85 | |||
| 86 | void collection_copy(struct collection *dest, struct collection *src) | ||
| 87 | { | ||
| 88 | if (!dest || !src) return; | ||
| 89 | dest->capacity = src->capacity; | ||
| 90 | dest->list = malloc(sizeof(void*) * src->capacity); | ||
| 91 | memcpy(dest->list, src->list, sizeof(void*) * src->capacity); | ||
| 92 | } | ||
