diff --git a/src/config.h b/src/config.h index e77089b68..d5ce58109 100644 --- a/src/config.h +++ b/src/config.h @@ -123,6 +123,10 @@ // - SUPPORT_FILEFORMAT_JPG #define SUPPORT_CLIPBOARD_IMAGE 1 #endif +#ifndef SUPPORT_POSIX_STRDUP + // Use posix strdup or built-in one + #define SUPPORT_POSIX_STRDUP 1 +#endif // rcore: Configuration values // NOTE: Below values are already defined inside [rcore.c] so there is no need to be diff --git a/src/raylib.h b/src/raylib.h index 154158131..33187a4be 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -1113,6 +1113,7 @@ RLAPI void UnloadRandomSequence(int *sequence); // Unload random values RLAPI void TakeScreenshot(const char *fileName); // Takes a screenshot of current screen (filename extension defines format) RLAPI void SetConfigFlags(unsigned int flags); // Set up init configuration flags (view FLAGS) RLAPI void OpenURL(const char *url); // Open URL with default system browser (if available) +RLAPI char *StringDuplicate(const char *string); // Duplicates string into a new copy (platform agnostic strdup) // Logging system RLAPI void SetTraceLogLevel(int logLevel); // Set the current threshold (minimum) log level diff --git a/src/rcore.c b/src/rcore.c index dd60ab731..4999f0bae 100644 --- a/src/rcore.c +++ b/src/rcore.c @@ -1853,6 +1853,33 @@ void SetConfigFlags(unsigned int flags) FLAG_SET(CORE.Window.flags, flags); } +// Duplicates string into a new one (platform agnostic strdup) +char *StringDuplicate(const char *string) +{ +#if SUPPORT_POSIX_STRDUP + #if defined(_WIN32) || defined(_MSC_VER) + return _strdup(string); + #else + return strdup(string); + #endif +#else + if (!string) + return NULL; + + // NOTE: the final string (newString) should be stringLength + 1 (e.g. strlen("hello") + 1 = 6). + // one extra char is required for the null-terminator ('\0'). + + size_t stringLength = strlen(string); + + char *newString = RL_CALLOC(stringLength + 1, sizeof(char)); + strncpy(newString, string, stringLength); + + newString[stringLength + 1] = '\0'; + + return newString; +#endif +} + //---------------------------------------------------------------------------------- // Module Functions Definition: Logging system //----------------------------------------------------------------------------------