aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Eklöf <daniel@ekloef.se>2020-07-15 09:32:40 +0000
committerDaniel Eklöf <daniel@ekloef.se>2020-07-15 09:32:40 +0000
commite6acafa11871af8992d5935f3f60d18c79e791b1 (patch)
treed9e293526d510d0697ab6597338429cf99d556cc
parentselection: provide a const-wrapper for extract_one() (diff)
extract: extract_one() sets a fail flag that extract_finish() reads
This allows us to safely call extract_finish() when extract_one() failed, and we'll get the expected result; false, indicating extract_finish() failed.
-rw-r--r--extract.c26
1 files changed, 19 insertions, 7 deletions
diff --git a/extract.c b/extract.c
index 663f94ef..49cd198a 100644
--- a/extract.c
+++ b/extract.c
@@ -10,6 +10,7 @@ struct extraction_context {
size_t size;
size_t idx;
size_t empty_count;
+ bool failed;
const struct row *last_row;
const struct cell *last_cell;
enum selection_kind selection_kind;
@@ -28,9 +29,18 @@ extract_begin(enum selection_kind kind)
bool
extract_finish(struct extraction_context *ctx, char **text, size_t *len)
{
+ bool ret = false;
+
if (text == NULL)
return false;
+ *text = NULL;
+ if (len != NULL)
+ *len = 0;
+
+ if (ctx->failed)
+ goto out;
+
if (ctx->idx == 0) {
/* Selection of empty cells only */
ctx->buf[ctx->idx] = L'\0';
@@ -43,8 +53,6 @@ extract_finish(struct extraction_context *ctx, char **text, size_t *len)
ctx->buf[ctx->idx] = L'\0';
}
- bool ret = false;
-
size_t _len = wcstombs(NULL, ctx->buf, 0);
if (_len == (size_t)-1) {
LOG_ERRNO("failed to convert selection to UTF-8");
@@ -105,7 +113,7 @@ extract_one(const struct terminal *term, const struct row *row,
/* Row has a hard linebreak, or either last cell or
* current cell is empty */
if (!ensure_size(ctx, 1))
- return false;
+ goto err;
ctx->buf[ctx->idx++] = L'\n';
ctx->empty_count = 0;
@@ -115,7 +123,7 @@ extract_one(const struct terminal *term, const struct row *row,
else if (ctx->selection_kind == SELECTION_BLOCK) {
/* Always insert a linebreak */
if (!ensure_size(ctx, 1))
- return false;
+ goto err;
ctx->buf[ctx->idx++] = L'\n';
ctx->empty_count = 0;
@@ -131,7 +139,7 @@ extract_one(const struct terminal *term, const struct row *row,
/* Replace empty cells with spaces when followed by non-empty cell */
if (!ensure_size(ctx, ctx->empty_count))
- return false;
+ goto err;
for (size_t i = 0; i < ctx->empty_count; i++)
ctx->buf[ctx->idx++] = L' ';
@@ -144,7 +152,7 @@ extract_one(const struct terminal *term, const struct row *row,
= &term->composed[cell->wc - CELL_COMB_CHARS_LO];
if (!ensure_size(ctx, 1 + composed->count))
- return false;
+ goto err;
ctx->buf[ctx->idx++] = composed->base;
for (size_t i = 0; i < composed->count; i++)
@@ -153,11 +161,15 @@ extract_one(const struct terminal *term, const struct row *row,
else {
if (!ensure_size(ctx, 1))
- return false;
+ goto err;
ctx->buf[ctx->idx++] = cell->wc;
}
ctx->last_row = row;
ctx->last_cell = cell;
return true;
+
+err:
+ ctx->failed = true;
+ return false;
}