all repos — mgba @ 61ddffbcae2b1b213c13811754121e831608dc78

mGBA Game Boy Advance Emulator

src/util/formatting.c (view raw)

 1/* Copyright (c) 2013-2015 Jeffrey Pfau
 2 *
 3 * This Source Code Form is subject to the terms of the Mozilla Public
 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 6#include "formatting.h"
 7
 8#include <float.h>
 9
10int ftostr_l(char* restrict str, size_t size, float f, locale_t locale) {
11#ifdef HAVE_SNPRINTF_L
12	return snprintf_l(str, size, locale, "%*.g", FLT_DIG, f);
13#elif defined(HAVE_LOCALE)
14	locale_t old = uselocale(locale);
15	int res = snprintf(str, size, "%*.g", FLT_DIG, f);
16	uselocale(old);
17	return res;
18#else
19	char* old = setlocale(LC_NUMERIC, locale);
20	int res = snprintf(str, size, "%*.g", FLT_DIG, f);
21	setlocale(LC_NUMERIC, old);
22	return res;
23#endif
24}
25
26#ifndef HAVE_STRTOF_L
27float strtof_l(const char* restrict str, char** restrict end, locale_t locale) {
28#ifdef HAVE_LOCALE
29	locale_t old = uselocale(locale);
30	float res = strtof(str, end);
31	uselocale(old);
32	return res;
33#else
34	char* old = setlocale(LC_NUMERIC, locale);
35	float res = strtof(str, end);
36	setlocale(LC_NUMERIC, old);
37	return res;
38#endif
39}
40#endif
41
42int ftostr_u(char* restrict str, size_t size, float f) {
43#if HAVE_LOCALE
44	locale_t l = newlocale(LC_NUMERIC_MASK, "C", 0);
45#else
46	locale_t l = "C";
47#endif
48	int res = ftostr_l(str, size, f, l);
49#if HAVE_LOCALE
50	freelocale(l);
51#endif
52	return res;
53}
54
55float strtof_u(const char* restrict str, char** restrict end) {
56#if HAVE_LOCALE
57	locale_t l = newlocale(LC_NUMERIC_MASK, "C", 0);
58#else
59	locale_t l = "C";
60#endif
61	float res = strtof_l(str, end, l);
62#if HAVE_LOCALE
63	freelocale(l);
64#endif
65	return res;
66}