From 0bf1b6d3ff995ca1de37b60388582042f0faba76 Mon Sep 17 00:00:00 2001 From: Doyle T Date: Mon, 11 Jun 2018 18:40:54 +1000 Subject: [PATCH] Sprintf returns bytes copied. Add IS_DEBUG flag --- dqn.h | 68 +++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 40 insertions(+), 28 deletions(-) diff --git a/dqn.h b/dqn.h index 470a7e1..0a9aacf 100644 --- a/dqn.h +++ b/dqn.h @@ -175,6 +175,7 @@ namespace Dqn { enum struct ZeroClear { True = 1, False = 0}; enum struct IgnoreCase { True = 1, False = 0}; +FILE_SCOPE const bool IS_DEBUG = true; }; // namespace Dqn // #Win32 Prototypes @@ -2049,72 +2050,83 @@ struct DqnFixedString // Xprintf functions always modifies buffer and null-terminates even with insufficient buffer size. // Asserts on failure if DQN_ASSERT is defined. - // return: True if there was sufficient space. - bool Sprintf (char const *fmt, ...); - bool SprintfAppend (char const *fmt, ...); + // return: The number of characters copied to the buffer + int Sprintf (char const *fmt, ...); + int SprintfAppend (char const *fmt, ...); - bool VSprintf (char const *fmt, va_list argList); - bool VSprintfAppend(char const *fmt, va_list argList); + int VSprintf (char const *fmt, va_list argList); + int VSprintfAppend(char const *fmt, va_list argList); - void Clear (Dqn::ZeroClear clear = Dqn::ZeroClear::False) { if (clear == Dqn::ZeroClear::True) DqnMem_Set(str, 0, MAX); this = {}; } + void Clear (Dqn::ZeroClear clear = Dqn::ZeroClear::False) { if (clear == Dqn::ZeroClear::True) DqnMem_Set(str, 0, MAX); this = {}; } }; template -FILE_SCOPE bool +FILE_SCOPE int DqnFixedString__VSprintf(DqnFixedString *me, char const *fmt, va_list argList, int bufOffset) { - LOCAL_PERSIST char tmp[STB_SPRINTF_MIN]; - auto SprintfCallback = [](char *buf, void *user, int len) -> char * + int result = 0; + if (Dqn::IS_DEBUG) { - // TODO(doyle): Do the mem copy here into the string - (void)len; (void)user; - return buf; - }; + LOCAL_PERSIST char tmp[STB_SPRINTF_MIN]; + auto SprintfCallback = [](char *buf, void *user, int len) -> char * + { + (void)len; (void)user; + return buf; + }; - char *bufStart = me->str + bufOffset; - i32 const reqLen = Dqn_vsprintfcb(SprintfCallback, nullptr, tmp, fmt, argList) + 1 /*NULL*/; - i32 const remainingSpace = static_cast((me->str + MAX) - bufStart); + char *bufStart = me->str + bufOffset; + i32 const reqLen = Dqn_vsprintfcb(SprintfCallback, nullptr, tmp, fmt, argList) + 1 /*NULL*/; + i32 const remainingSpace = static_cast((me->str + MAX) - bufStart); - i32 numToCopy = DQN_MIN(remainingSpace, reqLen); - i32 numCopiedNotInclNull = Dqn_vsnprintf(bufStart, numToCopy, fmt, argList); - me->len = (bufOffset == 0) ? numCopiedNotInclNull : me->len + numCopiedNotInclNull; + i32 numToCopy = DQN_MIN(remainingSpace, reqLen); + i32 numCopiedNotInclNull = Dqn_vsnprintf(bufStart, numToCopy, fmt, argList); + me->len = (bufOffset == 0) ? numCopiedNotInclNull : me->len + numCopiedNotInclNull; + + DQN_ASSERT(numToCopy == (numCopiedNotInclNull + 1)); + result = numCopiedNotInclNull; + } + else + { + char *bufStart = me->str + bufOffset; + i32 const remainingSpace = static_cast((me->str + MAX) - bufStart); + result = Dqn_vsnprintf(bufStart, remainingSpace, fmt, argList); + } - bool result = (numToCopy == (numCopiedNotInclNull + 1)); return result; } template -bool DqnFixedString::VSprintf(char const *fmt, va_list argList) +int DqnFixedString::VSprintf(char const *fmt, va_list argList) { int bufOffset = 0; - bool result = DqnFixedString__VSprintf(this, fmt, argList, bufOffset); + int result = DqnFixedString__VSprintf(this, fmt, argList, bufOffset); return result; } template -bool DqnFixedString::Sprintf(char const *fmt, ...) +int DqnFixedString::Sprintf(char const *fmt, ...) { va_list argList; va_start(argList, fmt); - bool result = this->VSprintf(fmt, argList); + int result = this->VSprintf(fmt, argList); va_end(argList); return result; } template -bool DqnFixedString::VSprintfAppend(char const *fmt, va_list argList) +int DqnFixedString::VSprintfAppend(char const *fmt, va_list argList) { int bufOffset = this->len; - bool result = DqnFixedString__VSprintf(this, fmt, argList, bufOffset); + int result = DqnFixedString__VSprintf(this, fmt, argList, bufOffset); return result; } template -bool DqnFixedString::SprintfAppend(char const *fmt, ...) +int DqnFixedString::SprintfAppend(char const *fmt, ...) { va_list argList; va_start(argList, fmt); - bool result = this->VSprintfAppend(fmt, argList); + int result = this->VSprintfAppend(fmt, argList); va_end(argList); return result; }