all repos — mgba @ bcf6e5879be2b03b062d9e6f83185e76072c51df

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#elif defined(HAVE_SETLOCALE)
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#else
24	UNUSED(locale);
25	return snprintf(str, size, "%.*g", FLT_DIG, f);
26#endif
27}
28
29#ifndef HAVE_STRTOF_L
30float strtof_l(const char* restrict str, char** restrict end, locale_t locale) {
31#ifdef HAVE_LOCALE
32	locale_t old = uselocale(locale);
33	float res = strtof(str, end);
34	uselocale(old);
35	return res;
36#elif defined(HAVE_SETLOCALE)
37	char* old = setlocale(LC_NUMERIC, locale);
38	float res = strtof(str, end);
39	setlocale(LC_NUMERIC, old);
40	return res;
41#else
42	UNUSED(locale);
43	return strtof(str, end);
44#endif
45}
46#endif
47
48int ftostr_u(char* restrict str, size_t size, float f) {
49#if HAVE_LOCALE
50	locale_t l = newlocale(LC_NUMERIC_MASK, "C", 0);
51#else
52	locale_t l = "C";
53#endif
54	int res = ftostr_l(str, size, f, l);
55#if HAVE_LOCALE
56	freelocale(l);
57#endif
58	return res;
59}
60
61float strtof_u(const char* restrict str, char** restrict end) {
62#if HAVE_LOCALE
63	locale_t l = newlocale(LC_NUMERIC_MASK, "C", 0);
64#else
65	locale_t l = "C";
66#endif
67	float res = strtof_l(str, end, l);
68#if HAVE_LOCALE
69	freelocale(l);
70#endif
71	return res;
72}
73
74#ifndef HAVE_LOCALTIME_R
75#ifdef PSP2
76#include <psp2/rtc.h>
77#endif
78
79struct tm* localtime_r(const time_t* t, struct tm* date) {
80#ifdef _WIN32
81	localtime_s(date, t);
82	return date;
83#elif defined(PSP2)
84	SceRtcTime sceRtc;
85	sceRtcSetTime_t(&sceRtc, *t);
86	date->tm_year = sceRtc.year;
87	date->tm_mon = sceRtc.month;
88	date->tm_mday = sceRtc.day;
89	date->tm_hour = sceRtc.hour;
90	date->tm_min = sceRtc.minutes;
91	date->tm_sec = sceRtc.seconds;
92	date->tm_wday = sceRtcGetDayOfWeek(sceRtc.year, sceRtc.month, sceRtc.day);
93	return date;
94#else
95#warning localtime_r not emulated on this platform
96	return 0;
97#endif
98}
99#endif