File: continuous-json.c

package info (click to toggle)
util-linux 2.41.3-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 92,844 kB
  • sloc: ansic: 179,146; sh: 22,716; yacc: 1,284; makefile: 525; xml: 422; python: 316; lex: 89; ruby: 75; csh: 37; exp: 19; sed: 16; perl: 15; sql: 9
file content (77 lines) | stat: -rw-r--r-- 1,540 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
 * 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)
{
	struct libscols_line *ln = scols_table_new_line(tb, NULL);

	if (!ln)
		err(EXIT_FAILURE, "failed to create output line");

	if (scols_line_sprintf(ln, 0, "%d", i))
		goto fail;

	if (scols_line_sprintf(ln, 1, "text%d", i))
		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;
}