-
Notifications
You must be signed in to change notification settings - Fork 3
/
table.c
437 lines (386 loc) · 10.5 KB
/
table.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "string_util.h"
#include "table.h"
#include "printing.h"
#include "constraint.h"
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define STRBUILDER_STARTSIZE 5
void add_cell_internal(Table *table, char *text, bool needs_free)
{
assert(table != NULL);
assert(table->curr_col < MAX_COLS);
struct Cell *cell = &table->curr_row->cells[table->curr_col];
assert(!cell->is_set);
cell->is_set = true;
cell->text_needs_free = needs_free;
cell->text = text;
if (table->curr_col >= table->num_cols)
{
table->num_cols = table->curr_col + 1;
}
while (table->curr_col != MAX_COLS && table->curr_row->cells[table->curr_col].is_set)
{
table->curr_col++;
}
}
void override_alignment_internal(struct Cell *cell, TextAlignment alignment)
{
cell->align = alignment;
cell->override_align = true;
}
struct Row *malloc_row(size_t y)
{
struct Row *res = calloc(1, sizeof(struct Row));
if (res == NULL) return NULL;
for (size_t i = 0; i < MAX_COLS; i++)
{
res->cells[i] = (struct Cell){
.is_set = false,
.x = i,
.y = y,
.parent = NULL,
.text = NULL,
.override_align = false,
.override_border_left = false,
.override_border_above = false,
.span_x = 1,
.span_y = 1,
.text_needs_free = false,
.zeros_needed = 0,
.zero_position = 0,
.dot_needed = false
};
}
return res;
}
struct Row *append_row(Table *table)
{
struct Row *row = table->curr_row;
while (row->next_row != NULL)
{
row = row->next_row;
}
row->next_row = malloc_row(table->num_rows);
table->num_rows++;
return row->next_row;
}
struct Row *get_row(Table *table, size_t index)
{
struct Row *row = table->first_row;
while (index-- > 0 && row != NULL) row = row->next_row;
return row;
}
struct Cell *get_curr_cell(Table *table)
{
return &table->curr_row->cells[table->curr_col];
}
void free_row(Table *table, struct Row *row)
{
for (size_t i = 0; i < table->num_cols; i++)
{
if (row->cells[i].text_needs_free)
{
free(row->cells[i].text);
}
}
free(row);
}
// ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ User-functions ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
/*
Returns: A new table with a single, empty row.
*/
Table get_empty_table()
{
struct Row *first_row = malloc_row(0);
return (Table){
.num_cols = 0,
.curr_col = 0,
.first_row = first_row,
.curr_row = first_row,
.num_rows = 1,
.alignments = { ALIGN_LEFT },
.borders_left = { BORDER_NONE },
.border_left_counters = { 0 }
};
}
/*
Summary: Prints table to stdout
*/
void print_table(Table *table)
{
print_table_internal(table);
}
/*
Summary: Frees all rows and content strings in cells created by add_cell_fmt.
Don't use the table any more, get a new one!
*/
void free_table(Table *table)
{
struct Row *row = table->first_row;
while (row != NULL)
{
struct Row *next_row = row->next_row;
free_row(table, row);
row = next_row;
}
}
void set_position(Table *table, size_t x, size_t y)
{
assert(table != NULL);
assert(x < MAX_COLS);
table->curr_col = x;
if (y < table->num_rows)
{
table->curr_row = get_row(table, y);
}
else
{
while (y >= table->num_rows)
{
table->curr_row = append_row(table);
}
}
}
/*
Summary: Next inserted cell will be inserted at first unset column of next row.
*/
void next_row(Table *table)
{
assert(table != NULL);
// Extend linked list if necessary
table->curr_col = 0;
if (table->curr_row->next_row == NULL)
{
table->curr_row = append_row(table);
}
else
{
table->curr_row = table->curr_row->next_row;
while (table->curr_col < MAX_COLS && table->curr_row->cells[table->curr_col].is_set)
{
table->curr_col++;
}
}
}
/*
Summary: Adds next cell. Buffer is not copied. print_table will access it.
Ensure that lifetime of buffer outlasts last call of print_table!
*/
void add_cell(Table *table, char *text)
{
add_cell_internal(table, text, false);
}
/*
Summary: Same as add_cell, but frees buffer on reset
*/
void add_cell_gc(Table *table, char *text)
{
add_cell_internal(table, text, true);
}
void add_empty_cell(Table *table)
{
add_cell_internal(table, NULL, false);
}
/*
Summary: Adds next cell and maintains buffer of text.
Use add_cell to save memory if you maintain a content string yourself.
*/
void add_cell_fmt(Table *table, char *fmt, ...)
{
va_list args;
va_start(args, fmt);
add_cell_vfmt(table, fmt, args);
va_end(args);
}
void add_cell_vfmt(Table *table, char *fmt, va_list args)
{
StringBuilder builder = strbuilder_create(STRBUILDER_STARTSIZE);
vstrbuilder_append(&builder, fmt, args);
add_cell_internal(table, builder.buffer, true);
}
/*
Summary: Puts contents of memory-contiguous 2D array into table cell by cell.
Strings are not copied. Ensure that lifetime of array outlasts last call of print_table.
Position of next insertion is first cell in next row.
*/
void add_cells_from_array(Table *table, size_t width, size_t height, char **array)
{
if (table->curr_col + width > MAX_COLS) return;
for (size_t i = 0; i < height; i++)
{
for (size_t j = 0; j < width; j++)
{
add_cell(table, *(array + i * width + j));
}
next_row(table);
}
}
/*
Summary: Sets default alignment of columns
*/
void set_default_alignments(Table *table, size_t num_alignments, TextAlignment *alignments)
{
assert(table != NULL);
assert(num_alignments < MAX_COLS);
for (size_t i = 0; i < num_alignments; i++)
{
table->alignments[i] = alignments[i];
}
}
/*
Summary: Overrides alignment of current cell
*/
void override_alignment(Table *table, TextAlignment alignment)
{
assert(table != NULL);
override_alignment_internal(get_curr_cell(table), alignment);
}
/*
Summary: Overrides alignment of all cells in current row
*/
void override_alignment_of_row(Table *table, TextAlignment alignment)
{
assert(table != NULL);
for (size_t i = 0; i < MAX_COLS; i++)
{
override_alignment_internal(&table->curr_row->cells[i], alignment);
}
}
void set_hline(Table *table, BorderStyle style)
{
assert(table != NULL);
if (table->curr_row->border_above != BORDER_NONE)
{
table->curr_row->border_above_counter--;
}
if (style != BORDER_NONE)
{
table->curr_row->border_above_counter++;
}
table->curr_row->border_above = style;
}
void set_vline(Table *table, size_t index, BorderStyle style)
{
assert(table != NULL);
assert (index < MAX_COLS);
if (table->num_cols <= index)
{
table->num_cols = index + 1;
}
if (table->borders_left[index] != BORDER_NONE)
{
table->border_left_counters[index]--;
}
if (style != BORDER_NONE)
{
table->border_left_counters[index]++;
}
table->borders_left[index] = style;
}
void make_boxed(Table *table, BorderStyle style)
{
assert(table != NULL);
set_position(table, 0, 0);
set_vline(table, 0, style);
set_hline(table, style);
set_position(table, table->num_cols, table->num_rows - 1);
set_vline(table, table->num_cols, style);
set_hline(table, style);
}
void override_left_border(Table *table, BorderStyle style)
{
assert(table != NULL);
if (get_curr_cell(table)->override_border_left && get_curr_cell(table)->border_left != BORDER_NONE)
{
table->border_left_counters[table->curr_col]--;
}
if (style != BORDER_NONE)
{
table->border_left_counters[table->curr_col]++;
}
get_curr_cell(table)->border_left = style;
get_curr_cell(table)->override_border_left = true;
}
void override_above_border(Table *table, BorderStyle style)
{
assert(table != NULL);
if (get_curr_cell(table)->override_border_above && get_curr_cell(table)->border_above != BORDER_NONE)
{
table->curr_row->border_above_counter--;
}
if (style != BORDER_NONE)
{
table->curr_row->border_above_counter++;
}
get_curr_cell(table)->border_above = style;
get_curr_cell(table)->override_border_above = true;
}
/*
Summary: Changes span of current cell
When spanning cell clashes with already set cells, the span will be truncated
*/
void set_span(Table *table, size_t span_x, size_t span_y)
{
assert(table != NULL);
assert(span_x != 0);
assert(span_y != 0);
assert(table->curr_col + span_x <= MAX_COLS);
struct Cell *cell = &table->curr_row->cells[table->curr_col];
cell->span_x = span_x;
cell->span_y = span_y;
table->num_cols = MAX(table->curr_col + span_x, table->num_cols);
// Inserts rows and sets child cells
struct Row *row = table->curr_row;
for (size_t i = 0; i < span_y; i++)
{
for (size_t j = 0; j < span_x; j++)
{
if (i == 0 && j == 0) continue;
struct Cell *child = &row->cells[cell->x + j];
if (!child->is_set)
{
child->is_set = true;
child->parent = cell;
if (j != 0)
{
child->border_left = BORDER_NONE;
child->override_border_left = true;
}
if (i != 0)
{
child->border_above = BORDER_NONE;
child->override_border_above = true;
}
}
else
{
// Span clashes with already set cell, truncate it and finalize method
cell->span_y = i;
cell->span_x = j;
return;
}
}
if (i + 1 < span_y && row->next_row == NULL)
{
row = append_row(table);
}
else
{
row = row->next_row;
}
}
}
void set_all_vlines(Table *table, BorderStyle style)
{
assert(table != NULL);
size_t cc = table->curr_col;
for (size_t i = 1; i < table->num_cols; i++)
{
set_vline(table, i, style);
}
table->curr_col = cc;
}