6 #if defined(_MSC_VER) || defined(__DMC__)
28 #if defined(__clang__)
29 #pragma GCC diagnostic push
30 #pragma GCC diagnostic ignored "-Wlanguage-extension-token"
31 #pragma GCC diagnostic ignored "-Wlong-long"
32 #pragma GCC diagnostic ignored "-Wc++11-long-long"
42 #if defined(__clang__)
43 #pragma GCC diagnostic pop
67 #elif defined(__GNUC__)
88 #define DR_SIZE_MAX SIZE_MAX
90 #define DR_SIZE_MAX 0xFFFFFFFF
101 int dr_strcpy_s(
char* dst,
size_t dstSizeInBytes,
const char* src)
108 if (dstSizeInBytes == 0) {
116 for (i = 0; i < dstSizeInBytes && src[i] !=
'\0'; ++i) {
120 if (i < dstSizeInBytes) {
137 if (dstSizeInBytes == 0) {
146 if (
count == ((
size_t)-1) ||
count >= dstSizeInBytes) {
147 maxcount = dstSizeInBytes - 1;
150 for (i = 0; i < maxcount && src[i] !=
'\0'; ++i) {
154 if (src[i] ==
'\0' || i ==
count ||
count == ((
size_t)-1)) {
163 int dr_strcat_s(
char* dst,
size_t dstSizeInBytes,
const char* src)
170 if (dstSizeInBytes == 0) {
180 while (dstSizeInBytes > 0 && dst[0] !=
'\0') {
185 if (dstSizeInBytes == 0) {
190 while (dstSizeInBytes > 0 && src[0] !=
'\0') {
195 if (dstSizeInBytes > 0) {
212 if (dstSizeInBytes == 0) {
221 while (dstSizeInBytes > 0 && dst[0] !=
'\0') {
226 if (dstSizeInBytes == 0) {
231 if (
count == ((
size_t)-1)) {
232 count = dstSizeInBytes - 1;
235 while (dstSizeInBytes > 0 && src[0] !=
'\0' &&
count > 0) {
241 if (dstSizeInBytes > 0) {
254 int dr_append_path(
char* dst,
size_t dstSize,
const char* base,
const char* other)
263 if (base ==
NULL || other ==
NULL) {
275 if (dst[len-1] !=
'/' && dst[len-1] !=
'\\') {
298 const char* fileName = path;
305 while (path[0] !=
'\0') {
306 if (path[0] ==
'/' || path[0] ==
'\\') {
314 while (fileName[0] !=
'\0' && (fileName[0] ==
'/' || fileName[0] ==
'\\')) {
323 const char* extension = path;
324 const char* lastoccurance =
NULL;
331 while (extension[0] !=
'\0') {
332 if (extension[0] ==
'.') {
334 lastoccurance = extension;
340 return (lastoccurance != 0) ? lastoccurance : extension;
348 if (path ==
NULL || extension ==
NULL) {
355 #if defined(_MSC_VER) || defined(__DMC__)
358 return strcasecmp(ext1, ext2) == 0;
380 char folderPath[256];
381 char relativePath[256];
382 char absolutePath[256];
394 char searchQuery[MAX_PATH];
395 unsigned int searchQueryLength;
396 WIN32_FIND_DATAA ffd;
402 if (pState ==
NULL) {
406 memset(pState, 0,
sizeof(*pState));
408 if (pFolderPath ==
NULL) {
413 dr_strcpy_s(searchQuery,
sizeof(searchQuery), pFolderPath);
415 searchQueryLength = (
unsigned int)strlen(searchQuery);
416 if (searchQueryLength >= MAX_PATH - 3) {
420 searchQuery[searchQueryLength + 0] =
'\\';
421 searchQuery[searchQueryLength + 1] =
'*';
422 searchQuery[searchQueryLength + 2] =
'\0';
424 hFind = FindFirstFileA(searchQuery, &ffd);
425 if (hFind == INVALID_HANDLE_VALUE) {
430 while (strcmp(ffd.cFileName,
".") == 0 || strcmp(ffd.cFileName,
"..") == 0) {
431 if (!FindNextFileA(hFind, &ffd)) {
437 pState->hFind = hFind;
440 if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
448 dir = opendir(pFolderPath);
456 struct stat fileinfo;
465 if (strcmp(info->d_name,
".") == 0 || strcmp(info->d_name,
"..") == 0) {
472 if (stat(filePath, &fileinfo) != 0) {
476 if (S_ISDIR(fileinfo.st_mode)) {
498 WIN32_FIND_DATAA ffd;
501 if (pState ==
NULL) {
506 if (!FindNextFileA(pState->hFind, &ffd)) {
508 FindClose(pState->hFind);
509 pState->hFind =
NULL;
514 if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
525 struct stat fileinfo;
528 info = readdir(pState->
dir);
530 closedir(pState->
dir);
535 if (strcmp(info->d_name,
".") == 0 || strcmp(info->d_name,
"..") == 0) {
544 if (stat(filePath, &fileinfo) != 0) {
548 if (S_ISDIR(fileinfo.st_mode)) {
565 if (pState ==
NULL) {
570 FindClose(pState->hFind);
571 pState->hFind =
NULL;
573 closedir(pState->
dir);
584 static int dr_fopen(FILE** ppFile,
const char* pFilePath,
const char* pOpenMode)
586 #if defined(_MSC_VER) && _MSC_VER >= 1400
590 if (ppFile !=
NULL) {
594 if (pFilePath ==
NULL || pOpenMode ==
NULL || ppFile ==
NULL) {
598 #if defined(_MSC_VER) && _MSC_VER >= 1400
599 err = fopen_s(ppFile, pFilePath, pOpenMode);
604 #if defined(_WIN32) || defined(__APPLE__)
605 *ppFile = fopen(pFilePath, pOpenMode);
607 #if defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS == 64 && defined(_LARGEFILE64_SOURCE)
608 *ppFile = fopen64(pFilePath, pOpenMode);
610 *ppFile = fopen(pFilePath, pOpenMode);
613 if (*ppFile ==
NULL) {
633 if (pFilePath ==
NULL) {
639 if (
dr_fopen(&pFile, pFilePath,
"rb") != 0) {
643 fseek(pFile, 0, SEEK_END);
644 fileSize = ftell(pFile);
645 fseek(pFile, 0, SEEK_SET);
653 pFileData = malloc((
size_t)fileSize + extraBytes);
654 if (pFileData ==
NULL) {
659 bytesRead = fread(pFileData, 1, (
size_t)fileSize, pFile);
660 if (bytesRead != fileSize) {
669 *pFileSizeOut = (size_t)fileSize;
684 for (iarg = 0; iarg < argc; ++iarg) {
685 if (strcmp(
argv[iarg], value) == 0) {
704 if ((
unsigned int)width >
sizeof(buffer)) {
709 #if (defined(_MSC_VER) && _MSC_VER > 1200) || ((defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 500) || defined(_ISOC99_SOURCE) || (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L))
710 len = vsnprintf(buffer, width+1, format,
args);
712 len = vsprintf(buffer, format,
args);
720 printf(
"%s", buffer);
721 for (i = len; i < width; ++i) {
734 va_start(
args, format);
748 for (i = 0; i < margin; ++i) {
760 va_start(
args, format);
772 static LARGE_INTEGER g_DRTimerFrequency = {{0}};
775 LARGE_INTEGER counter;
777 if (g_DRTimerFrequency.QuadPart == 0) {
778 QueryPerformanceFrequency(&g_DRTimerFrequency);
781 QueryPerformanceCounter(&counter);
783 return counter.QuadPart / (double)g_DRTimerFrequency.QuadPart;
786 #if _POSIX_C_SOURCE >= 199309L
787 #if defined(CLOCK_MONOTONIC)
788 #define MA_CLOCK_ID CLOCK_MONOTONIC
790 #define MA_CLOCK_ID CLOCK_REALTIME
794 struct timespec newTime;
795 clock_gettime(CLOCK_MONOTONIC, &newTime);
797 return ((newTime.tv_sec * 1000000000LL) + newTime.tv_nsec) / 1000000000.0;
802 struct timeval newTime;
803 gettimeofday(&newTime,
NULL);
805 return ((newTime.tv_sec * 1000000) + newTime.tv_usec) / 1000000.0;
813 return lo + x*(hi-lo);
817 #define DR_LCG_M 2147483647
818 #define DR_LCG_A 48271
866 return lo +
dr_rand_u32() / (0xFFFFFFFF / (hi - lo + 1) + 1);
882 float* dst_f32 = (
float*)dst;
886 for (i = 0; i <
count; i += 1) {
887 double x = src_s32[i];
890 x = x + 2147483648.0;
891 x = x * 0.0000000004656612873077392578125;
894 x = x / 2147483648.0;
897 dst_f32[i] = (float)x;
907 for (i = 0; i <
count; i += 1) {
922 handle = (
dr_handle)LoadLibraryA(filename);
924 handle = (
dr_handle)dlopen(filename, RTLD_NOW);
933 FreeLibrary((HMODULE)handle);
935 dlclose((
void*)handle);
944 proc = (
dr_proc)GetProcAddress((HMODULE)handle, symbol);
946 #if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
947 #pragma GCC diagnostic push
948 #pragma GCC diagnostic ignored "-Wpedantic"
950 proc = (
dr_proc)dlsym((
void*)handle, symbol);
951 #if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
952 #pragma GCC diagnostic pop