main.c 3.8 KB

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