do_rename.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include <grass/gis.h>
  4. #include <grass/vector.h>
  5. #include <grass/G3d.h>
  6. #include "list.h"
  7. /*
  8. * returns 0 - success
  9. * 1 - error
  10. */
  11. int do_rename(int n, const char *old, const char *new)
  12. {
  13. int i, ret;
  14. int len;
  15. const char *mapset;
  16. int result = 0;
  17. int renamed = 0;
  18. /* verbosity: --quiet is completely quiet.
  19. GRASS_VERBOSE=1 shows only map names.
  20. normal and --verbose show map names and elements. */
  21. /* there should be used G_message or other derived fn */
  22. if (G_verbose() > G_verbose_min())
  23. fprintf(stderr, _("Rename %s <%s> to <%s>\n"),
  24. list[n].maindesc, old, new);
  25. if (G_strcasecmp(old, new) == 0)
  26. return 1;
  27. len = get_description_len(n);
  28. hold_signals(1);
  29. if (G_strcasecmp(list[n].alias, "vect") == 0) {
  30. if ((mapset = G_find_vector2(old, "")) == NULL) {
  31. G_warning(_("Vector map <%s> not found"), old);
  32. }
  33. else {
  34. ret = Vect_rename(old, new);
  35. if (ret != -1) {
  36. renamed = 1;
  37. }
  38. else {
  39. G_warning(_("Cannot rename <%s> to <%s>"), old, new);
  40. result = 1;
  41. }
  42. }
  43. }
  44. else {
  45. if (G_strcasecmp(list[n].alias, "rast") == 0) {
  46. if ((mapset = G_find_raster2(old, "")) == NULL)
  47. G_warning(_("Raster map <%s> not found"), old);
  48. }
  49. if (G_strcasecmp(list[n].alias, "rast3d") == 0) {
  50. if ((mapset = G_find_grid3(old, "")) == NULL)
  51. G_warning(_("3D raster map <%s> not found"), old);
  52. }
  53. for (i = 0; i < list[n].nelem; i++) {
  54. G_remove(list[n].element[i], new);
  55. switch (G_rename(list[n].element[i], old, new)) {
  56. case -1:
  57. G_warning(_("%s: couldn't be removed"), list[n].desc[i]);
  58. result = 1;
  59. break;
  60. case 0:
  61. if (G_verbose() == G_verbose_max())
  62. G_message(_("%s: missing"), list[n].desc[i]);
  63. break;
  64. case 1:
  65. if (G_verbose() == G_verbose_max())
  66. G_message(_("%s: renamed"), list[n].desc[i]);
  67. renamed = 1;
  68. break;
  69. }
  70. }
  71. if (G_strcasecmp(list[n].element[0], "cell") == 0) {
  72. char colr2[50];
  73. sprintf(colr2, "colr2/%s", G_mapset());
  74. G_remove(colr2, new);
  75. switch (G_rename(colr2, old, new)) {
  76. case -1:
  77. G_warning(_("%s: couldn't be renamed"), colr2);
  78. result = 1;
  79. break;
  80. case 0:
  81. if (G_verbose() == G_verbose_max())
  82. G_message(_("%s: missing"), colr2);
  83. break;
  84. case 1:
  85. if (G_verbose() == G_verbose_max())
  86. G_message(_("%s: renamed"), colr2);
  87. renamed = 1;
  88. break;
  89. }
  90. }
  91. }
  92. hold_signals(0);
  93. if (!renamed)
  94. G_warning(_("<%s> nothing renamed"), old);
  95. return result;
  96. }