Add a stringlist expand function.

This commit is contained in:
Andreas Schneider 2008-03-20 10:34:58 +01:00
parent 1dc975ad9d
commit b4811c36bc
5 changed files with 63 additions and 2 deletions

View file

@ -135,6 +135,8 @@ int csync_init(CSYNC *ctx) {
goto out;
}
/* TODO: load plugins */
ctx->initialized = 1;
rc = 0;

View file

@ -17,7 +17,7 @@
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* vim: ts=2 sw=2 et cindent
* vim: ft=c.doxygen ts=2 sw=2 et cindent
*/
/**
@ -66,6 +66,7 @@ enum csync_replica_e {
struct csync_s {
c_rbtree_t *local;
c_rbtree_t *remote;
c_strlist_t *excludes;
sqlite3 *journal;
struct {

View file

@ -20,6 +20,7 @@
* vim: ts=2 sw=2 et cindent
*/
#include <errno.h>
#include <stdlib.h>
#include "c_string.h"
@ -47,6 +48,7 @@ c_strlist_t *c_strlist_new(size_t size) {
c_strlist_t *strlist = NULL;
if (size == 0) {
errno = EINVAL;
return NULL;
}
@ -55,7 +57,7 @@ c_strlist_t *c_strlist_new(size_t size) {
return NULL;
}
strlist->vector = c_malloc(size * sizeof(char *));
strlist->vector = (char **) c_malloc(size * sizeof(char *));
if (strlist->vector == NULL) {
SAFE_FREE(strlist);
return NULL;
@ -66,6 +68,26 @@ c_strlist_t *c_strlist_new(size_t 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, char *string) {
if (strlist == NULL || string == NULL) {
return -1;

View file

@ -74,6 +74,16 @@ int c_streq(const char *a, const char *b);
*/
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 occured.
*/
c_strlist_t *c_strlist_expand(c_strlist_t *strlist, size_t size);
/**
* @brief Add a string to the stringlist.
*

View file

@ -74,11 +74,34 @@ START_TEST (check_c_strlist_add)
}
END_TEST
START_TEST (check_c_strlist_expand)
{
size_t i = 0;
c_strlist_t *strlist = NULL;
strlist = c_strlist_new(42);
fail_if(strlist == NULL, NULL);
fail_unless(strlist->size == 42, NULL);
fail_unless(strlist->count == 0, NULL);
strlist = c_strlist_expand(strlist, 84);
fail_if(strlist == NULL, NULL);
fail_unless(strlist->size == 84, NULL);
for (i = 0; i < strlist->size; i++) {
fail_unless(c_strlist_add(strlist, (char *) "foobar") == 0, NULL);
}
c_strlist_destroy(strlist);
}
END_TEST
static Suite *make_std_c_strlist_suite(void) {
Suite *s = suite_create("std:str:c_stringlist");
create_case(s, "check_c_strlist_new", check_c_strlist_new);
create_case(s, "check_c_strlist_add", check_c_strlist_add);
create_case(s, "check_c_strlist_expand", check_c_strlist_expand);
return s;
}
@ -91,6 +114,9 @@ int main(void) {
SRunner *sr;
sr = srunner_create(s);
#if 0
srunner_set_fork_status(sr, CK_NOFORK);
#endif
srunner_add_suite (sr, s2);
srunner_run_all(sr, CK_VERBOSE);
nf = srunner_ntests_failed(sr);