[rtext] TextFormat now alerts user to truncation.
This commit is contained in:
parent
101a9b0445
commit
08e9348db7
13
src/rcore.c
13
src/rcore.c
|
|
@ -2876,6 +2876,7 @@ static void PlayAutomationEvent(unsigned int frame)
|
||||||
#if !defined(SUPPORT_MODULE_RTEXT)
|
#if !defined(SUPPORT_MODULE_RTEXT)
|
||||||
// Formatting of text with variables to 'embed'
|
// Formatting of text with variables to 'embed'
|
||||||
// WARNING: String returned will expire after this function is called MAX_TEXTFORMAT_BUFFERS times
|
// WARNING: String returned will expire after this function is called MAX_TEXTFORMAT_BUFFERS times
|
||||||
|
|
||||||
const char *TextFormat(const char *text, ...)
|
const char *TextFormat(const char *text, ...)
|
||||||
{
|
{
|
||||||
#ifndef MAX_TEXTFORMAT_BUFFERS
|
#ifndef MAX_TEXTFORMAT_BUFFERS
|
||||||
|
|
@ -2894,12 +2895,22 @@ const char *TextFormat(const char *text, ...)
|
||||||
|
|
||||||
va_list args;
|
va_list args;
|
||||||
va_start(args, text);
|
va_start(args, text);
|
||||||
vsnprintf(currentBuffer, MAX_TEXT_BUFFER_LENGTH, text, args);
|
int charCountRequired = vsnprintf(currentBuffer, MAX_TEXT_BUFFER_LENGTH, text, args);
|
||||||
va_end(args);
|
va_end(args);
|
||||||
|
|
||||||
|
// If charCountRequired is larger than the MAX_TEXT_BUFFER_LENGTH, then overflow occured
|
||||||
|
if(charCountRequired > MAX_TEXT_BUFFER_LENGTH)
|
||||||
|
{
|
||||||
|
// We are going to insert [TRUN] at the end of the string so the user knows what happened
|
||||||
|
char *truncBuffer = buffers[index] + MAX_TEXT_BUFFER_LENGTH - 7; // 7 = six letters + '\0'
|
||||||
|
sprintf(truncBuffer, "[TRUN]");
|
||||||
|
TRACELOG(LOG_WARNING, "RTEXT: TextFormat string was [TRUN]cated. If you need longer strings, please increase MAX_TEXT_BUFFER_LENGTH.");
|
||||||
|
}
|
||||||
|
|
||||||
index += 1; // Move to next buffer for next function call
|
index += 1; // Move to next buffer for next function call
|
||||||
if (index >= MAX_TEXTFORMAT_BUFFERS) index = 0;
|
if (index >= MAX_TEXTFORMAT_BUFFERS) index = 0;
|
||||||
|
|
||||||
return currentBuffer;
|
return currentBuffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // !SUPPORT_MODULE_RTEXT
|
#endif // !SUPPORT_MODULE_RTEXT
|
||||||
|
|
|
||||||
12
src/rtext.c
12
src/rtext.c
|
|
@ -1371,15 +1371,25 @@ const char *TextFormat(const char *text, ...)
|
||||||
|
|
||||||
va_list args;
|
va_list args;
|
||||||
va_start(args, text);
|
va_start(args, text);
|
||||||
vsnprintf(currentBuffer, MAX_TEXT_BUFFER_LENGTH, text, args);
|
charCountRequired = vsnprintf(currentBuffer, MAX_TEXT_BUFFER_LENGTH, text, args);
|
||||||
va_end(args);
|
va_end(args);
|
||||||
|
|
||||||
|
// If charCountRequired is larger than the MAX_TEXT_BUFFER_LENGTH, then overflow occured
|
||||||
|
if(charCountRequired > MAX_TEXT_BUFFER_LENGTH)
|
||||||
|
{
|
||||||
|
// We are going to insert [TRUN] at the end of the string so the user knows what happened
|
||||||
|
char *truncBuffer = buffers[index] + MAX_TEXT_BUFFER_LENGTH - 7; // 7 = six letters + '\0'
|
||||||
|
sprintf(truncBuffer, "[TRUN]");
|
||||||
|
TRACELOG(LOG_WARNING, "RTEXT: TextFormat string was [TRUN]cated. If you need longer strings, please increase MAX_TEXT_BUFFER_LENGTH.");
|
||||||
|
}
|
||||||
|
|
||||||
index += 1; // Move to next buffer for next function call
|
index += 1; // Move to next buffer for next function call
|
||||||
if (index >= MAX_TEXTFORMAT_BUFFERS) index = 0;
|
if (index >= MAX_TEXTFORMAT_BUFFERS) index = 0;
|
||||||
|
|
||||||
return currentBuffer;
|
return currentBuffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Get integer value from text
|
// Get integer value from text
|
||||||
// NOTE: This function replaces atoi() [stdlib.h]
|
// NOTE: This function replaces atoi() [stdlib.h]
|
||||||
int TextToInteger(const char *text)
|
int TextToInteger(const char *text)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user