Browse Source

+ filelist.[ch]

master
Alex 'AdUser' Z 7 years ago
commit
feb8747282
  1. 149
      src/filelist.c
  2. 57
      src/filelist.h

149
src/filelist.c

@ -0,0 +1,149 @@
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor Boston, MA 02110-1301, USA
*/
#include <assert.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "filelist.h"
#define FREE(x) free((x)), (x) = NULL;
static const int grow_step = 100;
static bool
filelist_grow(filelist_t *list, int size) {
char **p = NULL;
assert(list != NULL);
if (size <= 0)
return false;
if (size <= list->caps)
return false;
if ((p = realloc(list->files, size * sizeof(char *))) == NULL)
return false;
list->files = p,
list->caps = size;
return true;
}
filelist_t *
filelist_create(int size) {
filelist_t *tmp = NULL;
if ((tmp = calloc(1, sizeof(filelist_t))) == NULL)
return NULL;
if (size == 0)
return tmp;
if (!filelist_grow(tmp, size))
FREE(tmp);
return tmp;
}
const char *
filelist_get(filelist_t *list, int num) {
assert(list != NULL);
if (num < 1 || num > list->size)
return NULL;
return list->files[num - 1];
}
int
filelist_append(filelist_t *list, const char *path) {
assert(list != NULL);
if (list->size >= list->caps) {
int newcaps = list->caps ? list->caps * 2 : grow_step;
if (!filelist_grow(list, newcaps))
return 0;
}
if ((list->files[list->size] = strdup(path)) == NULL)
return 0;
list->size++;
return list->size;
}
bool
filelist_set(filelist_t *list, int num, const char *path) {
char *p = NULL;
assert(list != NULL);
if (num-- < 1)
return false;
if (num >= list->caps) {
if (!filelist_grow(list, num + grow_step))
return false;
}
if (path == NULL) {
FREE(list->files[num]);
return true;
}
if ((p = strdup(path)) == NULL)
return false;
FREE(list->files[num]);
list->files[num] = p;
return true;
}
void
filelist_del(filelist_t *list, int num) {
assert(list != NULL);
if (num-- < 1)
return;
if (num > list->size)
return;
if (list->files[num] == NULL)
return;
FREE(list->files[num]);
if ((num + 1) == list->size)
list->size--;
return;
}
void
filelist_free(filelist_t *list) {
assert(list != NULL);
for (int i = 0; i < list->size; i++) {
if (list->files[i] == NULL)
continue;
FREE(list->files[i]);
}
FREE(list->files);
list->size = 0;
list->caps = 0;
return;
}

57
src/filelist.h

@ -0,0 +1,57 @@
#ifndef FILELIST_H_
#define FILELIST_H_ 1
/** Filelist of potential images */
typedef struct {
int size; /**< files in list */
int caps; /**< allocated slots count */
char **files; /**< Path's array */
} filelist_t;
/**
* @brief Create new filelist
* @param size Preallocated list slots count
* @returns Pointer to created list or NULL on error
*/
filelist_t * filelist_create(int size);
/**
* @brief Get image path by id
* @param list Pointer to filelist
* @param num Image number
* @returns Pointer to image path or NULL if slot is empty
*/
const char * filelist_get (filelist_t *list, int num);
/**
* @brief Add new path to filelist
* @param list Pointer to filelist
* @param path Path to image
* @returns 0 on error or >0 on success as slot number
*/
int filelist_append (filelist_t *list, const char *path);
/**
* @brief Replace given slot with new path
* @param list Pointer to filelist
* @param num Image number
* @param path Path to image
* @returns true on success, false on error
*/
bool filelist_set (filelist_t *list, int num, const char *path);
/**
* @brief Remove path with given slot number
* @param list Pointer to filelist
* @param num Image number
*/
void filelist_del (filelist_t *list, int num);
/**
* @brief Remove all filelist members and free memory
* @param list Pointer to filelist
* @note memory for filelist_t struct not freed
*/
void filelist_free(filelist_t *list);
#endif /* FILELIST_H_ */
Loading…
Cancel
Save