[GRASS-SVN] r61041 - grass/branches/releasebranch_7_0/general/g.mremove
svn_grass at osgeo.org
svn_grass at osgeo.org
Sat Jun 28 16:04:32 PDT 2014
Author: hcho
Date: 2014-06-28 16:04:32 -0700 (Sat, 28 Jun 2014)
New Revision: 61041
Modified:
grass/branches/releasebranch_7_0/general/g.mremove/g.mremove.html
grass/branches/releasebranch_7_0/general/g.mremove/main.c
Log:
gg.mremove: backport from trunk
Modified: grass/branches/releasebranch_7_0/general/g.mremove/g.mremove.html
===================================================================
--- grass/branches/releasebranch_7_0/general/g.mremove/g.mremove.html 2014-06-28 23:01:22 UTC (rev 61040)
+++ grass/branches/releasebranch_7_0/general/g.mremove/g.mremove.html 2014-06-28 23:04:32 UTC (rev 61041)
@@ -1,22 +1,28 @@
<h2>DESCRIPTION</h2>
-<em>g.mremove</em> removes data files matching a pattern given by wildcards or POSIX Extended Regular Expressions.
-If the <b>-f</b> force flag is not given then nothing is removed, instead
-the list of selected file names is printed to <tt>stdout</tt>
-as a preview of the files to be deleted.
+<em>g.mremove</em> removes data files matching a pattern given by wildcards or
+POSIX Extended Regular Expressions. If the <b>-f</b> force flag is not given
+then nothing is removed, instead the list of selected file names is printed to
+<tt>stdout</tt> as a preview of the files to be deleted.
-<h2>EXAMPLE</h2>
+<h2>EXAMPLES</h2>
Delete all raster maps starting with "<tt>tmp_</tt>" in the current mapset:
<div class="code"><pre>
# show matching raster maps but do not delete yet (as verification)
-g.mremove rast="tmp_*"
+g.mremove type=rast pattern="tmp_*"
# actually delete the matching raster maps
-g.mremove -f rast="tmp_*"
+g.mremove -f type=rast pattern="tmp_*"
</pre></div>
+Delete all raster maps starting with "<tt>stream_</tt>" in the current mapset,
+but exclude those ending with "<tt>_final</tt>":
+<div class="code"><pre>
+g.mremove -f type=rast pattern="stream_*" exclude="*_final"
+</pre></div>
+
<h2>SEE ALSO</h2>
<em>
Modified: grass/branches/releasebranch_7_0/general/g.mremove/main.c
===================================================================
--- grass/branches/releasebranch_7_0/general/g.mremove/main.c 2014-06-28 23:01:22 UTC (rev 61040)
+++ grass/branches/releasebranch_7_0/general/g.mremove/main.c 2014-06-28 23:04:32 UTC (rev 61041)
@@ -17,7 +17,7 @@
*
* PURPOSE: lets users remove GRASS database files
*
- * COPYRIGHT: (C) 1999-2011 by the GRASS Development Team
+ * COPYRIGHT: (C) 1999-2014 by the GRASS Development Team
*
* This program is free software under the GNU General
* Public License (>=v2). Read the file COPYING that
@@ -26,8 +26,8 @@
*****************************************************************************/
#include <stdlib.h>
+#include <string.h>
#include <unistd.h>
-
#include <grass/gis.h>
#include <grass/manage.h>
#include <grass/glocale.h>
@@ -38,20 +38,23 @@
int main(int argc, char *argv[])
{
struct GModule *module;
- struct Option **opt, *o;
struct
{
+ struct Option *type;
+ struct Option *pattern;
+ struct Option *exclude;
+ } opt;
+ struct
+ {
struct Flag *regex;
struct Flag *extended;
struct Flag *force;
struct Flag *basemap;
} flag;
const char *mapset;
- char *name, path[GPATH_MAX], **files;
- const struct list *option;
- int num_files, rast, result;
- int i, j, n, nlist;
- void *filter;
+ int result;
+ int i, all, num_types, nlist;
+ void *filter, *exclude;
G_gisinit(argv[0]);
@@ -64,6 +67,28 @@
module->description =
_("Removes data base element files from "
"the user's current mapset using the search pattern.");
+
+ M_read_list(FALSE, &nlist);
+
+ opt.type = G_define_standard_option(G_OPT_M_DATATYPE);
+ opt.type->multiple = YES;
+ opt.type->options = M_get_options(TRUE);
+ opt.type->descriptions = M_get_option_desc(TRUE);
+
+ opt.pattern = G_define_option();
+ opt.pattern->key = "pattern";
+ opt.pattern->type = TYPE_STRING;
+ opt.pattern->required = YES;
+ opt.pattern->description = _("Map 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->description = _("Map name exclusion pattern (default: none)");
+ opt.exclude->guisection = _("Pattern");
+
flag.regex = G_define_flag();
flag.regex->key = 'r';
flag.regex->description =
@@ -84,14 +109,6 @@
flag.basemap->description = _("Remove base raster maps");
flag.basemap->guisection = _("Raster");
- M_read_list(FALSE, &nlist);
-
- opt = (struct Option **)G_calloc(nlist, sizeof(struct Option *));
-
- for (n = 0; n < nlist; n++) {
- o = opt[n] = M_define_option(n, _("removed"), YES);
- }
-
if (G_parser(argc, argv))
exit(EXIT_FAILURE);
@@ -99,60 +116,105 @@
G_fatal_error(_("-%c and -%c are mutually exclusive"),
flag.regex->key, flag.extended->key);
+ if (flag.regex->answer || flag.extended->answer)
+ filter = G_ls_regex_filter(opt.pattern->answer, 0,
+ (int)flag.extended->answer);
+ else {
+ /* handle individual map names */
+ if (strchr(opt.pattern->answer, ',')) {
+ char *pattern;
+
+ pattern = (char *)G_malloc(strlen(opt.pattern->answer) + 3);
+ sprintf(pattern, "{%s}", opt.pattern->answer);
+
+ filter = G_ls_glob_filter(pattern, 0);
+ }
+ else
+ filter = G_ls_glob_filter(opt.pattern->answer, 0);
+ }
+ if (!filter)
+ G_fatal_error(_("Unable to compile pattern <%s>"), opt.pattern->answer);
+
+ if (opt.exclude->answer) {
+ if (flag.regex->answer || flag.extended->answer)
+ exclude = G_ls_regex_filter(opt.exclude->answer, 1,
+ (int)flag.extended->answer);
+ else {
+ /* handle individual map names */
+ if (strchr(opt.exclude->answer, ',')) {
+ char *pattern;
+
+ pattern = (char *)G_malloc(strlen(opt.exclude->answer) + 3);
+ sprintf(pattern, "{%s}", opt.exclude->answer);
+
+ exclude = G_ls_glob_filter(pattern, 1);
+ }
+ else
+ exclude = G_ls_glob_filter(opt.exclude->answer, 1);
+ }
+ if (!exclude)
+ G_fatal_error(_("Unable to compile pattern <%s>"),
+ opt.exclude->answer);
+ }
+ else
+ exclude = NULL;
+
if (!flag.force->answer)
G_message(_("The following data base element files would be deleted:"));
- for (n = 0; n < nlist; n++) {
- o = opt[n];
- G_free((char *)o->gisprompt);
- G_free((char *)o->description);
- }
-
mapset = G_mapset();
- for (n = 0; n < nlist; n++) {
- option = M_get_list(n);
- if (opt[n]->answers) {
- G_file_name(path, M_get_list(n)->element[0], "", mapset);
- if (access(path, 0) != 0)
- continue;
- rast = !G_strcasecmp(option->alias, "rast");
- for (i = 0; (name = opt[n]->answers[i]); i++) {
- if (!flag.regex->answer && !flag.extended->answer)
- filter = G_ls_glob_filter(name, 0);
- else
- filter = G_ls_regex_filter(name, 0,
- (int) flag.extended->answer);
- if (!filter)
- G_fatal_error(_("Unable to compile pattern <%s>"),
- name);
+ for (i = 0; opt.type->answers[i]; i++) {
+ if (strcmp(opt.type->answers[i], "all") == 0)
+ break;
+ }
+ if (opt.type->answers[i]) {
+ all = 1;
+ num_types = nlist;
+ }
+ else {
+ all = 0;
+ num_types = i;
+ }
- files = G__ls(path, &num_files);
+ for (i = 0; i < num_types; i++) {
+ int n, rast, num_files, j;
+ const struct list *elem;
+ char path[GPATH_MAX];
+ char **files;
- G_free_ls_filter(filter);
+ n = all ? i : M_get_element(opt.type->answers[i]);
+ elem = M_get_list(n);
- for (j = 0; j < num_files; j++) {
- if (!flag.force->answer) {
- fprintf(stdout, "%s/%s@%s\n", option->alias, files[j],
- mapset);
- continue;
- }
- if (rast &&
- check_reclass(files[j], mapset, flag.basemap->answer))
- continue;
+ G_file_name(path, elem->element[0], "", mapset);
+ if (access(path, 0) != 0)
+ continue;
- if (M_do_remove(n, (char *)files[j]) == 1)
- result = EXIT_FAILURE;
- }
+ 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;
}
}
- if (!flag.force->answer) {
+ G_free_ls_filter(filter);
+
+ if (exclude)
+ G_free_ls_filter(exclude);
+
+ if (!flag.force->answer)
G_important_message(_("You must use the force flag (-%c) to actually "
"remove them. Exiting."), flag.force->key);
- }
exit(result);
}
-
More information about the grass-commit
mailing list