changed rendering
[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 int
16 clamp(int v, int min, int max)
17 {
18     if (v > max) return max;
19     if (v < min) return min;
20     return v;
21 }
22
23 int
24 floorzero(int v)
25 {
26     return (v >= 0) ? v : 0;
27 }
28
29
30 char*
31 wrap_text(char* str, int max_width, int* lines)
32 {
33     char* wrapped_str;
34     char* str_read;
35     int totlen;
36     int line_count;
37
38     wrapped_str = malloc(sizeof(char));
39     wrapped_str[0] = 0;
40     str_read = str;
41     totlen = 0;
42     line_count = 0;
43
44     for (int i = 0; i < floor(strlen(str)/max_width)+1; i++) {
45
46         int curlen;
47         
48         curlen = min(strlen(str_read), max_width);
49         totlen += (curlen+1); // account for new line
50
51         wrapped_str = realloc(wrapped_str, sizeof(char)*totlen+1); // account for null
52         strncat(wrapped_str, str_read, curlen);
53         strcat(wrapped_str, "\n");
54
55         str_read += max_width;
56         line_count += 1;
57     }
58
59     *lines = line_count;
60
61     return wrapped_str;
62
63 }
64
65 /* array stuff */
66 int
67 ar_swap_item(void** arr, int src_index, int dest_index) 
68 {
69     void* temp;
70
71     temp = arr[dest_index];
72     arr[dest_index] = arr[src_index];
73     arr[src_index] = temp;
74
75     return 0;
76 }
77
78