static bool hidrd_ttbl_print(char **pbuf, size_t *psize, const hidrd_ttbl *tbl, const hidrd_ttbl_strip *markup) { bool result = false; hidrd_buf buf = HIDRD_BUF_EMPTY; const hidrd_ttbl_row *row; const hidrd_ttbl_strip *strip; size_t col; size_t pad; size_t len; const hidrd_ttbl_cell *cell; bool got_text; for (row = tbl->row; row != NULL; row = row->next) { got_text = false; for (pad = 0, strip = markup, col = 0, cell = row->cell; cell != NULL; col += 1 + cell->skip, cell = cell->next) { /* Lookup cell strip, adding to the field padding */ for (; strip != NULL && strip->col < col; pad += strip->width, strip = strip->next); if (cell->text == NULL) continue; got_text = true; /* Pad to the field start */ if (!hidrd_buf_add_span(&buf, ' ', pad)) goto cleanup; /* Output the text */ len = strlen(cell->text); if (!hidrd_buf_add_ptr(&buf, cell->text, len)) goto cleanup; /* Calculate padding to next field start */ pad = strip->width - len; /* Move on to the next strip */ strip = strip->next; } if (got_text && !hidrd_buf_add_span(&buf, '\n', 1)) goto cleanup; } hidrd_buf_retension(&buf); hidrd_buf_detach(&buf, (void **)pbuf, psize); result = true; cleanup: hidrd_buf_clnp(&buf); return result; }
bool hidrd_buf_add_str(hidrd_buf *buf, const char *str) { assert(hidrd_buf_valid(buf)); assert(str != NULL); return hidrd_buf_add_ptr(buf, str, strlen(str)); }