[GRASS-SVN] r60246 - grass/branches/releasebranch_6_4/vector/v.external

svn_grass at osgeo.org svn_grass at osgeo.org
Thu May 15 02:51:23 PDT 2014


Author: martinl
Date: 2014-05-15 02:51:23 -0700 (Thu, 15 May 2014)
New Revision: 60246

Modified:
   grass/branches/releasebranch_6_4/vector/v.external/main.c
Log:
v.external: remove formats from module description (see #2159)
            backport -f flag from trunk


Modified: grass/branches/releasebranch_6_4/vector/v.external/main.c
===================================================================
--- grass/branches/releasebranch_6_4/vector/v.external/main.c	2014-05-15 09:42:36 UTC (rev 60245)
+++ grass/branches/releasebranch_6_4/vector/v.external/main.c	2014-05-15 09:51:23 UTC (rev 60246)
@@ -25,11 +25,16 @@
 #include <grass/glocale.h>
 #include "ogr_api.h"
 
+static int cmp(const void *, const void *);
+static void list_formats(void);
+static char **format_list(int *, size_t *);
+
 int main(int argc, char *argv[])
 {
     int i;
     struct GModule *module;
     struct Option *dsn_opt, *layer_opt, *out_opt;
+    struct Flag* format_flag;
     char buf[2000];
     FILE *fd;
     struct Map_info Map;
@@ -50,7 +55,6 @@
     dsn_opt = G_define_option();
     dsn_opt->key = "dsn";
     dsn_opt->type = TYPE_STRING;
-    dsn_opt->required = YES;
     dsn_opt->gisprompt = "old_file,file,dsn";
     dsn_opt->description = "OGR datasource name. Examples:\n"
 	"\t\tESRI Shapefile: directory containing shapefiles\n"
@@ -71,14 +75,28 @@
 	 "\t\tESRI Shapefile: shapefile name\n"
 	 "\t\tMapInfo File: mapinfo file name");
 
+    format_flag = G_define_flag();
+    format_flag->key = 'f';
+    format_flag->description = _("List supported formats and exit");
+    format_flag->guisection = _("Print");
+
     if (G_parser(argc, argv))
 	exit(EXIT_FAILURE);
 
+    OGRRegisterAll();
+
+    if (format_flag->answer) {
+        /* list formats */
+        list_formats();
+        exit(EXIT_SUCCESS);
+    }
+
+    if (!dsn_opt)
+	G_fatal_error(_("Name of OGR data source not specified"));
+
     if (!out_opt->answer && layer_opt->answer)
 	G_fatal_error(_("Output vector name was not specified"));
 
-    OGRRegisterAll();
-
     /* Open OGR DSN */
     Ogr_ds = OGROpen(dsn_opt->answer, FALSE, NULL);
     if (Ogr_ds == NULL)
@@ -110,7 +128,7 @@
 
     if (!layer_opt->answer) {
 	fprintf(stdout, "\n");
-	exit(0);
+	exit(EXIT_SUCCESS);
     }
 
     if (layer == -1) {
@@ -152,3 +170,62 @@
 
     exit(EXIT_SUCCESS);
 }
+
+void list_formats(void)
+{
+    int i, count;
+    char **list;
+    
+    G_message(_("List of supported formats:"));
+
+    list = format_list(&count, NULL);
+    
+    for (i = 0; i < count; i++)
+	fprintf(stdout, "%s\n", list[i]);
+    fflush(stdout);
+
+    G_free(list);
+}
+
+char **format_list(int *count, size_t *len)
+{
+    int i;
+    char **list;
+
+    list = NULL;
+    *count = 0;
+    if (len)
+	*len = 0;
+
+    char buf[2000];
+    
+    OGRSFDriverH Ogr_driver;
+    
+    /* Open OGR DSN */
+    OGRRegisterAll();
+    G_debug(2, "driver count = %d", OGRGetDriverCount());
+    for (i = 0; i < OGRGetDriverCount(); i++) {
+	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 *));
+
+	/* 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);
+	if (len)
+	    *len += strlen(buf) + 1; /* + ',' */
+    }
+
+    /* order formats by name */
+    qsort(list, *count, sizeof(char *), cmp);
+
+    return list;
+}
+
+int cmp(const void *a, const void *b)
+{
+    return strcmp(*(char **)a, *(char **)b);
+}



More information about the grass-commit mailing list