ff6046
From a53afb337985f8f1c7fe8f620fe30cec87f554d5 Mon Sep 17 00:00:00 2001
ff6046
From: Lennart Poettering <lennart@poettering.net>
ff6046
Date: Thu, 8 Nov 2018 21:17:47 +0100
ff6046
Subject: [PATCH] format-table: add table_update() to update existing entries
ff6046
ff6046
(cherry picked from commit 27e730e6d0a7709c17ccef170f10846e92dca2a0)
ff6046
ff6046
Related: #1689832
ff6046
---
ff6046
 src/basic/format-table.c | 40 ++++++++++++++++++++++++++++++++++++++++
ff6046
 src/basic/format-table.h |  2 ++
ff6046
 2 files changed, 42 insertions(+)
ff6046
ff6046
diff --git a/src/basic/format-table.c b/src/basic/format-table.c
ff6046
index a3ff527e91..302642d748 100644
ff6046
--- a/src/basic/format-table.c
ff6046
+++ b/src/basic/format-table.c
ff6046
@@ -590,6 +590,46 @@ int table_set_url(Table *t, TableCell *cell, const char *url) {
ff6046
         return free_and_replace(table_get_data(t, cell)->url, copy);
ff6046
 }
ff6046
 
ff6046
+int table_update(Table *t, TableCell *cell, TableDataType type, const void *data) {
ff6046
+        _cleanup_free_ char *curl = NULL;
ff6046
+        TableData *nd, *od;
ff6046
+        size_t i;
ff6046
+
ff6046
+        assert(t);
ff6046
+        assert(cell);
ff6046
+
ff6046
+        i = TABLE_CELL_TO_INDEX(cell);
ff6046
+        if (i >= t->n_cells)
ff6046
+                return -ENXIO;
ff6046
+
ff6046
+        assert_se(od = t->data[i]);
ff6046
+
ff6046
+        if (od->url) {
ff6046
+                curl = strdup(od->url);
ff6046
+                if (!curl)
ff6046
+                        return -ENOMEM;
ff6046
+        }
ff6046
+
ff6046
+        nd = table_data_new(
ff6046
+                        type,
ff6046
+                        data,
ff6046
+                        od->minimum_width,
ff6046
+                        od->maximum_width,
ff6046
+                        od->weight,
ff6046
+                        od->align_percent,
ff6046
+                        od->ellipsize_percent);
ff6046
+        if (!nd)
ff6046
+                return -ENOMEM;
ff6046
+
ff6046
+        nd->color = od->color;
ff6046
+        nd->url = TAKE_PTR(curl);
ff6046
+
ff6046
+        table_data_unref(od);
ff6046
+        t->data[i] = nd;
ff6046
+
ff6046
+        return 0;
ff6046
+}
ff6046
+
ff6046
 int table_add_many_internal(Table *t, TableDataType first_type, ...) {
ff6046
         TableDataType type;
ff6046
         va_list ap;
ff6046
diff --git a/src/basic/format-table.h b/src/basic/format-table.h
ff6046
index 07cb2351cb..4273c8c49b 100644
ff6046
--- a/src/basic/format-table.h
ff6046
+++ b/src/basic/format-table.h
ff6046
@@ -46,6 +46,8 @@ int table_set_ellipsize_percent(Table *t, TableCell *cell, unsigned percent);
ff6046
 int table_set_color(Table *t, TableCell *cell, const char *color);
ff6046
 int table_set_url(Table *t, TableCell *cell, const char *color);
ff6046
 
ff6046
+int table_update(Table *t, TableCell *cell, TableDataType type, const void *data);
ff6046
+
ff6046
 int table_add_many_internal(Table *t, TableDataType first_type, ...);
ff6046
 #define table_add_many(t, ...) table_add_many_internal(t, __VA_ARGS__, _TABLE_DATA_TYPE_MAX)
ff6046