refactor + man
[taskasaur.git] / utils.c
1
2 #include <stdlib.h>
3 #include <string.h>
4 #include <stdio.h>
5 #include <math.h>
6
7 #include "headers/utils.h"
8
9 int
10 min(int a, int b)
11 {
12     return (a < b) ? a : b;
13 }
14
15 char*
16 wrap_text(char* str, int max_width, int* lines)
17 {
18     char* wrapped_str;
19     char* str_read;
20     int totlen;
21     int line_count;
22
23     wrapped_str = malloc(sizeof(char));
24     wrapped_str[0] = 0;
25     str_read = str;
26     totlen = 0;
27     line_count = 0;
28
29     for (int i = 0; i < floor(strlen(str)/max_width)+1; i++) {
30
31         int curlen;
32         
33         curlen = min(strlen(str_read), max_width);
34         totlen += (curlen+1); // account for new line
35
36         wrapped_str = realloc(wrapped_str, sizeof(char)*totlen+1); // account for null
37         strncat(wrapped_str, str_read, curlen);
38         strcat(wrapped_str, "\n");
39
40         str_read += max_width;
41         line_count += 1;
42     }
43
44     *lines = line_count;
45
46     return wrapped_str;
47
48 }
49
50 /* array stuff */
51 int
52 ar_swap_item(void** arr, int src_index, int dest_index) 
53 {
54     void* temp;
55
56     temp = arr[dest_index];
57     arr[dest_index] = arr[src_index];
58     arr[src_index] = temp;
59
60     return 0;
61 }
62
63