HelenOS sources
This source file includes following definitions.
- table_add_row
- table_row_add_cell
- table_add_column
- table_write_next_cell
- table_write_next_row
- table_row_first
- table_row_next
- table_row_cell_first
- table_row_cell_next
- table_column_first
- table_column_next
- table_cell_extend
- table_create
- table_destroy
- table_print_out
- table_header_row
- table_printf
- table_get_error
- table_set_margin_left
#include <errno.h>
#include <io/table.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <str.h>
static table_column_t *table_column_first(table_t *);
static table_column_t *table_column_next(table_column_t *);
static errno_t table_add_row(table_t *table, table_row_t **rrow)
{
table_row_t *row;
row = calloc(1, sizeof(table_row_t));
if (row == NULL)
return ENOMEM;
row->table = table;
list_initialize(&row->cells);
list_append(&row->ltable, &table->rows);
if (rrow != NULL)
*rrow = row;
return EOK;
}
static errno_t table_row_add_cell(table_row_t *row, table_cell_t **rcell)
{
table_cell_t *cell;
cell = calloc(1, sizeof(table_cell_t));
if (cell == NULL)
return ENOMEM;
cell->text = NULL;
cell->row = row;
list_append(&cell->lrow, &row->cells);
if (rcell != NULL)
*rcell = cell;
return EOK;
}
static errno_t table_add_column(table_t *table, table_column_t **rcolumn)
{
table_column_t *column;
column = calloc(1, sizeof(table_column_t));
if (column == NULL)
return ENOMEM;
column->table = table;
column->width = 0;
list_append(&column->ltable, &table->columns);
if (rcolumn != NULL)
*rcolumn = column;
return EOK;
}
static errno_t table_write_next_cell(table_t *table)
{
errno_t rc;
rc = table_row_add_cell(table->wrow, &table->wcell);
if (rc != EOK) {
assert(rc == ENOMEM);
return rc;
}
if (list_count(&table->wrow->cells) == 1) {
table->wcolumn = table_column_first(table);
} else {
table->wcolumn = table_column_next(table->wcolumn);
}
if (table->wcolumn == NULL) {
rc = table_add_column(table, &table->wcolumn);
if (rc != EOK) {
assert(rc == ENOMEM);
return rc;
}
}
return EOK;
}
static errno_t table_write_next_row(table_t *table)
{
errno_t rc;
rc = table_add_row(table, &table->wrow);
if (rc != EOK) {
assert(rc == ENOMEM);
return rc;
}
table->wcell = NULL;
return EOK;
}
static table_row_t *table_row_first(table_t *table)
{
link_t *link;
link = list_first(&table->rows);
if (link == NULL)
return NULL;
return list_get_instance(link, table_row_t, ltable);
}
static table_row_t *table_row_next(table_row_t *cur)
{
link_t *link;
link = list_next(&cur->ltable, &cur->table->rows);
if (link == NULL)
return NULL;
return list_get_instance(link, table_row_t, ltable);
}
static table_cell_t *table_row_cell_first(table_row_t *row)
{
link_t *link;
link = list_first(&row->cells);
if (link == NULL)
return NULL;
return list_get_instance(link, table_cell_t, lrow);
}
static table_cell_t *table_row_cell_next(table_cell_t *cur)
{
link_t *link;
link = list_next(&cur->lrow, &cur->row->cells);
if (link == NULL)
return NULL;
return list_get_instance(link, table_cell_t, lrow);
}
static table_column_t *table_column_first(table_t *table)
{
link_t *link;
link = list_first(&table->columns);
if (link == NULL)
return NULL;
return list_get_instance(link, table_column_t, ltable);
}
static table_column_t *table_column_next(table_column_t *cur)
{
link_t *link;
link = list_next(&cur->ltable, &cur->table->columns);
if (link == NULL)
return NULL;
return list_get_instance(link, table_column_t, ltable);
}
static errno_t table_cell_extend(table_cell_t *cell, const char *str, size_t len)
{
char *cstr;
int rc;
if (cell->text == NULL) {
cell->text = str_ndup(str, len);
if (cell->text == NULL)
return ENOMEM;
} else {
rc = asprintf(&cstr, "%s%.*s", cell->text, (int)len, str);
if (rc < 0)
return ENOMEM;
free(cell->text);
cell->text = cstr;
}
return EOK;
}
errno_t table_create(table_t **rtable)
{
table_t *table;
errno_t rc;
table = calloc(1, sizeof(table_t));
if (table == NULL)
return ENOMEM;
table->error = EOK;
list_initialize(&table->rows);
list_initialize(&table->columns);
rc = table_add_row(table, &table->wrow);
if (rc != EOK)
goto error;
rc = table_row_add_cell(table->wrow, &table->wcell);
if (rc != EOK)
goto error;
rc = table_add_column(table, &table->wcolumn);
if (rc != EOK)
goto error;
*rtable = table;
return EOK;
error:
table_destroy(table);
return rc;
}
void table_destroy(table_t *table)
{
table_row_t *row;
table_cell_t *cell;
table_column_t *column;
if (table == NULL)
return;
row = table_row_first(table);
while (row != NULL) {
cell = table_row_cell_first(row);
while (cell != NULL) {
list_remove(&cell->lrow);
free(cell->text);
free(cell);
cell = table_row_cell_first(row);
}
list_remove(&row->ltable);
free(row);
row = table_row_first(table);
}
column = table_column_first(table);
while (column != NULL) {
list_remove(&column->ltable);
free(column);
column = table_column_first(table);
}
free(table);
}
errno_t table_print_out(table_t *table, FILE *f)
{
table_row_t *row;
table_cell_t *cell;
table_column_t *column;
bool firstc;
bool firstr;
size_t spacing;
size_t i;
int rc;
if (table->error != EOK)
return table->error;
row = table_row_first(table);
firstr = true;
while (row != NULL) {
cell = table_row_cell_first(row);
if (cell == NULL)
break;
column = table_column_first(table);
firstc = true;
while (cell != NULL && cell->text != NULL) {
spacing = firstc ? table->metrics.margin_left : 1;
for (i = 0; i < spacing; i++) {
rc = fprintf(f, " ");
if (rc < 0)
return EIO;
}
rc = fprintf(f, "%*s", -(int)column->width, cell->text);
if (rc < 0)
return EIO;
cell = table_row_cell_next(cell);
column = table_column_next(column);
firstc = false;
}
rc = fprintf(f, "\n");
if (rc < 0)
return EIO;
if (firstr && table->header_row) {
column = table_column_first(table);
firstc = true;
while (column != NULL) {
spacing = firstc ? table->metrics.margin_left : 1;
for (i = 0; i < spacing; i++) {
rc = fprintf(f, " ");
if (rc < 0)
return EIO;
}
for (i = 0; i < column->width; i++) {
rc = fprintf(f, "=");
if (rc < 0)
return EIO;
}
column = table_column_next(column);
firstc = false;
}
rc = fprintf(f, "\n");
if (rc < 0)
return EIO;
}
row = table_row_next(row);
firstr = false;
}
return EOK;
}
void table_header_row(table_t *table)
{
assert(list_count(&table->rows) == 1);
assert(!table->header_row);
table->header_row = true;
}
errno_t table_printf(table_t *table, const char *fmt, ...)
{
va_list args;
errno_t rc;
int ret;
char *str;
char *sp, *ep;
size_t width;
if (table->error != EOK)
return table->error;
va_start(args, fmt);
ret = vasprintf(&str, fmt, args);
va_end(args);
if (ret < 0) {
table->error = ENOMEM;
return table->error;
}
sp = str;
while (*sp != '\0' && table->error == EOK) {
ep = sp;
while (*ep != '\0' && *ep != '\t' && *ep != '\n')
++ep;
if (table->wcell == NULL) {
rc = table_write_next_cell(table);
if (rc != EOK) {
assert(rc == ENOMEM);
goto out;
}
}
rc = table_cell_extend(table->wcell, sp, ep - sp);
if (rc != EOK) {
assert(rc == ENOMEM);
table->error = ENOMEM;
goto out;
}
width = str_width(table->wcell->text);
if (width > table->wcolumn->width)
table->wcolumn->width = width;
if (*ep == '\t')
rc = table_write_next_cell(table);
else if (*ep == '\n')
rc = table_write_next_row(table);
else
break;
if (rc != EOK) {
assert(rc == ENOMEM);
table->error = ENOMEM;
goto out;
}
sp = ep + 1;
}
rc = table->error;
out:
free(str);
return rc;
}
errno_t table_get_error(table_t *table)
{
return table->error;
}
void table_set_margin_left(table_t *table, size_t mleft)
{
table->metrics.margin_left = mleft;
}
HelenOS homepage, sources at GitHub