main.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /****************************************************************************
  2. *
  3. * MODULE: g.mremove
  4. *
  5. * AUTHOR(S): Huidae Cho <grass4u gmail.com>
  6. *
  7. * Based on general/manage/cmd/remove.c by
  8. * CERL (original contributor),
  9. * Radim Blazek <radim.blazek gmail.com>,
  10. * Cedric Shock <cedricgrass shockfamily.net>,
  11. * Huidae Cho <grass4u gmail.com>,
  12. * Glynn Clements <glynn gclements.plus.com>,
  13. * Jachym Cepicky <jachym les-ejk.cz>,
  14. * Markus Neteler <neteler itc.it>,
  15. * Martin Landa <landa.martin gmail.com>
  16. *
  17. * PURPOSE: lets users remove GRASS database files
  18. *
  19. * COPYRIGHT: (C) 1999-2011 by the GRASS Development Team
  20. *
  21. * This program is free software under the GNU General
  22. * Public License (>=v2). Read the file COPYING that
  23. * comes with GRASS for details.
  24. *
  25. *****************************************************************************/
  26. #include <stdlib.h>
  27. #include <unistd.h>
  28. #include <grass/gis.h>
  29. #include <grass/manage.h>
  30. #include <grass/glocale.h>
  31. /* check_reclass.c */
  32. int check_reclass(const char *, const char *, int);
  33. int main(int argc, char *argv[])
  34. {
  35. struct GModule *module;
  36. struct Option **opt, *o;
  37. struct
  38. {
  39. struct Flag *regex;
  40. struct Flag *extended;
  41. struct Flag *force;
  42. struct Flag *basemap;
  43. } flag;
  44. const char *mapset;
  45. char *name, path[GPATH_MAX], **files;
  46. const struct list *option;
  47. int num_files, rast, result;
  48. int i, j, n, nlist;
  49. void *filter;
  50. G_gisinit(argv[0]);
  51. result = EXIT_SUCCESS;
  52. module = G_define_module();
  53. G_add_keyword(_("general"));
  54. G_add_keyword(_("map management"));
  55. G_add_keyword(_("remove"));
  56. G_add_keyword(_("multi"));
  57. module->description =
  58. _("Removes data base element files from "
  59. "the user's current mapset using regular expressions.");
  60. flag.regex = G_define_flag();
  61. flag.regex->key = 'r';
  62. flag.regex->description =
  63. _("Use basic regular expressions instead of wildcards");
  64. flag.extended = G_define_flag();
  65. flag.extended->key = 'e';
  66. flag.extended->description =
  67. _("Use extended regular expressions instead of wildcards");
  68. flag.force = G_define_flag();
  69. flag.force->key = 'f';
  70. flag.force->description =
  71. _("Force removal (required for actual deletion of files)");
  72. flag.basemap = G_define_flag();
  73. flag.basemap->key = 'b';
  74. flag.basemap->description = _("Remove base raster maps");
  75. flag.basemap->guisection = _("Raster");
  76. M_read_list(FALSE, &nlist);
  77. opt = (struct Option **)G_calloc(nlist, sizeof(struct Option *));
  78. for (n = 0; n < nlist; n++) {
  79. o = opt[n] = M_define_option(n, _("removed"), YES);
  80. }
  81. if (G_parser(argc, argv))
  82. exit(EXIT_FAILURE);
  83. if (flag.regex->answer && flag.extended->answer)
  84. G_fatal_error(_("-%c and -%c are mutually exclusive"),
  85. flag.regex->key, flag.extended->key);
  86. if (!flag.force->answer)
  87. G_message(_("The following data base element files would be deleted:"));
  88. for (n = 0; n < nlist; n++) {
  89. o = opt[n];
  90. G_free((char *)o->gisprompt);
  91. G_free((char *)o->description);
  92. }
  93. mapset = G_mapset();
  94. for (n = 0; n < nlist; n++) {
  95. option = M_get_list(n);
  96. if (opt[n]->answers) {
  97. G_file_name(path, M_get_list(n)->element[0], "", mapset);
  98. if (access(path, 0) != 0)
  99. continue;
  100. rast = !G_strcasecmp(option->alias, "rast");
  101. for (i = 0; (name = opt[n]->answers[i]); i++) {
  102. if (!flag.regex->answer && !flag.extended->answer)
  103. filter = G_ls_glob_filter(name, 0);
  104. else
  105. filter = G_ls_regex_filter(name, 0,
  106. (int) flag.extended->answer);
  107. if (!filter)
  108. G_fatal_error(_("Unable to compile pattern <%s>"),
  109. name);
  110. files = G__ls(path, &num_files);
  111. G_free_ls_filter(filter);
  112. for (j = 0; j < num_files; j++) {
  113. if (!flag.force->answer) {
  114. fprintf(stdout, "%s/%s@%s\n", option->alias, files[j],
  115. mapset);
  116. continue;
  117. }
  118. if (rast &&
  119. check_reclass(files[j], mapset, flag.basemap->answer))
  120. continue;
  121. if (M_do_remove(n, (char *)files[j]) == 1)
  122. result = EXIT_FAILURE;
  123. }
  124. }
  125. }
  126. }
  127. if (!flag.force->answer) {
  128. G_important_message(_("You must use the force flag (-%c) to actually "
  129. "remove them. Exiting."), flag.force->key);
  130. }
  131. exit(result);
  132. }