[GRASS-SVN] r48945 - grass/trunk/vector/v.external.out

svn_grass at osgeo.org svn_grass at osgeo.org
Tue Oct 25 16:53:39 EDT 2011


Author: martinl
Date: 2011-10-25 13:53:39 -0700 (Tue, 25 Oct 2011)
New Revision: 48945

Modified:
   grass/trunk/vector/v.external.out/list.c
Log:
v.external.out: sort formats


Modified: grass/trunk/vector/v.external.out/list.c
===================================================================
--- grass/trunk/vector/v.external.out/list.c	2011-10-25 20:46:03 UTC (rev 48944)
+++ grass/trunk/vector/v.external.out/list.c	2011-10-25 20:53:39 UTC (rev 48945)
@@ -3,47 +3,61 @@
 
 #include "ogr_api.h"
 
+static int cmp(const void *, const void *);
+
 char *format_list(void)
 {
-    char *buf, *p;
-    int len = 0;
-    char first = TRUE;
-    int i;
+    int i, count;
+    size_t len;
+    OGRSFDriverH Ogr_driver;
+    char buf[2000];
+    
+    char **list, *ret;
 
-    OGRSFDriverH driver;
+    list = NULL;
+    count = len = 0;
     
+    /* Open OGR DSN */
+    OGRRegisterAll();
+    G_debug(2, "driver count = %d", OGRGetDriverCount());
     for (i = 0; i < OGRGetDriverCount(); i++) {
-	driver = OGRGetDriver(i);
-
-	if (!OGR_Dr_TestCapability(driver, ODrCCreateDataSource))
+	/* only fetch read/write drivers */
+	if (!OGR_Dr_TestCapability(OGRGetDriver(i), ODrCCreateDataSource))
 	    continue;
+	
+	Ogr_driver = OGRGetDriver(i);
+	G_debug(2, "driver %d/%d : %s", i, OGRGetDriverCount(),
+		OGR_Dr_GetName(Ogr_driver));
+	
+	list = G_realloc(list, (count + 1) * sizeof(char *));
 
-	len += strlen(OGR_Dr_GetName(driver)) + 1;
+	/* chg white space to underscore in OGR driver names */
+	sprintf(buf, "%s", OGR_Dr_GetName(Ogr_driver));
+	G_strchg(buf, ' ', '_');
+	list[count++] = G_store(buf);
+	len += strlen(buf) + 1; /* + ',' */
     }
 
-    buf = G_malloc(len);
-    p = buf;
+    qsort(list, count, sizeof(char *), cmp);
 
-    for (i = 0; i < OGRGetDriverCount(); i++) {
-	const char *name;
-
-	driver = OGRGetDriver(i);
-	if (!OGR_Dr_TestCapability(driver, ODrCCreateDataSource))
-	    continue;
-
-	if (first)
-	    first = FALSE;
-	else
-	    *p++ = ',';
-
-	name = OGR_Dr_GetName(driver);
-	G_strchg(buf, ' ', '_');
-	strcpy(p, name);
-	p += strlen(name);
+    if (len > 0) {
+	ret = G_malloc((len + 1) * sizeof(char)); /* \0 */
+	*ret = '\0';
+	for (i = 0; i < count; i++) {
+	    if (i > 0)
+		strcat(ret, ",");
+	    strcat(ret, list[i]);
+	    G_free(list[i]);
+	}
+	G_free(list);
     }
-    *p++ = '\0';
+    else {
+	ret = G_store("");
+    }
+    
+    G_debug(2, "all drivers: %s", ret);
 
-    return buf;
+    return ret;
 }
 
 void list_formats(void)
@@ -67,3 +81,8 @@
     }
     fflush(stdout);
 }
+
+int cmp(const void *a, const void *b) 
+{
+    return (strcmp(*(char **)a, *(char **)b));
+}



More information about the grass-commit mailing list