diff --git a/src/StrSplit.c b/src/StrSplit.c new file mode 100644 index 0000000..3a64040 --- /dev/null +++ b/src/StrSplit.c @@ -0,0 +1,65 @@ +#include + +#include +#include +#include +#include + +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); +} diff --git a/src/include/Parsee.h b/src/include/Parsee.h index fe6bd98..017e5b6 100644 --- a/src/include/Parsee.h +++ b/src/include/Parsee.h @@ -231,6 +231,9 @@ extern bool ParseeIsAdmin(ParseeData *data, char *user); /* Measures Parsee's overall uptime */ extern uint64_t ParseeUptime(void); -/* Turns a date into a nice "X minutes, Y seconds" string */ +/** Turns a duration into a nice "X minutes, Y seconds" string + * --------- + * Returns: A human-readable string showing the duration[LA:HEAP] + * Modifies: NOTHING */ extern char * ParseeStringifyDate(uint64_t millis); #endif diff --git a/src/include/StringSplit.h b/src/include/StringSplit.h new file mode 100644 index 0000000..fdae543 --- /dev/null +++ b/src/include/StringSplit.h @@ -0,0 +1,35 @@ +#ifndef PARSEE_STRINGSPLIT_H +#define PARSEE_STRINGSPLIT_H + +#include + +/* Represents a boundary in a linesplit string */ +typedef struct StringRect { + char **source_lines; + + /* NOTE: All of these values are inclusive */ + + /* The start line/character index */ + size_t start_line; + size_t start_char; + + /* The end line/character index */ + size_t end_line; + size_t end_char; +} StringRect; + +/** Splits a string into a set of NULL-terminated substrings, which is + * terminated by a NULL pointer. + * --------- + * Returns: A set of lines(excluding them) [LA:HEAP] + * Modifies: NOTHING + * Thrasher: StrFreeLines */ +extern char ** StrSplitLines(char *text); +extern void StrFreeLines(char **split); + +extern size_t StrLines(char **split); + +/* Creates a full zone covering every part of the split */ +extern StringRect StrFullRect(char **split); + +#endif