all repos — mgba @ bbc63a23924d7f24b162f9e94b1a5b07b1514baf

mGBA Game Boy Advance Emulator

Util: Add strdup implementation for platforms without it
Jeffrey Pfau jeffrey@endrift.com
Sun, 14 Jun 2015 23:25:49 -0700
commit

bbc63a23924d7f24b162f9e94b1a5b07b1514baf

parent

021ada03f0fc41e85d4f02d9476abb0cbccd98ca

3 files changed, 19 insertions(+), 0 deletions(-)

jump to
M CMakeLists.txtCMakeLists.txt

@@ -211,6 +211,7 @@ enable_language(ASM)

endif() include(CheckFunctionExists) +check_function_exists(strdup HAVE_STRDUP) check_function_exists(strndup HAVE_STRNDUP) check_function_exists(snprintf_l HAVE_SNPRINTF_L) if(CMAKE_SYSTEM_NAME STREQUAL "Linux")

@@ -222,6 +223,10 @@ endif()

check_function_exists(newlocale HAVE_NEWLOCALE) check_function_exists(freelocale HAVE_FREELOCALE) check_function_exists(uselocale HAVE_USELOCALE) + +if(HAVE_STRDUP) + add_definitions(-DHAVE_STRDUP) +endif() if(HAVE_STRNDUP) add_definitions(-DHAVE_STRNDUP)
M src/util/string.csrc/util/string.c

@@ -17,6 +17,16 @@ return out;

} #endif +#ifndef HAVE_STRDUP +char* strdup(const char* str) { + size_t len = strlen(str); + char* out = malloc(len + 1); + strncpy(out, str, len); + out[len] = '\0'; + return out; +} +#endif + char* strnrstr(const char* restrict haystack, const char* restrict needle, size_t len) { char* last = 0; const char* next = haystack;
M src/util/string.hsrc/util/string.h

@@ -13,6 +13,10 @@ // This is sometimes a macro

char* strndup(const char* start, size_t len); #endif +#ifndef strdup +char* strdup(const char* str); +#endif + char* strnrstr(const char* restrict s1, const char* restrict s2, size_t len); int utfcmp(const uint16_t* utf16, const char* utf8, size_t utf16Length, size_t utf8Length);