[5e962ad] | 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 | /** @file
|
---|
| 33 | */
|
---|
| 34 |
|
---|
| 35 | #ifndef LIBC_IO_TABLE_H_
|
---|
| 36 | #define LIBC_IO_TABLE_H_
|
---|
| 37 |
|
---|
| 38 | #include <adt/list.h>
|
---|
| 39 | #include <stdbool.h>
|
---|
| 40 | #include <stddef.h>
|
---|
| 41 | #include <stdio.h>
|
---|
[d5c1051] | 42 | #include <errno.h>
|
---|
[5e962ad] | 43 |
|
---|
| 44 | /** Table metrics */
|
---|
| 45 | typedef struct {
|
---|
| 46 | /** Space to the left of the table */
|
---|
| 47 | size_t margin_left;
|
---|
| 48 | } table_metrics_t;
|
---|
| 49 |
|
---|
| 50 | /** Table cell */
|
---|
| 51 | typedef struct {
|
---|
| 52 | /** Containing row */
|
---|
| 53 | struct table_row *row;
|
---|
| 54 | /** Link to table_row_t.cells */
|
---|
| 55 | link_t lrow;
|
---|
| 56 | /** Cell text */
|
---|
| 57 | char *text;
|
---|
| 58 | } table_cell_t;
|
---|
| 59 |
|
---|
| 60 | /** Table row */
|
---|
| 61 | typedef struct table_row {
|
---|
| 62 | /** Containing table */
|
---|
| 63 | struct table *table;
|
---|
| 64 | /** Link to table_t.rows */
|
---|
| 65 | link_t ltable;
|
---|
| 66 | /** Cells of this row */
|
---|
| 67 | list_t cells;
|
---|
| 68 | } table_row_t;
|
---|
| 69 |
|
---|
| 70 | /** Table column */
|
---|
| 71 | typedef struct {
|
---|
| 72 | /** Containing table */
|
---|
| 73 | struct table *table;
|
---|
| 74 | /** Link to table_t.columns */
|
---|
| 75 | link_t ltable;
|
---|
| 76 | /** Column width */
|
---|
| 77 | size_t width;
|
---|
| 78 | } table_column_t;
|
---|
| 79 |
|
---|
| 80 | /** Table */
|
---|
| 81 | typedef struct table {
|
---|
| 82 | /** @c true if the first row is a header row */
|
---|
| 83 | bool header_row;
|
---|
| 84 | /** Encountered error while writing to table */
|
---|
| 85 | int error;
|
---|
| 86 | /** Table rows */
|
---|
| 87 | list_t rows; /* of table_row_t */
|
---|
| 88 | /** Table columns */
|
---|
| 89 | list_t columns; /* of table_column_t */
|
---|
| 90 | /** Current row */
|
---|
| 91 | table_row_t *wrow;
|
---|
| 92 | /** Current cell */
|
---|
| 93 | table_cell_t *wcell;
|
---|
| 94 | /** Current column */
|
---|
| 95 | table_column_t *wcolumn;
|
---|
| 96 | /** Table metrics */
|
---|
| 97 | table_metrics_t metrics;
|
---|
| 98 | } table_t;
|
---|
| 99 |
|
---|
| 100 | extern int table_create(table_t **);
|
---|
| 101 | extern void table_destroy(table_t *);
|
---|
| 102 | extern int table_print_out(table_t *, FILE *);
|
---|
| 103 | extern void table_header_row(table_t *);
|
---|
| 104 | extern int table_printf(table_t *, const char *, ...);
|
---|
| 105 | extern int table_get_error(table_t *);
|
---|
| 106 | extern void table_set_margin_left(table_t *, size_t);
|
---|
| 107 |
|
---|
| 108 | #endif
|
---|
| 109 |
|
---|
| 110 | /** @}
|
---|
| 111 | */
|
---|