diff options
author | Karel Zak | 2014-05-20 15:04:11 +0200 |
---|---|---|
committer | Karel Zak | 2014-05-20 15:04:11 +0200 |
commit | 57a86f9bff68f24b167df9c571a09c6b89712296 (patch) | |
tree | b5872b3929c083b96c14fa0140157c59f8e78d2d /libsmartcols/src/table.c | |
parent | setterm: fix 'bright' in usage, remove unnecessary error message (diff) | |
download | kernel-qcow2-util-linux-57a86f9bff68f24b167df9c571a09c6b89712296.tar.gz kernel-qcow2-util-linux-57a86f9bff68f24b167df9c571a09c6b89712296.tar.xz kernel-qcow2-util-linux-57a86f9bff68f24b167df9c571a09c6b89712296.zip |
libsmartcols: add scols_sort_table()
* add pointer to column cmp() function [scols_column_set_cmpfunc()]
* allow to store per-cell application private data
(to make it possible to sort tables on data independent on cell
output data) [scols_cell_set_userdata() ...]
* make it possible to access line cell by column
[scols_line_get_column_cell()]
Sort and cmp() stuff based on patches from Shakur Shams Mullick.
Co-Author: Shakur Shams Mullick <shakursmullick@gmail.com>
Signed-off-by: Shakur Shams Mullick <shakursmullick@gmail.com>
Signed-off-by: Karel Zak <kzak@redhat.com>
Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'libsmartcols/src/table.c')
-rw-r--r-- | libsmartcols/src/table.c | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/libsmartcols/src/table.c b/libsmartcols/src/table.c index d4c61eea3..a51a84abc 100644 --- a/libsmartcols/src/table.c +++ b/libsmartcols/src/table.c @@ -977,3 +977,42 @@ char *scols_table_get_line_separator(struct libscols_table *tb) return tb->linesep; } + +static int cells_cmp_wrapper(struct list_head *a, struct list_head *b, void *data) +{ + struct libscols_column *cl = (struct libscols_column *) data; + struct libscols_line *ra, *rb; + struct libscols_cell *ca, *cb; + + assert(a); + assert(b); + assert(cl); + + ra = list_entry(a, struct libscols_line, ln_lines); + rb = list_entry(b, struct libscols_line, ln_lines); + ca = scols_line_get_cell(ra, cl->seqnum); + cb = scols_line_get_cell(rb, cl->seqnum); + + return cl->cmpfunc(ca, cb, cl->cmpfunc_data); +} + +/** + * scols_sort_table: + * @tb: table + * @cl: order by this column + * + * Orders the table by the column. See also scols_column_set_cmpfunc(). + * + * Returns: 0, a negative value in case of an error. + */ +int scols_sort_table(struct libscols_table *tb, struct libscols_column *cl) +{ + assert(tb); + assert(cl); + + if (!tb || !cl) + return -EINVAL; + + list_sort(&tb->tb_lines, cells_cmp_wrapper, cl); + return 0; +} |