diff --git a/canfigger.c b/canfigger.c index e7a4037..dcdbe98 100644 --- a/canfigger.c +++ b/canfigger.c @@ -30,6 +30,7 @@ static int canfigger_delimiter = 0; char *canfigger_attr = NULL; static char *grab_str_segment(char *a, char **dest, const int c); +static void canfigger_free(struct Canfigger **node); struct line { @@ -49,7 +50,7 @@ cleanup_1(char **buf) void -canfigger_get_next_attr(struct attributes *attributes) +canfigger_free_current_attr_str_advance(struct attributes *attributes) { if (!attributes) { @@ -77,15 +78,21 @@ canfigger_get_next_attr(struct attributes *attributes) return; } -void -canfigger_init_attrs(struct attributes *attributes) + +// Clearly a wrapper function... +static void +init_first_attr(struct attributes *attributes) { - canfigger_get_next_attr(attributes); + canfigger_free_current_attr_str_advance(attributes); return; } +// that probably exists to help with code clarity. It will copy the first +// attribute in a 'attr1, attr2, attr3,...' string to attributes->current and +// point 'canfigger_attr' to that address. + void -canfigger_get_next_key(st_canfigger_list **node) +canfigger_free_current_key_node_advance(struct Canfigger **node) { if (*node) { @@ -116,22 +123,24 @@ canfigger_get_next_key(st_canfigger_list **node) free((*node)->key); (*node)->key = NULL; - st_canfigger_node *temp_node = (*node)->next; + struct Canfigger *temp_node = (*node)->next; free(*node); *node = temp_node; + if (*node) + init_first_attr((*node)->attributes); } return; } -void -canfigger_free(st_canfigger_node **node) +static void +canfigger_free(struct Canfigger **node) { if (*node) { while (*node) - canfigger_get_next_key(node); + canfigger_free_current_key_node_advance(node); } return; @@ -223,9 +232,9 @@ grab_str_segment(char *a, char **dest, const int c) static int -add_key_node(st_canfigger_node **root, st_canfigger_node **cur_node) +add_key_node(struct Canfigger **root, struct Canfigger **cur_node) { - st_canfigger_node *tmp_node = malloc(sizeof(struct st_canfigger_node)); + struct Canfigger *tmp_node = malloc(sizeof(struct Canfigger)); if (!tmp_node) { perror("canfigger->malloc:"); @@ -285,12 +294,12 @@ read_entire_file(const char *filename) } -st_canfigger_list * +struct Canfigger * canfigger_parse_file(const char *file, const int delimiter) { err_strdup = 0; - canfigger_delimiter = delimiter; - st_canfigger_node *root = NULL, *cur_node = NULL; + + struct Canfigger *root = NULL, *cur_node = NULL; struct line line; char *file_contents = read_entire_file(file); @@ -383,5 +392,8 @@ canfigger_parse_file(const char *file, const int delimiter) cleanup_1(&file_contents); free(line.line); + + canfigger_delimiter = delimiter; + init_first_attr(root->attributes); return root; } diff --git a/canfigger.h b/canfigger.h index 60975bd..c8a4f12 100644 --- a/canfigger.h +++ b/canfigger.h @@ -41,27 +41,21 @@ struct attributes char *ptr; }; -typedef struct st_canfigger_node +struct Canfigger { char *key; ///< Key string (left of '='). char *value; struct attributes *attributes; - struct st_canfigger_node *next; ///< Pointer to the next configuration node. -} st_canfigger_node; - -typedef st_canfigger_node st_canfigger_list; + struct Canfigger *next; ///< Pointer to the next configuration node. +}; -st_canfigger_list *canfigger_parse_file(const char *file, +struct Canfigger *canfigger_parse_file(const char *file, const int delimiter); /** * \example example-01.c */ -void canfigger_get_next_key(st_canfigger_list ** list); - -void canfigger_get_next_attr(struct attributes *attributes); - -void canfigger_init_attrs(struct attributes *attributes); +void canfigger_free_current_key_node_advance(struct Canfigger ** list); -void canfigger_free(st_canfigger_node ** node); +void canfigger_free_current_attr_str_advance(struct attributes *attributes); diff --git a/example-01.c b/example-01.c index b16a1d8..4b12ca7 100644 --- a/example-01.c +++ b/example-01.c @@ -5,22 +5,21 @@ int main(void) { - st_canfigger_list *config = canfigger_parse_file("../example-01.conf", ','); + struct Canfigger *config = canfigger_parse_file("../example-01.conf", ','); while (config != NULL) { printf("Key: %s, Value: %s\n", config->key, config->value != NULL ? config->value : "NULL"); // Process attributes if necessary - canfigger_init_attrs(config->attributes); while (canfigger_attr) { printf("Attribute: %s\n", canfigger_attr); - canfigger_get_next_attr(config->attributes); + canfigger_free_current_attr_str_advance(config->attributes); } // Move to the next node and automatically free the current node - canfigger_get_next_key(&config); + canfigger_free_current_key_node_advance(&config); putchar('\n'); } diff --git a/tests/test_multiple_attributes.c b/tests/test_multiple_attributes.c index 4bafe20..e1a6ff9 100644 --- a/tests/test_multiple_attributes.c +++ b/tests/test_multiple_attributes.c @@ -23,7 +23,7 @@ main(void) SOURCE_DIR) < sizeof test_config_file); // call the primary library function to read your config file - st_canfigger_list *list = canfigger_parse_file(test_config_file, ','); + struct Canfigger *list = canfigger_parse_file(test_config_file, ','); if (list == NULL) { fprintf(stderr, "Error"); @@ -42,7 +42,6 @@ Attribute: %s\n", list->key, list->value, list->attributes->current); // assert (strcmp (data[i].value, list->value) == 0); int j = 0; - canfigger_init_attrs(list->attributes); while (canfigger_attr) { fprintf(stderr, "attr: %s\n", list->attributes->current); @@ -60,7 +59,7 @@ Attribute: %s\n", list->key, list->value, list->attributes->current); } j++; - canfigger_get_next_attr(list->attributes); + canfigger_free_current_attr_str_advance(list->attributes); } fprintf(stderr, "j: %d\n", j); @@ -82,7 +81,7 @@ Attribute: %s\n", list->key, list->value, list->attributes->current); i++; - canfigger_get_next_key(&list); + canfigger_free_current_key_node_advance(&list); } assert(i == 3); diff --git a/tests/test_parse_file.c b/tests/test_parse_file.c index 3c05899..993507d 100644 --- a/tests/test_parse_file.c +++ b/tests/test_parse_file.c @@ -24,7 +24,7 @@ main(void) SOURCE_DIR) < sizeof test_config_file); // call the primary library function to read your config file - st_canfigger_list *list = canfigger_parse_file(test_config_file, ','); + struct Canfigger *list = canfigger_parse_file(test_config_file, ','); if (list == NULL) { @@ -35,7 +35,6 @@ main(void) int i = 0; while (list) { - canfigger_init_attrs(list->attributes); fprintf(stderr, "\n\ Key: %s | Expected: %s\n\ Value: %s | Expected: %s\n\ @@ -52,17 +51,12 @@ Attribute: %s | Expected: %s\n", list->key, data[i].key, list->value != NULL ? l list->attributes != NULL ? list->attributes->current : "NULL") == 0); i++; - canfigger_get_next_key(&list); + canfigger_free_current_key_node_advance(&list); } // 'list' should be NULL, not a dangling pointer assert(list == NULL); - // This should not cause a crash. All formerly freed pointers - // should be set to NULL, and the free function will return early - // if value is NULL. - canfigger_free(&list); - assert(i == ARRAY_SIZE(data)); return 0; diff --git a/tests/test_parse_file2.c b/tests/test_parse_file2.c index 93e6b72..ea54cf1 100644 --- a/tests/test_parse_file2.c +++ b/tests/test_parse_file2.c @@ -9,7 +9,7 @@ main(void) "%s/no_exist_test_canfigger.conf", SOURCE_DIR) < sizeof test_config_file); - st_canfigger_list *list = canfigger_parse_file(test_config_file, ','); + struct Canfigger *list = canfigger_parse_file(test_config_file, ','); assert(list == NULL); assert(errno); perror(__func__); diff --git a/tests/test_parse_file_colons.c b/tests/test_parse_file_colons.c index 1e6bdd2..5b480de 100644 --- a/tests/test_parse_file_colons.c +++ b/tests/test_parse_file_colons.c @@ -16,7 +16,7 @@ main(void) char test_config_file[PATH_MAX]; sprintf(test_config_file, "%s/test_canfigger_colons.conf", SOURCE_DIR); - st_canfigger_list *list = canfigger_parse_file(test_config_file, ':'); + struct Canfigger *list = canfigger_parse_file(test_config_file, ':'); if (list == NULL) { fprintf(stderr, "Error"); @@ -26,7 +26,6 @@ main(void) int i = 0; while (list != NULL) { - canfigger_init_attrs(list->attributes); printf("\n\ Key: %s\n\ Value: %s\n\ @@ -39,7 +38,7 @@ Attribute: %s\n", list->key, list->value, list->attributes->current); assert(strcmp(data[i].attribute, list->attributes->current) == 0); i++; - canfigger_get_next_key(&list); + canfigger_free_current_key_node_advance(&list); } assert(i == sizeof data / sizeof data[0]); diff --git a/tests/test_unicode.c b/tests/test_unicode.c index 0dd61e8..c8abd8f 100644 --- a/tests/test_unicode.c +++ b/tests/test_unicode.c @@ -23,7 +23,7 @@ main(void) SOURCE_DIR) < sizeof test_config_file); // call the primary library function to read your config file - st_canfigger_list *list = canfigger_parse_file(test_config_file, ';'); + struct Canfigger *list = canfigger_parse_file(test_config_file, ';'); if (list == NULL) { fprintf(stderr, "Error"); @@ -33,7 +33,6 @@ main(void) int i = 0; while (list) { - canfigger_init_attrs(list->attributes); printf("\n\ Key: %s\n\ Value: %s\n\ @@ -50,7 +49,7 @@ Attribute: %s\n", list->key, list->value != NULL ? list->value : "NULL", list->a list->attributes != NULL ? list->attributes->current : "NULL") == 0); i++; - canfigger_get_next_key(&list); + canfigger_free_current_key_node_advance(&list); } return 0;