blob: d98829cc17104f3179ca960c932d04aa788c70f8 [file] [log] [blame]
Paul Cercueil542cbb42014-10-21 13:00:38 +02001/*
2 * libiio - Library for interfacing industrial I/O (IIO) devices
3 *
4 * Copyright (C) 2014 Analog Devices, Inc.
5 * Author: Paul Cercueil <paul.cercueil@analog.com>
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * */
18
19#include "iio-private.h"
20
21#include <errno.h>
22#include <locale.h>
23#include <stdio.h>
24#include <stdlib.h>
25
Paul Cercueil2812f502014-11-05 14:40:56 +010026#define LOCALE_SUPPORT defined(_WIN32) || (defined(__USE_XOPEN2K8) && \
27 (!defined(__UCLIBC__) || defined(__UCLIBC_HAS_LOCALE__)))
28
Paul Cercueil542cbb42014-10-21 13:00:38 +020029int read_double(const char *str, double *val)
30{
31 double value;
32 char *end;
Paul Cercueil2812f502014-11-05 14:40:56 +010033#if LOCALE_SUPPORT == 1
Paul Cercueil542cbb42014-10-21 13:00:38 +020034#ifdef _WIN32
35 int config;
36 _locale_t old_locale;
37
38 config = _configurethreadlocale(_ENABLE_PER_THREAD_LOCALE);
39 old_locale = _get_current_locale();
40 setlocale(LC_NUMERIC, "POSIX");
41#else
42 locale_t old_locale, new_locale;
43
44 new_locale = newlocale(LC_NUMERIC_MASK, "POSIX", (locale_t) 0);
45 old_locale = uselocale(new_locale);
46#endif
Paul Cercueil2812f502014-11-05 14:40:56 +010047#endif
Paul Cercueil542cbb42014-10-21 13:00:38 +020048
49 value = strtod(str, &end);
50
Paul Cercueil2812f502014-11-05 14:40:56 +010051#if LOCALE_SUPPORT == 1
Paul Cercueil542cbb42014-10-21 13:00:38 +020052#ifdef _WIN32
53 setlocale(old_locale);
54 _configurethreadlocale(config);
55#else
56 uselocale(old_locale);
57 freelocale(new_locale);
58#endif
Paul Cercueil2812f502014-11-05 14:40:56 +010059#endif
Paul Cercueil542cbb42014-10-21 13:00:38 +020060
61 if (end == str)
62 return -EINVAL;
63
64 *val = value;
65 return 0;
66}
67
68void write_double(char *buf, size_t len, double val)
69{
Paul Cercueil2812f502014-11-05 14:40:56 +010070#if LOCALE_SUPPORT == 1
Paul Cercueil542cbb42014-10-21 13:00:38 +020071#ifdef _WIN32
72 int config;
73 _locale_t old_locale;
74
75 config = _configurethreadlocale(_ENABLE_PER_THREAD_LOCALE);
76 old_locale = _get_current_locale();
77 setlocale(LC_NUMERIC, "POSIX");
78#else
79 locale_t old_locale, new_locale;
80
81 new_locale = newlocale(LC_NUMERIC_MASK, "POSIX", (locale_t) 0);
82 old_locale = uselocale(new_locale);
83#endif
Paul Cercueil2812f502014-11-05 14:40:56 +010084#endif
Paul Cercueil542cbb42014-10-21 13:00:38 +020085
86 snprintf(buf, len, "%lf", val);
87
Paul Cercueil2812f502014-11-05 14:40:56 +010088#if LOCALE_SUPPORT == 1
Paul Cercueil542cbb42014-10-21 13:00:38 +020089#ifdef _WIN32
90 setlocale(old_locale);
91 _configurethreadlocale(config);
92#else
93 uselocale(old_locale);
94 freelocale(new_locale);
95#endif
Paul Cercueil2812f502014-11-05 14:40:56 +010096#endif
Paul Cercueil542cbb42014-10-21 13:00:38 +020097}