blob: 6bd226457218d577326c8877874b80727b992c27 [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>
Paul Cercueil53ed9432014-11-20 13:46:04 +010025#include <string.h>
Paul Cercueil542cbb42014-10-21 13:00:38 +020026
Paul Cercueil2812f502014-11-05 14:40:56 +010027#define LOCALE_SUPPORT defined(_WIN32) || (defined(__USE_XOPEN2K8) && \
28 (!defined(__UCLIBC__) || defined(__UCLIBC_HAS_LOCALE__)))
29
Paul Cercueil542cbb42014-10-21 13:00:38 +020030int read_double(const char *str, double *val)
31{
32 double value;
33 char *end;
Paul Cercueil2812f502014-11-05 14:40:56 +010034#if LOCALE_SUPPORT == 1
Paul Cercueil542cbb42014-10-21 13:00:38 +020035#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 Cercueil2812f502014-11-05 14:40:56 +010048#endif
Paul Cercueil542cbb42014-10-21 13:00:38 +020049
50 value = strtod(str, &end);
51
Paul Cercueil2812f502014-11-05 14:40:56 +010052#if LOCALE_SUPPORT == 1
Paul Cercueil542cbb42014-10-21 13:00:38 +020053#ifdef _WIN32
54 setlocale(old_locale);
55 _configurethreadlocale(config);
56#else
57 uselocale(old_locale);
58 freelocale(new_locale);
59#endif
Paul Cercueil2812f502014-11-05 14:40:56 +010060#endif
Paul Cercueil542cbb42014-10-21 13:00:38 +020061
62 if (end == str)
63 return -EINVAL;
64
65 *val = value;
66 return 0;
67}
68
69void write_double(char *buf, size_t len, double val)
70{
Paul Cercueil2812f502014-11-05 14:40:56 +010071#if LOCALE_SUPPORT == 1
Paul Cercueil542cbb42014-10-21 13:00:38 +020072#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 Cercueil2812f502014-11-05 14:40:56 +010085#endif
Paul Cercueil542cbb42014-10-21 13:00:38 +020086
87 snprintf(buf, len, "%lf", val);
88
Paul Cercueil2812f502014-11-05 14:40:56 +010089#if LOCALE_SUPPORT == 1
Paul Cercueil542cbb42014-10-21 13:00:38 +020090#ifdef _WIN32
91 setlocale(old_locale);
92 _configurethreadlocale(config);
93#else
94 uselocale(old_locale);
95 freelocale(new_locale);
96#endif
Paul Cercueil2812f502014-11-05 14:40:56 +010097#endif
Paul Cercueil542cbb42014-10-21 13:00:38 +020098}
Paul Cercueil53ed9432014-11-20 13:46:04 +010099
100void 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}