Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
andy5995 committed Feb 23, 2024
1 parent 00e08a3 commit f4cdc13
Show file tree
Hide file tree
Showing 8 changed files with 156 additions and 141 deletions.
236 changes: 120 additions & 116 deletions canfigger.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,26 +35,25 @@ cleanup_1(char **line, FILE **fp)
free(*line);

if (fclose(*fp) != 0)
perror("canfigger:");
perror("canfigger->fclose:");

return;
}


void
canfigger_free_attr(st_canfigger_attr_node **node)
canfigger_free_attr(st_canfigger_attr_node **node, st_canfigger_node **key)
{
if (*node)
{
while (*node)
canfigger_get_next_attr(node);
}
while (*node)
canfigger_get_next_attr(node, key);

return;
}


void
canfigger_get_next_attr(st_canfigger_attr_node **attr_node)
canfigger_get_next_attr(st_canfigger_attr_node **attr_node,
st_canfigger_node **key)
{
if (*attr_node)
{
Expand All @@ -64,24 +63,23 @@ canfigger_get_next_attr(st_canfigger_attr_node **attr_node)
(*attr_node)->str = NULL;
}

st_canfigger_attr_node *temp_node = *attr_node;
*attr_node = (*attr_node)->next;
free(temp_node);
temp_node = NULL;
st_canfigger_attr_node *temp_node = (*attr_node)->next;
free(*attr_node);
*attr_node = temp_node;

return;
// Replace the pointer to the head attr_node
(*key)->attr_node = *attr_node;
}

return;
}

void
canfigger_get_next_key(st_canfigger_list **list)
canfigger_get_next_key(st_canfigger_list **node)
{
st_canfigger_node **node = list;
if (*node)
{
if ((*node)->attr_node)
canfigger_free_attr(&(*node)->attr_node);
canfigger_free_attr(&(*node)->attr_node, node);

if ((*node)->value)
{
Expand All @@ -92,13 +90,11 @@ canfigger_get_next_key(st_canfigger_list **list)
free((*node)->key);
(*node)->key = NULL;

st_canfigger_node *temp_node = *node;
*node = (*node)->next;
free(temp_node);
temp_node = NULL;

return;
st_canfigger_node *temp_node = (*node)->next;
free(*node);
*node = temp_node;
}

return;
}

Expand Down Expand Up @@ -177,7 +173,6 @@ trim_whitespace(char *str)
static char *
grab_str_segment(char *a, char **dest, const int c)
{
free(*dest);
a = erase_lead_char(' ', a);

char *b = strchr(a, c);
Expand All @@ -200,13 +195,55 @@ grab_str_segment(char *a, char **dest, const int c)
return b + 1;
}

static int
add_attr_node(st_canfigger_attr_node **root,
st_canfigger_attr_node **cur_node)
{
st_canfigger_attr_node *tmp_node =
malloc(sizeof(struct st_canfigger_attr_node));
if (!tmp_node)
{
perror("canfigger->malloc:");
return -1;
}

if (*root)
(*cur_node)->next = tmp_node;
else
*root = tmp_node;

*cur_node = tmp_node;

return 0;
}


static int
add_key_node(st_canfigger_node **root, st_canfigger_node **cur_node)
{
st_canfigger_node *tmp_node = malloc(sizeof(struct st_canfigger_node));
if (!tmp_node)
{
perror("canfigger->malloc:");
return -1;
}

if (*root)
(*cur_node)->next = tmp_node;
else
*root = tmp_node;

*cur_node = tmp_node;

return 0;
}


st_canfigger_list *
canfigger_parse_file(const char *file, const int delimiter)
{
err_strdup = 0;
st_canfigger_node *root = NULL;
st_canfigger_list *list = NULL;
st_canfigger_node *root = NULL, *cur_node = NULL;

FILE *fp = fopen(file, "r");
if (!fp)
Expand All @@ -222,8 +259,6 @@ canfigger_parse_file(const char *file, const int delimiter)
// getline() malloc's the memory needed for line
while ((read = getline(&line, &len, fp)) != -1)
{
static const char *empty_str = "";

if (!line)
{
fclose(fp);
Expand All @@ -240,112 +275,81 @@ canfigger_parse_file(const char *file, const int delimiter)
if (*a == '\0' || *a == '#')
continue;

st_canfigger_node *tmp_node = malloc(sizeof(struct st_canfigger_node));
if (tmp_node)
int r = add_key_node(&root, &cur_node);
if (r == -1)
{
if (list)
list->next = tmp_node;
else
root = tmp_node;
canfigger_free(&root);
cleanup_1(&line, &fp);
return NULL;
}

st_canfigger_attr_node *attr_root = NULL;
st_canfigger_attr_node *attr_list = NULL;
// Get key
cur_node->key = NULL;
char *b = grab_str_segment(a, &cur_node->key, '=');

tmp_node->key = strdup(empty_str);
if (!tmp_node->key)
{
cleanup_1(&line, &fp);
return NULL;
}
if (err_strdup)
{
cleanup_1(&line, &fp);
return NULL;
}

char *b = grab_str_segment(a, &tmp_node->key, '=');
// Get value
cur_node->value = NULL;

if (err_strdup)
if (b)
{
a = b;
b = grab_str_segment(a, &cur_node->value, delimiter);
}

if (err_strdup)
{
cleanup_1(&line, &fp);
return NULL;
}

// Get attributes
cur_node->attr_node = NULL;
st_canfigger_attr_node *attr_root = NULL, *cur_attr_node = NULL;

while (b)
{
r = add_attr_node(&attr_root, &cur_attr_node);
if (r == -1)
{
canfigger_free_attr(&attr_root, &cur_node);
canfigger_free(&root);
cleanup_1(&line, &fp);
return NULL;
}

// fprintf(stderr, "key: '%s'\n", tmp_node->key);
cur_attr_node->str = NULL;
a = b;
b = grab_str_segment(a, &cur_attr_node->str, delimiter);
if (strlen(cur_attr_node->str) == 0)
fputs("error\n", stderr);

if (cur_attr_node->str == NULL)
fputs("error\n", stderr);

tmp_node->value = strdup(empty_str);
if (!tmp_node->value)
if (err_strdup)
{
cleanup_1(&line, &fp);
return NULL;
}

if (b)
{
a = b;
b = grab_str_segment(a, &tmp_node->value, delimiter);

if (err_strdup)
{
cleanup_1(&line, &fp);
return NULL;
}
}
do
{
st_canfigger_attr_node *cur_attr_node =
malloc(sizeof(struct st_canfigger_attr_node));
if (cur_attr_node == NULL)
{
if (attr_root)
canfigger_free_attr(&attr_root);

if (root)
canfigger_free(&root);

cleanup_1(&line, &fp);
return NULL;
}

if (attr_list)
attr_list->next = cur_attr_node;
else
attr_root = cur_attr_node;

cur_attr_node->str = strdup(empty_str);
if (!cur_attr_node->str)
{
cleanup_1(&line, &fp);
return NULL;
}
if (b)
{
a = b;
b = grab_str_segment(a, &cur_attr_node->str, delimiter);

if (err_strdup)
{
cleanup_1(&line, &fp);
return NULL;
}
}
attr_list = cur_attr_node;
cur_attr_node->next = NULL;
}
while (b);

tmp_node->attr_node = attr_root;
tmp_node->next = NULL;

list = tmp_node;
cur_attr_node->next = NULL;
}
else
{
if (root)
canfigger_free(&root);

cleanup_1(&line, &fp);
return NULL;
}
cur_node->attr_node = attr_root;
cur_node->next = NULL;
}

cleanup_1(&line, &fp);
if (!root)
{
cleanup_1(&line, &fp);
return NULL;
}

list = root;
return list;
cleanup_1(&line, &fp);
return root;
}
6 changes: 4 additions & 2 deletions canfigger.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ st_canfigger_list *canfigger_parse_file(const char *file,
*
* @param attr_node A pointer to the current attribute node pointer.
*/
void canfigger_get_next_attr(st_canfigger_attr_node ** attr_node);
void canfigger_get_next_attr(st_canfigger_attr_node ** attr_node,
st_canfigger_node ** key);

/**
* @brief Advances to the next configuration node, freeing the current one.
Expand Down Expand Up @@ -125,4 +126,5 @@ void canfigger_free(st_canfigger_node ** node);
*
* @param node A pointer to the pointer of the root attribute node of the list.
*/
void canfigger_free_attr(st_canfigger_attr_node ** node);
void canfigger_free_attr(st_canfigger_attr_node ** node,
st_canfigger_node ** key);
7 changes: 5 additions & 2 deletions example-01.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,21 @@ main(void)
st_canfigger_list *config = canfigger_parse_file("../example-01.conf", ',');
while (config != NULL)
{
printf("Key: %s, Value: %s\n", config->key, config->value);
printf("Key: %s, Value: %s\n", config->key,
config->value != NULL ? config->value : "NULL");

// Process attributes if necessary
st_canfigger_attr_node *attr = config->attr_node;

while (attr)
{
printf("Attribute: %s\n", attr->str);
canfigger_get_next_attr(&attr);
canfigger_get_next_attr(&attr, &config);
}

// Move to the next node and automatically free the current node
canfigger_get_next_key(&config);
putchar('\n');
}

return 0;
Expand Down
5 changes: 1 addition & 4 deletions tests/test_canfigger_colons.conf
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
foo=bar
foo=bar:high
blue=color:shiny

# spaces adjacent to the '=' or the attribute delimiter ',' will be ignored,
# and tabs will be removed
statement = hello world : obvious

# An option with no value or attributes
FeatureFooEnabled
2 changes: 1 addition & 1 deletion tests/test_multiple_attributes.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ Attribute: %s\n", list->key, list->value, list->attr_node->str);
}
j++;

canfigger_get_next_attr(&list->attr_node);
canfigger_get_next_attr(&list->attr_node, &list);
}

fprintf(stderr, "j: %d\n", j);
Expand Down
Loading

0 comments on commit f4cdc13

Please sign in to comment.