main.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /****************************************************************************
  2. *
  3. * MODULE: g.copy
  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. * Markus Neteler <neteler itc.it>,
  10. * Martin Landa <landa.martin gmail.com>
  11. * PURPOSE: lets users copy database files
  12. * COPYRIGHT: (C) 2003-2007 by the GRASS Development Team
  13. *
  14. * This program is free software under the GNU General Public
  15. * License (>=v2). Read the file COPYING that comes with GRASS
  16. * for details.
  17. *
  18. *****************************************************************************/
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <grass/gis.h>
  22. #include <grass/glocale.h>
  23. #include <grass/manage.h>
  24. int main(int argc, char *argv[])
  25. {
  26. int i, n, nlist;
  27. const char *mapset;
  28. struct GModule *module;
  29. struct Option **parm;
  30. char *from, *to;
  31. int result = EXIT_SUCCESS;
  32. int successful = 0;
  33. G_gisinit(argv[0]);
  34. M_read_list(FALSE, &nlist);
  35. module = G_define_module();
  36. G_add_keyword(_("general"));
  37. G_add_keyword(_("map management"));
  38. G_add_keyword(_("copy"));
  39. module->label = _("Creates copies of maps and other elements");
  40. module->description =
  41. _("Copies available data files in the current mapset "
  42. "search path to the user's current mapset.");
  43. module->overwrite = 1;
  44. parm = (struct Option **) G_calloc(nlist, sizeof(struct Option *));
  45. for (n = 0; n < nlist; n++) {
  46. parm[n] = M_define_option(n, _("copied"), NO);
  47. }
  48. if (G_parser(argc, argv))
  49. exit(EXIT_FAILURE);
  50. for (n = 0; n < nlist; n++) {
  51. if (parm[n]->answers == NULL)
  52. continue;
  53. i = 0;
  54. while (parm[n]->answers[i]) {
  55. from = parm[n]->answers[i++];
  56. to = parm[n]->answers[i++];
  57. mapset = M_find(n, from, "");
  58. if (!mapset) {
  59. G_warning(_("<%s> does not exist. Skipping."), from);
  60. continue;
  61. }
  62. if (G_strcasecmp(mapset, G_mapset()) == 0 &&
  63. G_strcasecmp(from, to) == 0) {
  64. G_warning(_("%s=%s,%s: files are the same, no copy required. Skipping."),
  65. parm[n]->key, from, to);
  66. continue;
  67. }
  68. if (M_find(n, to, G_mapset()) && !(module->overwrite)) {
  69. G_warning(_("<%s> already exists and"
  70. " overwrite is not enabled. Skipping."), to);
  71. continue;
  72. }
  73. if (G_legal_filename(to) < 0) {
  74. G_warning(_("<%s> is an illegal file name. Skipping."), to);
  75. continue;
  76. }
  77. if (M_do_copy(n, from, mapset, to) == 1) {
  78. result = EXIT_FAILURE;
  79. }
  80. else {
  81. successful += 1;
  82. }
  83. G_remove_misc("cell_misc", "reclassed_to", to);
  84. }
  85. }
  86. if (successful == 0 && result != EXIT_FAILURE) {
  87. /* No successful ones and no failures means all were skipped. */
  88. G_fatal_error(_("All inputs had to be skipped. Nothing to copy."
  89. " See the warning messages above for details."));
  90. }
  91. if (successful == 0 && result == EXIT_FAILURE) {
  92. /* No successful ones and a failure means at least
  93. * one errored, some possibly skipped. */
  94. G_fatal_error(_("Nothing was copied."
  95. " See the warning messages above for details."));
  96. }
  97. if (result == EXIT_FAILURE) {
  98. /* Some successful ones and at least one failure means we are ending
  99. * with an error regardless of skipped ones. */
  100. G_fatal_error(_("Errors occurred during copying."
  101. " See the warning messages above for details."));
  102. }
  103. /* This should be success only. */
  104. exit(result);
  105. }