do_rename.c 2.7 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 <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <grass/gis.h>
  13. #include <grass/vector.h>
  14. #include <grass/raster3d.h>
  15. #include <grass/glocale.h>
  16. #include "manage_local_proto.h"
  17. /*!
  18. \brief Rename element
  19. \param n element id
  20. \param old source name
  21. \param new destination name
  22. \return 0 on success
  23. \return 1 on error
  24. */
  25. int M_do_rename(int n, const char *old, const char *new)
  26. {
  27. int i, ret;
  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. M__hold_signals(1);
  36. if (G_strcasecmp(list[n].alias, "vector") == 0) {
  37. if ((mapset = G_find_vector2(old, "")) == NULL) {
  38. G_warning(_("Vector map <%s> not found"), old);
  39. }
  40. else {
  41. ret = Vect_rename(old, new);
  42. if (ret != -1) {
  43. renamed = 1;
  44. }
  45. else {
  46. G_warning(_("Unable to rename vector map <%s> to <%s>"),
  47. old, new);
  48. result = 1;
  49. }
  50. }
  51. }
  52. else {
  53. if (G_strcasecmp(list[n].alias, "raster") == 0) {
  54. if ((mapset = G_find_raster2(old, "")) == NULL)
  55. G_warning(_("Raster map <%s> not found"), old);
  56. }
  57. if (G_strcasecmp(list[n].alias, "raster_3d") == 0) {
  58. if ((mapset = G_find_raster3d(old, "")) == NULL)
  59. G_warning(_("3D raster map <%s> not found"), old);
  60. }
  61. for (i = 0; i < list[n].nelem; i++) {
  62. G_remove(list[n].element[i], new);
  63. switch (G_rename(list[n].element[i], old, new)) {
  64. case -1:
  65. G_warning(_("Unable to rename %s"), list[n].desc[i]);
  66. result = 1;
  67. break;
  68. case 0:
  69. G_verbose_message(_("%s is missing"), list[n].desc[i]);
  70. break;
  71. case 1:
  72. G_verbose_message(_("%s renamed"), list[n].desc[i]);
  73. renamed = 1;
  74. break;
  75. }
  76. }
  77. if (G_strcasecmp(list[n].element[0], "cell") == 0) {
  78. char colr2[6 + GMAPSET_MAX];
  79. if (snprintf(colr2, 6 + GMAPSET_MAX, "colr2/%s", G_mapset()) >=
  80. 6 + GMAPSET_MAX)
  81. G_warning(_("String for secondary color table has been truncated"));
  82. G_remove(colr2, new);
  83. switch (G_rename(colr2, old, new)) {
  84. case -1:
  85. G_warning(_("Unable to rename %s"), colr2);
  86. result = 1;
  87. break;
  88. case 0:
  89. G_verbose_message(_("%s is missing"), colr2);
  90. break;
  91. case 1:
  92. G_verbose_message(_("%s renamed"), colr2);
  93. renamed = 1;
  94. break;
  95. }
  96. }
  97. }
  98. M__hold_signals(0);
  99. if (!renamed)
  100. G_warning(_("<%s> nothing renamed"), old);
  101. return result;
  102. }