Skip to content

Commit d2f6c49

Browse files
committed
ADDED: [rtext] LoadTextLines()/UnloadTextLines()
1 parent ed3b5b2 commit d2f6c49

File tree

3 files changed

+36
-39
lines changed

3 files changed

+36
-39
lines changed

src/raylib.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1506,6 +1506,8 @@ RLAPI const char *CodepointToUTF8(int codepoint, int *utf8Size);
15061506
// Text strings management functions (no UTF-8 strings, only byte chars)
15071507
// WARNING 1: Most of these functions use internal static buffers, it's recommended to store returned data on user-side for re-use
15081508
// WARNING 2: Some strings allocate memory internally for the returned strings, those strings must be free by user using MemFree()
1509+
RLAPI char **LoadTextLines(const char *text, int *count); // Load text as separate lines ('\n')
1510+
RLAPI void UnloadTextLines(char **text); // Unload text lines
15091511
RLAPI int TextCopy(char *dst, const char *src); // Copy one string to another, returns bytes copied
15101512
RLAPI bool TextIsEqual(const char *text1, const char *text2); // Check if two text string are equal
15111513
RLAPI unsigned int TextLength(const char *text); // Get text length, checks for '\0' ending
@@ -1522,7 +1524,6 @@ RLAPI char *TextToLower(const char *text);
15221524
RLAPI char *TextToPascal(const char *text); // Get Pascal case notation version of provided string
15231525
RLAPI char *TextToSnake(const char *text); // Get Snake case notation version of provided string
15241526
RLAPI char *TextToCamel(const char *text); // Get Camel case notation version of provided string
1525-
15261527
RLAPI int TextToInteger(const char *text); // Get integer value from text
15271528
RLAPI float TextToFloat(const char *text); // Get float value from text
15281529

src/rtext.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1415,6 +1415,40 @@ Rectangle GetGlyphAtlasRec(Font font, int codepoint)
14151415
//----------------------------------------------------------------------------------
14161416
// Text strings management functions
14171417
//----------------------------------------------------------------------------------
1418+
// Load text as separate lines ('\n')
1419+
// WARNING: There is a limit set for number of lines and line-size
1420+
char **LoadTextLines(const char *text, int *count)
1421+
{
1422+
#define MAX_TEXTLINES_COUNT 512
1423+
#define MAX_TEXTLINES_LINE_LEN 512
1424+
1425+
char **lines = (char **)RL_CALLOC(MAX_TEXTLINES_COUNT, sizeof(char *));
1426+
for (int i = 0; i < MAX_TEXTLINES_COUNT; i++) lines[i] = (char *)RL_CALLOC(MAX_TEXTLINES_LINE_LEN, 1);
1427+
int textSize = (int)strlen(text);
1428+
int k = 0;
1429+
1430+
for (int i = 0, len = 0; (i < textSize) && (k < MAX_TEXTLINES_COUNT); i++)
1431+
{
1432+
if ((text[i] == '\n') || (len == (MAX_TEXTLINES_LINE_LEN - 1)))
1433+
{
1434+
strncpy(lines[k], &text[i - len], len);
1435+
len = 0;
1436+
k++;
1437+
}
1438+
else len++;
1439+
}
1440+
1441+
*count += k;
1442+
return lines;
1443+
}
1444+
1445+
// Unload text lines
1446+
void UnloadTextLines(char **lines)
1447+
{
1448+
for (int i = 0; i < MAX_TEXTLINES_COUNT; i++) RL_FREE(lines[i]);
1449+
RL_FREE(lines);
1450+
}
1451+
14181452
// Get text length in bytes, check for \0 character
14191453
unsigned int TextLength(const char *text)
14201454
{

tools/rexm/rexm.c

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,6 @@ static int UpdateRequiredFiles(void);
148148
static rlExampleInfo *LoadExamplesData(const char *fileName, const char *category, bool sort, int *exCount);
149149
static void UnloadExamplesData(rlExampleInfo *exInfo);
150150

151-
// Get text lines (by line-breaks '\n')
152-
// WARNING: It does not copy text data, just returns line pointers
153-
static char **LoadTextLines(const char *text, int *count);
154-
static void UnloadTextLines(char **text);
155-
156151
// Load example info from file header
157152
static rlExampleInfo *LoadExampleInfo(const char *exFileName);
158153
static void UnloadExampleInfo(rlExampleInfo *exInfo);
@@ -1775,39 +1770,6 @@ static int FileMove(const char *srcPath, const char *dstPath)
17751770
return result;
17761771
}
17771772

1778-
// Load text lines
1779-
static char **LoadTextLines(const char *text, int *count)
1780-
{
1781-
#define MAX_TEXT_LINES 512
1782-
#define MAX_TEXT_LINE_LEN 512
1783-
1784-
char **lines = (char **)RL_CALLOC(MAX_TEXT_LINES, sizeof(char *));
1785-
for (int i = 0; i < MAX_TEXT_LINES; i++) lines[i] = (char *)RL_CALLOC(MAX_TEXT_LINE_LEN, 1);
1786-
int textSize = (int)strlen(text);
1787-
int k = 0;
1788-
1789-
for (int i = 0, len = 0; (i < textSize) && (k < MAX_TEXT_LINES); i++)
1790-
{
1791-
if ((text[i] == '\n') || (len == (MAX_TEXT_LINE_LEN - 1)))
1792-
{
1793-
strncpy(lines[k], &text[i - len], len);
1794-
len = 0;
1795-
k++;
1796-
}
1797-
else len++;
1798-
}
1799-
1800-
*count += k;
1801-
return lines;
1802-
}
1803-
1804-
// Unload text lines
1805-
static void UnloadTextLines(char **lines)
1806-
{
1807-
for (int i = 0; i < MAX_TEXT_LINES; i++) RL_FREE(lines[i]);
1808-
RL_FREE(lines);
1809-
}
1810-
18111773
// Get example info from example file header
18121774
// NOTE: Expecting the example to follow raylib_example_template.c
18131775
rlExampleInfo *LoadExampleInfo(const char *exFileName)

0 commit comments

Comments
 (0)