X-Git-Url: https://git.danieliu.xyz/?p=taskasaur.git;a=blobdiff_plain;f=utils.c;h=ecf4b70ce604ba51a0ed5332f24771fc6fca7c16;hp=2f5c53bbd97a364f5d157a3e4e9212578e68ca8e;hb=303071d6e0488d7da4dc810897948b8ed97354d2;hpb=63ddce932065c2d05cf6412f52fd5c4637e195a3 diff --git a/utils.c b/utils.c index 2f5c53b..ecf4b70 100644 --- a/utils.c +++ b/utils.c @@ -1,4 +1,51 @@ +#include +#include +#include +#include + #include "headers/utils.h" +int +min(int a, int b) +{ + return (a < b) ? a : b; +} + +char* +wrap_text(char* str, int max_width, int* lines) +{ + char* wrapped_str; + char* str_read; + int totlen; + int line_count; + + wrapped_str = malloc(sizeof(char)); + wrapped_str[0] = 0; + str_read = str; + totlen = 0; + line_count = 0; + + for (int i = 0; i < floor(strlen(str)/max_width)+1; i++) { + + int curlen; + + curlen = min(strlen(str_read), max_width); + totlen += (curlen+1); // account for new line + + wrapped_str = realloc(wrapped_str, sizeof(char)*totlen+1); // account for null + strncat(wrapped_str, str_read, curlen); + strcat(wrapped_str, "\n"); + + str_read += max_width; + line_count += 1; + + } + + *lines = line_count; + + return wrapped_str; + +} +