mirror of
https://forge.fsky.io/lda/Parsee.git
synced 2026-03-13 21:25:11 +00:00
65 lines
1.1 KiB
C
65 lines
1.1 KiB
C
#include <StringSplit.h>
|
|
|
|
#include <Cytoplasm/Memory.h>
|
|
#include <stdbool.h>
|
|
#include <string.h>
|
|
#include <stdlib.h>
|
|
|
|
char **
|
|
StrSplitLines(char *text)
|
|
{
|
|
char **ret = NULL;
|
|
size_t lines = 0;
|
|
|
|
char *next = text, *ptr = text;
|
|
bool done = false;
|
|
if (!text)
|
|
{
|
|
return NULL;
|
|
}
|
|
|
|
while (!done)
|
|
{
|
|
size_t bytes;
|
|
char *line;
|
|
|
|
next = strchr(ptr, '\n');
|
|
if (!next)
|
|
{
|
|
next = text + strlen(text);
|
|
done = true;
|
|
}
|
|
|
|
/* Take the chunk from ptr to [text] */
|
|
bytes = next - ptr;
|
|
line = Malloc(bytes + 1);
|
|
memcpy(line, ptr, bytes);
|
|
line[bytes] = '\0';
|
|
|
|
ret = Realloc(ret, sizeof(char *) * ++lines);
|
|
ret[lines - 1] = line;
|
|
|
|
ptr = *next ? next + 1 : next;
|
|
}
|
|
|
|
ret = Realloc(ret, sizeof(char *) * ++lines);
|
|
ret[lines - 1] = NULL;
|
|
return ret;
|
|
}
|
|
void
|
|
StrFreeLines(char **split)
|
|
{
|
|
char *line, **orig;
|
|
if (!split)
|
|
{
|
|
return;
|
|
}
|
|
|
|
orig = split;
|
|
while ((line = *split++))
|
|
{
|
|
Free(line);
|
|
}
|
|
|
|
Free(orig);
|
|
}
|