From d41d649353b1d90608d770e9c801eb99edf5e39c Mon Sep 17 00:00:00 2001 From: Jan Vidar Krey Date: Sun, 18 Jul 2010 19:43:11 +0200 Subject: [PATCH] Encapsulate token API. --- autotest/test_tokenizer.tcc | 19 +++++++------ src/util/config_token.c | 56 +++++++++++++++++++++---------------- src/util/config_token.h | 16 +++++++---- 3 files changed, 52 insertions(+), 39 deletions(-) diff --git a/autotest/test_tokenizer.tcc b/autotest/test_tokenizer.tcc index eae51ee..34495b0 100644 --- a/autotest/test_tokenizer.tcc +++ b/autotest/test_tokenizer.tcc @@ -1,7 +1,8 @@ #include -#define SETUP(X, STR) struct linked_list* tokens = cfg_tokenize(STR) -#define CLEANUP(X) do { list_clear(X, hub_free); list_destroy(X); } while(0) +#define SETUP(X, STR) struct cfg_tokens* tokens = cfg_tokenize(STR) +#define CLEANUP_LIST(X) do { list_clear(X, hub_free); list_destroy(X); } while(0) +#define CLEANUP_TOKENS(X) do { cfg_tokens_free(X); } while(0) static int match_str(const char* str1, char* str2) { @@ -21,8 +22,8 @@ static int match_str(const char* str1, char* str2) static int count(const char* STR, size_t EXPECT) { SETUP(tokens, STR); - int pass = list_size(tokens) == EXPECT; - CLEANUP(tokens); + int pass = cfg_token_count(tokens) == EXPECT; + CLEANUP_TOKENS(tokens); return pass; } @@ -31,11 +32,11 @@ static int compare(const char* str, const char* ref) { struct linked_list* compare = list_create(); SETUP(tokens, str); split_string(ref, " ", compare, 0); - int pass = list_size(tokens) == list_size(compare); + int pass = cfg_token_count(tokens) == list_size(compare); if (pass) { - max = list_size(tokens); + max = cfg_token_count(tokens); for (i = 0; i < max; i++) { - char* token = (char*) list_get_index(tokens, i); + char* token = (char*) cfg_token_get(tokens, i); char* refer = (char*) list_get_index(compare, i); if (match_str(token, refer)) { pass = 0; @@ -43,8 +44,8 @@ static int compare(const char* str, const char* ref) { } } } - CLEANUP(tokens); - CLEANUP(compare); + CLEANUP_TOKENS(tokens); + CLEANUP_LIST(compare); return pass; } diff --git a/src/util/config_token.c b/src/util/config_token.c index f77c7b1..e647ef8 100644 --- a/src/util/config_token.c +++ b/src/util/config_token.c @@ -20,21 +20,17 @@ #include "uhub.h" #define ADD_CHAR(X) do { *out = X; out++; token_size++; } while(0) -#define RESET_TOKEN do { ADD_CHAR('\0'); out = buffer; if (add_token(tokens, out)) token_count++; token_size = 0; buffer[0] = '\0'; } while (0) +#define RESET_TOKEN do { ADD_CHAR('\0'); out = buffer; if (cfg_token_add(tokens, out)) token_count++; token_size = 0; buffer[0] = '\0'; } while (0) -static int add_token(struct linked_list* list, const char* token) +struct cfg_tokens { - if (*token) - { - list_append(list, hub_strdup(token)); - return 1; - } - return 0; -} + struct linked_list* list; +}; -struct linked_list* cfg_tokenize(const char* line) +struct cfg_tokens* cfg_tokenize(const char* line) { - struct linked_list* tokens = list_create(); + struct cfg_tokens* tokens = hub_malloc_zero(sizeof(struct cfg_tokens)); + tokens->list = list_create(); char* buffer = hub_malloc_zero(strlen(line)); char* out = buffer; const char* p = line; @@ -122,27 +118,39 @@ struct linked_list* cfg_tokenize(const char* line) return tokens; } -void cfg_tokens_free(struct linked_list* list) +void cfg_tokens_free(struct cfg_tokens* tokens) { - list_clear(list, hub_free); - list_destroy(list); + list_clear(tokens->list, hub_free); + list_destroy(tokens->list); + hub_free(tokens); } -/* -size_t cfg_token_count(const char* line) +int cfg_token_add(struct cfg_tokens* tokens, char* new_token) { - if (!line || !*line) - return 0; - - - + if (*new_token) + { + list_append(tokens->list, hub_strdup(new_token)); + return 1; + } + return 0; } -char* cfg_token_get(const char* line, size_t token) +size_t cfg_token_count(struct cfg_tokens* tokens) { + return list_size(tokens->list); } -char* cfg_token_add(const char* line, char* new_token) +char* cfg_token_get(struct cfg_tokens* tokens, size_t offset) { + return list_get_index(tokens->list, offset); +} + +char* cfg_token_get_first(struct cfg_tokens* tokens) +{ + return list_get_first(tokens->list); +} + +char* cfg_token_get_next(struct cfg_tokens* tokens) +{ + return list_get_next(tokens->list); } -*/ diff --git a/src/util/config_token.h b/src/util/config_token.h index 773c392..f8ad081 100644 --- a/src/util/config_token.h +++ b/src/util/config_token.h @@ -20,14 +20,18 @@ #ifndef HAVE_UHUB_CONFIG_TOKEN_H #define HAVE_UHUB_CONFIG_TOKEN_H -struct linked_list; +struct cfg_tokens; -struct linked_list* cfg_tokenize(const char* line); -void cfg_tokens_free(struct linked_list*); +struct cfg_tokens* cfg_tokenize(const char* line); +void cfg_tokens_free(struct cfg_tokens*); -size_t cfg_token_count(const char* line); -char* cfg_token_get(const char* line, size_t token); -char* cfg_token_add(const char* line, char* new_token); +int cfg_token_add(struct cfg_tokens*, char* new_token); + +size_t cfg_token_count(struct cfg_tokens*); + +char* cfg_token_get(struct cfg_tokens*, size_t offset); +char* cfg_token_get_first(struct cfg_tokens*); +char* cfg_token_get_next(struct cfg_tokens*); #endif /* HAVE_UHUB_CONFIG_TOKEN_H */