[GRASS-SVN] r62303 - grass/trunk/general/g.remove

svn_grass at osgeo.org svn_grass at osgeo.org
Mon Oct 20 07:40:27 PDT 2014


Author: hcho
Date: 2014-10-20 07:40:27 -0700 (Mon, 20 Oct 2014)
New Revision: 62303

Added:
   grass/trunk/general/g.remove/construct_pattern.c
Modified:
   grass/trunk/general/g.remove/main.c
Log:
g.remove: names= and ignore= accept fully qualified names

Added: grass/trunk/general/g.remove/construct_pattern.c
===================================================================
--- grass/trunk/general/g.remove/construct_pattern.c	                        (rev 0)
+++ grass/trunk/general/g.remove/construct_pattern.c	2014-10-20 14:40:27 UTC (rev 62303)
@@ -0,0 +1,52 @@
+#include <string.h>
+#include <grass/gis.h>
+#include <grass/glocale.h>
+
+char *construct_pattern(char **names)
+{
+    char *pattern, *p;
+    int i, len, found_illegal_names;
+    const char *mapset;
+    char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
+
+    len = 0;
+    for (i = 0; names[i]; i++) {
+	if ((p = strchr(names[i], '@')))
+	    len += p - names[i];
+	else
+	    len += strlen(names[i]);
+    }
+    len += i; /* # names - 1 commas + \0 */
+
+    pattern = p = (char *)G_malloc(len);
+
+    mapset = G_mapset();
+    found_illegal_names = 0;
+
+    for (i = 0; names[i]; i++) {
+	char *name;
+
+	name = names[i];
+	if (G_name_is_fully_qualified(name, xname, xmapset)) {
+	    if (strcmp(xmapset, mapset) != 0)
+		G_fatal_error(_("%s: Cannot remove or exclude files not in "
+				"the current mapset."), name);
+	    name = xname;
+	}
+
+	if (G_legal_filename(name) == -1)
+	    found_illegal_names = 1;
+
+	if (!i)
+	    strcpy(p, name);
+	else
+	    sprintf(p++, ",%s", name);
+	p += strlen(name);
+    }
+
+    if (found_illegal_names)
+	G_fatal_error(_("Illegal filenames not allowed in the names or ignore "
+			"option."));
+
+    return pattern;
+}

Modified: grass/trunk/general/g.remove/main.c
===================================================================
--- grass/trunk/general/g.remove/main.c	2014-10-20 13:41:02 UTC (rev 62302)
+++ grass/trunk/general/g.remove/main.c	2014-10-20 14:40:27 UTC (rev 62303)
@@ -31,8 +31,8 @@
 #include <grass/manage.h>
 #include <grass/glocale.h>
 
-static int find_illegal_filenames(char **);
-
+/* construct_pattern.c */
+char *construct_pattern(char **);
 /* check_reclass.c */
 int check_reclass(const char *, const char *, int);
 
@@ -63,7 +63,7 @@
     G_gisinit(argv[0]);
 
     result = EXIT_SUCCESS;
-    
+
     module = G_define_module();
     G_add_keyword(_("general"));
     G_add_keyword(_("map management"));
@@ -140,22 +140,15 @@
 
     if (opt.pattern->answer)
 	pattern = opt.pattern->answer;
-    else {
-	if (find_illegal_filenames(opt.names->answers))
-	    G_fatal_error(_("Illegal filenames not allowed "
-			    "in the names option."));
-	pattern = opt.names->answer;
-    }
+    else
+	pattern = construct_pattern(opt.names->answers);
 
-    exclude = opt.exclude->answer ? opt.exclude->answer : opt.ignore->answer;
     if (opt.exclude->answer)
 	exclude = opt.exclude->answer;
-    else {
-	if (opt.ignore->answer && find_illegal_filenames(opt.ignore->answers))
-	    G_fatal_error(_("Illegal filenames not allowed "
-			    "in the ignore option."));
-	exclude = opt.ignore->answer;
-    }
+    else if (opt.ignore->answer)
+	exclude = construct_pattern(opt.ignore->answers);
+    else
+	exclude = NULL;
 
     if ((flag.regex->answer || flag.extended->answer) && opt.pattern->answer)
 	filter = G_ls_regex_filter(pattern, 0, (int)flag.extended->answer);
@@ -232,16 +225,16 @@
 
 	rast = !G_strcasecmp(elem->alias, "rast");
 	files = G__ls(path, &num_files);
-	
+
 	for (j = 0; j < num_files; j++) {
 	    if (!flag.force->answer) {
 		fprintf(stdout, "%s/%s@%s\n", elem->alias, files[j], mapset);
 		continue;
 	    }
-	    
+
 	    if (rast && check_reclass(files[j], mapset, flag.basemap->answer))
 		continue;
-	    
+
 	    if (M_do_remove(n, (char *)files[j]) == 1)
 		result = EXIT_FAILURE;
 	}
@@ -258,16 +251,3 @@
 
     exit(result);
 }
-
-static int find_illegal_filenames(char **names)
-{
-    int i, found;
-
-    found = 0;
-    for (i = 0; names[i]; i++) {
-	if (G_legal_filename(names[i]) == -1)
-	    found = 1;
-    }
-
-    return found;
-}



More information about the grass-commit mailing list