From: Shakur Shams Mullick <shakursmullick@gmail.com>
To: util-linux@vger.kernel.org
Cc: Shakur Shams Mullick <shakursmullick@gmail.com>
Subject: [PATCH 5/8] libsmartcols/src/table_print.c: sort table before printing
Date: Sat, 3 May 2014 23:41:25 +0600 [thread overview]
Message-ID: <1399138888-7554-5-git-send-email-shakursmullick@gmail.com> (raw)
In-Reply-To: <1399138888-7554-1-git-send-email-shakursmullick@gmail.com>
It fascilitates sorting before printing the table. Sorting can be done by size or name.
Signed-off-by: Shakur Shams Mullick <shakursmullick@gmail.com>
---
libsmartcols/src/table_print.c | 60 +++++++++++++++++++++++++++++++++++++++++-
1 file changed, 59 insertions(+), 1 deletion(-)
diff --git a/libsmartcols/src/table_print.c b/libsmartcols/src/table_print.c
index 2942053..b256198 100644
--- a/libsmartcols/src/table_print.c
+++ b/libsmartcols/src/table_print.c
@@ -20,6 +20,7 @@
#include <string.h>
#include <termios.h>
#include <ctype.h>
+#include <list.h>
#include "nls.h"
#include "mbsalign.h"
@@ -27,6 +28,7 @@
#include "ttyutils.h"
#include "carefulputc.h"
#include "smartcolsP.h"
+#include "strutils.h"
/* This is private struct to work with output data */
struct libscols_buffer {
@@ -38,6 +40,8 @@ struct libscols_buffer {
size_t art_idx; /* begin of the tree ascii art or zero */
};
+static uint sq;
+
static struct libscols_buffer *new_buffer(size_t sz)
{
struct libscols_buffer *buf = malloc(sz + sizeof(struct libscols_buffer));
@@ -694,7 +698,35 @@ static size_t strlen_line(struct libscols_line *ln)
return sz;
}
+int size_comparison(struct list_head *a, struct list_head *b, void *data)
+{
+ struct libscols_line *ra, *rb;
+ uint64_t n, m;
+
+ ra = list_entry(a, struct libscols_line, ln_lines);
+ rb = list_entry(b, struct libscols_line, ln_lines);
+
+ strtosize(ra->cells[sq].data, &n);
+ strtosize(rb->cells[sq].data, &m);
+
+ if (n >= m)
+ return 1;
+ return -1;
+}
+int name_comparison(struct list_head *a, struct list_head *b, void *data)
+{
+ struct libscols_line *ra, *rb;
+ int n, m;
+
+ ra = list_entry(a, struct libscols_line, ln_lines);
+ rb = list_entry(b, struct libscols_line, ln_lines);
+
+ n = strlen(ra->cells[sq].data);
+ m = strlen(rb->cells[sq].data);
+
+ return strncmp(ra->cells[sq].data, rb->cells[sq].data, (n>=m ? n : m));
+}
/**
* scols_print_table:
@@ -711,7 +743,9 @@ int scols_print_table(struct libscols_table *tb)
struct libscols_line *ln;
struct libscols_iter itr;
struct libscols_buffer *buf;
-
+ struct list_head *p;
+ char *sdata;
+
assert(tb);
if (!tb)
return -1;
@@ -743,6 +777,30 @@ int scols_print_table(struct libscols_table *tb)
if (scols_table_is_tree(tb))
rc = print_tree(tb, buf);
+ else if (scols_table_is_namesort(tb))
+ {
+ list_for_each(p, &tb->tb_columns) {
+ struct libscols_column *cl = list_entry(p, struct libscols_column, cl_columns);
+ if(strncmp(cl->header.data, "NAME", 4) == 0){
+ sq = cl->seqnum;
+ break;
+ }
+ }
+ list_sort(&tb->tb_lines, name_comparison, NULL);
+ print_table(tb, buf);
+ }
+ else if (scols_table_is_sizesort(tb))
+ {
+ list_for_each(p, &tb->tb_columns) {
+ struct libscols_column *cl = list_entry(p, struct libscols_column, cl_columns);
+ if(strncmp(cl->header.data, "SIZE", 4) == 0){
+ sq = cl->seqnum;
+ break;
+ }
+ }
+ list_sort(&tb->tb_lines, size_comparison, NULL);
+ print_table(tb, buf);
+ }
else
rc = print_table(tb, buf);
--
1.8.3.2
next prev parent reply other threads:[~2014-05-03 17:42 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-05-03 17:41 [PATCH 1/8] libsmartcols/src/libsmartcols.h.in: add function name Shakur Shams Mullick
2014-05-03 17:41 ` [PATCH 2/8] libsmartcols/src/libsmartcols.sym: add function symbol Shakur Shams Mullick
2014-05-03 17:41 ` [PATCH 3/8] libsmartcols/src/smartcolsP.h: add sort flag and related enum Shakur Shams Mullick
2014-05-03 17:41 ` [PATCH 4/8] libsmartcols/src/table.c: implementation of 4 newly added functions Shakur Shams Mullick
2014-05-03 17:41 ` Shakur Shams Mullick [this message]
2014-05-03 17:41 ` [PATCH 6/8] misc-utils/lsblk.c: add sorting support for lsblk command Shakur Shams Mullick
2014-05-06 10:43 ` Karel Zak
2014-05-03 17:41 ` [PATCH 7/8] misc-utils/lslocks.c: add sorting support for lslocks command Shakur Shams Mullick
2014-05-03 17:41 ` [PATCH 8/8] disk-utils/partx.c: add sorting support for partx command Shakur Shams Mullick
2014-05-06 10:33 ` [PATCH 1/8] libsmartcols/src/libsmartcols.h.in: add function name Karel Zak
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1399138888-7554-5-git-send-email-shakursmullick@gmail.com \
--to=shakursmullick@gmail.com \
--cc=util-linux@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox