diff options
author | Bryce Harrington <bryce@osg.samsung.com> | 2016-07-14 18:28:03 -0700 |
---|---|---|
committer | Bryce Harrington <bryce@osg.samsung.com> | 2016-07-26 15:57:14 -0700 |
commit | e776f2a4d9a335a41d9cbc8b06ba97a660051614 (patch) | |
tree | 53c674532942a7d24a2a44a22bf9abaaa92210ab /shared/config-parser.c | |
parent | cbfde138597e5a3f0693aa4bc7c08a515d1e0f82 (diff) |
config-parser: Add weston_config_section_get_color
Previously weston_config_section_get_uint was serving dual purpose for
parsing both unsigned decimal integer values (ids, counts, seconds,
etc.) and hexadecimal values (colors), by relying on strtoul's
auto-detection mechanism.
However, this usage is unable to catch certain kinds of error
conditions, such as specifying a negative number where an unsigned
should be used. And for colors in particular, it would misparse hex
values if the leading 0x was omitted. E.g. "background-color=99999999"
would render a near-black background (effectively 0x05f5e0ff) instead of
medium grey, and "background-color=ffffffff" would be treated as an
error rather than white. "background-color=0x01234567",
"background-color=01234567", and "background-color=1234567" each
resulted in the value being parsed as hexadecimal, octal, and decimal
respectively, resulting in colors 0x01234567, 0x00053977, and 0x0012d687
being displayed.
This new routine forces hexadecimal to be used in all cases when parsing
color values, so "0x01234567" and "01234567" result in the same color
value, "99999999" is grey, and "ffffffff" is white. It also requires
exactly 8 or 10 digits (other lengths likely indicate typos), or the
value "0" (black).
Signed-off-by: Bryce Harrington <bryce@osg.samsung.com>
Reviewed-by: Eric Engestrom <eric.engestrom@imgtec.com>
Diffstat (limited to 'shared/config-parser.c')
-rw-r--r-- | shared/config-parser.c | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/shared/config-parser.c b/shared/config-parser.c index 1e08759e..33d5e361 100644 --- a/shared/config-parser.c +++ b/shared/config-parser.c @@ -209,6 +209,45 @@ weston_config_section_get_uint(struct weston_config_section *section, WL_EXPORT int +weston_config_section_get_color(struct weston_config_section *section, + const char *key, + uint32_t *color, uint32_t default_color) +{ + struct weston_config_entry *entry; + int len; + char *end; + + entry = config_section_get_entry(section, key); + if (entry == NULL) { + *color = default_color; + errno = ENOENT; + return -1; + } + + len = strlen(entry->value); + if (len == 1 && entry->value[0] == '0') { + *color = 0; + return 0; + } else if (len != 8 && len != 10) { + fprintf(stderr, "string '%s' is length %d\n", entry->value, len); + *color = default_color; + errno = EINVAL; + return -1; + } + + errno = 0; + *color = strtoul(entry->value, &end, 16); + if (errno != 0 || end == entry->value || *end != '\0') { + *color = default_color; + errno = EINVAL; + return -1; + } + + return 0; +} + +WL_EXPORT +int weston_config_section_get_double(struct weston_config_section *section, const char *key, double *value, double default_value) |