From: Jann Horn Date: Sat, 22 Jun 2013 20:59:02 +0000 (+0200) Subject: add buf_to_linearray X-Git-Url: http://git.thejh.net/?a=commitdiff_plain;h=4455aa6469db5cc86ff175ab0b6eafbe75a16ea4;p=libjh.git add buf_to_linearray --- diff --git a/string.c b/string.c index 5ef61b9..b0db54c 100644 --- a/string.c +++ b/string.c @@ -145,3 +145,23 @@ PUBLIC_FN int ends_with(char *str, char *sub) { if (sub_len>str_len) return 0; return streq(str+str_len-sub_len, sub); } + +PUBLIC_FN char **buf_to_linearray(char *buf, ssize_t buflen) { + if (buflen == -1) buflen = strlen(buf); + size_t linecount = count_char_occurences_in_buf(buf, buflen, '\n')+1; + char **ret = malloc(linecount * sizeof(char*) + 1); + ret[linecount] = NULL; + if (ret == NULL) return NULL; + char **r = ret; + *(r++) = buf; /* first line starts at byte zero */ + char *b = buf; + char *be = buf+buflen; + while (b