main.c 3.7 KB

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