Sprintf returns bytes copied. Add IS_DEBUG flag

This commit is contained in:
Doyle T 2018-06-11 18:40:54 +10:00
parent d740efdf06
commit 0bf1b6d3ff

44
dqn.h
View File

@ -175,6 +175,7 @@ namespace Dqn
{ {
enum struct ZeroClear { True = 1, False = 0}; enum struct ZeroClear { True = 1, False = 0};
enum struct IgnoreCase { True = 1, False = 0}; enum struct IgnoreCase { True = 1, False = 0};
FILE_SCOPE const bool IS_DEBUG = true;
}; // namespace Dqn }; // namespace Dqn
// #Win32 Prototypes // #Win32 Prototypes
@ -2049,24 +2050,26 @@ struct DqnFixedString
// Xprintf functions always modifies buffer and null-terminates even with insufficient buffer size. // Xprintf functions always modifies buffer and null-terminates even with insufficient buffer size.
// Asserts on failure if DQN_ASSERT is defined. // Asserts on failure if DQN_ASSERT is defined.
// return: True if there was sufficient space. // return: The number of characters copied to the buffer
bool Sprintf (char const *fmt, ...); int Sprintf (char const *fmt, ...);
bool SprintfAppend (char const *fmt, ...); int SprintfAppend (char const *fmt, ...);
bool VSprintf (char const *fmt, va_list argList); int VSprintf (char const *fmt, va_list argList);
bool VSprintfAppend(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 <unsigned int MAX> template <unsigned int MAX>
FILE_SCOPE bool FILE_SCOPE int
DqnFixedString__VSprintf(DqnFixedString<MAX> *me, char const *fmt, va_list argList, int bufOffset) DqnFixedString__VSprintf(DqnFixedString<MAX> *me, char const *fmt, va_list argList, int bufOffset)
{ {
int result = 0;
if (Dqn::IS_DEBUG)
{
LOCAL_PERSIST char tmp[STB_SPRINTF_MIN]; LOCAL_PERSIST char tmp[STB_SPRINTF_MIN];
auto SprintfCallback = [](char *buf, void *user, int len) -> char * auto SprintfCallback = [](char *buf, void *user, int len) -> char *
{ {
// TODO(doyle): Do the mem copy here into the string
(void)len; (void)user; (void)len; (void)user;
return buf; return buf;
}; };
@ -2079,42 +2082,51 @@ DqnFixedString__VSprintf(DqnFixedString<MAX> *me, char const *fmt, va_list argLi
i32 numCopiedNotInclNull = Dqn_vsnprintf(bufStart, numToCopy, fmt, argList); i32 numCopiedNotInclNull = Dqn_vsnprintf(bufStart, numToCopy, fmt, argList);
me->len = (bufOffset == 0) ? numCopiedNotInclNull : me->len + numCopiedNotInclNull; me->len = (bufOffset == 0) ? numCopiedNotInclNull : me->len + numCopiedNotInclNull;
bool result = (numToCopy == (numCopiedNotInclNull + 1)); DQN_ASSERT(numToCopy == (numCopiedNotInclNull + 1));
result = numCopiedNotInclNull;
}
else
{
char *bufStart = me->str + bufOffset;
i32 const remainingSpace = static_cast<i32>((me->str + MAX) - bufStart);
result = Dqn_vsnprintf(bufStart, remainingSpace, fmt, argList);
}
return result; return result;
} }
template <unsigned int MAX> template <unsigned int MAX>
bool DqnFixedString<MAX>::VSprintf(char const *fmt, va_list argList) int DqnFixedString<MAX>::VSprintf(char const *fmt, va_list argList)
{ {
int bufOffset = 0; int bufOffset = 0;
bool result = DqnFixedString__VSprintf(this, fmt, argList, bufOffset); int result = DqnFixedString__VSprintf(this, fmt, argList, bufOffset);
return result; return result;
} }
template <unsigned int MAX> template <unsigned int MAX>
bool DqnFixedString<MAX>::Sprintf(char const *fmt, ...) int DqnFixedString<MAX>::Sprintf(char const *fmt, ...)
{ {
va_list argList; va_list argList;
va_start(argList, fmt); va_start(argList, fmt);
bool result = this->VSprintf(fmt, argList); int result = this->VSprintf(fmt, argList);
va_end(argList); va_end(argList);
return result; return result;
} }
template <unsigned int MAX> template <unsigned int MAX>
bool DqnFixedString<MAX>::VSprintfAppend(char const *fmt, va_list argList) int DqnFixedString<MAX>::VSprintfAppend(char const *fmt, va_list argList)
{ {
int bufOffset = this->len; int bufOffset = this->len;
bool result = DqnFixedString__VSprintf(this, fmt, argList, bufOffset); int result = DqnFixedString__VSprintf(this, fmt, argList, bufOffset);
return result; return result;
} }
template <unsigned int MAX> template <unsigned int MAX>
bool DqnFixedString<MAX>::SprintfAppend(char const *fmt, ...) int DqnFixedString<MAX>::SprintfAppend(char const *fmt, ...)
{ {
va_list argList; va_list argList;
va_start(argList, fmt); va_start(argList, fmt);
bool result = this->VSprintfAppend(fmt, argList); int result = this->VSprintfAppend(fmt, argList);
va_end(argList); va_end(argList);
return result; return result;
} }