-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformat_cell.c
150 lines (138 loc) · 3.17 KB
/
format_cell.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include "neokanban.h"
int next_wlen(char* str)
{
int len = 0;
while (str[len] != ' ' && str[len] != '\0' && str[len] != '\n')
{
len++;
}
return len;
}
//////////
void format(size_t extra_lines,
size_t col_id,
char* str,
int limit,
formatted_str *container)
{
char* word = calloc(1, MAX_BUF);
char* line = calloc(1, MAX_BUF);
char* begin = col_id == 0 ? "|" : "";
char space = ' ';
char* end = " |";
int len_e = strlen(end);
int len_b = strlen(begin);
int len_w = 0;
size_t n_lines = 0;
size_t n_spaces;
if (str[0] == '\0')
{
n_spaces = limit - len_b - len_e;
for (int i = 0; i < extra_lines + 1; i++)
{
line[0] = '\0';
strcat(line, begin);
for (int j = 0; j < n_spaces; j++)
{
strncat(line, &space, 1);
}
strcat(line, end);
container->text_a[i] = malloc(MAX_BUF);
container->text_a[i][0] = '\0';
strcpy(container->text_a[i], line);
n_lines++;
}
container->n_lines = n_lines;
free(word);
free(line);
return;
}
// string not empty
// go through every character and store it in a word var if it's not space
// if it is space, lookup next word
// if next word is bigger than the remaining space left, add array to cont
// if next word still fits, just save the word to the line and continue
//
strcpy(line, begin);
for (int i = 0; str[i] != '\0'; i++)
{
if (str[i] != ' ')
{
strncat(word, &str[i], 1);
continue;
}
strncat(line, &space, 1);
strcat(line, word);
word[0] = '\0';
int nxt_len = next_wlen(&str[i + 1]);
if (strlen(line) + nxt_len + len_e >= limit)
{
// next word doesn't fit
// add spaces and end
n_spaces = limit - strlen(line) - len_e;
for (int j = 0; j < n_spaces; j++)
{
strncat(line, &space, 1);
}
strcat(line, end);
// store and reset line
container->text_a[n_lines] = calloc(1, MAX_BUF);
strcpy(container->text_a[n_lines], line);
line[0] = '\0';
strcpy(line, begin);
n_lines++;
}
}
if (word[0] != '\0')
{
if (strlen(line) + strlen(word) + len_e >= limit)
{
// word + line overflows
// write line first and setup a new one just for the word
n_spaces = limit - strlen(line) - len_e;
for (int i = 0; i < n_spaces; i++)
{
strncat(line, &space, 1);
}
strcat(line, end);
container->text_a[n_lines] = calloc(1, MAX_BUF);
strcpy(container->text_a[n_lines], line);
n_lines++;
line[0] = '\0';
strcpy(line, begin);
}
strncat(line, &space, 1);
strcat(line, word);
n_spaces = limit - strlen(line) - len_e;
for (int i = 0; i < n_spaces; i++)
{
strncat(line, &space, 1);
}
strcat(line, end);
container->text_a[n_lines] = calloc(1, MAX_BUF);
strcpy(container->text_a[n_lines], line);
n_lines++;
}
// now extra lines
n_spaces = limit - len_b - len_e;
for (int i = 0; i < extra_lines; i++)
{
line[0] = '\0';
strcat(line, begin);
for (int j = 0; j < n_spaces; j++)
{
strncat(line, &space, 1);
}
strcat(line, end);
container->text_a[n_lines] = calloc(1, MAX_BUF);
strcpy(container->text_a[n_lines], line);
n_lines++;
}
container->n_lines = n_lines;
free(word);
free(line);
}