#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); }