[GRASS-dev] sorting of db.tables -p output?

Glynn Clements glynn at gclements.plus.com
Wed May 9 11:19:08 EDT 2007


Markus Neteler wrote:

> I would like to get the output of
>  db.tables -p
> sorted alphabetically. Looking at the code, I assume that it
> might be best done in db_list_tables():
> 
> lib/db/dbmi_client/c_list_tabs.c
> 
> but I am getting lost there with the DB_RECV_STRING_ARRAY
> macro.

Try the attached patch (untested).

-- 
Glynn Clements <glynn at gclements.plus.com>

-------------- next part --------------
Index: c_list_tabs.c
===================================================================
RCS file: /grassrepository/grass6/lib/db/dbmi_client/c_list_tabs.c,v
retrieving revision 1.3
diff -u -r1.3 c_list_tabs.c
--- c_list_tabs.c	9 Feb 2006 03:08:54 -0000	1.3
+++ c_list_tabs.c	9 May 2007 15:17:25 -0000
@@ -1,6 +1,15 @@
+#include <stdlib.h>
+#include <string.h>
 #include <grass/dbmi.h>
 #include "macros.h"
 
+static int cmp_dbstr(const void *pa, const void *pb)
+{
+    const char *a = db_get_string((dbString *)pa);
+    const char *b = db_get_string((dbString *)pb);
+    return strcmp(a, b);
+}
+
 /*!
  \fn int db_list_tables (dbDriver *driver, dbString **names, int *count, int system)
  \brief list available tables for given connection
@@ -29,6 +38,8 @@
 
 /* results */
     DB_RECV_STRING_ARRAY (names, count);
+
+    qsort(*names, *count, sizeof(dbString), cmp_dbstr);
 
     return DB_OK;
 }


More information about the grass-dev mailing list