Util: Add strdup implementation for platforms without it

This commit is contained in:
Jeffrey Pfau 2015-06-14 23:25:49 -07:00
parent 021ada03f0
commit bbc63a2392
3 changed files with 19 additions and 0 deletions

View File

@ -211,6 +211,7 @@ if(CMAKE_SYSTEM_PROCESSOR MATCHES "arm.*")
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")
@ -223,6 +224,10 @@ 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)
endif()

View File

@ -17,6 +17,16 @@ char* strndup(const char* start, size_t len) {
}
#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;

View File

@ -13,6 +13,10 @@
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);