1 | /*
|
---|
2 | * Copyright (c) 2017 Jiri Svoboda
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup libc
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /**
|
---|
33 | * @file
|
---|
34 | * @brief Tabulate text
|
---|
35 | */
|
---|
36 |
|
---|
37 | #include <errno.h>
|
---|
38 | #include <io/table.h>
|
---|
39 | #include <stdarg.h>
|
---|
40 | #include <stdio.h>
|
---|
41 | #include <stdlib.h>
|
---|
42 |
|
---|
43 | static table_column_t *table_column_first(table_t *);
|
---|
44 | static table_column_t *table_column_next(table_column_t *);
|
---|
45 |
|
---|
46 | /** Add a new row at the end of the table.
|
---|
47 | *
|
---|
48 | * @param table Table
|
---|
49 | * @param rrow Place to store pointer to new row or @c NULL
|
---|
50 | *
|
---|
51 | * @return EOK on success, ENOMEM if out of memory
|
---|
52 | */
|
---|
53 | static int table_add_row(table_t *table, table_row_t **rrow)
|
---|
54 | {
|
---|
55 | table_row_t *row;
|
---|
56 |
|
---|
57 | row = calloc(1, sizeof(table_row_t));
|
---|
58 | if (row == NULL)
|
---|
59 | return ENOMEM;
|
---|
60 |
|
---|
61 | row->table = table;
|
---|
62 | list_initialize(&row->cells);
|
---|
63 | list_append(&row->ltable, &table->rows);
|
---|
64 |
|
---|
65 | if (rrow != NULL)
|
---|
66 | *rrow = row;
|
---|
67 | return EOK;
|
---|
68 | }
|
---|
69 |
|
---|
70 | /** Add a new cell at the end of the row.
|
---|
71 | *
|
---|
72 | * @param row Row
|
---|
73 | * @param rcell Place to store pointer to new cell or @c NULL
|
---|
74 | * @return EOK on success, ENOMEM if out of memory
|
---|
75 | */
|
---|
76 | static int table_row_add_cell(table_row_t *row, table_cell_t **rcell)
|
---|
77 | {
|
---|
78 | table_cell_t *cell;
|
---|
79 |
|
---|
80 | cell = calloc(1, sizeof(table_cell_t));
|
---|
81 | if (cell == NULL)
|
---|
82 | return ENOMEM;
|
---|
83 |
|
---|
84 | cell->text = NULL;
|
---|
85 |
|
---|
86 | cell->row = row;
|
---|
87 | list_append(&cell->lrow, &row->cells);
|
---|
88 |
|
---|
89 | if (rcell != NULL)
|
---|
90 | *rcell = cell;
|
---|
91 | return EOK;
|
---|
92 | }
|
---|
93 |
|
---|
94 | /** Add a new column at the right side of the table.
|
---|
95 | *
|
---|
96 | * @param table Table
|
---|
97 | * @param rcolumn Place to store pointer to new column or @c NULL
|
---|
98 | *
|
---|
99 | * @return EOK on success, ENOMEM if out of memory
|
---|
100 | */
|
---|
101 | static int table_add_column(table_t *table, table_column_t **rcolumn)
|
---|
102 | {
|
---|
103 | table_column_t *column;
|
---|
104 |
|
---|
105 | column = calloc(1, sizeof(table_column_t));
|
---|
106 | if (column == NULL)
|
---|
107 | return ENOMEM;
|
---|
108 |
|
---|
109 | column->table = table;
|
---|
110 | column->width = 0;
|
---|
111 | list_append(&column->ltable, &table->columns);
|
---|
112 |
|
---|
113 | if (rcolumn != NULL)
|
---|
114 | *rcolumn = column;
|
---|
115 | return EOK;
|
---|
116 | }
|
---|
117 |
|
---|
118 | /** Start writing next table cell.
|
---|
119 | *
|
---|
120 | * @param table Table
|
---|
121 | * @return EOK on success, ENOMEM if out of memory
|
---|
122 | */
|
---|
123 | static int table_write_next_cell(table_t *table)
|
---|
124 | {
|
---|
125 | int rc;
|
---|
126 |
|
---|
127 | rc = table_row_add_cell(table->wrow, &table->wcell);
|
---|
128 | if (rc != EOK) {
|
---|
129 | assert(rc == ENOMEM);
|
---|
130 | return rc;
|
---|
131 | }
|
---|
132 |
|
---|
133 | if (list_count(&table->wrow->cells) == 1) {
|
---|
134 | /* First column */
|
---|
135 | table->wcolumn = table_column_first(table);
|
---|
136 | } else {
|
---|
137 | /* Next column */
|
---|
138 | table->wcolumn = table_column_next(table->wcolumn);
|
---|
139 | }
|
---|
140 |
|
---|
141 | if (table->wcolumn == NULL) {
|
---|
142 | rc = table_add_column(table, &table->wcolumn);
|
---|
143 | if (rc != EOK) {
|
---|
144 | assert(rc == ENOMEM);
|
---|
145 | return rc;
|
---|
146 | }
|
---|
147 | }
|
---|
148 |
|
---|
149 | return EOK;
|
---|
150 | }
|
---|
151 |
|
---|
152 | /** Start writing next table row.
|
---|
153 | *
|
---|
154 | * @param table Table
|
---|
155 | * @return EOK on success, ENOMEM if out of memory
|
---|
156 | */
|
---|
157 | static int table_write_next_row(table_t *table)
|
---|
158 | {
|
---|
159 | int rc;
|
---|
160 |
|
---|
161 | rc = table_add_row(table, &table->wrow);
|
---|
162 | if (rc != EOK) {
|
---|
163 | assert(rc == ENOMEM);
|
---|
164 | return rc;
|
---|
165 | }
|
---|
166 |
|
---|
167 | table->wcell = NULL;
|
---|
168 | return EOK;
|
---|
169 | }
|
---|
170 |
|
---|
171 | /** Get first table row.
|
---|
172 | *
|
---|
173 | * @param table Table
|
---|
174 | * @return First table row or @c NULL if none
|
---|
175 | */
|
---|
176 | static table_row_t *table_row_first(table_t *table)
|
---|
177 | {
|
---|
178 | link_t *link;
|
---|
179 |
|
---|
180 | link = list_first(&table->rows);
|
---|
181 | if (link == NULL)
|
---|
182 | return NULL;
|
---|
183 |
|
---|
184 | return list_get_instance(link, table_row_t, ltable);
|
---|
185 | }
|
---|
186 |
|
---|
187 | /** Get next table row.
|
---|
188 | *
|
---|
189 | * @param cur Current row
|
---|
190 | * @return Next table row or @c NULL if none
|
---|
191 | */
|
---|
192 | static table_row_t *table_row_next(table_row_t *cur)
|
---|
193 | {
|
---|
194 | link_t *link;
|
---|
195 |
|
---|
196 | link = list_next(&cur->ltable, &cur->table->rows);
|
---|
197 | if (link == NULL)
|
---|
198 | return NULL;
|
---|
199 |
|
---|
200 | return list_get_instance(link, table_row_t, ltable);
|
---|
201 | }
|
---|
202 |
|
---|
203 | /** Get first cell in table row.
|
---|
204 | *
|
---|
205 | * @param row Table row
|
---|
206 | * @return First cell in row or @c NULL if none
|
---|
207 | */
|
---|
208 | static table_cell_t *table_row_cell_first(table_row_t *row)
|
---|
209 | {
|
---|
210 | link_t *link;
|
---|
211 |
|
---|
212 | link = list_first(&row->cells);
|
---|
213 | if (link == NULL)
|
---|
214 | return NULL;
|
---|
215 |
|
---|
216 | return list_get_instance(link, table_cell_t, lrow);
|
---|
217 | }
|
---|
218 |
|
---|
219 | /** Get next cell in table row.
|
---|
220 | *
|
---|
221 | * @param cur Current cell
|
---|
222 | * @return Next cell in table row or @c NULL if none
|
---|
223 | */
|
---|
224 | static table_cell_t *table_row_cell_next(table_cell_t *cur)
|
---|
225 | {
|
---|
226 | link_t *link;
|
---|
227 |
|
---|
228 | link = list_next(&cur->lrow, &cur->row->cells);
|
---|
229 | if (link == NULL)
|
---|
230 | return NULL;
|
---|
231 |
|
---|
232 | return list_get_instance(link, table_cell_t, lrow);
|
---|
233 | }
|
---|
234 |
|
---|
235 | /** Get first table column.
|
---|
236 | *
|
---|
237 | * @param table Table
|
---|
238 | * @return First table column or @c NULL if none
|
---|
239 | */
|
---|
240 | static table_column_t *table_column_first(table_t *table)
|
---|
241 | {
|
---|
242 | link_t *link;
|
---|
243 |
|
---|
244 | link = list_first(&table->columns);
|
---|
245 | if (link == NULL)
|
---|
246 | return NULL;
|
---|
247 |
|
---|
248 | return list_get_instance(link, table_column_t, ltable);
|
---|
249 | }
|
---|
250 |
|
---|
251 | /** Get next table column.
|
---|
252 | *
|
---|
253 | * @param cur Current column
|
---|
254 | * @return Next table column or @c NULL if none
|
---|
255 | */
|
---|
256 | static table_column_t *table_column_next(table_column_t *cur)
|
---|
257 | {
|
---|
258 | link_t *link;
|
---|
259 |
|
---|
260 | link = list_next(&cur->ltable, &cur->table->columns);
|
---|
261 | if (link == NULL)
|
---|
262 | return NULL;
|
---|
263 |
|
---|
264 | return list_get_instance(link, table_column_t, ltable);
|
---|
265 | }
|
---|
266 |
|
---|
267 | /** Append to cell text.
|
---|
268 | *
|
---|
269 | * @param cell Cell
|
---|
270 | * @param str Text to append
|
---|
271 | * @param len Max number of bytes to read from str
|
---|
272 | * @return EOK on success, ENOMEM if out of memory
|
---|
273 | */
|
---|
274 | static int table_cell_extend(table_cell_t *cell, const char *str, size_t len)
|
---|
275 | {
|
---|
276 | char *cstr;
|
---|
277 | int rc;
|
---|
278 |
|
---|
279 | if (cell->text == NULL) {
|
---|
280 | cell->text = str_ndup(str, len);
|
---|
281 | if (cell->text == NULL)
|
---|
282 | return ENOMEM;
|
---|
283 | } else {
|
---|
284 | rc = asprintf(&cstr, "%s%.*s", cell->text, (int)len, str);
|
---|
285 | if (rc < 0)
|
---|
286 | return ENOMEM;
|
---|
287 |
|
---|
288 | free(cell->text);
|
---|
289 | cell->text = cstr;
|
---|
290 | }
|
---|
291 |
|
---|
292 | return EOK;
|
---|
293 | }
|
---|
294 |
|
---|
295 | /** Create table.
|
---|
296 | *
|
---|
297 | * @para, rtable Place to store pointer to new table.
|
---|
298 | * @return EOK on success, ENOMEM if out of memory
|
---|
299 | */
|
---|
300 | int table_create(table_t **rtable)
|
---|
301 | {
|
---|
302 | table_t *table;
|
---|
303 | int rc;
|
---|
304 |
|
---|
305 | table = calloc(1, sizeof(table_t));
|
---|
306 | if (table == NULL)
|
---|
307 | return ENOMEM;
|
---|
308 |
|
---|
309 | table->error = EOK;
|
---|
310 | list_initialize(&table->rows);
|
---|
311 | list_initialize(&table->columns);
|
---|
312 |
|
---|
313 | rc = table_add_row(table, &table->wrow);
|
---|
314 | if (rc != EOK)
|
---|
315 | goto error;
|
---|
316 |
|
---|
317 | rc = table_row_add_cell(table->wrow, &table->wcell);
|
---|
318 | if (rc != EOK)
|
---|
319 | goto error;
|
---|
320 |
|
---|
321 | rc = table_add_column(table, &table->wcolumn);
|
---|
322 | if (rc != EOK)
|
---|
323 | goto error;
|
---|
324 |
|
---|
325 | *rtable = table;
|
---|
326 | return EOK;
|
---|
327 | error:
|
---|
328 | table_destroy(table);
|
---|
329 | return rc;
|
---|
330 | }
|
---|
331 |
|
---|
332 | /** Destroy table.
|
---|
333 | *
|
---|
334 | * @param table Table
|
---|
335 | */
|
---|
336 | void table_destroy(table_t *table)
|
---|
337 | {
|
---|
338 | table_row_t *row;
|
---|
339 | table_cell_t *cell;
|
---|
340 | table_column_t *column;
|
---|
341 |
|
---|
342 | if (table == NULL)
|
---|
343 | return;
|
---|
344 |
|
---|
345 | row = table_row_first(table);
|
---|
346 | while (row != NULL) {
|
---|
347 | cell = table_row_cell_first(row);
|
---|
348 | while (cell != NULL) {
|
---|
349 | list_remove(&cell->lrow);
|
---|
350 | free(cell->text);
|
---|
351 | free(cell);
|
---|
352 | cell = table_row_cell_first(row);
|
---|
353 | }
|
---|
354 |
|
---|
355 | list_remove(&row->ltable);
|
---|
356 | free(row);
|
---|
357 |
|
---|
358 | row = table_row_first(table);
|
---|
359 | }
|
---|
360 |
|
---|
361 | column = table_column_first(table);
|
---|
362 | while (column != NULL) {
|
---|
363 | list_remove(&column->ltable);
|
---|
364 | free(column);
|
---|
365 |
|
---|
366 | column = table_column_first(table);
|
---|
367 | }
|
---|
368 |
|
---|
369 | free(table);
|
---|
370 | }
|
---|
371 |
|
---|
372 | /** Print out table contents to a file stream.
|
---|
373 | *
|
---|
374 | * @param table Table
|
---|
375 | * @param f File where to write the output
|
---|
376 | *
|
---|
377 | * @return EOK on success, EIO on I/O error
|
---|
378 | */
|
---|
379 | int table_print_out(table_t *table, FILE *f)
|
---|
380 | {
|
---|
381 | table_row_t *row;
|
---|
382 | table_cell_t *cell;
|
---|
383 | table_column_t *column;
|
---|
384 | bool firstc;
|
---|
385 | bool firstr;
|
---|
386 | size_t spacing;
|
---|
387 | size_t i;
|
---|
388 | int rc;
|
---|
389 |
|
---|
390 | if (table->error != EOK)
|
---|
391 | return table->error;
|
---|
392 |
|
---|
393 | row = table_row_first(table);
|
---|
394 | firstr = true;
|
---|
395 | while (row != NULL) {
|
---|
396 | cell = table_row_cell_first(row);
|
---|
397 | if (cell == NULL)
|
---|
398 | break;
|
---|
399 |
|
---|
400 | column = table_column_first(table);
|
---|
401 | firstc = true;
|
---|
402 |
|
---|
403 | while (cell != NULL && cell->text != NULL) {
|
---|
404 | spacing = firstc ? table->metrics.margin_left : 1;
|
---|
405 | for (i = 0; i < spacing; i++) {
|
---|
406 | rc = fprintf(f, " ");
|
---|
407 | if (rc < 0)
|
---|
408 | return EIO;
|
---|
409 | }
|
---|
410 |
|
---|
411 | rc = fprintf(f, "%*s", -(int)column->width, cell->text);
|
---|
412 | if (rc < 0)
|
---|
413 | return EIO;
|
---|
414 |
|
---|
415 | cell = table_row_cell_next(cell);
|
---|
416 | column = table_column_next(column);
|
---|
417 | firstc = false;
|
---|
418 | }
|
---|
419 | rc = fprintf(f, "\n");
|
---|
420 | if (rc < 0)
|
---|
421 | return EIO;
|
---|
422 |
|
---|
423 | if (firstr && table->header_row) {
|
---|
424 | /* Display header separator */
|
---|
425 | column = table_column_first(table);
|
---|
426 | firstc = true;
|
---|
427 | while (column != NULL) {
|
---|
428 | spacing = firstc ? table->metrics.margin_left : 1;
|
---|
429 | for (i = 0; i < spacing; i++) {
|
---|
430 | rc = fprintf(f, " ");
|
---|
431 | if (rc < 0)
|
---|
432 | return EIO;
|
---|
433 | }
|
---|
434 |
|
---|
435 | for (i = 0; i < column->width; i++) {
|
---|
436 | rc = fprintf(f, "=");
|
---|
437 | if (rc < 0)
|
---|
438 | return EIO;
|
---|
439 | }
|
---|
440 |
|
---|
441 | column = table_column_next(column);
|
---|
442 | firstc = false;
|
---|
443 | }
|
---|
444 |
|
---|
445 | rc = fprintf(f, "\n");
|
---|
446 | if (rc < 0)
|
---|
447 | return EIO;
|
---|
448 | }
|
---|
449 |
|
---|
450 | row = table_row_next(row);
|
---|
451 | firstr = false;
|
---|
452 | }
|
---|
453 | return EOK;
|
---|
454 | }
|
---|
455 |
|
---|
456 | /** Start a header row.
|
---|
457 | *
|
---|
458 | * @param table Table
|
---|
459 | */
|
---|
460 | void table_header_row(table_t *table)
|
---|
461 | {
|
---|
462 | assert(list_count(&table->rows) == 1);
|
---|
463 | assert(!table->header_row);
|
---|
464 | table->header_row = true;
|
---|
465 | }
|
---|
466 |
|
---|
467 | /** Insert text into table cell(s).
|
---|
468 | *
|
---|
469 | * Appends text to the current cell. A tab character starts a new cell.
|
---|
470 | * A newline character starts a new row.
|
---|
471 | *
|
---|
472 | * @param table Table
|
---|
473 | * @param fmt Format string
|
---|
474 | * @return EOK on success, ENOMEM if out of memory
|
---|
475 | */
|
---|
476 | int table_printf(table_t *table, const char *fmt, ...)
|
---|
477 | {
|
---|
478 | va_list args;
|
---|
479 | int rc;
|
---|
480 | int ret;
|
---|
481 | char *str;
|
---|
482 | char *sp, *ep;
|
---|
483 | size_t width;
|
---|
484 |
|
---|
485 | if (table->error != EOK)
|
---|
486 | return table->error;
|
---|
487 |
|
---|
488 | va_start(args, fmt);
|
---|
489 | ret = vasprintf(&str, fmt, args);
|
---|
490 | va_end(args);
|
---|
491 |
|
---|
492 | if (ret < 0) {
|
---|
493 | table->error = ENOMEM;
|
---|
494 | return table->error;
|
---|
495 | }
|
---|
496 |
|
---|
497 | sp = str;
|
---|
498 | while (*sp != '\0' && table->error == EOK) {
|
---|
499 | ep = sp;
|
---|
500 | while (*ep != '\0' && *ep != '\t' && *ep != '\n')
|
---|
501 | ++ep;
|
---|
502 |
|
---|
503 | if (table->wcell == NULL) {
|
---|
504 | rc = table_write_next_cell(table);
|
---|
505 | if (rc != EOK) {
|
---|
506 | assert(rc == ENOMEM);
|
---|
507 | goto out;
|
---|
508 | }
|
---|
509 | }
|
---|
510 |
|
---|
511 | rc = table_cell_extend(table->wcell, sp, ep - sp);
|
---|
512 | if (rc != EOK) {
|
---|
513 | assert(rc == ENOMEM);
|
---|
514 | table->error = ENOMEM;
|
---|
515 | goto out;
|
---|
516 | }
|
---|
517 |
|
---|
518 | /* Update column width */
|
---|
519 | width = str_width(table->wcell->text);
|
---|
520 | if (width > table->wcolumn->width)
|
---|
521 | table->wcolumn->width = width;
|
---|
522 |
|
---|
523 | if (*ep == '\t')
|
---|
524 | rc = table_write_next_cell(table);
|
---|
525 | else if (*ep == '\n')
|
---|
526 | rc = table_write_next_row(table);
|
---|
527 | else
|
---|
528 | break;
|
---|
529 |
|
---|
530 | if (rc != EOK) {
|
---|
531 | assert(rc == ENOMEM);
|
---|
532 | table->error = ENOMEM;
|
---|
533 | goto out;
|
---|
534 | }
|
---|
535 |
|
---|
536 | sp = ep + 1;
|
---|
537 | }
|
---|
538 |
|
---|
539 | rc = table->error;
|
---|
540 | out:
|
---|
541 | free(str);
|
---|
542 | return rc;
|
---|
543 | }
|
---|
544 |
|
---|
545 | /** Return table error code.
|
---|
546 | *
|
---|
547 | * @param table Table
|
---|
548 | * @return EOK if no error indicated, non-zero error code otherwise
|
---|
549 | */
|
---|
550 | int table_get_error(table_t *table)
|
---|
551 | {
|
---|
552 | return table->error;
|
---|
553 | }
|
---|
554 |
|
---|
555 | /** Set left table margin.
|
---|
556 | *
|
---|
557 | * @param table Table
|
---|
558 | * @param mleft Left margin
|
---|
559 | */
|
---|
560 | void table_set_margin_left(table_t *table, size_t mleft)
|
---|
561 | {
|
---|
562 | table->metrics.margin_left = mleft;
|
---|
563 | }
|
---|
564 |
|
---|
565 | /** @}
|
---|
566 | */
|
---|