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

svn_grass at osgeo.org svn_grass at osgeo.org
Fri Oct 10 09:23:33 PDT 2014


Author: hcho
Date: 2014-10-10 09:23:33 -0700 (Fri, 10 Oct 2014)
New Revision: 62244

Modified:
   grass/trunk/general/g.remove/main.c
Log:
g.remove: Add names= and ignores= for a list of filenames

Modified: grass/trunk/general/g.remove/main.c
===================================================================
--- grass/trunk/general/g.remove/main.c	2014-10-10 14:14:17 UTC (rev 62243)
+++ grass/trunk/general/g.remove/main.c	2014-10-10 16:23:33 UTC (rev 62244)
@@ -31,6 +31,8 @@
 #include <grass/manage.h>
 #include <grass/glocale.h>
 
+static int find_illegal_filenames(char **);
+
 /* check_reclass.c */
 int check_reclass(const char *, const char *, int);
 
@@ -42,6 +44,8 @@
 	struct Option *type;
 	struct Option *pattern;
 	struct Option *exclude;
+	struct Option *names;
+	struct Option *ignore;
     } opt;
     struct
     {
@@ -50,10 +54,11 @@
 	struct Flag *force;
 	struct Flag *basemap;
     } flag;
+    char *pattern, *exclude;
     const char *mapset;
     int result;
     int i, all, num_types, nlist;
-    void *filter, *exclude;
+    void *filter, *exclude_filter;
 
     G_gisinit(argv[0]);
 
@@ -73,24 +78,37 @@
     opt.type->multiple = YES;
     opt.type->options = M_get_options(TRUE);
     opt.type->descriptions = M_get_option_desc(TRUE);
-    opt.type->guidependency = "pattern,exclude";
+    opt.type->guidependency = "pattern,exclude,names,ignore";
 
     opt.pattern = G_define_option();
     opt.pattern->key = "pattern";
     opt.pattern->type = TYPE_STRING;
-    opt.pattern->required = YES;
-    opt.pattern->gisprompt = "new,element,element";
-    opt.pattern->description = _("Map name search pattern or map names separated by a comma");
+    opt.pattern->description = _("File name search pattern");
     opt.pattern->guisection = _("Pattern");
 
     opt.exclude = G_define_option();
     opt.exclude->key = "exclude";
     opt.exclude->type = TYPE_STRING;
-    opt.exclude->required = NO;
-    opt.exclude->gisprompt = "new,element,element";
-    opt.exclude->description = _("Map name exclusion pattern (default: none)");
+    opt.exclude->description = _("File name exclusion pattern (default: none)");
     opt.exclude->guisection = _("Pattern");
 
+    opt.names = G_define_option();
+    opt.names->key = "names";
+    opt.names->type = TYPE_STRING;
+    opt.names->multiple = YES;
+    opt.names->gisprompt = "old,element,element";
+    opt.names->description = _("File names separated by a comma");
+    opt.names->guisection = _("Names");
+
+    opt.ignore = G_define_option();
+    opt.ignore->key = "ignore";
+    opt.ignore->type = TYPE_STRING;
+    opt.ignore->multiple = YES;
+    opt.ignore->gisprompt = "old,element,element";
+    opt.ignore->description =
+	_("File names to ignore separated by a comma (default: none)");
+    opt.ignore->guisection = _("Names");
+
     flag.regex = G_define_flag();
     flag.regex->key = 'r';
     flag.regex->description =
@@ -111,55 +129,74 @@
     flag.basemap->description = _("Remove base raster maps");
     flag.basemap->guisection = _("Raster");
     
+    G_option_exclusive(flag.regex, flag.extended, NULL);
+    G_option_exclusive(opt.pattern, opt.names, NULL);
+    G_option_exclusive(opt.exclude, opt.ignore, NULL);
+    G_option_required(opt.pattern, opt.names, NULL);
+
     if (G_parser(argc, argv))
 	exit(EXIT_FAILURE);
 
-    if (flag.regex->answer && flag.extended->answer)
-	G_fatal_error(_("-%c and -%c are mutually exclusive"),
-		      flag.regex->key, flag.extended->key);
+    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;
+    }
 
-    if (flag.regex->answer || flag.extended->answer)
-	filter = G_ls_regex_filter(opt.pattern->answer, 0,
-				   (int)flag.extended->answer);
+    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;
+    }
+
+    if ((flag.regex->answer || flag.extended->answer) && opt.pattern->answer)
+	filter = G_ls_regex_filter(pattern, 0, (int)flag.extended->answer);
+    else {
 	/* handle individual map names */
-	if (strchr(opt.pattern->answer, ',')) {
-	    char *pattern;
+	if (strchr(pattern, ',')) {
+	    char *buf;
 
-	    pattern = (char *)G_malloc(strlen(opt.pattern->answer) + 3);
-	    sprintf(pattern, "{%s}", opt.pattern->answer);
+	    buf = (char *)G_malloc(strlen(pattern) + 3);
+	    sprintf(buf, "{%s}", pattern);
 
-	    filter = G_ls_glob_filter(pattern, 0);
+	    filter = G_ls_glob_filter(buf, 0);
 	}
 	else
-	    filter = G_ls_glob_filter(opt.pattern->answer, 0);
+	    filter = G_ls_glob_filter(pattern, 0);
     }
     if (!filter)
-	G_fatal_error(_("Unable to compile pattern <%s>"), opt.pattern->answer);
+	G_fatal_error(_("Unable to compile pattern <%s>"), pattern);
 
-    if (opt.exclude->answer) {
-	if (flag.regex->answer || flag.extended->answer)
-	    exclude = G_ls_regex_filter(opt.exclude->answer, 1,
-			    		(int)flag.extended->answer);
+    if (exclude) {
+	if ((flag.regex->answer || flag.extended->answer) &&
+	    opt.exclude->answer)
+	    exclude_filter = G_ls_regex_filter(exclude, 1,
+					       (int)flag.extended->answer);
 	else {
 	    /* handle individual map names */
-	    if (strchr(opt.exclude->answer, ',')) {
-		char *pattern;
+	    if (strchr(exclude, ',')) {
+		char *buf;
 
-		pattern = (char *)G_malloc(strlen(opt.exclude->answer) + 3);
-		sprintf(pattern, "{%s}", opt.exclude->answer);
+		buf = (char *)G_malloc(strlen(exclude) + 3);
+		sprintf(buf, "{%s}", exclude);
 
-		exclude = G_ls_glob_filter(pattern, 1);
+		exclude_filter = G_ls_glob_filter(buf, 1);
 	    }
 	    else
-		exclude = G_ls_glob_filter(opt.exclude->answer, 1);
+		exclude_filter = G_ls_glob_filter(exclude, 1);
 	}
-	if (!exclude)
-	    G_fatal_error(_("Unable to compile pattern <%s>"),
-			  opt.exclude->answer);
+	if (!exclude_filter)
+	    G_fatal_error(_("Unable to compile pattern <%s>"), exclude);
     }
     else
-	exclude = NULL;
+	exclude_filter = NULL;
 
     if (!flag.force->answer)
 	G_message(_("The following data base element files would be deleted:"));
@@ -211,8 +248,8 @@
 
     G_free_ls_filter(filter);
 
-    if (exclude)
-	G_free_ls_filter(exclude);
+    if (exclude_filter)
+	G_free_ls_filter(exclude_filter);
 
     if (!flag.force->answer)
 	G_important_message(_("You must use the force flag (-%c) to actually "
@@ -220,3 +257,16 @@
 
     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