do_copy.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*!
  2. \file lib/manage/do_copy.c
  3. \brief Manage Library - Copy element
  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 <string.h>
  11. #include <unistd.h>
  12. #include <grass/gis.h>
  13. #include <grass/glocale.h>
  14. #include <grass/vector.h>
  15. #include "manage_local_proto.h"
  16. /*!
  17. \brief Copy element
  18. \param n element id
  19. \param old source name
  20. \param mapset name of source mapset
  21. \param new destination name
  22. \return 0 on success
  23. \return 1 on error
  24. */
  25. int M_do_copy(int n, const char *old, const char *mapset, const char *new)
  26. {
  27. int i, ret;
  28. char path[GPATH_MAX], path2[GPATH_MAX];
  29. int result = 0;
  30. G_debug(3, "Copy %s", list[n].alias);
  31. G_message(_("Copying %s <%s> to current mapset as <%s>"),
  32. list[n].maindesc, G_fully_qualified_name(old, mapset), new);
  33. M__hold_signals(1);
  34. if (G_strcasecmp(list[n].alias, "vector") == 0) {
  35. ret = Vect_copy(old, mapset, new);
  36. if (ret == -1) {
  37. G_warning(_("Unable to copy <%s> to current mapset as <%s>"),
  38. G_fully_qualified_name(old, mapset), new);
  39. result = 1;
  40. }
  41. }
  42. else {
  43. for (i = 0; i < list[n].nelem; i++) {
  44. G_make_mapset_object_group(list[n].element[i]);
  45. G_file_name(path, list[n].element[i], old, mapset);
  46. if (access(path, 0) != 0) {
  47. G_remove(list[n].element[i], new);
  48. G_verbose_message(_("%s is missing"), list[n].desc[i]);
  49. continue;
  50. }
  51. G_file_name(path2, list[n].element[i], new, G_mapset());
  52. if (G_recursive_copy(path, path2) == 1) {
  53. G_warning(_("Unable to copy <%s> to current mapset as <%s>"),
  54. G_fully_qualified_name(old, mapset), new);
  55. result = 1;
  56. }
  57. else {
  58. G_verbose_message(_("%s copied"), list[n].desc[i]);
  59. }
  60. }
  61. }
  62. /* special case: remove (yes, remove) the secondary color table, if it exists */
  63. if (G_strcasecmp(list[n].element[0], "cell") == 0) {
  64. char colr2[6 + GMAPSET_MAX];
  65. if (snprintf(colr2, 6 + GMAPSET_MAX, "colr2/%s", G_mapset()) >=
  66. 6 + GMAPSET_MAX)
  67. G_warning(_("String for secondary color table has been truncated"));
  68. G_remove(colr2, new);
  69. }
  70. M__hold_signals(0);
  71. return result;
  72. }