all repos — mgba @ a58458b9438354970adb306d79caa53735c90807

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
 76#ifdef PSP2
 77#include <psp2/rtc.h>
 78#endif
 79
 80struct tm* localtime_r(const time_t* t, struct tm* date) {
 81#ifdef _WIN32
 82	localtime_s(date, t);
 83	return date;
 84#elif defined(PSP2)
 85	SceRtcTime sceRtc;
 86	sceRtcSetTime_t(&sceRtc, *t);
 87	date->tm_year = sceRtc.year;
 88	date->tm_mon = sceRtc.month;
 89	date->tm_mday = sceRtc.day;
 90	date->tm_hour = sceRtc.hour;
 91	date->tm_min = sceRtc.minutes;
 92	date->tm_sec = sceRtc.seconds;
 93	date->tm_wday = sceRtcGetDayOfWeek(sceRtc.year, sceRtc.month, sceRtc.day);
 94	return date;
 95#else
 96#warning localtime_r not emulated on this platform
 97	return 0;
 98#endif
 99}
100#endif