Encapsulate token API.

This commit is contained in:
Jan Vidar Krey 2010-07-18 19:43:11 +02:00
parent 56e5557146
commit d41d649353
3 changed files with 52 additions and 39 deletions

View File

@ -1,7 +1,8 @@
#include <uhub.h> #include <uhub.h>
#define SETUP(X, STR) struct linked_list* tokens = cfg_tokenize(STR) #define SETUP(X, STR) struct cfg_tokens* tokens = cfg_tokenize(STR)
#define CLEANUP(X) do { list_clear(X, hub_free); list_destroy(X); } while(0) #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) 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) { static int count(const char* STR, size_t EXPECT) {
SETUP(tokens, STR); SETUP(tokens, STR);
int pass = list_size(tokens) == EXPECT; int pass = cfg_token_count(tokens) == EXPECT;
CLEANUP(tokens); CLEANUP_TOKENS(tokens);
return pass; return pass;
} }
@ -31,11 +32,11 @@ static int compare(const char* str, const char* ref) {
struct linked_list* compare = list_create(); struct linked_list* compare = list_create();
SETUP(tokens, str); SETUP(tokens, str);
split_string(ref, " ", compare, 0); split_string(ref, " ", compare, 0);
int pass = list_size(tokens) == list_size(compare); int pass = cfg_token_count(tokens) == list_size(compare);
if (pass) { if (pass) {
max = list_size(tokens); max = cfg_token_count(tokens);
for (i = 0; i < max; i++) { 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); char* refer = (char*) list_get_index(compare, i);
if (match_str(token, refer)) { if (match_str(token, refer)) {
pass = 0; pass = 0;
@ -43,8 +44,8 @@ static int compare(const char* str, const char* ref) {
} }
} }
} }
CLEANUP(tokens); CLEANUP_TOKENS(tokens);
CLEANUP(compare); CLEANUP_LIST(compare);
return pass; return pass;
} }

View File

@ -20,21 +20,17 @@
#include "uhub.h" #include "uhub.h"
#define ADD_CHAR(X) do { *out = X; out++; token_size++; } while(0) #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) struct linked_list* list;
{ };
list_append(list, hub_strdup(token));
return 1;
}
return 0;
}
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* buffer = hub_malloc_zero(strlen(line));
char* out = buffer; char* out = buffer;
const char* p = line; const char* p = line;
@ -122,27 +118,39 @@ struct linked_list* cfg_tokenize(const char* line)
return tokens; return tokens;
} }
void cfg_tokens_free(struct linked_list* list) void cfg_tokens_free(struct cfg_tokens* tokens)
{ {
list_clear(list, hub_free); list_clear(tokens->list, hub_free);
list_destroy(list); list_destroy(tokens->list);
hub_free(tokens);
} }
/* int cfg_token_add(struct cfg_tokens* tokens, char* new_token)
size_t cfg_token_count(const char* line)
{ {
if (!line || !*line) if (*new_token)
{
list_append(tokens->list, hub_strdup(new_token));
return 1;
}
return 0; 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);
} }
*/

View File

@ -20,14 +20,18 @@
#ifndef HAVE_UHUB_CONFIG_TOKEN_H #ifndef HAVE_UHUB_CONFIG_TOKEN_H
#define 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); struct cfg_tokens* cfg_tokenize(const char* line);
void cfg_tokens_free(struct linked_list*); void cfg_tokens_free(struct cfg_tokens*);
size_t cfg_token_count(const char* line); int cfg_token_add(struct cfg_tokens*, char* new_token);
char* cfg_token_get(const char* line, size_t token);
char* cfg_token_add(const char* line, 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 */ #endif /* HAVE_UHUB_CONFIG_TOKEN_H */