main.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 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/Rast.h>
  22. #include <grass/glocale.h>
  23. #include <grass/list.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;
  31. const char *mapset, *location_path;
  32. char **rmaps;
  33. int result = EXIT_SUCCESS;
  34. G_gisinit(argv[0]);
  35. read_list(0);
  36. module = G_define_module();
  37. module->keywords = _("general, map management");
  38. module->description =
  39. _("Renames data base element files in the user's current mapset.");
  40. parm = (struct Option **)G_calloc(nlist, sizeof(struct Option *));
  41. for (n = 0; n < nlist; n++) {
  42. char *str;
  43. p = parm[n] = G_define_option();
  44. p->key = list[n].alias;
  45. p->key_desc = "old,new";
  46. p->type = TYPE_STRING;
  47. p->required = NO;
  48. p->multiple = NO;
  49. G_asprintf(&str, "old,%s,%s", list[n].mainelem, list[n].maindesc);
  50. p->gisprompt = str;
  51. G_asprintf(&str, _("%s file(s) to be renamed"), list[n].alias);
  52. p->description = str;
  53. }
  54. if (G_parser(argc, argv))
  55. exit(EXIT_FAILURE);
  56. location_path = G__location_path();
  57. mapset = G_mapset();
  58. for (n = 0; n < nlist; n++) {
  59. if (parm[n]->answers == NULL)
  60. continue;
  61. i = 0;
  62. while (parm[n]->answers[i]) {
  63. old = parm[n]->answers[i++];
  64. new = parm[n]->answers[i++];
  65. if (!find(n, old, mapset)) {
  66. G_warning(_("%s <%s> not found"), list[n].maindesc, old);
  67. continue;
  68. }
  69. if (find(n, new, "") && !(module->overwrite)) {
  70. G_warning(_("<%s> already exists in mapset <%s>"), new,
  71. find(n, new, ""));
  72. continue;
  73. }
  74. if (G_legal_filename(new) < 0) {
  75. G_warning(_("<%s> is an illegal file name"), new);
  76. continue;
  77. }
  78. if (G_strcasecmp(old, new) == 0) {
  79. /* avoid problems on case-insensitive file systems (FAT, NTFS, ...) */
  80. G_warning(_("%s=%s,%s: files could be the same, no rename possible"),
  81. parm[n]->key, old, new);
  82. continue;
  83. }
  84. if (Rast_is_reclassed_to(old, mapset, &nrmaps, &rmaps) > 0) {
  85. int ptr, l;
  86. char buf1[256], buf2[256], buf3[256], *str;
  87. FILE *fp;
  88. G_message(_("Renaming reclass maps"));
  89. for (; *rmaps; rmaps++) {
  90. G_message(" %s", *rmaps);
  91. sprintf(buf3, "%s", *rmaps);
  92. if ((str = strchr(buf3, '@'))) {
  93. *str = 0;
  94. sprintf(buf2, "%s", str + 1);
  95. }
  96. else {
  97. sprintf(buf2, "%s", mapset);
  98. }
  99. sprintf(buf1, "%s/%s/cellhd/%s", location_path, buf2,
  100. buf3);
  101. fp = fopen(buf1, "r");
  102. if (fp == NULL)
  103. continue;
  104. fgets(buf2, 255, fp);
  105. fgets(buf2, 255, fp);
  106. fgets(buf2, 255, fp);
  107. ptr = ftell(fp);
  108. fseek(fp, 0L, SEEK_END);
  109. l = ftell(fp) - ptr;
  110. str = (char *)G_malloc(l);
  111. fseek(fp, ptr, SEEK_SET);
  112. fread(str, l, 1, fp);
  113. fclose(fp);
  114. fp = fopen(buf1, "w");
  115. fprintf(fp, "reclass\n");
  116. fprintf(fp, "name: %s\n", new);
  117. fprintf(fp, "mapset: %s\n", mapset);
  118. fwrite(str, l, 1, fp);
  119. G_free(str);
  120. fclose(fp);
  121. }
  122. }
  123. if (do_rename(n, old, new) == 1) {
  124. result = EXIT_FAILURE;
  125. }
  126. }
  127. }
  128. exit(result);
  129. }