[GRASS-SVN] r39374 - grass/trunk/vector/v.out.ascii

svn_grass at osgeo.org svn_grass at osgeo.org
Fri Oct 2 08:04:24 EDT 2009


Author: martinl
Date: 2009-10-02 08:04:22 -0400 (Fri, 02 Oct 2009)
New Revision: 39374

Added:
   grass/trunk/vector/v.out.ascii/args.c
   grass/trunk/vector/v.out.ascii/local_proto.h
Modified:
   grass/trunk/vector/v.out.ascii/main.c
Log:
v.out.ascii parse arguments in parse_args()


Added: grass/trunk/vector/v.out.ascii/args.c
===================================================================
--- grass/trunk/vector/v.out.ascii/args.c	                        (rev 0)
+++ grass/trunk/vector/v.out.ascii/args.c	2009-10-02 12:04:22 UTC (rev 39374)
@@ -0,0 +1,110 @@
+#include <stdlib.h>
+#include <string.h>
+
+#include <grass/gis.h>
+#include <grass/vector.h>
+#include <grass/glocale.h>
+
+#include "local_proto.h"
+
+void parse_args(int argc, char **argv,
+		char **input, char**output, int *format, int *dp, char **delim,
+		int *field, char ***columns, char **where, int *region, int *old_format)
+{
+    struct Option *input_opt, *output_opt, *format_opt, *dp_opt, *delim_opt,
+	*field_opt, *column_opt, *where_opt;
+    struct Flag *old_flag, *region_flag;
+    
+    input_opt = G_define_standard_option(G_OPT_V_INPUT);
+
+    output_opt = G_define_standard_option(G_OPT_F_OUTPUT);
+    output_opt->description =
+	_("Path to resulting ASCII file ('-' for standard output) "
+	  "or ASCII vector name if '-o' is defined");
+    output_opt->answer = "-";
+    
+    format_opt = G_define_option();
+    format_opt->key = "format";
+    format_opt->type = TYPE_STRING;
+    format_opt->required = YES;
+    format_opt->multiple = NO;
+    format_opt->options = "point,standard";
+    format_opt->answer = "point";
+    format_opt->description = _("Output format");
+    
+    delim_opt = G_define_standard_option(G_OPT_F_SEP);
+    delim_opt->description = _("Field separator (points mode)");
+    delim_opt->guisection = _("Points");
+    
+    dp_opt = G_define_option();
+    dp_opt->key = "dp";
+    dp_opt->type = TYPE_INTEGER;
+    dp_opt->required = NO;
+    dp_opt->options = "0-32";
+    dp_opt->answer = "8";	/* This value is taken from the lib settings in G_format_easting() */
+    dp_opt->description =
+	_("Number of significant digits (floating point only)");
+    dp_opt->guisection = _("Points");
+    
+    field_opt = G_define_standard_option(G_OPT_V_FIELD);
+    field_opt->guisection = _("Selection");
+
+    column_opt = G_define_standard_option(G_OPT_DB_COLUMNS);
+    column_opt->description = _("Name of attribute column(s) to be exported (point mode)");
+    column_opt->guisection = _("Points");
+    
+    where_opt = G_define_standard_option(G_OPT_DB_WHERE);
+    where_opt->guisection = _("Selection");
+
+    old_flag = G_define_flag();
+    old_flag->key = 'o';
+    old_flag->description = _("Create old (version 4) ASCII file");
+
+    region_flag = G_define_flag();
+    region_flag->key = 'r';
+    region_flag->description =
+	_("Only export points falling within current 3D region (points mode)");
+    region_flag->guisection = _("Points");
+
+    if (G_parser(argc, argv))
+	exit(EXIT_FAILURE);
+
+    *input = G_store(input_opt->answer);
+    *output = G_store(output_opt->answer);
+    if (strcmp(format_opt->answer, "point") == 0)
+	*format = GV_ASCII_FORMAT_POINT;
+    else
+	*format = GV_ASCII_FORMAT_ALL;
+    if (sscanf(dp_opt->answer, "%d", dp) != 1)
+	G_fatal_error(_("Failed to interpret 'dp' parameter as an integer"));
+    /* the field separator */
+    if (strcmp(delim_opt->answer, "\\t") == 0)
+	*delim = G_store("\t");
+    else if (strcmp(delim_opt->answer, "tab") == 0)
+	*delim = G_store("\t");
+    else if (strcmp(delim_opt->answer, "space") == 0)
+	*delim = G_store(" ");
+    else if (strcmp(delim_opt->answer, "comma") == 0)
+	*delim = G_store(",");
+    else
+	*delim = G_store(delim_opt->answer);
+    
+    *field = atoi(field_opt->answer);
+    *columns = NULL;
+    if (column_opt->answer) {
+	int i, nopt;
+	nopt = 0;
+	while(column_opt->answers[nopt++])
+	    ;
+	*columns = (char **) G_malloc(nopt * sizeof(char *));
+	for (i = 0; i < nopt - 1; i++)
+	    (*columns)[i] = G_store(column_opt->answers[i]);
+	(*columns)[nopt - 1] = NULL;
+    }
+    *where = NULL;
+    if (where_opt->answer) {
+	*where = G_store(where_opt->answer);
+    }
+    *region = region_flag->answer ? 1 : 0;
+    *old_format = old_flag->answer ? 1 : 0;
+}


Property changes on: grass/trunk/vector/v.out.ascii/args.c
___________________________________________________________________
Added: svn:mime-type
   + text/x-csrc
Added: svn:keywords
   + Author Date Id
Added: svn:eol-style
   + native

Added: grass/trunk/vector/v.out.ascii/local_proto.h
===================================================================
--- grass/trunk/vector/v.out.ascii/local_proto.h	                        (rev 0)
+++ grass/trunk/vector/v.out.ascii/local_proto.h	2009-10-02 12:04:22 UTC (rev 39374)
@@ -0,0 +1,4 @@
+/* args.c */
+void parse_args(int, char **,
+		char **, char**, int *, int *, char **,
+		int *, char ***, char **, int *, int *);


Property changes on: grass/trunk/vector/v.out.ascii/local_proto.h
___________________________________________________________________
Added: svn:mime-type
   + text/x-chdr
Added: svn:keywords
   + Author Date Id
Added: svn:eol-style
   + native

Modified: grass/trunk/vector/v.out.ascii/main.c
===================================================================
--- grass/trunk/vector/v.out.ascii/main.c	2009-10-02 08:40:54 UTC (rev 39373)
+++ grass/trunk/vector/v.out.ascii/main.c	2009-10-02 12:04:22 UTC (rev 39374)
@@ -5,14 +5,14 @@
  * AUTHOR(S):  Michael Higgins, U.S. Army Construction Engineering Research Laboratory
  *             James Westervelt, U.S. Army Construction Engineering Research Laboratory
  *             Radim Blazek, ITC-Irst, Trento, Italy
- *             Martin Landa, CTU in Prague, Czech Republic (v.out.ascii.db merged)
+ *             Martin Landa, CTU in Prague, Czech Republic (v.out.ascii.db merged & update for GRASS7)
  *
- * PURPOSE:    v.out.ascii: writes GRASS vector data as ASCII files
+ * PURPOSE:    Writes GRASS vector data as ASCII files
  * COPYRIGHT:  (C) 2000-2009 by the GRASS Development Team
  *
- *             This program is free software under the GNU General Public
- *              License (>=v2). Read the file COPYING that comes with GRASS
- *              for details.
+ *             This program is free software under the GNU General
+ *             Public License (>=v2). Read the file COPYING that comes
+ *             with GRASS for details.
  *
  ****************************************************************************
  */
@@ -25,18 +25,18 @@
 #include <grass/vector.h>
 #include <grass/glocale.h>
 
+#include "local_proto.h"
+
 int main(int argc, char *argv[])
 {
-    FILE *ascii, *att;
-    struct Option *input, *output, *format_opt, *dp_opt, *delim_opt,
-	*field_opt, *column_opt, *where_opt;
-    struct Flag *verf, *region_flag;
-    int format, dp, field, ret;
-    char *fs;
-    struct Map_info Map;
-    int ver = 5, pnt = 0;
     struct GModule *module;
+    struct Map_info Map;
 
+    FILE *ascii, *att;
+    char *input, *output, *delim, **columns, *where;
+    int format, dp, field, ret, region, old_format;
+    int ver, pnt;
+
     G_gisinit(argv[0]);
 
     module = G_define_module();
@@ -46,120 +46,44 @@
     module->description =
 	_("Converts a GRASS binary vector map to a GRASS ASCII vector map.");
 
-    input = G_define_standard_option(G_OPT_V_INPUT);
+    parse_args(argc, argv, &input, &output, &format, &dp, &delim,
+	       &field, &columns, &where, &region, &old_format);
 
-    output = G_define_standard_option(G_OPT_F_OUTPUT);
-    output->description =
-	_("Path to resulting ASCII file ('-' for standard output) "
-	  "or ASCII vector name if '-o' is defined");
-    output->answer = "-";
-    
-    format_opt = G_define_option();
-    format_opt->key = "format";
-    format_opt->type = TYPE_STRING;
-    format_opt->required = YES;
-    format_opt->multiple = NO;
-    format_opt->options = "point,standard";
-    format_opt->answer = "point";
-    format_opt->description = _("Output format");
-
-    delim_opt = G_define_standard_option(G_OPT_F_SEP);
-    delim_opt->description = _("Field separator (points mode)");
-    delim_opt->guisection = _("Points");
-
-    dp_opt = G_define_option();
-    dp_opt->key = "dp";
-    dp_opt->type = TYPE_INTEGER;
-    dp_opt->required = NO;
-    dp_opt->options = "0-32";
-    dp_opt->answer = "8";	/* This value is taken from the lib settings in G_format_easting() */
-    dp_opt->description =
-	_("Number of significant digits (floating point only)");
-    dp_opt->guisection = _("Points");
-
-    field_opt = G_define_standard_option(G_OPT_V_FIELD);
-    field_opt->guisection = _("Selection");
-
-    column_opt = G_define_standard_option(G_OPT_DB_COLUMNS);
-    column_opt->description = _("Name of attribute column(s) to be exported (point mode)");
-    column_opt->guisection = _("Points");
-    
-    where_opt = G_define_standard_option(G_OPT_DB_WHERE);
-    where_opt->guisection = _("Selection");
-
-    verf = G_define_flag();
-    verf->key = 'o';
-    verf->description = _("Create old (version 4) ASCII file");
-
-    region_flag = G_define_flag();
-    region_flag->key = 'r';
-    region_flag->description =
-	_("Only export points falling within current 3D region (points mode)");
-    region_flag->guisection = _("Points");
-
-    if (G_parser(argc, argv))
-	exit(EXIT_FAILURE);
-
-
-    field = atoi(field_opt->answer);
-        
-    if (format_opt->answer[0] == 'p')
-	format = GV_ASCII_FORMAT_POINT;
-    else
-	format = GV_ASCII_FORMAT_ALL;
-
-    if (format == GV_ASCII_FORMAT_ALL && column_opt->answer) {
-	G_warning(_("Parameter '%s' ignored in standard mode"),
-		  column_opt->key);
+    if (format == GV_ASCII_FORMAT_ALL && columns) {
+	G_warning(_("Parameter 'column' ignored in standard mode"));
     }
 
-    if (verf->answer)
+    ver = 5;
+    pnt = 0;
+    if (old_format)
 	ver = 4;
-
+    
     if (ver == 4 && format == GV_ASCII_FORMAT_POINT) {
 	G_fatal_error(_("Format 'point' is not supported for old version"));
     }
-
-    if (ver == 4 && strcmp(output->answer, "-") == 0) {
+    
+    if (ver == 4 && strcmp(output, "-") == 0) {
 	G_fatal_error(_("'output' must be given for old version"));
     }
 
-    /* the field separator */
-    fs = delim_opt->answer;
-    if (strcmp(fs, "\\t") == 0)
-	fs = "\t";
-    if (strcmp(fs, "tab") == 0)
-	fs = "\t";
-    if (strcmp(fs, "space") == 0)
-	fs = " ";
-    if (strcmp(fs, "comma") == 0)
-	fs = ",";
-
-    /*The precision of the output */
-    if (dp_opt->answer) {
-	if (sscanf(dp_opt->answer, "%d", &dp) != 1)
-	    G_fatal_error(_("Failed to interpret 'dp' parameter as an integer"));
-    }
-
-
     Vect_set_open_level(1);	/* only need level I */
-    if (Vect_open_old(&Map, input->answer, "") < 0)
+    if (Vect_open_old(&Map, input, "") < 0)
 	G_fatal_error(_("Unable to open vector map <%s>"),
-		      input->answer);
+		      input);
     
-    if (strcmp(output->answer, "-") != 0) {
+    if (strcmp(output, "-") != 0) {
 	if (ver == 4) {
-	    ascii = G_fopen_new("dig_ascii", output->answer);
+	    ascii = G_fopen_new("dig_ascii", output);
 	}
-	else if (strcmp(output->answer, "-") == 0) {
+	else if (strcmp(output, "-") == 0) {
 	    ascii = stdout;
 	}
 	else {
-	    ascii = fopen(output->answer, "w");
+	    ascii = fopen(output, "w");
 	}
 
 	if (ascii == NULL) {
-	    G_fatal_error(_("Unable to open file <%s>"), output->answer);
+	    G_fatal_error(_("Unable to open file <%s>"), output);
 	}
     }
     else {
@@ -174,17 +98,19 @@
     /* Open dig_att */
     att = NULL;
     if (ver == 4 && !pnt) {
-	if (G_find_file("dig_att", output->answer, G_mapset()) != NULL)
+	if (G_find_file("dig_att", output, G_mapset()) != NULL)
 	    G_fatal_error(_("dig_att file already exist"));
 
-	if ((att = G_fopen_new("dig_att", output->answer)) == NULL)
+	if ((att = G_fopen_new("dig_att", output)) == NULL)
 	    G_fatal_error(_("Unable to open dig_att file <%s>"),
-			  output->answer);
+			  output);
     }
 
-    ret = Vect_write_ascii(ascii, att, &Map, ver, format, dp, fs,
-			   region_flag->answer, field, where_opt->answer,
-			   column_opt->answers);
+    if (where || columns)
+	G_message(_("Fetching data..."));
+    ret = Vect_write_ascii(ascii, att, &Map, ver, format, dp, delim,
+			   region, field, where,
+			   columns);
 
     if (ret < 1) {
 	if (format == GV_ASCII_FORMAT_POINT) {



More information about the grass-commit mailing list