source: mainline/uspace/lib/c/generic/io/table.c@ b5746a2

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since b5746a2 was 1d6dd2a, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 7 years ago

Remove unnecessary includes from <stdio.h>.

  • Property mode set to 100644
File size: 11.5 KB
Line 
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#include <str.h>
43
44static table_column_t *table_column_first(table_t *);
45static table_column_t *table_column_next(table_column_t *);
46
47/** Add a new row at the end of the table.
48 *
49 * @param table Table
50 * @param rrow Place to store pointer to new row or @c NULL
51 *
52 * @return EOK on success, ENOMEM if out of memory
53 */
54static errno_t table_add_row(table_t *table, table_row_t **rrow)
55{
56 table_row_t *row;
57
58 row = calloc(1, sizeof(table_row_t));
59 if (row == NULL)
60 return ENOMEM;
61
62 row->table = table;
63 list_initialize(&row->cells);
64 list_append(&row->ltable, &table->rows);
65
66 if (rrow != NULL)
67 *rrow = row;
68 return EOK;
69}
70
71/** Add a new cell at the end of the row.
72 *
73 * @param row Row
74 * @param rcell Place to store pointer to new cell or @c NULL
75 * @return EOK on success, ENOMEM if out of memory
76 */
77static errno_t table_row_add_cell(table_row_t *row, table_cell_t **rcell)
78{
79 table_cell_t *cell;
80
81 cell = calloc(1, sizeof(table_cell_t));
82 if (cell == NULL)
83 return ENOMEM;
84
85 cell->text = NULL;
86
87 cell->row = row;
88 list_append(&cell->lrow, &row->cells);
89
90 if (rcell != NULL)
91 *rcell = cell;
92 return EOK;
93}
94
95/** Add a new column at the right side of the table.
96 *
97 * @param table Table
98 * @param rcolumn Place to store pointer to new column or @c NULL
99 *
100 * @return EOK on success, ENOMEM if out of memory
101 */
102static errno_t table_add_column(table_t *table, table_column_t **rcolumn)
103{
104 table_column_t *column;
105
106 column = calloc(1, sizeof(table_column_t));
107 if (column == NULL)
108 return ENOMEM;
109
110 column->table = table;
111 column->width = 0;
112 list_append(&column->ltable, &table->columns);
113
114 if (rcolumn != NULL)
115 *rcolumn = column;
116 return EOK;
117}
118
119/** Start writing next table cell.
120 *
121 * @param table Table
122 * @return EOK on success, ENOMEM if out of memory
123 */
124static errno_t table_write_next_cell(table_t *table)
125{
126 errno_t rc;
127
128 rc = table_row_add_cell(table->wrow, &table->wcell);
129 if (rc != EOK) {
130 assert(rc == ENOMEM);
131 return rc;
132 }
133
134 if (list_count(&table->wrow->cells) == 1) {
135 /* First column */
136 table->wcolumn = table_column_first(table);
137 } else {
138 /* Next column */
139 table->wcolumn = table_column_next(table->wcolumn);
140 }
141
142 if (table->wcolumn == NULL) {
143 rc = table_add_column(table, &table->wcolumn);
144 if (rc != EOK) {
145 assert(rc == ENOMEM);
146 return rc;
147 }
148 }
149
150 return EOK;
151}
152
153/** Start writing next table row.
154 *
155 * @param table Table
156 * @return EOK on success, ENOMEM if out of memory
157 */
158static errno_t table_write_next_row(table_t *table)
159{
160 errno_t rc;
161
162 rc = table_add_row(table, &table->wrow);
163 if (rc != EOK) {
164 assert(rc == ENOMEM);
165 return rc;
166 }
167
168 table->wcell = NULL;
169 return EOK;
170}
171
172/** Get first table row.
173 *
174 * @param table Table
175 * @return First table row or @c NULL if none
176 */
177static table_row_t *table_row_first(table_t *table)
178{
179 link_t *link;
180
181 link = list_first(&table->rows);
182 if (link == NULL)
183 return NULL;
184
185 return list_get_instance(link, table_row_t, ltable);
186}
187
188/** Get next table row.
189 *
190 * @param cur Current row
191 * @return Next table row or @c NULL if none
192 */
193static table_row_t *table_row_next(table_row_t *cur)
194{
195 link_t *link;
196
197 link = list_next(&cur->ltable, &cur->table->rows);
198 if (link == NULL)
199 return NULL;
200
201 return list_get_instance(link, table_row_t, ltable);
202}
203
204/** Get first cell in table row.
205 *
206 * @param row Table row
207 * @return First cell in row or @c NULL if none
208 */
209static table_cell_t *table_row_cell_first(table_row_t *row)
210{
211 link_t *link;
212
213 link = list_first(&row->cells);
214 if (link == NULL)
215 return NULL;
216
217 return list_get_instance(link, table_cell_t, lrow);
218}
219
220/** Get next cell in table row.
221 *
222 * @param cur Current cell
223 * @return Next cell in table row or @c NULL if none
224 */
225static table_cell_t *table_row_cell_next(table_cell_t *cur)
226{
227 link_t *link;
228
229 link = list_next(&cur->lrow, &cur->row->cells);
230 if (link == NULL)
231 return NULL;
232
233 return list_get_instance(link, table_cell_t, lrow);
234}
235
236/** Get first table column.
237 *
238 * @param table Table
239 * @return First table column or @c NULL if none
240 */
241static table_column_t *table_column_first(table_t *table)
242{
243 link_t *link;
244
245 link = list_first(&table->columns);
246 if (link == NULL)
247 return NULL;
248
249 return list_get_instance(link, table_column_t, ltable);
250}
251
252/** Get next table column.
253 *
254 * @param cur Current column
255 * @return Next table column or @c NULL if none
256 */
257static table_column_t *table_column_next(table_column_t *cur)
258{
259 link_t *link;
260
261 link = list_next(&cur->ltable, &cur->table->columns);
262 if (link == NULL)
263 return NULL;
264
265 return list_get_instance(link, table_column_t, ltable);
266}
267
268/** Append to cell text.
269 *
270 * @param cell Cell
271 * @param str Text to append
272 * @param len Max number of bytes to read from str
273 * @return EOK on success, ENOMEM if out of memory
274 */
275static errno_t table_cell_extend(table_cell_t *cell, const char *str, size_t len)
276{
277 char *cstr;
278 int rc;
279
280 if (cell->text == NULL) {
281 cell->text = str_ndup(str, len);
282 if (cell->text == NULL)
283 return ENOMEM;
284 } else {
285 rc = asprintf(&cstr, "%s%.*s", cell->text, (int)len, str);
286 if (rc < 0)
287 return ENOMEM;
288
289 free(cell->text);
290 cell->text = cstr;
291 }
292
293 return EOK;
294}
295
296/** Create table.
297 *
298 * @para, rtable Place to store pointer to new table.
299 * @return EOK on success, ENOMEM if out of memory
300 */
301errno_t table_create(table_t **rtable)
302{
303 table_t *table;
304 errno_t rc;
305
306 table = calloc(1, sizeof(table_t));
307 if (table == NULL)
308 return ENOMEM;
309
310 table->error = EOK;
311 list_initialize(&table->rows);
312 list_initialize(&table->columns);
313
314 rc = table_add_row(table, &table->wrow);
315 if (rc != EOK)
316 goto error;
317
318 rc = table_row_add_cell(table->wrow, &table->wcell);
319 if (rc != EOK)
320 goto error;
321
322 rc = table_add_column(table, &table->wcolumn);
323 if (rc != EOK)
324 goto error;
325
326 *rtable = table;
327 return EOK;
328error:
329 table_destroy(table);
330 return rc;
331}
332
333/** Destroy table.
334 *
335 * @param table Table
336 */
337void table_destroy(table_t *table)
338{
339 table_row_t *row;
340 table_cell_t *cell;
341 table_column_t *column;
342
343 if (table == NULL)
344 return;
345
346 row = table_row_first(table);
347 while (row != NULL) {
348 cell = table_row_cell_first(row);
349 while (cell != NULL) {
350 list_remove(&cell->lrow);
351 free(cell->text);
352 free(cell);
353 cell = table_row_cell_first(row);
354 }
355
356 list_remove(&row->ltable);
357 free(row);
358
359 row = table_row_first(table);
360 }
361
362 column = table_column_first(table);
363 while (column != NULL) {
364 list_remove(&column->ltable);
365 free(column);
366
367 column = table_column_first(table);
368 }
369
370 free(table);
371}
372
373/** Print out table contents to a file stream.
374 *
375 * @param table Table
376 * @param f File where to write the output
377 *
378 * @return EOK on success, EIO on I/O error
379 */
380errno_t table_print_out(table_t *table, FILE *f)
381{
382 table_row_t *row;
383 table_cell_t *cell;
384 table_column_t *column;
385 bool firstc;
386 bool firstr;
387 size_t spacing;
388 size_t i;
389 int rc;
390
391 if (table->error != EOK)
392 return table->error;
393
394 row = table_row_first(table);
395 firstr = true;
396 while (row != NULL) {
397 cell = table_row_cell_first(row);
398 if (cell == NULL)
399 break;
400
401 column = table_column_first(table);
402 firstc = true;
403
404 while (cell != NULL && cell->text != NULL) {
405 spacing = firstc ? table->metrics.margin_left : 1;
406 for (i = 0; i < spacing; i++) {
407 rc = fprintf(f, " ");
408 if (rc < 0)
409 return EIO;
410 }
411
412 rc = fprintf(f, "%*s", -(int)column->width, cell->text);
413 if (rc < 0)
414 return EIO;
415
416 cell = table_row_cell_next(cell);
417 column = table_column_next(column);
418 firstc = false;
419 }
420 rc = fprintf(f, "\n");
421 if (rc < 0)
422 return EIO;
423
424 if (firstr && table->header_row) {
425 /* Display header separator */
426 column = table_column_first(table);
427 firstc = true;
428 while (column != NULL) {
429 spacing = firstc ? table->metrics.margin_left : 1;
430 for (i = 0; i < spacing; i++) {
431 rc = fprintf(f, " ");
432 if (rc < 0)
433 return EIO;
434 }
435
436 for (i = 0; i < column->width; i++) {
437 rc = fprintf(f, "=");
438 if (rc < 0)
439 return EIO;
440 }
441
442 column = table_column_next(column);
443 firstc = false;
444 }
445
446 rc = fprintf(f, "\n");
447 if (rc < 0)
448 return EIO;
449 }
450
451 row = table_row_next(row);
452 firstr = false;
453 }
454 return EOK;
455}
456
457/** Start a header row.
458 *
459 * @param table Table
460 */
461void table_header_row(table_t *table)
462{
463 assert(list_count(&table->rows) == 1);
464 assert(!table->header_row);
465 table->header_row = true;
466}
467
468/** Insert text into table cell(s).
469 *
470 * Appends text to the current cell. A tab character starts a new cell.
471 * A newline character starts a new row.
472 *
473 * @param table Table
474 * @param fmt Format string
475 * @return EOK on success, ENOMEM if out of memory
476 */
477errno_t table_printf(table_t *table, const char *fmt, ...)
478{
479 va_list args;
480 errno_t rc;
481 int ret;
482 char *str;
483 char *sp, *ep;
484 size_t width;
485
486 if (table->error != EOK)
487 return table->error;
488
489 va_start(args, fmt);
490 ret = vasprintf(&str, fmt, args);
491 va_end(args);
492
493 if (ret < 0) {
494 table->error = ENOMEM;
495 return table->error;
496 }
497
498 sp = str;
499 while (*sp != '\0' && table->error == EOK) {
500 ep = sp;
501 while (*ep != '\0' && *ep != '\t' && *ep != '\n')
502 ++ep;
503
504 if (table->wcell == NULL) {
505 rc = table_write_next_cell(table);
506 if (rc != EOK) {
507 assert(rc == ENOMEM);
508 goto out;
509 }
510 }
511
512 rc = table_cell_extend(table->wcell, sp, ep - sp);
513 if (rc != EOK) {
514 assert(rc == ENOMEM);
515 table->error = ENOMEM;
516 goto out;
517 }
518
519 /* Update column width */
520 width = str_width(table->wcell->text);
521 if (width > table->wcolumn->width)
522 table->wcolumn->width = width;
523
524 if (*ep == '\t')
525 rc = table_write_next_cell(table);
526 else if (*ep == '\n')
527 rc = table_write_next_row(table);
528 else
529 break;
530
531 if (rc != EOK) {
532 assert(rc == ENOMEM);
533 table->error = ENOMEM;
534 goto out;
535 }
536
537 sp = ep + 1;
538 }
539
540 rc = table->error;
541out:
542 free(str);
543 return rc;
544}
545
546/** Return table error code.
547 *
548 * @param table Table
549 * @return EOK if no error indicated, non-zero error code otherwise
550 */
551errno_t table_get_error(table_t *table)
552{
553 return table->error;
554}
555
556/** Set left table margin.
557 *
558 * @param table Table
559 * @param mleft Left margin
560 */
561void table_set_margin_left(table_t *table, size_t mleft)
562{
563 table->metrics.margin_left = mleft;
564}
565
566/** @}
567 */
Note: See TracBrowser for help on using the repository browser.