| 1 |
/** |
| 2 |
* This file is part of the Detox package. |
| 3 |
* |
| 4 |
* Copyright (c) Doug Harple <[email protected]> |
| 5 |
* |
| 6 |
* For the full copyright and license information, please view the LICENSE |
| 7 |
* file that was distributed with this source code. |
| 8 |
*/ |
| 9 |
|
| 10 |
#include <stdio.h> |
| 11 |
#include <stdlib.h> |
| 12 |
#include <string.h> |
| 13 |
|
| 14 |
#include "filelist.h" |
| 15 |
#include "wrapped.h" |
| 16 |
|
| 17 |
#define FILELIST_CHUNK 16 |
| 18 |
|
| 19 |
/** |
| 20 |
* Returns the current count of files in the list. |
| 21 |
* |
| 22 |
* @param list |
| 23 |
*/ |
| 24 |
int filelist_count(filelist_t *list) |
| 25 |
{ |
| 26 |
if (list == NULL) { |
| 27 |
return 0; |
| 28 |
} |
| 29 |
|
| 30 |
return list->count; |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Releases memory currently held by a list. Do *not* use the list after |
| 35 |
* calling this function on it. |
| 36 |
* |
| 37 |
* @param list |
| 38 |
*/ |
| 39 |
void filelist_free(filelist_t *list) |
| 40 |
{ |
| 41 |
int i; |
| 42 |
|
| 43 |
if (list == NULL) { |
| 44 |
return; |
| 45 |
} |
| 46 |
|
| 47 |
for (i = 0; i < list->count; i++) { |
| 48 |
free(list->files[i]); |
| 49 |
} |
| 50 |
|
| 51 |
free(list->files); |
| 52 |
free(list); |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Retrieves the next member of the filelist. |
| 57 |
* |
| 58 |
* @param list |
| 59 |
* |
| 60 |
* @return |
| 61 |
*/ |
| 62 |
char *filelist_get(filelist_t *list) |
| 63 |
{ |
| 64 |
if (list == NULL || list->count == 0) { |
| 65 |
return NULL; |
| 66 |
} |
| 67 |
|
| 68 |
if (list->ptr == list->count) { |
| 69 |
list->ptr = 0; |
| 70 |
return NULL; |
| 71 |
} |
| 72 |
|
| 73 |
return list->files[list->ptr++]; |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Initializes a new filelist. |
| 78 |
* |
| 79 |
* @return |
| 80 |
*/ |
| 81 |
filelist_t *filelist_init(void) |
| 82 |
{ |
| 83 |
filelist_t *list; |
| 84 |
|
| 85 |
list = wrapped_malloc(sizeof(filelist_t)); |
| 86 |
memset(list, 0, sizeof(filelist_t)); |
| 87 |
|
| 88 |
list->max = FILELIST_CHUNK; |
| 89 |
list->files = wrapped_malloc(sizeof(char *) * list->max); |
| 90 |
|
| 91 |
return list; |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Adds a file to a file list. |
| 96 |
* |
| 97 |
* @param list |
| 98 |
* @param file |
| 99 |
*/ |
| 100 |
void filelist_put(filelist_t *list, const char *file) |
| 101 |
{ |
| 102 |
if (list == NULL) { |
| 103 |
fprintf(stderr, "detox: could not write to a null filelist\n"); |
| 104 |
exit(EXIT_FAILURE); |
| 105 |
} |
| 106 |
|
| 107 |
if (list->count == list->max) { |
| 108 |
char **new_files; |
| 109 |
int new_max; |
| 110 |
size_t new_size; |
| 111 |
int i; |
| 112 |
|
| 113 |
new_max = list->max + FILELIST_CHUNK; |
| 114 |
new_size = sizeof(char *) * new_max; |
| 115 |
new_files = wrapped_malloc(new_size); |
| 116 |
memset(new_files, 0, new_size); |
| 117 |
|
| 118 |
for (i = 0; i < list->count; i++) { |
| 119 |
new_files[i] = list->files[i]; |
| 120 |
} |
| 121 |
|
| 122 |
free(list->files); |
| 123 |
|
| 124 |
list->files = new_files; |
| 125 |
list->max = new_max; |
| 126 |
} |
| 127 |
|
| 128 |
list->files[list->count++] = wrapped_strdup(file); |
| 129 |
filelist_reset(list); |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Resets the internal pointer of a file list. |
| 134 |
* |
| 135 |
* @param list |
| 136 |
*/ |
| 137 |
inline void filelist_reset(filelist_t *list) |
| 138 |
{ |
| 139 |
if (list == NULL) { |
| 140 |
return; |
| 141 |
} |
| 142 |
|
| 143 |
list->ptr = 0; |
| 144 |
} |