do_rename.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*!
  2. \file lib/manage/do_rename.c
  3. \brief Manage Library - Rename elements
  4. (C) 2001-2011 by the GRASS Development Team
  5. This program is free software under the GNU General Public License
  6. (>=v2). Read the file COPYING that comes with GRASS for details.
  7. \author Original author CERL
  8. */
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <grass/gis.h>
  12. #include <grass/vector.h>
  13. #include <grass/raster3d.h>
  14. #include <grass/glocale.h>
  15. #include "manage_local_proto.h"
  16. /*!
  17. \brief Rename element
  18. \param n element id
  19. \param old source name
  20. \param new destination name
  21. \return 0 on success
  22. \return 1 on error
  23. */
  24. int M_do_rename(int n, const char *old, const char *new)
  25. {
  26. int i, ret;
  27. int len;
  28. const char *mapset;
  29. int result = 0;
  30. int renamed = 0;
  31. G_message(_("Rename %s <%s> to <%s>"),
  32. list[n].maindesc, old, new);
  33. if (G_strcasecmp(old, new) == 0)
  34. return 1;
  35. len = M__get_description_len(n);
  36. M__hold_signals(1);
  37. if (G_strcasecmp(list[n].alias, "vector") == 0) {
  38. if ((mapset = G_find_vector2(old, "")) == NULL) {
  39. G_warning(_("Vector map <%s> not found"), old);
  40. }
  41. else {
  42. ret = Vect_rename(old, new);
  43. if (ret != -1) {
  44. renamed = 1;
  45. }
  46. else {
  47. G_warning(_("Unable to rename vector map <%s> to <%s>"),
  48. old, new);
  49. result = 1;
  50. }
  51. }
  52. }
  53. else {
  54. if (G_strcasecmp(list[n].alias, "raster") == 0) {
  55. if ((mapset = G_find_raster2(old, "")) == NULL)
  56. G_warning(_("Raster map <%s> not found"), old);
  57. }
  58. if (G_strcasecmp(list[n].alias, "raster_3d") == 0) {
  59. if ((mapset = G_find_raster3d(old, "")) == NULL)
  60. G_warning(_("3D raster map <%s> not found"), old);
  61. }
  62. for (i = 0; i < list[n].nelem; i++) {
  63. G_remove(list[n].element[i], new);
  64. switch (G_rename(list[n].element[i], old, new)) {
  65. case -1:
  66. G_warning(_("Unable to rename %s"), list[n].desc[i]);
  67. result = 1;
  68. break;
  69. case 0:
  70. G_verbose_message(_("%s is missing"), list[n].desc[i]);
  71. break;
  72. case 1:
  73. G_verbose_message(_("%s renamed"), list[n].desc[i]);
  74. renamed = 1;
  75. break;
  76. }
  77. }
  78. if (G_strcasecmp(list[n].element[0], "cell") == 0) {
  79. char colr2[50];
  80. sprintf(colr2, "colr2/%s", G_mapset());
  81. G_remove(colr2, new);
  82. switch (G_rename(colr2, old, new)) {
  83. case -1:
  84. G_warning(_("Unable to rename %s"), colr2);
  85. result = 1;
  86. break;
  87. case 0:
  88. G_verbose_message(_("%s is missing"), colr2);
  89. break;
  90. case 1:
  91. G_verbose_message(_("%s renamed"), colr2);
  92. renamed = 1;
  93. break;
  94. }
  95. }
  96. }
  97. M__hold_signals(0);
  98. if (!renamed)
  99. G_warning(_("<%s> nothing renamed"), old);
  100. return result;
  101. }