Paul Cercueil | 542cbb4 | 2014-10-21 13:00:38 +0200 | [diff] [blame] | 1 | /* |
| 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 Cercueil | 2812f50 | 2014-11-05 14:40:56 +0100 | [diff] [blame^] | 26 | #define LOCALE_SUPPORT defined(_WIN32) || (defined(__USE_XOPEN2K8) && \ |
| 27 | (!defined(__UCLIBC__) || defined(__UCLIBC_HAS_LOCALE__))) |
| 28 | |
Paul Cercueil | 542cbb4 | 2014-10-21 13:00:38 +0200 | [diff] [blame] | 29 | int read_double(const char *str, double *val) |
| 30 | { |
| 31 | double value; |
| 32 | char *end; |
Paul Cercueil | 2812f50 | 2014-11-05 14:40:56 +0100 | [diff] [blame^] | 33 | #if LOCALE_SUPPORT == 1 |
Paul Cercueil | 542cbb4 | 2014-10-21 13:00:38 +0200 | [diff] [blame] | 34 | #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 Cercueil | 2812f50 | 2014-11-05 14:40:56 +0100 | [diff] [blame^] | 47 | #endif |
Paul Cercueil | 542cbb4 | 2014-10-21 13:00:38 +0200 | [diff] [blame] | 48 | |
| 49 | value = strtod(str, &end); |
| 50 | |
Paul Cercueil | 2812f50 | 2014-11-05 14:40:56 +0100 | [diff] [blame^] | 51 | #if LOCALE_SUPPORT == 1 |
Paul Cercueil | 542cbb4 | 2014-10-21 13:00:38 +0200 | [diff] [blame] | 52 | #ifdef _WIN32 |
| 53 | setlocale(old_locale); |
| 54 | _configurethreadlocale(config); |
| 55 | #else |
| 56 | uselocale(old_locale); |
| 57 | freelocale(new_locale); |
| 58 | #endif |
Paul Cercueil | 2812f50 | 2014-11-05 14:40:56 +0100 | [diff] [blame^] | 59 | #endif |
Paul Cercueil | 542cbb4 | 2014-10-21 13:00:38 +0200 | [diff] [blame] | 60 | |
| 61 | if (end == str) |
| 62 | return -EINVAL; |
| 63 | |
| 64 | *val = value; |
| 65 | return 0; |
| 66 | } |
| 67 | |
| 68 | void write_double(char *buf, size_t len, double val) |
| 69 | { |
Paul Cercueil | 2812f50 | 2014-11-05 14:40:56 +0100 | [diff] [blame^] | 70 | #if LOCALE_SUPPORT == 1 |
Paul Cercueil | 542cbb4 | 2014-10-21 13:00:38 +0200 | [diff] [blame] | 71 | #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 Cercueil | 2812f50 | 2014-11-05 14:40:56 +0100 | [diff] [blame^] | 84 | #endif |
Paul Cercueil | 542cbb4 | 2014-10-21 13:00:38 +0200 | [diff] [blame] | 85 | |
| 86 | snprintf(buf, len, "%lf", val); |
| 87 | |
Paul Cercueil | 2812f50 | 2014-11-05 14:40:56 +0100 | [diff] [blame^] | 88 | #if LOCALE_SUPPORT == 1 |
Paul Cercueil | 542cbb4 | 2014-10-21 13:00:38 +0200 | [diff] [blame] | 89 | #ifdef _WIN32 |
| 90 | setlocale(old_locale); |
| 91 | _configurethreadlocale(config); |
| 92 | #else |
| 93 | uselocale(old_locale); |
| 94 | freelocale(new_locale); |
| 95 | #endif |
Paul Cercueil | 2812f50 | 2014-11-05 14:40:56 +0100 | [diff] [blame^] | 96 | #endif |
Paul Cercueil | 542cbb4 | 2014-10-21 13:00:38 +0200 | [diff] [blame] | 97 | } |