main.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. \
  2. /****************************************************************************
  3. *
  4. * MODULE: cmd
  5. * AUTHOR(S): CERL (original contributor)
  6. * Radim Blazek <radim.blazek gmail.com>,
  7. * Cedric Shock <cedricgrass shockfamily.net>,
  8. * Huidae Cho <grass4u gmail.com>,
  9. * Glynn Clements <glynn gclements.plus.com>,
  10. * Jachym Cepicky <jachym les-ejk.cz>,
  11. * Markus Neteler <neteler itc.it>,
  12. * Martin Landa <landa.martin gmail.com>
  13. * PURPOSE: lets users remove GRASS database files
  14. * COPYRIGHT: (C) 1999-2007, 2011 by the GRASS Development Team
  15. *
  16. * This program is free software under the GNU General Public
  17. * License (>=v2). Read the file COPYING that comes with GRASS
  18. * for details.
  19. *
  20. *****************************************************************************/
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <grass/raster.h>
  24. #include <grass/glocale.h>
  25. #include <grass/manage.h>
  26. static int check_reclass(const char *name, const char *mapset, int force)
  27. {
  28. char rname[GNAME_MAX], rmapset[GMAPSET_MAX];
  29. char **rmaps;
  30. int nrmaps;
  31. if (Rast_is_reclassed_to(name, mapset, &nrmaps, &rmaps) > 0) {
  32. for (; *rmaps; rmaps++) {
  33. /* force remove */
  34. if (force)
  35. G_warning(_("Raster map <%s@%s> is a base map for <%s>. Remove forced."),
  36. name, mapset, *rmaps);
  37. else
  38. G_warning(_("Raster <%s@%s> is a base map. Remove reclassed map <%s> first."),
  39. name, mapset, *rmaps);
  40. }
  41. if (!force)
  42. return 1;
  43. }
  44. if (Rast_is_reclass(name, mapset, rname, rmapset) > 0 &&
  45. Rast_is_reclassed_to(rname, rmapset, &nrmaps, &rmaps) > 0) {
  46. char path[GPATH_MAX];
  47. char *p = strchr(rname, '@');
  48. char *qname = G_fully_qualified_name(name, mapset);
  49. if (p)
  50. *p = '\0';
  51. G_file_name_misc(path, "cell_misc", "reclassed_to", rname, rmapset);
  52. if (nrmaps == 1 && !G_strcasecmp(rmaps[0], qname)) {
  53. if (remove(path) < 0)
  54. G_warning(_("Removing information about reclassed map from <%s@%s> failed"),
  55. rname, rmapset);
  56. }
  57. else {
  58. FILE *fp = fopen(path, "w");
  59. if (fp) {
  60. for (; *rmaps; rmaps++)
  61. if (G_strcasecmp(*rmaps, qname))
  62. fprintf(fp, "%s\n", *rmaps);
  63. fclose(fp);
  64. }
  65. else
  66. G_warning(_("Removing information about reclassed map from <%s@%s> failed"),
  67. rname, rmapset);
  68. }
  69. }
  70. return 0;
  71. }
  72. int main(int argc, char *argv[])
  73. {
  74. int i, n, nlist;
  75. struct GModule *module;
  76. struct Option **parm;
  77. struct Flag *force_flag;
  78. const char *name, *mapset;
  79. int result;
  80. G_gisinit(argv[0]);
  81. result = EXIT_SUCCESS;
  82. M_read_list(FALSE, &nlist);
  83. module = G_define_module();
  84. G_add_keyword(_("general"));
  85. G_add_keyword(_("map management"));
  86. G_add_keyword(_("remove"));
  87. module->description =
  88. _("Removes data base element files from "
  89. "the user's current mapset.");
  90. force_flag = G_define_flag();
  91. force_flag->key = 'f';
  92. force_flag->description = _("Force removal for base raster maps");
  93. parm = (struct Option **)G_calloc(nlist, sizeof(struct Option *));
  94. for (n = 0; n < nlist; n++) {
  95. parm[n] = M_define_option(n, "removed", YES);
  96. }
  97. if (G_parser(argc, argv))
  98. exit(EXIT_FAILURE);
  99. mapset = G_mapset();
  100. for (n = 0; n < nlist; n++) {
  101. if (parm[n]->answers)
  102. for (i = 0; (name = parm[n]->answers[i]); i++) {
  103. if (G_strcasecmp(M_get_list(n)->alias, "rast") == 0 &&
  104. check_reclass(name, mapset, force_flag->answer))
  105. continue;
  106. if (M_do_remove(n, name) == 1) {
  107. result = EXIT_FAILURE;
  108. }
  109. }
  110. }
  111. exit(result);
  112. }