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> |
Romain Roffé | cead1da | 2015-06-30 13:35:51 +0200 | [diff] [blame] | 22 | #include <fcntl.h> |
Paul Cercueil | 542cbb4 | 2014-10-21 13:00:38 +0200 | [diff] [blame] | 23 | #include <locale.h> |
| 24 | #include <stdio.h> |
| 25 | #include <stdlib.h> |
Paul Cercueil | 53ed943 | 2014-11-20 13:46:04 +0100 | [diff] [blame] | 26 | #include <string.h> |
Paul Cercueil | 542cbb4 | 2014-10-21 13:00:38 +0200 | [diff] [blame] | 27 | |
Paul Cercueil | 24a6212 | 2015-02-09 17:53:14 +0100 | [diff] [blame] | 28 | #if defined(_WIN32) || (defined(__USE_XOPEN2K8) && \ |
Paul Cercueil | 2812f50 | 2014-11-05 14:40:56 +0100 | [diff] [blame] | 29 | (!defined(__UCLIBC__) || defined(__UCLIBC_HAS_LOCALE__))) |
Paul Cercueil | 24a6212 | 2015-02-09 17:53:14 +0100 | [diff] [blame] | 30 | #define LOCALE_SUPPORT |
| 31 | #endif |
Paul Cercueil | 2812f50 | 2014-11-05 14:40:56 +0100 | [diff] [blame] | 32 | |
Paul Cercueil | 24a6212 | 2015-02-09 17:53:14 +0100 | [diff] [blame] | 33 | #ifdef LOCALE_SUPPORT |
Paul Cercueil | 4202e5f | 2015-03-03 18:14:46 +0100 | [diff] [blame] | 34 | #if defined(__MINGW32__) |
| 35 | static int read_double_locale(const char *str, double *val) |
| 36 | { |
| 37 | char *end, *old_locale; |
| 38 | double value; |
| 39 | |
| 40 | /* XXX: This is not thread-safe, but it's the only way we have to |
| 41 | * support locales under MinGW without linking with Visual Studio |
| 42 | * libraries. */ |
| 43 | old_locale = strdup(setlocale(LC_NUMERIC, NULL)); |
| 44 | if (!old_locale) |
| 45 | return -ENOMEM; |
| 46 | |
| 47 | setlocale(LC_NUMERIC, "C"); |
| 48 | value = strtod(str, &end); |
| 49 | setlocale(LC_NUMERIC, old_locale); |
| 50 | free(old_locale); |
| 51 | |
| 52 | if (end == str) |
| 53 | return -EINVAL; |
| 54 | |
| 55 | *val = value; |
| 56 | return 0; |
| 57 | } |
| 58 | |
| 59 | static int write_double_locale(char *buf, size_t len, double val) |
| 60 | { |
| 61 | /* XXX: Not thread-safe, see above */ |
| 62 | char *old_locale = strdup(setlocale(LC_NUMERIC, NULL)); |
| 63 | if (!old_locale) |
| 64 | return -ENOMEM; |
| 65 | |
| 66 | setlocale(LC_NUMERIC, "C"); |
Paul Cercueil | 972a496 | 2015-03-26 13:21:01 +0100 | [diff] [blame] | 67 | snprintf(buf, len, "%f", val); |
Paul Cercueil | 4202e5f | 2015-03-03 18:14:46 +0100 | [diff] [blame] | 68 | setlocale(LC_NUMERIC, old_locale); |
| 69 | free(old_locale); |
| 70 | return 0; |
| 71 | } |
| 72 | #elif defined(_WIN32) |
Paul Cercueil | 24a6212 | 2015-02-09 17:53:14 +0100 | [diff] [blame] | 73 | static int read_double_locale(const char *str, double *val) |
Paul Cercueil | 542cbb4 | 2014-10-21 13:00:38 +0200 | [diff] [blame] | 74 | { |
Paul Cercueil | 542cbb4 | 2014-10-21 13:00:38 +0200 | [diff] [blame] | 75 | char *end; |
Paul Cercueil | 24a6212 | 2015-02-09 17:53:14 +0100 | [diff] [blame] | 76 | double value; |
Paul Cercueil | 77cbc4b | 2015-03-03 18:10:37 +0100 | [diff] [blame] | 77 | _locale_t locale = _create_locale(LC_NUMERIC, "C"); |
Paul Cercueil | 225a3e1 | 2015-03-03 18:08:41 +0100 | [diff] [blame] | 78 | if (!locale) |
| 79 | return -ENOMEM; |
Paul Cercueil | 542cbb4 | 2014-10-21 13:00:38 +0200 | [diff] [blame] | 80 | |
Paul Cercueil | 24a6212 | 2015-02-09 17:53:14 +0100 | [diff] [blame] | 81 | value = _strtod_l(str, &end, locale); |
| 82 | _free_locale(locale); |
Paul Cercueil | 542cbb4 | 2014-10-21 13:00:38 +0200 | [diff] [blame] | 83 | |
| 84 | if (end == str) |
| 85 | return -EINVAL; |
| 86 | |
| 87 | *val = value; |
| 88 | return 0; |
| 89 | } |
| 90 | |
Paul Cercueil | 225a3e1 | 2015-03-03 18:08:41 +0100 | [diff] [blame] | 91 | static int write_double_locale(char *buf, size_t len, double val) |
Paul Cercueil | 542cbb4 | 2014-10-21 13:00:38 +0200 | [diff] [blame] | 92 | { |
Paul Cercueil | 77cbc4b | 2015-03-03 18:10:37 +0100 | [diff] [blame] | 93 | _locale_t locale = _create_locale(LC_NUMERIC, "C"); |
Paul Cercueil | 225a3e1 | 2015-03-03 18:08:41 +0100 | [diff] [blame] | 94 | if (!locale) |
| 95 | return -ENOMEM; |
Paul Cercueil | 542cbb4 | 2014-10-21 13:00:38 +0200 | [diff] [blame] | 96 | |
Paul Cercueil | 972a496 | 2015-03-26 13:21:01 +0100 | [diff] [blame] | 97 | _snprintf_l(buf, len, "%f", locale, val); |
Paul Cercueil | 24a6212 | 2015-02-09 17:53:14 +0100 | [diff] [blame] | 98 | _free_locale(locale); |
Paul Cercueil | 225a3e1 | 2015-03-03 18:08:41 +0100 | [diff] [blame] | 99 | return 0; |
Paul Cercueil | 24a6212 | 2015-02-09 17:53:14 +0100 | [diff] [blame] | 100 | } |
Paul Cercueil | 542cbb4 | 2014-10-21 13:00:38 +0200 | [diff] [blame] | 101 | #else |
Paul Cercueil | 24a6212 | 2015-02-09 17:53:14 +0100 | [diff] [blame] | 102 | static int read_double_locale(const char *str, double *val) |
| 103 | { |
| 104 | char *end; |
| 105 | double value; |
Paul Cercueil | 542cbb4 | 2014-10-21 13:00:38 +0200 | [diff] [blame] | 106 | locale_t old_locale, new_locale; |
| 107 | |
Paul Cercueil | 77cbc4b | 2015-03-03 18:10:37 +0100 | [diff] [blame] | 108 | new_locale = newlocale(LC_NUMERIC_MASK, "C", (locale_t) 0); |
Paul Cercueil | 225a3e1 | 2015-03-03 18:08:41 +0100 | [diff] [blame] | 109 | if (!new_locale) |
| 110 | return -errno; |
| 111 | |
Paul Cercueil | 542cbb4 | 2014-10-21 13:00:38 +0200 | [diff] [blame] | 112 | old_locale = uselocale(new_locale); |
Paul Cercueil | 24a6212 | 2015-02-09 17:53:14 +0100 | [diff] [blame] | 113 | |
| 114 | value = strtod(str, &end); |
| 115 | uselocale(old_locale); |
| 116 | freelocale(new_locale); |
| 117 | |
| 118 | if (end == str) |
| 119 | return -EINVAL; |
| 120 | |
| 121 | *val = value; |
| 122 | return 0; |
| 123 | } |
| 124 | |
Paul Cercueil | 225a3e1 | 2015-03-03 18:08:41 +0100 | [diff] [blame] | 125 | static int write_double_locale(char *buf, size_t len, double val) |
Paul Cercueil | 24a6212 | 2015-02-09 17:53:14 +0100 | [diff] [blame] | 126 | { |
| 127 | locale_t old_locale, new_locale; |
| 128 | |
Paul Cercueil | 77cbc4b | 2015-03-03 18:10:37 +0100 | [diff] [blame] | 129 | new_locale = newlocale(LC_NUMERIC_MASK, "C", (locale_t) 0); |
Paul Cercueil | 225a3e1 | 2015-03-03 18:08:41 +0100 | [diff] [blame] | 130 | if (!new_locale) |
| 131 | return -errno; |
| 132 | |
Paul Cercueil | 24a6212 | 2015-02-09 17:53:14 +0100 | [diff] [blame] | 133 | old_locale = uselocale(new_locale); |
Paul Cercueil | 542cbb4 | 2014-10-21 13:00:38 +0200 | [diff] [blame] | 134 | |
Paul Cercueil | 972a496 | 2015-03-26 13:21:01 +0100 | [diff] [blame] | 135 | snprintf(buf, len, "%f", val); |
Paul Cercueil | 542cbb4 | 2014-10-21 13:00:38 +0200 | [diff] [blame] | 136 | |
Paul Cercueil | 542cbb4 | 2014-10-21 13:00:38 +0200 | [diff] [blame] | 137 | uselocale(old_locale); |
| 138 | freelocale(new_locale); |
Paul Cercueil | 225a3e1 | 2015-03-03 18:08:41 +0100 | [diff] [blame] | 139 | return 0; |
Paul Cercueil | 24a6212 | 2015-02-09 17:53:14 +0100 | [diff] [blame] | 140 | } |
Paul Cercueil | 542cbb4 | 2014-10-21 13:00:38 +0200 | [diff] [blame] | 141 | #endif |
Paul Cercueil | 2812f50 | 2014-11-05 14:40:56 +0100 | [diff] [blame] | 142 | #endif |
Paul Cercueil | 24a6212 | 2015-02-09 17:53:14 +0100 | [diff] [blame] | 143 | |
| 144 | int read_double(const char *str, double *val) |
| 145 | { |
| 146 | #ifdef LOCALE_SUPPORT |
| 147 | return read_double_locale(str, val); |
| 148 | #else |
| 149 | char *end; |
| 150 | double value = strtod(str, &end); |
| 151 | |
| 152 | if (end == str) |
| 153 | return -EINVAL; |
| 154 | |
| 155 | *val = value; |
| 156 | return 0; |
| 157 | #endif |
| 158 | } |
| 159 | |
Paul Cercueil | 225a3e1 | 2015-03-03 18:08:41 +0100 | [diff] [blame] | 160 | int write_double(char *buf, size_t len, double val) |
Paul Cercueil | 24a6212 | 2015-02-09 17:53:14 +0100 | [diff] [blame] | 161 | { |
| 162 | #ifdef LOCALE_SUPPORT |
Paul Cercueil | 225a3e1 | 2015-03-03 18:08:41 +0100 | [diff] [blame] | 163 | return write_double_locale(buf, len, val); |
Paul Cercueil | 24a6212 | 2015-02-09 17:53:14 +0100 | [diff] [blame] | 164 | #else |
Paul Cercueil | 972a496 | 2015-03-26 13:21:01 +0100 | [diff] [blame] | 165 | snprintf(buf, len, "%f", val); |
Paul Cercueil | 225a3e1 | 2015-03-03 18:08:41 +0100 | [diff] [blame] | 166 | return 0; |
Paul Cercueil | 24a6212 | 2015-02-09 17:53:14 +0100 | [diff] [blame] | 167 | #endif |
Paul Cercueil | 542cbb4 | 2014-10-21 13:00:38 +0200 | [diff] [blame] | 168 | } |
Paul Cercueil | 53ed943 | 2014-11-20 13:46:04 +0100 | [diff] [blame] | 169 | |
| 170 | void iio_library_get_version(unsigned int *major, |
| 171 | unsigned int *minor, char git_tag[8]) |
| 172 | { |
| 173 | if (major) |
| 174 | *major = LIBIIO_VERSION_MAJOR; |
| 175 | if (minor) |
| 176 | *minor = LIBIIO_VERSION_MINOR; |
| 177 | if (git_tag) { |
| 178 | strncpy(git_tag, LIBIIO_VERSION_GIT, 8); |
| 179 | git_tag[7] = '\0'; |
| 180 | } |
| 181 | } |
Paul Cercueil | 3fa3438 | 2015-05-22 10:54:28 +0200 | [diff] [blame] | 182 | |
| 183 | void iio_strerror(int err, char *buf, size_t len) |
| 184 | { |
| 185 | #ifdef _WIN32 |
| 186 | int ret = strerror_s(buf, len, err); |
Paul Cercueil | 3fa3438 | 2015-05-22 10:54:28 +0200 | [diff] [blame] | 187 | if (ret != 0) |
| 188 | snprintf(buf, len, "Unknown error %i", err); |
Paul Cercueil | 3a8fdb3 | 2015-06-29 17:44:02 +0200 | [diff] [blame] | 189 | #elif (_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && ! _GNU_SOURCE |
| 190 | int ret = strerror_r(err, buf, len); |
| 191 | if (ret != 0) |
| 192 | snprintf(buf, len, "Unknown error %i", err); |
| 193 | #else |
| 194 | char *str = strerror_r(err, buf, len); |
| 195 | if (str != buf) |
| 196 | strncpy(buf, str, len); |
| 197 | #endif |
Paul Cercueil | 3fa3438 | 2015-05-22 10:54:28 +0200 | [diff] [blame] | 198 | } |
Romain Roffé | cead1da | 2015-06-30 13:35:51 +0200 | [diff] [blame] | 199 | |
| 200 | int set_blocking_mode(int fd, bool blocking) |
| 201 | { |
| 202 | #ifndef _WIN32 |
| 203 | int ret = fcntl(fd, F_GETFL, 0); |
| 204 | if (ret < 0) |
| 205 | return -errno; |
| 206 | |
| 207 | if (blocking) |
| 208 | ret &= ~O_NONBLOCK; |
| 209 | else |
| 210 | ret |= O_NONBLOCK; |
| 211 | |
| 212 | ret = fcntl(fd, F_SETFL, ret); |
| 213 | return ret < 0 ? -errno : 0; |
| 214 | #else |
| 215 | return -ENOSYS; |
| 216 | #endif |
| 217 | } |