Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions ChangeLog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

* Doxyfile: Exclude tests (remove unused structs from docs)
+ Add canfigger_get_int_attrs(): parse node attributes as an int array
+ Add canfigger_get_double_attrs(): parse node attributes as a double array
+ Add canfigger_parse_color_hex(): parse #RRGGBB / #RRGGBBAA into uint8_t RGBA components
+ Add canfigger_parse_color(): parse a color node in hex or integer-attribute format
* canfigger_parse_color_hex(): reject strings with non-hex characters (strspn guard)
+ Add canfigger_parse_color_pair(): parse a foreground/background hex color pair from one node

2026-05-11
Expand Down
62 changes: 2 additions & 60 deletions canfigger.c
Original file line number Diff line number Diff line change
Expand Up @@ -549,46 +549,6 @@ canfigger_get_int_attrs(const struct Canfigger *node, int *out, size_t max)
}


size_t
canfigger_get_double_attrs(const struct Canfigger *node, double *out,
size_t max)
{
if (!node || !node->attributes || !node->attributes->str || !out || !max)
return 0;

size_t count = 0;
char *p = node->attributes->str;

while (count < max)
{
while (*p == ' ' || *p == '\t')
p++;

if (*p == '\0' || *p == '\n')
break;

char *endptr;
errno = 0;
double val = strtod(p, &endptr);
if (endptr == p || errno == ERANGE)
break;
out[count++] = val;

p = endptr;
while (*p == ' ' || *p == '\t')
p++;

if (*p == '\0')
break;
if (*p != '\n')
break;
p++;
}

return count;
}


int
canfigger_parse_color_hex(const char *str, uint8_t *r, uint8_t *g, uint8_t *b,
uint8_t *a)
Expand All @@ -599,6 +559,8 @@ canfigger_parse_color_hex(const char *str, uint8_t *r, uint8_t *g, uint8_t *b,
size_t len = strlen(str);
if (len != 6 && len != 8)
return 0;
if (strspn(str, "0123456789abcdefABCDEF") != len)
return 0;
unsigned int rv, gv, bv, av = 255;
if (sscanf(str, "%2x%2x%2x", &rv, &gv, &bv) != 3)
return 0;
Expand All @@ -612,26 +574,6 @@ canfigger_parse_color_hex(const char *str, uint8_t *r, uint8_t *g, uint8_t *b,
}


int
canfigger_parse_color(const struct Canfigger *node, uint8_t *r, uint8_t *g,
uint8_t *b, uint8_t *a)
{
if (!node || !r || !g || !b || !a)
return 0;
if (node->value && node->value[0] == '#')
return canfigger_parse_color_hex(node->value, r, g, b, a);
int v[4];
size_t count = canfigger_get_int_attrs(node, v, 4);
if (count < 3)
return 0;
*r = (uint8_t) v[0];
*g = (uint8_t) v[1];
*b = (uint8_t) v[2];
*a = count == 4 ? (uint8_t) v[3] : 255;
return (int) count;
}


int
canfigger_parse_color_pair(const struct Canfigger *node,
uint8_t *r1, uint8_t *g1, uint8_t *b1, uint8_t *a1,
Expand Down
45 changes: 0 additions & 45 deletions canfigger.h
Original file line number Diff line number Diff line change
Expand Up @@ -323,25 +323,6 @@ extern "C"
size_t canfigger_get_int_attrs(const struct Canfigger *node, int *out,
size_t max);

/**
* @brief Parse a node's attributes as an array of doubles.
*
* Like canfigger_get_int_attrs() but parses each attribute as a
* floating-point value using strtod().
*
* @param node Node whose attributes are to be parsed (may be NULL).
* @param out Output array; must have room for at least @p max elements.
* @param max Maximum number of values to store.
* @return Number of values actually parsed; 0 if @p node has no
* attributes or arguments are invalid.
*
* @snippet examples/canfigger_get_double_attrs.c canfigger_get_double_attrs
*
* @since 0.3.3
*/
size_t canfigger_get_double_attrs(const struct Canfigger *node, double *out,
size_t max);

/**
* @brief Parse a hex color string into RGBA components.
*
Expand All @@ -368,32 +349,6 @@ extern "C"
int canfigger_parse_color_hex(const char *str, uint8_t * r, uint8_t * g,
uint8_t * b, uint8_t * a);

/**
* @brief Parse a color from a config node, accepting hex or integer-attribute format.
*
* Dispatches on @c node->value:
* - If it starts with @c '#', delegates to canfigger_parse_color_hex().
* Config line form: @c key=\#RRGGBB or @c key=\#RRGGBBAA.
* - Otherwise reads 3 or 4 integer attributes via canfigger_get_int_attrs().
* Conventional form: @c key=rgb,R,G,B or @c key=rgba,R,G,B,A where the
* value acts as a type tag and the integers follow as comma-separated attributes.
*
* In both cases @p a is set to 255 when no alpha is present.
*
* @param node Config node to read (may be NULL).
* @param r Output: red component.
* @param g Output: green component.
* @param b Output: blue component.
* @param a Output: alpha component.
* @return 3 or 4 on success (number of components found), 0 on failure.
*
* @since 0.3.3
*
* @snippet examples/canfigger_parse_color.c canfigger_parse_color
*/
int canfigger_parse_color(const struct Canfigger *node, uint8_t * r,
uint8_t * g, uint8_t * b, uint8_t * a);

/**
* @brief Parse a foreground/background color pair from a config node.
*
Expand Down
35 changes: 0 additions & 35 deletions examples/canfigger_get_double_attrs.c

This file was deleted.

26 changes: 0 additions & 26 deletions examples/canfigger_parse_color.c

This file was deleted.

2 changes: 0 additions & 2 deletions examples/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ snippet_examples = [
'canfigger_config_file',
'canfigger_path_join',
'canfigger_get_int_attrs',
'canfigger_get_double_attrs',
'canfigger_parse_color_hex',
'canfigger_parse_color',
'canfigger_parse_color_pair',
]

Expand Down
5 changes: 5 additions & 0 deletions tests/color_hex.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,10 @@ main(void)
assert(canfigger_parse_color_hex("#FF80", &r, &g, &b, &a) == 0);
assert(canfigger_parse_color_hex("#FF80000", &r, &g, &b, &a) == 0);

/* non-hex characters — must be rejected, not silently mis-parsed */
assert(canfigger_parse_color_hex("#12345g", &r, &g, &b, &a) == 0);
assert(canfigger_parse_color_hex("#GGGGGG", &r, &g, &b, &a) == 0);
assert(canfigger_parse_color_hex("#FF800X80", &r, &g, &b, &a) == 0);

return 0;
}
38 changes: 0 additions & 38 deletions tests/double_attrs.c

This file was deleted.

2 changes: 0 additions & 2 deletions tests/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ test_cases = [
'check_version',
'bom',
'color_hex',
'double_attrs',
'int_attrs',
'colons',
'config_file',
Expand All @@ -11,7 +10,6 @@ test_cases = [
'incomplete_parse_file',
'multiple_attributes',
'no_trailing_newline',
'parse_color',
'parse_color_pair',
'parse_file',
'skip_attributes',
Expand Down
47 changes: 0 additions & 47 deletions tests/parse_color.c

This file was deleted.

5 changes: 0 additions & 5 deletions tests/parse_color.conf

This file was deleted.

Loading