ryantimwilson / rpms / systemd

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