summaryrefslogtreecommitdiffstats
path: root/libsmartcols/samples/continuous-json.c
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-14 19:33:32 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-14 19:33:32 +0000
commit8bb05ac73a5b448b339ce0bc8d396c82c459b47f (patch)
tree1fdda006866bca20d41cb206767ea5241e36852f /libsmartcols/samples/continuous-json.c
parentAdding debian version 2.39.3-11. (diff)
downloadutil-linux-8bb05ac73a5b448b339ce0bc8d396c82c459b47f.tar.xz
util-linux-8bb05ac73a5b448b339ce0bc8d396c82c459b47f.zip
Merging upstream version 2.40.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'libsmartcols/samples/continuous-json.c')
-rw-r--r--libsmartcols/samples/continuous-json.c80
1 files changed, 80 insertions, 0 deletions
diff --git a/libsmartcols/samples/continuous-json.c b/libsmartcols/samples/continuous-json.c
new file mode 100644
index 0000000..ae1f405
--- /dev/null
+++ b/libsmartcols/samples/continuous-json.c
@@ -0,0 +1,80 @@
+/*
+ * SPDX-License-Identifier: LGPL-2.1-or-later
+ *
+ * Copyright (C) 2016 Karel Zak <kzak@redhat.com>
+ * Copyright (C) 2023 Thomas Weißschuh <thomas@t-8ch.de>
+ */
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+#include <errno.h>
+
+#include "c.h"
+#include "xalloc.h"
+
+#include "libsmartcols.h"
+
+/* add columns to the @tb */
+static void setup_columns(struct libscols_table *tb)
+{
+ scols_table_enable_maxout(tb, 1);
+ if (!scols_table_new_column(tb, "COUNT", 0.1, SCOLS_FL_RIGHT))
+ goto fail;
+ if (!scols_table_new_column(tb, "TEXT", 0.9, 0))
+ goto fail;
+ return;
+fail:
+ scols_unref_table(tb);
+ err(EXIT_FAILURE, "failed to create output columns");
+}
+
+static struct libscols_line *add_line(struct libscols_table *tb, int i)
+{
+ char *p;
+ struct libscols_line *ln = scols_table_new_line(tb, NULL);
+
+ if (!ln)
+ err(EXIT_FAILURE, "failed to create output line");
+
+ xasprintf(&p, "%d", i);
+ if (scols_line_refer_data(ln, 0, p))
+ goto fail;
+
+ xasprintf(&p, "text%d", i);
+ if (scols_line_refer_data(ln, 1, p))
+ goto fail;
+
+ return ln;
+fail:
+ scols_unref_table(tb);
+ err(EXIT_FAILURE, "failed to create output line");
+}
+
+int main(void)
+{
+ struct libscols_table *tb;
+ size_t i;
+
+ scols_init_debug(0);
+
+ tb = scols_new_table();
+ if (!tb)
+ err(EXIT_FAILURE, "failed to create output table");
+ scols_table_enable_json(tb, 1);
+
+ setup_columns(tb);
+
+ for (i = 0; i < 10; i++) {
+ struct libscols_line *line;
+
+ line = add_line(tb, i);
+
+ /* print the line */
+ scols_table_print_range(tb, line, NULL);
+
+ fflush(scols_table_get_stream(tb));
+ }
+
+ scols_unref_table(tb);
+ return EXIT_SUCCESS;
+}