Skip to content

Commit

Permalink
Added swnprintf.
Browse files Browse the repository at this point in the history
  • Loading branch information
bkaradzic authored and kevinic committed Aug 5, 2013
1 parent 0ebd45d commit 225bf37
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions include/bx/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <stdio.h> // vsnprintf
#include <string.h>
#include <string>
#include <wchar.h> // wchar_t

namespace bx
{
Expand Down Expand Up @@ -172,21 +173,34 @@ namespace bx
/// Cross platform implementation of vsnprintf that returns number of
/// characters which would have been written to the final string if
/// enough space had been available.
inline int32_t vsnprintf(char* _str, size_t _size, const char* _format, va_list _argList)
inline int32_t vsnprintf(char* _str, size_t _count, const char* _format, va_list _argList)
{
#if BX_COMPILER_MSVC
int32_t len = ::vsnprintf(_str, _size, _format, _argList);
int32_t len = ::vsnprintf(_str, _count, _format, _argList);
return -1 == len ? ::_vscprintf(_format, _argList) : len;
#else
return ::vsnprintf(_str, _size, _format, _argList);
return ::vsnprintf(_str, _count, _format, _argList);
#endif // BX_COMPILER_MSVC
}

inline int32_t snprintf(char* _str, size_t _size, const char* _format, ...) // BX_PRINTF_ARGS(3, 4)
inline int32_t snprintf(char* _str, size_t _count, const char* _format, ...) // BX_PRINTF_ARGS(3, 4)
{
va_list argList;
va_start(argList, _format);
int32_t len = vsnprintf(_str, _size, _format, argList);
int32_t len = vsnprintf(_str, _count, _format, argList);
va_end(argList);
return len;
}

inline int32_t swnprintf(wchar_t* _out, size_t _count, const wchar_t* _format, ...)
{
va_list argList;
va_start(argList, _format);
#if defined(__MINGW32__)
int32_t len = swprintf(_out, _format, argList);
#else
int32_t len = swprintf(_out, _count, _format, argList);
#endif // defined(__MINGW__)
va_end(argList);
return len;
}
Expand All @@ -213,6 +227,7 @@ namespace bx
stringPrintfVargs(_out, _format, argList);
va_end(argList);
}

} // namespace bx

#endif // __BX_PRINTF_H__

0 comments on commit 225bf37

Please sign in to comment.