[winpr,str] add winpr_vasprintf

This commit is contained in:
Armin Novak 2023-11-02 14:12:52 +01:00 committed by akallabeth
parent 7d02a97a88
commit f1ddc19806
2 changed files with 14 additions and 4 deletions

View File

@ -40,6 +40,7 @@ extern "C"
const char* separator); const char* separator);
WINPR_API int winpr_asprintf(char** s, size_t* slen, const char* templ, ...); WINPR_API int winpr_asprintf(char** s, size_t* slen, const char* templ, ...);
WINPR_API int winpr_vasprintf(char** s, size_t* slen, const char* templ, va_list ap);
#ifndef _WIN32 #ifndef _WIN32

View File

@ -180,12 +180,21 @@ int winpr_asprintf(char** s, size_t* slen, WINPR_FORMAT_ARG const char* templ, .
{ {
va_list ap; va_list ap;
WINPR_ASSERT(s); va_start(ap, templ);
WINPR_ASSERT(slen); int rc = winpr_vasprintf(s, slen, templ, ap);
va_end(ap);
return rc;
}
WINPR_ATTR_FORMAT_ARG(3, 0)
int winpr_vasprintf(char** s, size_t* slen, WINPR_FORMAT_ARG const char* templ, va_list oap)
{
va_list ap;
*s = NULL; *s = NULL;
*slen = 0; *slen = 0;
va_start(ap, templ); va_copy(ap, oap);
const int length = vsnprintf(NULL, 0, templ, ap); const int length = vsnprintf(NULL, 0, templ, ap);
va_end(ap); va_end(ap);
if (length < 0) if (length < 0)
@ -195,7 +204,7 @@ int winpr_asprintf(char** s, size_t* slen, WINPR_FORMAT_ARG const char* templ, .
if (!str) if (!str)
return -1; return -1;
va_start(ap, templ); va_copy(ap, oap);
const int plen = vsprintf(str, templ, ap); const int plen = vsprintf(str, templ, ap);
va_end(ap); va_end(ap);