Why vsnwprintf missing


Why vsnwprintf missing



I using vsnprintf for ansi string and it works well, returns -1 only on error and returns count of needen size if it does not fit into buffer.
I need same function for wide strings, vsnwprintf just not exists. I also tried _vsnwprintf, but it acts really different, it returns -1 even if buffer too small, while I need to know how much to expand a buffer, but only when it's necessary, I don't want to make dummy calls just to determine needen size and only then expand a buffer.


vsnprintf


-1


vsnwprintf


_vsnwprintf



If look at vsnprintf code:


vsnprintf


int const _Result = __stdio_common_vsprintf(
_CRT_INTERNAL_LOCAL_PRINTF_OPTIONS | _CRT_INTERNAL_PRINTF_STANDARD_SNPRINTF_BEHAVIOR,
_Buffer, _BufferCount, _Format, NULL, _ArgList);
return _Result < 0 ? -1 : _Result;



While _vsnwprintf approximately has:


int const _Result = __stdio_common_vswprintf(
_CRT_INTERNAL_LOCAL_PRINTF_OPTIONS | _CRT_INTERNAL_PRINTF_LEGACY_VSPRINTF_NULL_TERMINATION,
_Buffer, _BufferCount, _Format, NULL, _ArgList);
return _Result < 0 ? -1 : _Result;



Looks like difference only in flags, I tried to call __stdio_common_vswprintf manually with _CRT_INTERNAL_PRINTF_STANDARD_SNPRINTF_BEHAVIOR flag, and it works exactly as I need, but also causing random crashes/freezes when application exiting. So I need vsnwprintf function, maybe someone know how to do it?


__stdio_common_vswprintf


_CRT_INTERNAL_PRINTF_STANDARD_SNPRINTF_BEHAVIOR


vsnwprintf



P.S. I using Microsoft Visual Studio 2017 on Windows 7.




2 Answers
2



The wide character version of vsnprintf is called vswprintf (note: no n). According to the documentation, it is compliant with ISO C so you can check the behaviour in the C Standard or here.


vsnprintf


vswprintf


n



It is correct that it returns -1 when the output would exceed the buffer size. There is no option to truncate output; you are supposed to make the "dummy call" to check buffer size.


-1



You could always wrap a vswprintf call in another function that has the behaviour you desire (make the dummy call and then either truncate to fit, or return a malloc'd buffer). Be ware of truncating in the middle of a UTF-16 character.


vswprintf



Note that MSVC's vsnprintf changed its behaviour at some point (2015 maybe?) to be ISO C compliant; the original MSVC behaviour can be retrieved by calling _vsnprintf.


vsnprintf


_vsnprintf





Thank you for reply. Is ISO C standart return -1 on short buffer, or it's only a MSVC behaviour?
– Stormgate Studio
Jul 2 at 13:43





@StormgateStudio it could be any negative number, according to the standard
– M.M
Jul 2 at 21:28



You can use _vsnwprintf(). Per the documentation for _vsnwprintf() (note the leading underscore):


_vsnwprintf()


_vsnwprintf()



...


int _vsnwprintf(
wchar_t *buffer,
size_t count,
const wchar_t *format,
va_list argptr
);



...



The value returned by all these functions does not include the
terminating null, whether one is written or not. When count is zero,
the value returned is the number of characters the functions would
write, not including any terminating null. You can use this result to
allocate sufficient buffer space for the string and its terminating
null, and then call the function again to fill the buffer.


count





Thank you for reply. As I said before I already tried _vsnwprintf function and it's returns -1 on short buffer, while I need same function as vsnprintf, but for wide characters.
– Stormgate Studio
Jul 2 at 13:37





@StormgateStudio Did you set count to zero first to find out how large a buffer you need?
– Andrew Henle
Jul 2 at 13:40


count





I setting as count a size of buffer I have, and if function returns more that I have, then I reserving more and calling again with new buffer, it's working well with vsnprintf and usually from first call. But _vsnwprintf acts absolutely different
– Stormgate Studio
Jul 2 at 13:50







By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

How to make file upload 'Required' in Contact Form 7?

Rothschild family

amazon EC2 - How to make wp-config.php to writable?