Skip to content

Commit

Permalink
Rename functions, structs, remove typedef
Browse files Browse the repository at this point in the history
  • Loading branch information
andy5995 committed Feb 25, 2024
1 parent 126f9ae commit 3ae54fb
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 49 deletions.
40 changes: 26 additions & 14 deletions canfigger.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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)
{
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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:");
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
18 changes: 6 additions & 12 deletions canfigger.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
7 changes: 3 additions & 4 deletions example-01.c
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

Expand Down
7 changes: 3 additions & 4 deletions tests/test_multiple_attributes.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down
10 changes: 2 additions & 8 deletions tests/test_parse_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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\
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion tests/test_parse_file2.c
Original file line number Diff line number Diff line change
Expand Up @@ -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__);
Expand Down
5 changes: 2 additions & 3 deletions tests/test_parse_file_colons.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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\
Expand All @@ -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]);
Expand Down
5 changes: 2 additions & 3 deletions tests/test_unicode.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -33,7 +33,6 @@ main(void)
int i = 0;
while (list)
{
canfigger_init_attrs(list->attributes);
printf("\n\
Key: %s\n\
Value: %s\n\
Expand All @@ -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;
Expand Down

0 comments on commit 3ae54fb

Please sign in to comment.