|
@@ -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,7 +38,12 @@ int check_reclass(const char *, const char *, int);
|
|
|
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;
|
|
@@ -47,11 +52,9 @@ int main(int argc, char *argv[])
|
|
|
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 @@ int main(int argc, char *argv[])
|
|
|
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 @@ int main(int argc, char *argv[])
|
|
|
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 @@ int main(int argc, char *argv[])
|
|
|
G_fatal_error(_("-%c and -%c are mutually exclusive"),
|
|
|
flag.regex->key, flag.extended->key);
|
|
|
|
|
|
- if (!flag.force->answer)
|
|
|
- G_message(_("The following data base element files would be deleted:"));
|
|
|
+ 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;
|
|
|
|
|
|
- for (n = 0; n < nlist; n++) {
|
|
|
- o = opt[n];
|
|
|
- G_free((char *)o->gisprompt);
|
|
|
- G_free((char *)o->description);
|
|
|
+ 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:"));
|
|
|
|
|
|
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)
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+
|
|
|
+ for (i = 0; i < num_types; i++) {
|
|
|
+ int n, rast, num_files, j;
|
|
|
+ const struct list *elem;
|
|
|
+ char path[GPATH_MAX];
|
|
|
+ char **files;
|
|
|
+
|
|
|
+ n = all ? i : M_get_element(opt.type->answers[i]);
|
|
|
+ elem = M_get_list(n);
|
|
|
+
|
|
|
+ G_file_name(path, elem->element[0], "", mapset);
|
|
|
+ if (access(path, 0) != 0)
|
|
|
+ continue;
|
|
|
+
|
|
|
+ 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;
|
|
|
- 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);
|
|
|
-
|
|
|
- files = G__ls(path, &num_files);
|
|
|
-
|
|
|
- G_free_ls_filter(filter);
|
|
|
-
|
|
|
- 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;
|
|
|
-
|
|
|
- if (M_do_remove(n, (char *)files[j]) == 1)
|
|
|
- result = EXIT_FAILURE;
|
|
|
- }
|
|
|
}
|
|
|
+
|
|
|
+ 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);
|
|
|
}
|
|
|
-
|