X-Git-Url: https://git.danieliu.xyz/?p=taskasaur.git;a=blobdiff_plain;f=utils.c;h=a649239b71f33e2e9d82adcd436d1476dc30c384;hp=2f5c53bbd97a364f5d157a3e4e9212578e68ca8e;hb=7cd288f609ab7d99e5fb5b8da07d2c1b6f32907d;hpb=82204679646dbb05f22adf6dca9bca18943ea9f6 diff --git a/utils.c b/utils.c index 2f5c53b..a649239 100644 --- a/utils.c +++ b/utils.c @@ -1,4 +1,50 @@ +#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; + +} +