main.c 3.9 KB

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