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 | 24a6212 | 2015-02-09 17:53:14 +0100 | [diff] [blame] | 27 | #if defined(_WIN32) || (defined(__USE_XOPEN2K8) && \ |
Paul Cercueil | 2812f50 | 2014-11-05 14:40:56 +0100 | [diff] [blame] | 28 | (!defined(__UCLIBC__) || defined(__UCLIBC_HAS_LOCALE__))) |
Paul Cercueil | 24a6212 | 2015-02-09 17:53:14 +0100 | [diff] [blame] | 29 | #define LOCALE_SUPPORT |
| 30 | #endif |
Paul Cercueil | 2812f50 | 2014-11-05 14:40:56 +0100 | [diff] [blame] | 31 | |
Paul Cercueil | 24a6212 | 2015-02-09 17:53:14 +0100 | [diff] [blame] | 32 | #ifdef LOCALE_SUPPORT |
Paul Cercueil | 4202e5f | 2015-03-03 18:14:46 +0100 | [diff] [blame^] | 33 | #if defined(__MINGW32__) |
| 34 | static int read_double_locale(const char *str, double *val) |
| 35 | { |
| 36 | char *end, *old_locale; |
| 37 | double value; |
| 38 | |
| 39 | /* XXX: This is not thread-safe, but it's the only way we have to |
| 40 | * support locales under MinGW without linking with Visual Studio |
| 41 | * libraries. */ |
| 42 | old_locale = strdup(setlocale(LC_NUMERIC, NULL)); |
| 43 | if (!old_locale) |
| 44 | return -ENOMEM; |
| 45 | |
| 46 | setlocale(LC_NUMERIC, "C"); |
| 47 | value = strtod(str, &end); |
| 48 | setlocale(LC_NUMERIC, old_locale); |
| 49 | free(old_locale); |
| 50 | |
| 51 | if (end == str) |
| 52 | return -EINVAL; |
| 53 | |
| 54 | *val = value; |
| 55 | return 0; |
| 56 | } |
| 57 | |
| 58 | static int write_double_locale(char *buf, size_t len, double val) |
| 59 | { |
| 60 | /* XXX: Not thread-safe, see above */ |
| 61 | char *old_locale = strdup(setlocale(LC_NUMERIC, NULL)); |
| 62 | if (!old_locale) |
| 63 | return -ENOMEM; |
| 64 | |
| 65 | setlocale(LC_NUMERIC, "C"); |
| 66 | snprintf(buf, len, "%lf", val); |
| 67 | setlocale(LC_NUMERIC, old_locale); |
| 68 | free(old_locale); |
| 69 | return 0; |
| 70 | } |
| 71 | #elif defined(_WIN32) |
Paul Cercueil | 24a6212 | 2015-02-09 17:53:14 +0100 | [diff] [blame] | 72 | static int read_double_locale(const char *str, double *val) |
Paul Cercueil | 542cbb4 | 2014-10-21 13:00:38 +0200 | [diff] [blame] | 73 | { |
Paul Cercueil | 542cbb4 | 2014-10-21 13:00:38 +0200 | [diff] [blame] | 74 | char *end; |
Paul Cercueil | 24a6212 | 2015-02-09 17:53:14 +0100 | [diff] [blame] | 75 | double value; |
Paul Cercueil | 77cbc4b | 2015-03-03 18:10:37 +0100 | [diff] [blame] | 76 | _locale_t locale = _create_locale(LC_NUMERIC, "C"); |
Paul Cercueil | 225a3e1 | 2015-03-03 18:08:41 +0100 | [diff] [blame] | 77 | if (!locale) |
| 78 | return -ENOMEM; |
Paul Cercueil | 542cbb4 | 2014-10-21 13:00:38 +0200 | [diff] [blame] | 79 | |
Paul Cercueil | 24a6212 | 2015-02-09 17:53:14 +0100 | [diff] [blame] | 80 | value = _strtod_l(str, &end, locale); |
| 81 | _free_locale(locale); |
Paul Cercueil | 542cbb4 | 2014-10-21 13:00:38 +0200 | [diff] [blame] | 82 | |
| 83 | if (end == str) |
| 84 | return -EINVAL; |
| 85 | |
| 86 | *val = value; |
| 87 | return 0; |
| 88 | } |
| 89 | |
Paul Cercueil | 225a3e1 | 2015-03-03 18:08:41 +0100 | [diff] [blame] | 90 | static int write_double_locale(char *buf, size_t len, double val) |
Paul Cercueil | 542cbb4 | 2014-10-21 13:00:38 +0200 | [diff] [blame] | 91 | { |
Paul Cercueil | 77cbc4b | 2015-03-03 18:10:37 +0100 | [diff] [blame] | 92 | _locale_t locale = _create_locale(LC_NUMERIC, "C"); |
Paul Cercueil | 225a3e1 | 2015-03-03 18:08:41 +0100 | [diff] [blame] | 93 | if (!locale) |
| 94 | return -ENOMEM; |
Paul Cercueil | 542cbb4 | 2014-10-21 13:00:38 +0200 | [diff] [blame] | 95 | |
Paul Cercueil | 3910491 | 2015-02-10 11:40:14 +0100 | [diff] [blame] | 96 | _snprintf_l(buf, len, "%lf", locale, val); |
Paul Cercueil | 24a6212 | 2015-02-09 17:53:14 +0100 | [diff] [blame] | 97 | _free_locale(locale); |
Paul Cercueil | 225a3e1 | 2015-03-03 18:08:41 +0100 | [diff] [blame] | 98 | return 0; |
Paul Cercueil | 24a6212 | 2015-02-09 17:53:14 +0100 | [diff] [blame] | 99 | } |
Paul Cercueil | 542cbb4 | 2014-10-21 13:00:38 +0200 | [diff] [blame] | 100 | #else |
Paul Cercueil | 24a6212 | 2015-02-09 17:53:14 +0100 | [diff] [blame] | 101 | static int read_double_locale(const char *str, double *val) |
| 102 | { |
| 103 | char *end; |
| 104 | double value; |
Paul Cercueil | 542cbb4 | 2014-10-21 13:00:38 +0200 | [diff] [blame] | 105 | locale_t old_locale, new_locale; |
| 106 | |
Paul Cercueil | 77cbc4b | 2015-03-03 18:10:37 +0100 | [diff] [blame] | 107 | new_locale = newlocale(LC_NUMERIC_MASK, "C", (locale_t) 0); |
Paul Cercueil | 225a3e1 | 2015-03-03 18:08:41 +0100 | [diff] [blame] | 108 | if (!new_locale) |
| 109 | return -errno; |
| 110 | |
Paul Cercueil | 542cbb4 | 2014-10-21 13:00:38 +0200 | [diff] [blame] | 111 | old_locale = uselocale(new_locale); |
Paul Cercueil | 24a6212 | 2015-02-09 17:53:14 +0100 | [diff] [blame] | 112 | |
| 113 | value = strtod(str, &end); |
| 114 | uselocale(old_locale); |
| 115 | freelocale(new_locale); |
| 116 | |
| 117 | if (end == str) |
| 118 | return -EINVAL; |
| 119 | |
| 120 | *val = value; |
| 121 | return 0; |
| 122 | } |
| 123 | |
Paul Cercueil | 225a3e1 | 2015-03-03 18:08:41 +0100 | [diff] [blame] | 124 | static int write_double_locale(char *buf, size_t len, double val) |
Paul Cercueil | 24a6212 | 2015-02-09 17:53:14 +0100 | [diff] [blame] | 125 | { |
| 126 | locale_t old_locale, new_locale; |
| 127 | |
Paul Cercueil | 77cbc4b | 2015-03-03 18:10:37 +0100 | [diff] [blame] | 128 | new_locale = newlocale(LC_NUMERIC_MASK, "C", (locale_t) 0); |
Paul Cercueil | 225a3e1 | 2015-03-03 18:08:41 +0100 | [diff] [blame] | 129 | if (!new_locale) |
| 130 | return -errno; |
| 131 | |
Paul Cercueil | 24a6212 | 2015-02-09 17:53:14 +0100 | [diff] [blame] | 132 | old_locale = uselocale(new_locale); |
Paul Cercueil | 542cbb4 | 2014-10-21 13:00:38 +0200 | [diff] [blame] | 133 | |
| 134 | snprintf(buf, len, "%lf", val); |
| 135 | |
Paul Cercueil | 542cbb4 | 2014-10-21 13:00:38 +0200 | [diff] [blame] | 136 | uselocale(old_locale); |
| 137 | freelocale(new_locale); |
Paul Cercueil | 225a3e1 | 2015-03-03 18:08:41 +0100 | [diff] [blame] | 138 | return 0; |
Paul Cercueil | 24a6212 | 2015-02-09 17:53:14 +0100 | [diff] [blame] | 139 | } |
Paul Cercueil | 542cbb4 | 2014-10-21 13:00:38 +0200 | [diff] [blame] | 140 | #endif |
Paul Cercueil | 2812f50 | 2014-11-05 14:40:56 +0100 | [diff] [blame] | 141 | #endif |
Paul Cercueil | 24a6212 | 2015-02-09 17:53:14 +0100 | [diff] [blame] | 142 | |
| 143 | int read_double(const char *str, double *val) |
| 144 | { |
| 145 | #ifdef LOCALE_SUPPORT |
| 146 | return read_double_locale(str, val); |
| 147 | #else |
| 148 | char *end; |
| 149 | double value = strtod(str, &end); |
| 150 | |
| 151 | if (end == str) |
| 152 | return -EINVAL; |
| 153 | |
| 154 | *val = value; |
| 155 | return 0; |
| 156 | #endif |
| 157 | } |
| 158 | |
Paul Cercueil | 225a3e1 | 2015-03-03 18:08:41 +0100 | [diff] [blame] | 159 | int write_double(char *buf, size_t len, double val) |
Paul Cercueil | 24a6212 | 2015-02-09 17:53:14 +0100 | [diff] [blame] | 160 | { |
| 161 | #ifdef LOCALE_SUPPORT |
Paul Cercueil | 225a3e1 | 2015-03-03 18:08:41 +0100 | [diff] [blame] | 162 | return write_double_locale(buf, len, val); |
Paul Cercueil | 24a6212 | 2015-02-09 17:53:14 +0100 | [diff] [blame] | 163 | #else |
| 164 | snprintf(buf, len, "%lf", val); |
Paul Cercueil | 225a3e1 | 2015-03-03 18:08:41 +0100 | [diff] [blame] | 165 | return 0; |
Paul Cercueil | 24a6212 | 2015-02-09 17:53:14 +0100 | [diff] [blame] | 166 | #endif |
Paul Cercueil | 542cbb4 | 2014-10-21 13:00:38 +0200 | [diff] [blame] | 167 | } |
Paul Cercueil | 53ed943 | 2014-11-20 13:46:04 +0100 | [diff] [blame] | 168 | |
| 169 | void iio_library_get_version(unsigned int *major, |
| 170 | unsigned int *minor, char git_tag[8]) |
| 171 | { |
| 172 | if (major) |
| 173 | *major = LIBIIO_VERSION_MAJOR; |
| 174 | if (minor) |
| 175 | *minor = LIBIIO_VERSION_MINOR; |
| 176 | if (git_tag) { |
| 177 | strncpy(git_tag, LIBIIO_VERSION_GIT, 8); |
| 178 | git_tag[7] = '\0'; |
| 179 | } |
| 180 | } |