Skip to content

Commit

Permalink
initialize strings with ""
Browse files Browse the repository at this point in the history
  • Loading branch information
andy5995 committed Feb 21, 2024
1 parent 6fdd2bd commit 290a106
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions canfigger.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,20 +115,19 @@ trim_whitespace(char *str)
static char *
grab_str_segment(char *a, char **dest, const int c)
{
free(*dest);
a = erase_lead_char(' ', a);
trim_whitespace(a);

char *b = strchr(a, c);
if (!b)
{
*dest = strdup(a);
fprintf(stderr, "dest: '%s'\n", *dest);
return NULL;
}

size_t len = b - a;
*dest = strndup(a, len);
fprintf(stderr, "b-a = %d\n", len);
// fprintf(stderr, "delimiter: %c\n", c);

if (!*dest)
{
fprintf(stderr, "not_dest: '%s'\n", *dest);
Expand Down Expand Up @@ -156,6 +155,8 @@ 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 Down Expand Up @@ -183,9 +184,11 @@ canfigger_parse_file(const char *file, const int delimiter)
st_canfigger_attr_node *attr_root = NULL;
st_canfigger_attr_node *attr_list = NULL;

tmp_node->key = strdup(empty_str);
char *b = grab_str_segment(a, &tmp_node->key, '=');
// fprintf(stderr, "key: '%s'\n", tmp_node->key);

fprintf(stderr, "key: '%s'\n", tmp_node->key);
tmp_node->value = strdup(empty_str);
if (b)
{
a = b;
Expand All @@ -212,7 +215,7 @@ canfigger_parse_file(const char *file, const int delimiter)
else
attr_root = cur_attr_node;

cur_attr_node->str = "";
cur_attr_node->str = strdup(empty_str);
if (b)
{
a = b;
Expand Down

0 comments on commit 290a106

Please sign in to comment.