Sprintf returns bytes copied. Add IS_DEBUG flag
This commit is contained in:
parent
d740efdf06
commit
0bf1b6d3ff
68
dqn.h
68
dqn.h
@ -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,72 +2050,83 @@ 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)
|
||||||
{
|
{
|
||||||
LOCAL_PERSIST char tmp[STB_SPRINTF_MIN];
|
int result = 0;
|
||||||
auto SprintfCallback = [](char *buf, void *user, int len) -> char *
|
if (Dqn::IS_DEBUG)
|
||||||
{
|
{
|
||||||
// TODO(doyle): Do the mem copy here into the string
|
LOCAL_PERSIST char tmp[STB_SPRINTF_MIN];
|
||||||
(void)len; (void)user;
|
auto SprintfCallback = [](char *buf, void *user, int len) -> char *
|
||||||
return buf;
|
{
|
||||||
};
|
(void)len; (void)user;
|
||||||
|
return buf;
|
||||||
|
};
|
||||||
|
|
||||||
char *bufStart = me->str + bufOffset;
|
char *bufStart = me->str + bufOffset;
|
||||||
i32 const reqLen = Dqn_vsprintfcb(SprintfCallback, nullptr, tmp, fmt, argList) + 1 /*NULL*/;
|
i32 const reqLen = Dqn_vsprintfcb(SprintfCallback, nullptr, tmp, fmt, argList) + 1 /*NULL*/;
|
||||||
i32 const remainingSpace = static_cast<i32>((me->str + MAX) - bufStart);
|
i32 const remainingSpace = static_cast<i32>((me->str + MAX) - bufStart);
|
||||||
|
|
||||||
i32 numToCopy = DQN_MIN(remainingSpace, reqLen);
|
i32 numToCopy = DQN_MIN(remainingSpace, reqLen);
|
||||||
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;
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
bool result = (numToCopy == (numCopiedNotInclNull + 1));
|
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user