From 465a968ddd93bddf77cb9d54039155abe5d73294 Mon Sep 17 00:00:00 2001 From: Yuxuan Shui Date: Sat, 2 Feb 2019 01:22:41 +0000 Subject: [PATCH] Parse number locale-independently Previously we were using glibc's strtod function to parse floating point numbers. The problem is, strtod is locale dependent. Meaning 7,7 might be parsed as two numbers (7 and 7) in one locale, and parsed as one number (7 point 7) in another locale. This is undesirable. We need to set the locale to a value we know to make number parsing consistently. We could use setlocale(), but that is not thread-safe. We can also use uselocale(), which is thread-safe, but doesn't cover strtod (Yeah, some of the locale-aware functions only acknowledge the global locale, not the thread local one). So in frustration, I just wrote a simple floating point number parser myself. This parser obviously doesn't cover all cases strtod covers, but is good enough for our needs. Signed-off-by: Yuxuan Shui --- man/compton.1.asciidoc | 6 +++++- src/config.c | 4 ++-- src/string_utils.c | 31 +++++++++++++++++++++++++++++++ src/string_utils.h | 3 +++ 4 files changed, 41 insertions(+), 3 deletions(-) diff --git a/man/compton.1.asciidoc b/man/compton.1.asciidoc index aa0b5fbd..10a87552 100644 --- a/man/compton.1.asciidoc +++ b/man/compton.1.asciidoc @@ -199,7 +199,11 @@ OPTIONS WIDTH,HEIGHT,ELE1,ELE2,ELE3,ELE4,ELE5... ---- + -The element in the center must not be included, it will be forever 1.0 or changing based on opacity, depending on whether you have `--blur-background-fixed`. Yet the automatic adjustment of blur factor may not work well with a custom blur kernel. +In other words, the matrix is formatted as a list of comma separated numbers. The first two numbers must be integers, which specify the width and height of the matrix. They must be odd numbers. Then, the following 'width * height - 1' numbers specifies the numbers in the matrix, row by row, excluding the center element. ++ +The elements are finite floating point numbers. The decimal pointer has to be '.' (a period), and scientific notation is not supported. ++ +The element in the center will either be 1.0 or changing based on opacity, depending on whether you have `--blur-background-fixed`. Yet the automatic adjustment of blur factor may not work well with a custom blur kernel. + A 7x7 Gaussian blur kernel (sigma = 0.84089642) looks like: + diff --git a/src/config.c b/src/config.c index 58bf1a30..0c923835 100644 --- a/src/config.c +++ b/src/config.c @@ -47,8 +47,8 @@ parse_long(const char *s, long *dest) { */ const char * parse_matrix_readnum(const char *src, double *dest) { - char *pc = NULL; - double val = strtod(src, &pc); + const char *pc = NULL; + double val = strtod_simple(src, &pc); if (!pc || pc == src) { log_error("No number found: %s", src); return src; diff --git a/src/string_utils.c b/src/string_utils.c index df0ac8dd..6b3a8d2a 100644 --- a/src/string_utils.c +++ b/src/string_utils.c @@ -40,3 +40,34 @@ void mstrextend(char **psrc1, const char *src2) { strncpy(*psrc1 + len1, src2, len2); (*psrc1)[len - 1] = '\0'; } + +/// Parse a floating point number of form (+|-)?[0-9]*(\.[0-9]*) +double strtod_simple(const char *src, const char **end) { + double neg = 1; + if (*src == '-') { + neg = -1; + src++; + } else if (*src == '+') { + src++; + } + + double ret = 0; + while (*src >= '0' && *src <= '9') { + ret = ret * 10 + (*src - '0'); + src++; + } + + if (*src == '.') { + double frac = 0, mult = 0.1; + src++; + while (*src >= '0' && *src <= '9') { + frac += mult * (*src - '0'); + mult *= 0.1; + src++; + } + ret += frac; + } + + *end = src; + return ret * neg; +} diff --git a/src/string_utils.h b/src/string_utils.h index bff0bd6e..8581104a 100644 --- a/src/string_utils.h +++ b/src/string_utils.h @@ -11,6 +11,9 @@ char * mstrjoin3(const char *src1, const char *src2, const char *src3); void mstrextend(char **psrc1, const char *src2); +/// Parse a floating point number of form (+|-)?[0-9]*(\.[0-9]*) +double strtod_simple(const char *, const char **); + static inline int uitostr(unsigned int n, char *buf) { int ret = 0; unsigned int tmp = n;