summaryrefslogtreecommitdiffstats
path: root/libsmartcols/src/walk.c
blob: 0b51fed5fc2ffa9506266aa7d2c5ee0c7c1a6b91 (plain)
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include "smartcolsP.h"

static int walk_line(struct libscols_table *tb,
		     struct libscols_line *ln,
		     struct libscols_column *cl,
		     int (*callback)(struct libscols_table *,
			            struct libscols_line *,
				    struct libscols_column *,
				    void *),
		    void *data)
{
	int rc = 0;

/*	DBG(LINE, ul_debugobj(ln, " wall line")); */

	/* we list group children in __scols_print_tree() after tree root node */
	if (is_group_member(ln) && is_last_group_member(ln) && has_group_children(ln))
		tb->ngrpchlds_pending++;

	if (has_groups(tb))
		rc = scols_groups_update_grpset(tb, ln);
	if (rc == 0)
		rc = callback(tb, ln, cl, data);

	/* children */
	if (rc == 0 && has_children(ln)) {
		struct list_head *p;

/*		DBG(LINE, ul_debugobj(ln, " children walk"));*/

		list_for_each(p, &ln->ln_branch) {
			struct libscols_line *chld = list_entry(p,
					struct libscols_line, ln_children);

			rc = walk_line(tb, chld, cl, callback, data);
			if (rc)
				break;
		}
	}

/*	DBG(LINE, ul_debugobj(ln, "<- walk line done [rc=%d]", rc)); */
	return rc;
}

/* last line in the tree? */
int scols_walk_is_last(struct libscols_table *tb, struct libscols_line *ln)
{
	if (tb->walk_last_done == 0)
		return 0;
	if (tb->ngrpchlds_pending > 0)
		return 0;
	if (has_children(ln))
		return 0;
	if (is_tree_root(ln) && !is_last_tree_root(tb, ln))
		return 0;
	if (is_group_member(ln) && (!is_last_group_member(ln) || has_group_children(ln)))
		return 0;
	if (is_child(ln)) {
		struct libscols_line *parent = ln->parent;

		if (!is_last_child(ln))
			return 0;
		while (parent) {
			if (is_child(parent) && !is_last_child(parent))
				return 0;
			if (!parent->parent)
				break;
			parent = parent->parent;
		}
		if (is_tree_root(parent) && !is_last_tree_root(tb, parent))
			return 0;
	}
	if (is_group_child(ln) && !is_last_group_child(ln))
		return 0;

	DBG(LINE, ul_debugobj(ln, "last in table"));
	return 1;
}

int scols_walk_tree(struct libscols_table *tb,
		    struct libscols_column *cl,
		    int (*callback)(struct libscols_table *,
			            struct libscols_line *,
				    struct libscols_column *,
				    void *),
		    void *data)
{
	int rc = 0;
	struct libscols_line *ln;
	struct libscols_iter itr;

	assert(tb);
/*	DBG(TAB, ul_debugobj(tb, ">> walk start"));*/

	/* init */
	tb->ngrpchlds_pending = 0;
	tb->walk_last_tree_root = NULL;
	tb->walk_last_done = 0;

	if (has_groups(tb))
		scols_groups_reset_state(tb);

	/* set pointer to last tree root */
	scols_reset_iter(&itr, SCOLS_ITER_FORWARD);
	while (scols_table_next_line(tb, &itr, &ln) == 0) {
		if (!tb->walk_last_tree_root)
			tb->walk_last_tree_root = ln;
		if (is_child(ln) || is_group_child(ln))
			continue;
		tb->walk_last_tree_root = ln;
	}

	/* walk */
	scols_reset_iter(&itr, SCOLS_ITER_FORWARD);
	while (rc == 0 && scols_table_next_line(tb, &itr, &ln) == 0) {
		if (ln->parent || ln->parent_group)
			continue;

		if (tb->walk_last_tree_root == ln)
			tb->walk_last_done = 1;
		rc = walk_line(tb, ln, cl, callback, data);

		/* walk group's children */
		while (rc == 0 && tb->ngrpchlds_pending) {
			struct libscols_group *gr = scols_grpset_get_printable_children(tb);
			struct list_head *p;

			DBG(LINE, ul_debugobj(ln, " walk group children [pending=%zu]", tb->ngrpchlds_pending));
			if (!gr) {
				DBG(LINE, ul_debugobj(ln, " *** ngrpchlds_pending counter invalid"));
				tb->ngrpchlds_pending = 0;
				break;
			}

			tb->ngrpchlds_pending--;

			list_for_each(p, &gr->gr_children) {
				struct libscols_line *chld =
					list_entry(p, struct libscols_line, ln_children);

				rc = walk_line(tb, chld, cl, callback, data);
				if (rc)
					break;
			}
		}
	}

	tb->ngrpchlds_pending = 0;
	tb->walk_last_done = 0;
/*	DBG(TAB, ul_debugobj(tb, "<< walk end [rc=%d]", rc));*/
	return rc;
}