From: "Cercueil, Paul" <Paul.Cercueil@analog.com>
To: "lars@metafoo.de" <lars@metafoo.de>,
"geert@linux-m68k.org" <geert@linux-m68k.org>
Cc: "linux-iio@vger.kernel.org" <linux-iio@vger.kernel.org>
Subject: Re: [PATCH libiio] iio-monitor: Set locale before parsing double values from sysfs
Date: Wed, 21 Dec 2016 17:04:13 +0000 [thread overview]
Message-ID: <1482339839.2295.0.camel@analog.com> (raw)
In-Reply-To: <1482329388-12376-1-git-send-email-geert@linux-m68k.org>
[-- Attachment #1: Type: text/plain, Size: 1660 bytes --]
Hi,
Le mercredi 21 décembre 2016 à 15:09 +0100, Geert Uytterhoeven a
écrit :
> strtod() uses the current locale when parsing numbers, while all
> values
> in sysfs use the "C" locale. Hence the former may expect a comma as
> the
> radix character, while the latter always uses a decimal point,
> leading
> to the display of incorrect values.
>
> Temporarily switch to the "C" locale before parsing double numbers to
> fix this.
>
> Signed-off-by: Geert Uytterhoeven <geert@linux-m68k.org>
> ---
> examples/iio-monitor.c | 8 ++++++++
> 1 file changed, 8 insertions(+)
>
> diff --git a/examples/iio-monitor.c b/examples/iio-monitor.c
> index 8b632187829869ae..558c0f33a36ef7b9 100644
> --- a/examples/iio-monitor.c
> +++ b/examples/iio-monitor.c
> @@ -21,6 +21,7 @@
> #define _DEFAULT_SOURCE
>
> #include <cdk/cdk.h>
> +#include <locale.h>
> #include <pthread.h>
> #include <stdbool.h>
> #include <unistd.h>
> @@ -61,9 +62,13 @@ static bool is_valid_channel(struct iio_channel
> *chn)
>
> static double get_channel_value(struct iio_channel *chn)
> {
> + char *old_locale;
> char buf[1024];
> double val;
>
> + old_locale = strdup(setlocale(LC_NUMERIC, NULL));
> + setlocale(LC_NUMERIC, "C");
> +
> if (channel_has_attr(chn, "input")) {
> iio_channel_attr_read(chn, "input", buf,
> sizeof(buf));
> val = strtod(buf, NULL);
> @@ -82,6 +87,9 @@ static double get_channel_value(struct iio_channel
> *chn)
> }
> }
>
> + setlocale(LC_NUMERIC, old_locale);
> + free(old_locale);
> +
> return val / 1000.0;
> }
>
Applied. Thanks!
-Paul
[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 488 bytes --]
prev parent reply other threads:[~2016-12-21 17:38 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-12-21 14:09 [PATCH libiio] iio-monitor: Set locale before parsing double values from sysfs Geert Uytterhoeven
2016-12-21 17:04 ` Cercueil, Paul [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1482339839.2295.0.camel@analog.com \
--to=paul.cercueil@analog.com \
--cc=geert@linux-m68k.org \
--cc=lars@metafoo.de \
--cc=linux-iio@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).