This commit is contained in:
Olivier Goffart 2017-12-14 18:57:12 +01:00 коммит произвёл Dominik Schmidt
Родитель 3ae327ea8e
Коммит 437d45981e
3 изменённых файлов: 0 добавлений и 254 удалений

Просмотреть файл

@ -63,106 +63,3 @@ int c_streq(const char *a, const char *b) {
return 0;
}
c_strlist_t *c_strlist_new(size_t size) {
c_strlist_t *strlist = NULL;
if (size == 0) {
errno = EINVAL;
return NULL;
}
strlist = c_malloc(sizeof(c_strlist_t));
if (strlist == NULL) {
return NULL;
}
strlist->vector = (char **) c_malloc(size * sizeof(char *));
strlist->count = 0;
strlist->size = size;
return strlist;
}
c_strlist_t *c_strlist_expand(c_strlist_t *strlist, size_t size) {
if (strlist == NULL || size == 0) {
errno = EINVAL;
return NULL;
}
if (strlist->size >= size) {
return strlist;
}
strlist->vector = (char **) c_realloc(strlist->vector, size * sizeof(char *));
if (strlist->vector == NULL) {
return NULL;
}
strlist->size = size;
return strlist;
}
int c_strlist_add(c_strlist_t *strlist, const char *string) {
if (strlist == NULL || string == NULL) {
return -1;
}
if (strlist->count < strlist->size) {
strlist->vector[strlist->count] = c_strdup(string);
if (strlist->vector[strlist->count] == NULL) {
return -1;
}
strlist->count++;
} else {
errno = ENOBUFS;
return -1;
}
return 0;
}
int c_strlist_add_grow(c_strlist_t **strlist, const char *string) {
if (*strlist == NULL) {
*strlist = c_strlist_new(32);
if (*strlist == NULL) {
return -1;
}
}
if ((*strlist)->count == (*strlist)->size) {
c_strlist_t *list = c_strlist_expand(*strlist, 2 * (*strlist)->size);
if (list == NULL) {
return -1;
}
*strlist = list;
}
return c_strlist_add(*strlist, string);
}
void c_strlist_clear(c_strlist_t *strlist) {
size_t i = 0;
if (strlist == NULL) {
return;
}
for (i = 0; i < strlist->count; i++) {
SAFE_FREE(strlist->vector[i]);
}
strlist->count = 0;
}
void c_strlist_destroy(c_strlist_t *strlist) {
if (strlist == NULL) {
return;
}
c_strlist_clear(strlist);
SAFE_FREE(strlist->vector);
SAFE_FREE(strlist);
}

Просмотреть файл

@ -41,28 +41,6 @@ extern "C" {
#include <stdlib.h>
struct c_strlist_s; typedef struct c_strlist_s c_strlist_t;
/**
* @brief Structure for a stringlist
*
* Using a for loop you can access the strings saved in the vector.
*
* c_strlist_t strlist;
* int i;
* for (i = 0; i < strlist->count; i++) {
* printf("value: %s", strlist->vector[i];
* }
*/
struct c_strlist_s {
/** The string vector */
char **vector;
/** The count of the strings saved in the vector */
size_t count;
/** Size of strings allocated */
size_t size;
};
/**
* @brief Compare to strings case insensitively.
*
@ -84,68 +62,6 @@ int c_strncasecmp(const char *a, const char *b, size_t n);
*/
int c_streq(const char *a, const char *b);
/**
* @brief Create a new stringlist.
*
* @param size Size to allocate.
*
* @return Pointer to the newly allocated stringlist. NULL if an error occurred.
*/
c_strlist_t *c_strlist_new(size_t size);
/**
* @brief Expand the stringlist
*
* @param strlist Stringlist to expand
* @param size New size of the strlinglist to expand
*
* @return Pointer to the expanded stringlist. NULL if an error occurred.
*/
c_strlist_t *c_strlist_expand(c_strlist_t *strlist, size_t size);
/**
* @brief Add a string to the stringlist.
*
* Duplicates the string and stores it in the stringlist.
*
* @param strlist Stringlist to add the string.
* @param string String to add.
*
* @return 0 on success, less than 0 and errno set if an error occurred.
* ENOBUFS if the list is full.
*/
int c_strlist_add(c_strlist_t *strlist, const char *string);
/**
* @brief Add a string to the stringlist, growing it if necessary
*
* Duplicates the string and stores it in the stringlist.
* It also initializes the stringlist if it starts out as null.
*
* @param strlist Stringlist to add the string.
* @param string String to add.
*
* @return 0 on success, less than 0 and errno set if an error occurred.
*/
int c_strlist_add_grow(c_strlist_t **strlist, const char *string);
/**
* @brief Removes all strings from the list.
*
* Frees the strings.
*
* @param strlist Stringlist to clear
*/
void c_strlist_clear(c_strlist_t *strlist);
/**
* @brief Destroy the memory of the stringlist.
*
* Frees the strings and the stringlist.
*
* @param strlist Stringlist to destroy
*/
void c_strlist_destroy(c_strlist_t *strlist);
/**
* }@

Просмотреть файл

@ -49,70 +49,6 @@ static void check_c_streq_null(void **state)
assert_false(c_streq(NULL, NULL));
}
static void check_c_strlist_new(void **state)
{
c_strlist_t *strlist = NULL;
(void) state; /* unused */
strlist = c_strlist_new(42);
assert_non_null(strlist);
assert_int_equal(strlist->size, 42);
assert_int_equal(strlist->count, 0);
c_strlist_destroy(strlist);
}
static void check_c_strlist_add(void **state)
{
int rc;
size_t i = 0;
c_strlist_t *strlist = NULL;
(void) state; /* unused */
strlist = c_strlist_new(42);
assert_non_null(strlist);
assert_int_equal(strlist->size, 42);
assert_int_equal(strlist->count, 0);
for (i = 0; i < strlist->size; i++) {
rc = c_strlist_add(strlist, (char *) "foobar");
assert_int_equal(rc, 0);
}
assert_int_equal(strlist->count, 42);
assert_string_equal(strlist->vector[0], "foobar");
assert_string_equal(strlist->vector[41], "foobar");
c_strlist_destroy(strlist);
}
static void check_c_strlist_expand(void **state)
{
c_strlist_t *strlist;
size_t i = 0;
int rc;
(void) state; /* unused */
strlist = c_strlist_new(42);
assert_non_null(strlist);
assert_int_equal(strlist->size, 42);
assert_int_equal(strlist->count, 0);
strlist = c_strlist_expand(strlist, 84);
assert_non_null(strlist);
assert_int_equal(strlist->size, 84);
for (i = 0; i < strlist->size; i++) {
rc = c_strlist_add(strlist, (char *) "foobar");
assert_int_equal(rc, 0);
}
c_strlist_destroy(strlist);
}
int torture_run_tests(void)
@ -121,9 +57,6 @@ int torture_run_tests(void)
cmocka_unit_test(check_c_streq_equal),
cmocka_unit_test(check_c_streq_not_equal),
cmocka_unit_test(check_c_streq_null),
cmocka_unit_test(check_c_strlist_new),
cmocka_unit_test(check_c_strlist_add),
cmocka_unit_test(check_c_strlist_expand),
};
return cmocka_run_group_tests(tests, NULL, NULL);