main.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /****************************************************************************
  2. *
  3. * MODULE: g.rename cmd
  4. * AUTHOR(S): CERL (original contributor)
  5. * Radim Blazek <radim.blazek gmail.com>,
  6. * Cedric Shock <cedricgrass shockfamily.net>,
  7. * Glynn Clements <glynn gclements.plus.com>,
  8. * Markus Neteler <neteler itc.it>,
  9. * Martin Landa <landa.martin gmail.com>,
  10. * Huidae Cho <grass4u gmail.com>
  11. * PURPOSE:
  12. * COPYRIGHT: (C) 1994-2007, 2011-2014 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/raster.h>
  23. #include <grass/glocale.h>
  24. #include <grass/manage.h>
  25. int main(int argc, char *argv[])
  26. {
  27. int i, n;
  28. struct GModule *module;
  29. struct Option **parm;
  30. char *old, *new;
  31. int nrmaps, nlist;
  32. const char *mapset, *location_path;
  33. char **rmaps;
  34. int result = EXIT_SUCCESS;
  35. G_gisinit(argv[0]);
  36. M_read_list(FALSE, &nlist);
  37. module = G_define_module();
  38. G_add_keyword(_("general"));
  39. G_add_keyword(_("map management"));
  40. G_add_keyword(_("rename"));
  41. module->description =
  42. _("Renames data base element files in 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, _("renamed"), NO);
  47. }
  48. if (G_parser(argc, argv))
  49. exit(EXIT_FAILURE);
  50. location_path = G_location_path();
  51. mapset = G_mapset();
  52. for (n = 0; n < nlist; n++) {
  53. if (parm[n]->answers == NULL)
  54. continue;
  55. i = 0;
  56. while (parm[n]->answers[i]) {
  57. int renamed;
  58. old = parm[n]->answers[i++];
  59. new = parm[n]->answers[i++];
  60. if (!M_find(n, old, mapset)) {
  61. G_warning(_("%s <%s> not found"), M_get_list(n)->maindesc, old);
  62. continue;
  63. }
  64. if (M_find(n, new, "") && !(module->overwrite)) {
  65. G_warning(_("<%s> already exists in mapset <%s>"), new,
  66. M_find(n, new, ""));
  67. continue;
  68. }
  69. if (G_legal_filename(new) < 0) {
  70. G_warning(_("<%s> is an illegal file name"), new);
  71. continue;
  72. }
  73. if (G_strcasecmp(old, new) == 0) {
  74. /* avoid problems on case-insensitive file systems (FAT, NTFS, ...) */
  75. G_warning(_("%s=%s,%s: files could be the same, no rename possible"),
  76. parm[n]->key, old, new);
  77. continue;
  78. }
  79. if ((renamed = M_do_rename(n, old, new)) == 1) {
  80. result = EXIT_FAILURE;
  81. }
  82. if (!renamed && strcmp(parm[n]->key, "rast") == 0 &&
  83. Rast_is_reclassed_to(old, mapset, &nrmaps, &rmaps) > 0) {
  84. int ptr, l;
  85. char buf1[256], buf2[256], buf3[256], *str;
  86. FILE *fp;
  87. G_message(_("Renaming reclass maps"));
  88. for (; *rmaps; rmaps++) {
  89. G_message(" %s", *rmaps);
  90. sprintf(buf3, "%s", *rmaps);
  91. if ((str = strchr(buf3, '@'))) {
  92. *str = 0;
  93. sprintf(buf2, "%s", str + 1);
  94. }
  95. else {
  96. sprintf(buf2, "%s", mapset);
  97. }
  98. sprintf(buf1, "%s/%s/cellhd/%s", location_path, buf2,
  99. buf3);
  100. fp = fopen(buf1, "r");
  101. if (fp == NULL)
  102. continue;
  103. fgets(buf2, 255, fp);
  104. fgets(buf2, 255, fp);
  105. fgets(buf2, 255, fp);
  106. ptr = G_ftell(fp);
  107. G_fseek(fp, 0L, SEEK_END);
  108. l = G_ftell(fp) - ptr;
  109. str = (char *)G_malloc(l);
  110. G_fseek(fp, ptr, SEEK_SET);
  111. fread(str, l, 1, fp);
  112. fclose(fp);
  113. fp = fopen(buf1, "w");
  114. fprintf(fp, "reclass\n");
  115. fprintf(fp, "name: %s\n", new);
  116. fprintf(fp, "mapset: %s\n", mapset);
  117. fwrite(str, l, 1, fp);
  118. G_free(str);
  119. fclose(fp);
  120. }
  121. }
  122. }
  123. }
  124. exit(result);
  125. }