From: Karel Zak Date: Tue, 4 Feb 2014 22:52:17 +0000 (+0100) Subject: lib/tt: keep track about output order X-Git-Tag: v2.25-rc1~474 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=e11040bdfc31e2f8754ad503809f313a914c7752;p=thirdparty%2Futil-linux.git lib/tt: keep track about output order Signed-off-by: Karel Zak --- diff --git a/include/tt.h b/include/tt.h index ae81d4f3be..e74319f1ee 100644 --- a/include/tt.h +++ b/include/tt.h @@ -43,7 +43,8 @@ struct tt { int first_run; struct list_head tb_columns; - struct list_head tb_lines; + struct list_head tb_lines; /* lines as specified by user */ + struct list_head tb_output; /* lines in output order */ const struct tt_symbols *symbols; }; @@ -71,6 +72,7 @@ struct tt_line { size_t data_sz; /* strlen of all data */ struct list_head ln_lines; /* table lines */ + struct list_head ln_output; /* lines in output order */ struct list_head ln_branch; /* begin of branch (head of ln_children) */ struct list_head ln_children; @@ -94,6 +96,7 @@ extern struct tt_column *tt_define_column(struct tt *tb, const char *name, extern struct tt_column *tt_get_column(struct tt *tb, size_t colnum); extern struct tt_line *tt_add_line(struct tt *tb, struct tt_line *parent); +extern int tt_get_output_line(struct tt *tb, int i, struct tt_line **ln); extern int tt_line_set_data(struct tt_line *ln, int colnum, char *data); extern int tt_line_set_userdata(struct tt_line *ln, void *data); diff --git a/lib/tt.c b/lib/tt.c index 6b630f8e7f..086487544b 100644 --- a/lib/tt.c +++ b/lib/tt.c @@ -68,6 +68,7 @@ struct tt *tt_new_table(int flags) tb->flags = flags; tb->out = stdout; INIT_LIST_HEAD(&tb->tb_lines); + INIT_LIST_HEAD(&tb->tb_output); INIT_LIST_HEAD(&tb->tb_columns); #if defined(HAVE_WIDECHAR) @@ -242,6 +243,7 @@ struct tt_line *tt_add_line(struct tt *tb, struct tt_line *parent) ln->table = tb; ln->parent = parent; INIT_LIST_HEAD(&ln->ln_lines); + INIT_LIST_HEAD(&ln->ln_output); INIT_LIST_HEAD(&ln->ln_children); INIT_LIST_HEAD(&ln->ln_branch); @@ -722,6 +724,7 @@ static void print_data(struct tt *tb, struct tt_column *cl, char *data) static void print_line(struct tt_line *ln, char *buf, size_t bufsz) { struct list_head *p; + struct tt *tb = ln->table; /* set width according to the size of data */ @@ -731,7 +734,8 @@ static void print_line(struct tt_line *ln, char *buf, size_t bufsz) print_data(ln->table, cl, line_get_data(ln, cl, buf, bufsz)); } - fputc('\n', ln->table->out); + fputc('\n', tb->out); + list_add_tail(&ln->ln_output, &tb->tb_output); } static void print_header(struct tt *tb, char *buf, size_t bufsz) @@ -856,6 +860,21 @@ int tt_print_table(struct tt *tb) return 0; } +int tt_get_output_line(struct tt *tb, int i, struct tt_line **ln) +{ + struct list_head *p; + + if (!tb || !ln) + return -EINVAL; + + list_for_each(p, &tb->tb_output) { + *ln = list_entry(p, struct tt_line, ln_output); + if (i-- == 0) + return 0; + } + + return -1; +} /* * @tb: table