main.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. * PURPOSE:
  11. * COPYRIGHT: (C) 1994-2007, 2011 by the GRASS Development Team
  12. *
  13. * This program is free software under the GNU General Public
  14. * License (>=v2). Read the file COPYING that comes with GRASS
  15. * for details.
  16. *
  17. *****************************************************************************/
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <grass/gis.h>
  21. #include <grass/raster.h>
  22. #include <grass/glocale.h>
  23. #include <grass/manage.h>
  24. int main(int argc, char *argv[])
  25. {
  26. int i, n;
  27. struct GModule *module;
  28. struct Option **parm, *p;
  29. char *old, *new;
  30. int nrmaps, nlist;
  31. const char *mapset, *location_path;
  32. char **rmaps;
  33. int result = EXIT_SUCCESS;
  34. G_gisinit(argv[0]);
  35. M_read_list(FALSE, &nlist);
  36. module = G_define_module();
  37. G_add_keyword(_("general"));
  38. G_add_keyword(_("map management"));
  39. G_add_keyword(_("rename"));
  40. module->description =
  41. _("Renames data base element files in the user's current mapset.");
  42. module->overwrite = 1;
  43. parm = (struct Option **)G_calloc(nlist, sizeof(struct Option *));
  44. for (n = 0; n < nlist; n++) {
  45. p = parm[n] = M_define_option(n, _("renamed"), NO);
  46. }
  47. if (G_parser(argc, argv))
  48. exit(EXIT_FAILURE);
  49. location_path = G__location_path();
  50. mapset = G_mapset();
  51. for (n = 0; n < nlist; n++) {
  52. if (parm[n]->answers == NULL)
  53. continue;
  54. i = 0;
  55. while (parm[n]->answers[i]) {
  56. old = parm[n]->answers[i++];
  57. new = parm[n]->answers[i++];
  58. if (!M_find(n, old, mapset)) {
  59. G_warning(_("%s <%s> not found"), M_get_list(n)->maindesc, old);
  60. continue;
  61. }
  62. if (M_find(n, new, "") && !(module->overwrite)) {
  63. G_warning(_("<%s> already exists in mapset <%s>"), new,
  64. M_find(n, new, ""));
  65. continue;
  66. }
  67. if (G_legal_filename(new) < 0) {
  68. G_warning(_("<%s> is an illegal file name"), new);
  69. continue;
  70. }
  71. if (G_strcasecmp(old, new) == 0) {
  72. /* avoid problems on case-insensitive file systems (FAT, NTFS, ...) */
  73. G_warning(_("%s=%s,%s: files could be the same, no rename possible"),
  74. parm[n]->key, old, new);
  75. continue;
  76. }
  77. if (Rast_is_reclassed_to(old, mapset, &nrmaps, &rmaps) > 0) {
  78. int ptr, l;
  79. char buf1[256], buf2[256], buf3[256], *str;
  80. FILE *fp;
  81. G_message(_("Renaming reclass maps"));
  82. for (; *rmaps; rmaps++) {
  83. G_message(" %s", *rmaps);
  84. sprintf(buf3, "%s", *rmaps);
  85. if ((str = strchr(buf3, '@'))) {
  86. *str = 0;
  87. sprintf(buf2, "%s", str + 1);
  88. }
  89. else {
  90. sprintf(buf2, "%s", mapset);
  91. }
  92. sprintf(buf1, "%s/%s/cellhd/%s", location_path, buf2,
  93. buf3);
  94. fp = fopen(buf1, "r");
  95. if (fp == NULL)
  96. continue;
  97. fgets(buf2, 255, fp);
  98. fgets(buf2, 255, fp);
  99. fgets(buf2, 255, fp);
  100. ptr = G_ftell(fp);
  101. G_fseek(fp, 0L, SEEK_END);
  102. l = G_ftell(fp) - ptr;
  103. str = (char *)G_malloc(l);
  104. G_fseek(fp, ptr, SEEK_SET);
  105. fread(str, l, 1, fp);
  106. fclose(fp);
  107. fp = fopen(buf1, "w");
  108. fprintf(fp, "reclass\n");
  109. fprintf(fp, "name: %s\n", new);
  110. fprintf(fp, "mapset: %s\n", mapset);
  111. fwrite(str, l, 1, fp);
  112. G_free(str);
  113. fclose(fp);
  114. }
  115. }
  116. if (M_do_rename(n, old, new) == 1) {
  117. result = EXIT_FAILURE;
  118. }
  119. }
  120. }
  121. exit(result);
  122. }