all repos — mgba @ 50402c830729f2ba5a6fc3e6facfd8b258f7f97d

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#include <time.h>
10
11int ftostr_l(char* restrict str, size_t size, float f, locale_t locale) {
12#ifdef HAVE_SNPRINTF_L
13	return snprintf_l(str, size, locale, "%.*g", FLT_DIG, f);
14#elif defined(HAVE_LOCALE)
15	locale_t old = uselocale(locale);
16	int res = snprintf(str, size, "%.*g", FLT_DIG, f);
17	uselocale(old);
18	return res;
19#elif defined(HAVE_SETLOCALE)
20	char* old = setlocale(LC_NUMERIC, locale);
21	int res = snprintf(str, size, "%.*g", FLT_DIG, f);
22	setlocale(LC_NUMERIC, old);
23	return res;
24#else
25	UNUSED(locale);
26	return snprintf(str, size, "%.*g", FLT_DIG, f);
27#endif
28}
29
30#ifndef HAVE_STRTOF_L
31float strtof_l(const char* restrict str, char** restrict end, locale_t locale) {
32#ifdef HAVE_LOCALE
33	locale_t old = uselocale(locale);
34	float res = strtof(str, end);
35	uselocale(old);
36	return res;
37#elif defined(HAVE_SETLOCALE)
38	char* old = setlocale(LC_NUMERIC, locale);
39	float res = strtof(str, end);
40	setlocale(LC_NUMERIC, old);
41	return res;
42#else
43	UNUSED(locale);
44	return strtof(str, end);
45#endif
46}
47#endif
48
49int ftostr_u(char* restrict str, size_t size, float f) {
50#if HAVE_LOCALE
51	locale_t l = newlocale(LC_NUMERIC_MASK, "C", 0);
52#else
53	locale_t l = "C";
54#endif
55	int res = ftostr_l(str, size, f, l);
56#if HAVE_LOCALE
57	freelocale(l);
58#endif
59	return res;
60}
61
62float strtof_u(const char* restrict str, char** restrict end) {
63#if HAVE_LOCALE
64	locale_t l = newlocale(LC_NUMERIC_MASK, "C", 0);
65#else
66	locale_t l = "C";
67#endif
68	float res = strtof_l(str, end, l);
69#if HAVE_LOCALE
70	freelocale(l);
71#endif
72	return res;
73}
74
75#ifndef HAVE_LOCALTIME_R
76struct tm* localtime_r(const time_t* t, struct tm* date) {
77#ifdef _WIN32
78	localtime_s(date, t);
79	return date;
80#elif defined(PSP2)
81	return sceKernelLibcLocaltime_r(t, date);
82#else
83#warning localtime_r not emulated on this platform
84	return 0;
85#endif
86}
87#endif