main.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /****************************************************************************
  2. *
  3. * MODULE: g.rename
  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: Rename map names
  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. void update_reclass_maps(const char *, const char *);
  26. void update_base_map(const char *, const char *, const char *);
  27. int main(int argc, char *argv[])
  28. {
  29. int n;
  30. struct GModule *module;
  31. struct Option **parm;
  32. int nlist;
  33. const char *mapset;
  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. mapset = G_mapset();
  51. for (n = 0; n < nlist; n++) {
  52. int i;
  53. char *old, *new;
  54. if (parm[n]->answers == NULL)
  55. continue;
  56. i = 0;
  57. while (parm[n]->answers[i]) {
  58. int renamed;
  59. old = parm[n]->answers[i++];
  60. new = parm[n]->answers[i++];
  61. if (!M_find(n, old, mapset)) {
  62. G_warning(_("%s <%s> not found"), M_get_list(n)->maindesc, old);
  63. continue;
  64. }
  65. if (M_find(n, new, mapset) && !(module->overwrite)) {
  66. G_warning(_("<%s> already exists in mapset <%s>"), new,
  67. M_find(n, new, ""));
  68. continue;
  69. }
  70. if (G_legal_filename(new) < 0) {
  71. G_warning(_("<%s> is an illegal file name"), new);
  72. continue;
  73. }
  74. if (G_strcasecmp(old, new) == 0) {
  75. /* avoid problems on case-insensitive file systems (FAT, NTFS, ...) */
  76. G_warning(_("%s=%s,%s: files could be the same, no rename possible"),
  77. parm[n]->key, old, new);
  78. continue;
  79. }
  80. if ((renamed = M_do_rename(n, old, new)) == 1) {
  81. result = EXIT_FAILURE;
  82. }
  83. if (!renamed && strcmp(parm[n]->key, "raster") == 0) {
  84. update_reclass_maps(new, mapset);
  85. update_base_map(old, new, mapset);
  86. }
  87. }
  88. }
  89. exit(result);
  90. }
  91. void update_reclass_maps(const char *name, const char *mapset)
  92. {
  93. int nrmaps;
  94. char **rmaps;
  95. if (Rast_is_reclassed_to(name, mapset, &nrmaps, &rmaps) <= 0)
  96. return;
  97. G_message(_("Updating reclass maps"));
  98. for (; *rmaps; rmaps++) {
  99. char buf1[256], buf2[256], buf3[256], *str;
  100. FILE *fp;
  101. int ptr, l;
  102. G_message(" %s", *rmaps);
  103. sprintf(buf3, "%s", *rmaps);
  104. if ((str = strchr(buf3, '@'))) {
  105. *str = 0;
  106. sprintf(buf2, "%s", str + 1);
  107. }
  108. else {
  109. sprintf(buf2, "%s", mapset);
  110. }
  111. G_file_name(buf1, "cellhd", buf3, buf2);
  112. fp = fopen(buf1, "r");
  113. if (fp == NULL)
  114. continue;
  115. fgets(buf2, 255, fp);
  116. fgets(buf2, 255, fp);
  117. fgets(buf2, 255, fp);
  118. ptr = G_ftell(fp);
  119. G_fseek(fp, 0L, SEEK_END);
  120. l = G_ftell(fp) - ptr;
  121. str = (char *)G_malloc(l);
  122. G_fseek(fp, ptr, SEEK_SET);
  123. fread(str, l, 1, fp);
  124. fclose(fp);
  125. fp = fopen(buf1, "w");
  126. fprintf(fp, "reclass\n");
  127. fprintf(fp, "name: %s\n", name);
  128. fprintf(fp, "mapset: %s\n", mapset);
  129. fwrite(str, l, 1, fp);
  130. G_free(str);
  131. fclose(fp);
  132. }
  133. }
  134. void update_base_map(const char *old, const char *new, const char *mapset)
  135. {
  136. int i, nrmaps, found;
  137. char bname[GNAME_MAX], bmapset[GMAPSET_MAX], rpath[GPATH_MAX];
  138. char *xold, *xnew, **rmaps;
  139. FILE *fp;
  140. if (Rast_is_reclass(new, mapset, bname, bmapset) <= 0)
  141. return;
  142. if (Rast_is_reclassed_to(bname, bmapset, &nrmaps, &rmaps) <= 0)
  143. nrmaps = 0;
  144. found = 0;
  145. xold = G_fully_qualified_name(old, mapset);
  146. for (i = 0; i < nrmaps; i++) {
  147. if (strcmp(xold, rmaps[i]) == 0) {
  148. found = 1;
  149. break;
  150. }
  151. }
  152. if (!found) {
  153. G_fatal_error(_("Unable to find reclass information for <%s> in "
  154. "base map <%s@%s>"), xold, bname, bmapset);
  155. }
  156. G_message(_("Updating base map <%s@%s>"), bname, bmapset);
  157. G_file_name_misc(rpath, "cell_misc", "reclassed_to", bname, bmapset);
  158. fp = fopen(rpath, "w");
  159. if (fp == NULL) {
  160. G_fatal_error(_("Unable to update dependency file in <%s@%s>"),
  161. bname, bmapset);
  162. }
  163. xnew = G_fully_qualified_name(new, mapset);
  164. for (; *rmaps; rmaps++) {
  165. if (strcmp(xold, *rmaps) == 0) {
  166. fprintf(fp, "%s\n", xnew);
  167. }
  168. else {
  169. fprintf(fp, "%s\n", *rmaps);
  170. }
  171. }
  172. G_free(xold);
  173. G_free(xnew);
  174. fclose(fp);
  175. }