[winpr,string] add wcsndup

This commit is contained in:
akallabeth
2024-11-07 10:56:49 +01:00
parent 3a1251fa0e
commit d070857c03
2 changed files with 26 additions and 0 deletions

View File

@@ -427,9 +427,23 @@ extern "C"
WINPR_API INT64 GetLine(char** lineptr, size_t* size, FILE* stream);
#if !defined(WINPR_HAVE_STRNDUP)
WINPR_ATTR_MALLOC(free, 1)
WINPR_API char* strndup(const char* s, size_t n);
#endif
/** @brief WCHAR version of \b strndup
* creates a copy of a \b WCHAR string of \b n characters length. The copy will always be \b \0
* terminated
*
* @param s The \b WCHAR string to copy
* @param n The numer of WCHAR to copy
*
* @return An allocated copy of \b s, always \b \0 terminated
* @since version 3.10.0
*/
WINPR_ATTR_MALLOC(free, 1)
WINPR_API WCHAR* wcsndup(const WCHAR* s, size_t n);
#ifdef __cplusplus
}
#endif

View File

@@ -834,3 +834,15 @@ const WCHAR* InitializeConstWCharFromUtf8(const char* str, WCHAR* buffer, size_t
(void)ConvertUtf8ToWChar(str, buffer, len);
return buffer;
}
WCHAR* wcsndup(const WCHAR* s, size_t n)
{
if (!s)
return NULL;
WCHAR* copy = calloc(n + 1, sizeof(WCHAR));
if (!copy)
return NULL;
memcpy(copy, s, n * sizeof(WCHAR));
return copy;
}