Skip to content

Commit 6492a10

Browse files
andy5995claude
andcommitted
examples: clean up comments and remove redundant defaults loop
example-01.c: - Fix typo: '&addr' -> '&attr' in attribute iteration comment - Replace 'while (config != NULL)' with idiomatic 'while (list)' - Rename variable config -> list for clarity - Replace manual return-on-failure count check with assert() - Remove "only used for testing" comment by using assert example-02.c: - Remove opening comment about attributes->current internal state; that detail is now documented in canfigger.h - Remove redundant upfront defaults loop; the post-parse missing-keys loop is sufficient and is the canonical pattern Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent bfd81c8 commit 6492a10

2 files changed

Lines changed: 19 additions & 50 deletions

File tree

example-01.c

Lines changed: 19 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -18,63 +18,40 @@ main(int argc, char *argv[])
1818
return -1;
1919
}
2020

21-
//
22-
// Get a linked list containing the parsed config file. Each node contains
23-
// a key (or a "setting", or an "option"), a value and attributes (if they
24-
// are provided in your program's configuration file.
25-
//
26-
// The second argument is based on what the config file uses to separate
27-
// the attributes.
28-
//
29-
struct Canfigger *config = canfigger_parse_file(filename_ptr, ',');
30-
31-
if (!config)
21+
// Parse the config file. The second argument is the delimiter used to
22+
// separate the value from any attributes on the same line.
23+
struct Canfigger *list = canfigger_parse_file(filename_ptr, ',');
24+
if (!list)
3225
return -1;
3326

34-
// i is only used for testing
35-
int i = 0;
27+
int count = 0;
3628

37-
while (config != NULL)
29+
while (list)
3830
{
39-
//
40-
// The value member of the node must be checked for NULL
41-
// before using it.
42-
//
43-
printf("Key: %s, Value: %s\n", config->key,
44-
config->value != NULL ? config->value : "NULL");
31+
// value may be NULL for keys with no '=' on the line.
32+
printf("Key: %s, Value: %s\n", list->key,
33+
list->value ? list->value : "NULL");
4534

46-
//
47-
// Process attributes if necessary. If you know there are no attributes
48-
// for the current node, you can skip this, and there is no reason in
49-
// this case to call canfigger_free_current_attr_str_advance().
50-
//
51-
// attr must be declared and initialized before using it as an
52-
// argument to canfigger_free_current_attr_str_advance().
35+
// Iterate attributes if present. Initialize attr to NULL before the
36+
// first call; each call frees the previous string and loads the next.
37+
// Skip this block entirely if the node has no attributes.
5338
char *attr = NULL;
54-
//
55-
// Pass '&addr' to this function and it will get assigned an
56-
// attribute, or NULL if there are none.
57-
canfigger_free_current_attr_str_advance(config->attributes, &attr);
39+
canfigger_free_current_attr_str_advance(list->attributes, &attr);
5840
while (attr)
5941
{
6042
printf("Attribute: %s\n", attr);
61-
62-
//
63-
// Get the next attribute in the list (if there is one).
64-
//
65-
canfigger_free_current_attr_str_advance(config->attributes, &attr);
43+
canfigger_free_current_attr_str_advance(list->attributes, &attr);
6644
}
6745

68-
// Move to the next node and automatically free the current node
69-
canfigger_free_current_key_node_advance(&config);
46+
// Free the current node and advance to the next.
47+
canfigger_free_current_key_node_advance(&list);
7048
putchar('\n');
7149

72-
i++;
50+
count++;
7351
}
7452

75-
// This should be the number of keys in the example config
76-
if (i != 6)
77-
return -1;
53+
// Verify all entries in the example config were visited.
54+
assert(count == 6);
7855

7956
return 0;
8057
}

example-02.c

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@
55
*
66
* Numeric attribute lists use the form:
77
* key = list, v1, v2, ...
8-
*
9-
* attributes->current is NULL when a node is first returned; the caller must
10-
* invoke canfigger_free_current_attr_str_advance() with attr=NULL to load
11-
* the first attribute before entering the loop.
128
*/
139

1410
#include "tests/test.h"
@@ -158,10 +154,6 @@ main(int argc, char *argv[])
158154

159155
AppConfig_t config = { 0 };
160156

161-
/* Apply defaults for all scalar entries up front. */
162-
for (size_t i = 0; i < ARRAY_SIZE(entries); i++)
163-
config_set_field(&config, &entries[i], entries[i].default_value);
164-
165157
struct Canfigger *list = canfigger_parse_file(filename_ptr, ',');
166158
if (!list)
167159
return -1;

0 commit comments

Comments
 (0)