2021-02-21 06:58:40 +00:00
|
|
|
#include "rc_compat.h"
|
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
2023-08-09 09:39:42 +00:00
|
|
|
#ifdef RC_C89_HELPERS
|
|
|
|
|
2021-02-21 06:58:40 +00:00
|
|
|
int rc_strncasecmp(const char* left, const char* right, size_t length)
|
|
|
|
{
|
|
|
|
while (length)
|
|
|
|
{
|
|
|
|
if (*left != *right)
|
|
|
|
{
|
|
|
|
const int diff = tolower(*left) - tolower(*right);
|
|
|
|
if (diff != 0)
|
|
|
|
return diff;
|
|
|
|
}
|
|
|
|
|
|
|
|
++left;
|
|
|
|
++right;
|
|
|
|
--length;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int rc_strcasecmp(const char* left, const char* right)
|
|
|
|
{
|
|
|
|
while (*left || *right)
|
|
|
|
{
|
|
|
|
if (*left != *right)
|
|
|
|
{
|
|
|
|
const int diff = tolower(*left) - tolower(*right);
|
|
|
|
if (diff != 0)
|
|
|
|
return diff;
|
|
|
|
}
|
|
|
|
|
|
|
|
++left;
|
|
|
|
++right;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
char* rc_strdup(const char* str)
|
|
|
|
{
|
|
|
|
const size_t length = strlen(str);
|
|
|
|
char* buffer = (char*)malloc(length + 1);
|
2023-08-09 09:39:42 +00:00
|
|
|
if (buffer)
|
|
|
|
memcpy(buffer, str, length + 1);
|
2021-02-21 06:58:40 +00:00
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
int rc_snprintf(char* buffer, size_t size, const char* format, ...)
|
|
|
|
{
|
2023-08-09 09:39:42 +00:00
|
|
|
int result;
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
va_start(args, format);
|
|
|
|
|
|
|
|
#ifdef __STDC_WANT_SECURE_LIB__
|
|
|
|
result = vsprintf_s(buffer, size, format, args);
|
|
|
|
#else
|
|
|
|
/* assume buffer is large enough and ignore size */
|
|
|
|
(void)size;
|
|
|
|
result = vsprintf(buffer, format, args);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
2021-02-21 06:58:40 +00:00
|
|
|
|
2023-08-09 09:39:42 +00:00
|
|
|
#ifndef __STDC_WANT_SECURE_LIB__
|
2021-02-21 06:58:40 +00:00
|
|
|
|
2023-08-09 09:39:42 +00:00
|
|
|
struct tm* rc_gmtime_s(struct tm* buf, const time_t* timer)
|
|
|
|
{
|
|
|
|
struct tm* tm = gmtime(timer);
|
|
|
|
memcpy(buf, tm, sizeof(*tm));
|
|
|
|
return buf;
|
2021-02-21 06:58:40 +00:00
|
|
|
}
|
2023-08-09 09:39:42 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef RC_NO_THREADS
|
2024-02-24 04:52:57 +00:00
|
|
|
|
|
|
|
#if defined(_WIN32)
|
2023-08-09 09:39:42 +00:00
|
|
|
|
|
|
|
/* https://gist.github.com/roxlu/1c1af99f92bafff9d8d9 */
|
|
|
|
|
|
|
|
#define WIN32_LEAN_AND_MEAN
|
|
|
|
#include <windows.h>
|
|
|
|
|
|
|
|
void rc_mutex_init(rc_mutex_t* mutex)
|
|
|
|
{
|
|
|
|
/* default security, not owned by calling thread, unnamed */
|
|
|
|
mutex->handle = CreateMutex(NULL, FALSE, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void rc_mutex_destroy(rc_mutex_t* mutex)
|
|
|
|
{
|
|
|
|
CloseHandle(mutex->handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
void rc_mutex_lock(rc_mutex_t* mutex)
|
|
|
|
{
|
|
|
|
WaitForSingleObject(mutex->handle, 0xFFFFFFFF);
|
|
|
|
}
|
|
|
|
|
|
|
|
void rc_mutex_unlock(rc_mutex_t* mutex)
|
|
|
|
{
|
|
|
|
ReleaseMutex(mutex->handle);
|
|
|
|
}
|
|
|
|
|
2024-02-24 04:52:57 +00:00
|
|
|
#elif defined(GEKKO)
|
|
|
|
|
|
|
|
/* https://github.com/libretro/RetroArch/pull/16116 */
|
|
|
|
|
|
|
|
void rc_mutex_init(rc_mutex_t* mutex)
|
|
|
|
{
|
|
|
|
LWP_MutexInit(mutex, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void rc_mutex_destroy(rc_mutex_t* mutex)
|
|
|
|
{
|
|
|
|
LWP_MutexDestroy(mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
void rc_mutex_lock(rc_mutex_t* mutex)
|
|
|
|
{
|
|
|
|
LWP_MutexLock(mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
void rc_mutex_unlock(rc_mutex_t* mutex)
|
|
|
|
{
|
|
|
|
LWP_MutexUnlock(mutex);
|
|
|
|
}
|
|
|
|
|
2023-08-09 09:39:42 +00:00
|
|
|
#else
|
|
|
|
|
|
|
|
void rc_mutex_init(rc_mutex_t* mutex)
|
|
|
|
{
|
|
|
|
pthread_mutex_init(mutex, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void rc_mutex_destroy(rc_mutex_t* mutex)
|
|
|
|
{
|
|
|
|
pthread_mutex_destroy(mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
void rc_mutex_lock(rc_mutex_t* mutex)
|
|
|
|
{
|
|
|
|
pthread_mutex_lock(mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
void rc_mutex_unlock(rc_mutex_t* mutex)
|
|
|
|
{
|
|
|
|
pthread_mutex_unlock(mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
#endif /* RC_NO_THREADS */
|