[PATCH 4/8] libsmartcols/src/cell.c: add two functions to compare number and string cells

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



scols_cmpstr_cells and scols_cmpnum_cells compare string and number cells.
When comparing data of two cells for the purpose of sorting, it is not
necessary to use these two library functions. Developers can use their
own implementation of cell comparison function.

Furthermore these two functions can be extended to fascilitate sorting
in reverse order.
These two functions could be modified to fascilitate sorting in reverse order.
---
 libsmartcols/src/cell.c | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)

diff --git a/libsmartcols/src/cell.c b/libsmartcols/src/cell.c
index 491b8a1..9304d2a 100644
--- a/libsmartcols/src/cell.c
+++ b/libsmartcols/src/cell.c
@@ -25,6 +25,7 @@
 #include <ctype.h>
 
 #include "smartcolsP.h"
+#include "strutils.h"
 
 /*
  * The cell has no ref-counting, free() and new() functions. All is
@@ -181,3 +182,33 @@ int scols_cell_copy_content(struct libscols_cell *dest,
 		rc = scols_cell_set_color(dest, scols_cell_get_color(src));
 	return rc;
 }
+
+int scols_cmpstr_cells(struct libscols_cell *a, struct libscols_cell *b, void *data)
+{
+	int n, m;
+	const char *adata, *bdata;
+
+	adata = scols_cell_get_data(a);
+	bdata = scols_cell_get_data(b);
+	
+	n = strlen(adata);
+	m = strlen(bdata);
+	
+	return strncmp(adata, bdata, (n>=m ? n : m));
+}
+
+int scols_cmpnum_cells(struct libscols_cell *a, struct libscols_cell *b, void *data)
+{
+	uint64_t n, m;
+	const char *adata, *bdata;
+
+	adata = scols_cell_get_data(a);
+	bdata = scols_cell_get_data(b);
+	
+	strtosize(adata, &n);
+	strtosize(bdata, &m);
+	
+	if (n >= m)
+		return 1;
+	return -1;
+}
-- 
1.8.3.2

--
To unsubscribe from this list: send the line "unsubscribe util-linux" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html




[Index of Archives]     [Netdev]     [Ethernet Bridging]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux Admin]     [Samba]

  Powered by Linux