mirror of
https://github.com/raysan5/raylib.git
synced 2025-10-09 11:26:32 +00:00
REDESIGNED: LoadTextLines()
/UnloadTextLines()
This commit is contained in:
@@ -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 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()
|
// 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 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 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 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
|
RLAPI unsigned int TextLength(const char *text); // Get text length, checks for '\0' ending
|
||||||
|
40
src/rtext.c
40
src/rtext.c
@@ -1448,36 +1448,38 @@ Rectangle GetGlyphAtlasRec(Font font, int codepoint)
|
|||||||
// Text strings management functions
|
// Text strings management functions
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
// Load text as separate lines ('\n')
|
// 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)
|
char **LoadTextLines(const char *text, int *count)
|
||||||
{
|
{
|
||||||
#define MAX_TEXTLINES_COUNT 512
|
int lineCount = 1;
|
||||||
#define MAX_TEXTLINES_LINE_LEN 512
|
int textSize = strlen(text);
|
||||||
|
|
||||||
char **lines = (char **)RL_CALLOC(MAX_TEXTLINES_COUNT, sizeof(char *));
|
// Text pass to get required line count
|
||||||
for (int i = 0; i < MAX_TEXTLINES_COUNT; i++) lines[i] = (char *)RL_CALLOC(MAX_TEXTLINES_LINE_LEN, 1);
|
for (int i = 0; i < textSize; i++)
|
||||||
int textSize = (int)strlen(text);
|
|
||||||
int k = 0;
|
|
||||||
|
|
||||||
for (int i = 0, len = 0; (i < textSize) && (k < MAX_TEXTLINES_COUNT); i++)
|
|
||||||
{
|
{
|
||||||
if ((text[i] == '\n') || (len == (MAX_TEXTLINES_LINE_LEN - 1)))
|
if (text[i] == '\n') lineCount++;
|
||||||
{
|
|
||||||
strncpy(lines[k], &text[i - len], len);
|
|
||||||
len = 0;
|
|
||||||
k++;
|
|
||||||
}
|
|
||||||
else len++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
*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;
|
return lines;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Unload text 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);
|
RL_FREE(lines);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user