REDESIGNED: LoadTextLines()/UnloadTextLines()

This commit is contained in:
Ray
2025-09-04 23:25:42 +02:00
parent 8116ebd9bb
commit 0203a47bf9
2 changed files with 22 additions and 20 deletions

View File

@@ -1507,7 +1507,7 @@ RLAPI const char *CodepointToUTF8(int codepoint, int *utf8Size);
// WARNING 1: Most of these functions use internal static buffers, it's recommended to store returned data on user-side for re-use
// WARNING 2: Some strings allocate memory internally for the returned strings, those strings must be free by user using MemFree()
RLAPI char **LoadTextLines(const char *text, int *count); // Load text as separate lines ('\n')
RLAPI void UnloadTextLines(char **text); // Unload text lines
RLAPI void UnloadTextLines(char **text, int lineCount); // Unload text lines
RLAPI int TextCopy(char *dst, const char *src); // Copy one string to another, returns bytes copied
RLAPI bool TextIsEqual(const char *text1, const char *text2); // Check if two text string are equal
RLAPI unsigned int TextLength(const char *text); // Get text length, checks for '\0' ending

View File

@@ -1448,36 +1448,38 @@ Rectangle GetGlyphAtlasRec(Font font, int codepoint)
// Text strings management functions
//----------------------------------------------------------------------------------
// Load text as separate lines ('\n')
// WARNING: There is a limit set for number of lines and line-size
// NOTE: Returned lines end with null terminator '\0'
char **LoadTextLines(const char *text, int *count)
{
#define MAX_TEXTLINES_COUNT 512
#define MAX_TEXTLINES_LINE_LEN 512
int lineCount = 1;
int textSize = strlen(text);
char **lines = (char **)RL_CALLOC(MAX_TEXTLINES_COUNT, sizeof(char *));
for (int i = 0; i < MAX_TEXTLINES_COUNT; i++) lines[i] = (char *)RL_CALLOC(MAX_TEXTLINES_LINE_LEN, 1);
int textSize = (int)strlen(text);
int k = 0;
for (int i = 0, len = 0; (i < textSize) && (k < MAX_TEXTLINES_COUNT); i++)
// Text pass to get required line count
for (int i = 0; i < textSize; i++)
{
if ((text[i] == '\n') || (len == (MAX_TEXTLINES_LINE_LEN - 1)))
{
strncpy(lines[k], &text[i - len], len);
len = 0;
k++;
}
else len++;
if (text[i] == '\n') lineCount++;
}
*count += k;
char **lines = (char **)RL_CALLOC(lineCount, sizeof(char *));
for (int i = 0, l = 0, lineLen = 0; i < lineCount; i++, lineLen++)
{
if (text[i] == '\n')
{
lines[l] = (char *)RL_CALLOC(lineLen + 1, 1);
strncpy(lines[l], &text[i - lineLen], lineLen);
lineLen = 0;
l++;
}
}
*count = lineCount;
return lines;
}
// Unload text lines
void UnloadTextLines(char **lines)
void UnloadTextLines(char **lines, int lineCount)
{
for (int i = 0; i < MAX_TEXTLINES_COUNT; i++) RL_FREE(lines[i]);
for (int i = 0; i < lineCount; i++) RL_FREE(lines[i]);
RL_FREE(lines);
}