]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lib/tt: keep track about output order
authorKarel Zak <kzak@redhat.com>
Tue, 4 Feb 2014 22:52:17 +0000 (23:52 +0100)
committerKarel Zak <kzak@redhat.com>
Tue, 11 Mar 2014 10:35:14 +0000 (11:35 +0100)
Signed-off-by: Karel Zak <kzak@redhat.com>
include/tt.h
lib/tt.c

index ae81d4f3be995883cad2ac06184e39f321d9492d..e74319f1eeccd038604c44fb83e2d7e1720a9c4d 100644 (file)
@@ -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);
index 6b630f8e7ffc16dac26614039c61338d8a38543d..086487544b5087e66c74332fdbbe52cf363c1208 100644 (file)
--- 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