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> |
Paul Cercueil | 53ed943 | 2014-11-20 13:46:04 +0100 | [diff] [blame^] | 25 | #include <string.h> |
Paul Cercueil | 542cbb4 | 2014-10-21 13:00:38 +0200 | [diff] [blame] | 26 | |
Paul Cercueil | 2812f50 | 2014-11-05 14:40:56 +0100 | [diff] [blame] | 27 | #define LOCALE_SUPPORT defined(_WIN32) || (defined(__USE_XOPEN2K8) && \ |
| 28 | (!defined(__UCLIBC__) || defined(__UCLIBC_HAS_LOCALE__))) |
| 29 | |
Paul Cercueil | 542cbb4 | 2014-10-21 13:00:38 +0200 | [diff] [blame] | 30 | int read_double(const char *str, double *val) |
| 31 | { |
| 32 | double value; |
| 33 | char *end; |
Paul Cercueil | 2812f50 | 2014-11-05 14:40:56 +0100 | [diff] [blame] | 34 | #if LOCALE_SUPPORT == 1 |
Paul Cercueil | 542cbb4 | 2014-10-21 13:00:38 +0200 | [diff] [blame] | 35 | #ifdef _WIN32 |
| 36 | int config; |
| 37 | _locale_t old_locale; |
| 38 | |
| 39 | config = _configurethreadlocale(_ENABLE_PER_THREAD_LOCALE); |
| 40 | old_locale = _get_current_locale(); |
| 41 | setlocale(LC_NUMERIC, "POSIX"); |
| 42 | #else |
| 43 | locale_t old_locale, new_locale; |
| 44 | |
| 45 | new_locale = newlocale(LC_NUMERIC_MASK, "POSIX", (locale_t) 0); |
| 46 | old_locale = uselocale(new_locale); |
| 47 | #endif |
Paul Cercueil | 2812f50 | 2014-11-05 14:40:56 +0100 | [diff] [blame] | 48 | #endif |
Paul Cercueil | 542cbb4 | 2014-10-21 13:00:38 +0200 | [diff] [blame] | 49 | |
| 50 | value = strtod(str, &end); |
| 51 | |
Paul Cercueil | 2812f50 | 2014-11-05 14:40:56 +0100 | [diff] [blame] | 52 | #if LOCALE_SUPPORT == 1 |
Paul Cercueil | 542cbb4 | 2014-10-21 13:00:38 +0200 | [diff] [blame] | 53 | #ifdef _WIN32 |
| 54 | setlocale(old_locale); |
| 55 | _configurethreadlocale(config); |
| 56 | #else |
| 57 | uselocale(old_locale); |
| 58 | freelocale(new_locale); |
| 59 | #endif |
Paul Cercueil | 2812f50 | 2014-11-05 14:40:56 +0100 | [diff] [blame] | 60 | #endif |
Paul Cercueil | 542cbb4 | 2014-10-21 13:00:38 +0200 | [diff] [blame] | 61 | |
| 62 | if (end == str) |
| 63 | return -EINVAL; |
| 64 | |
| 65 | *val = value; |
| 66 | return 0; |
| 67 | } |
| 68 | |
| 69 | void write_double(char *buf, size_t len, double val) |
| 70 | { |
Paul Cercueil | 2812f50 | 2014-11-05 14:40:56 +0100 | [diff] [blame] | 71 | #if LOCALE_SUPPORT == 1 |
Paul Cercueil | 542cbb4 | 2014-10-21 13:00:38 +0200 | [diff] [blame] | 72 | #ifdef _WIN32 |
| 73 | int config; |
| 74 | _locale_t old_locale; |
| 75 | |
| 76 | config = _configurethreadlocale(_ENABLE_PER_THREAD_LOCALE); |
| 77 | old_locale = _get_current_locale(); |
| 78 | setlocale(LC_NUMERIC, "POSIX"); |
| 79 | #else |
| 80 | locale_t old_locale, new_locale; |
| 81 | |
| 82 | new_locale = newlocale(LC_NUMERIC_MASK, "POSIX", (locale_t) 0); |
| 83 | old_locale = uselocale(new_locale); |
| 84 | #endif |
Paul Cercueil | 2812f50 | 2014-11-05 14:40:56 +0100 | [diff] [blame] | 85 | #endif |
Paul Cercueil | 542cbb4 | 2014-10-21 13:00:38 +0200 | [diff] [blame] | 86 | |
| 87 | snprintf(buf, len, "%lf", val); |
| 88 | |
Paul Cercueil | 2812f50 | 2014-11-05 14:40:56 +0100 | [diff] [blame] | 89 | #if LOCALE_SUPPORT == 1 |
Paul Cercueil | 542cbb4 | 2014-10-21 13:00:38 +0200 | [diff] [blame] | 90 | #ifdef _WIN32 |
| 91 | setlocale(old_locale); |
| 92 | _configurethreadlocale(config); |
| 93 | #else |
| 94 | uselocale(old_locale); |
| 95 | freelocale(new_locale); |
| 96 | #endif |
Paul Cercueil | 2812f50 | 2014-11-05 14:40:56 +0100 | [diff] [blame] | 97 | #endif |
Paul Cercueil | 542cbb4 | 2014-10-21 13:00:38 +0200 | [diff] [blame] | 98 | } |
Paul Cercueil | 53ed943 | 2014-11-20 13:46:04 +0100 | [diff] [blame^] | 99 | |
| 100 | void iio_library_get_version(unsigned int *major, |
| 101 | unsigned int *minor, char git_tag[8]) |
| 102 | { |
| 103 | if (major) |
| 104 | *major = LIBIIO_VERSION_MAJOR; |
| 105 | if (minor) |
| 106 | *minor = LIBIIO_VERSION_MINOR; |
| 107 | if (git_tag) { |
| 108 | strncpy(git_tag, LIBIIO_VERSION_GIT, 8); |
| 109 | git_tag[7] = '\0'; |
| 110 | } |
| 111 | } |