2
/****************************************************************************
5
* AUTHOR(S): CERL (original contributor)
6
* Radim Blazek <radim.blazek gmail.com>,
7
* Cedric Shock <cedricgrass shockfamily.net>,
8
* Glynn Clements <glynn gclements.plus.com>,
9
* Markus Neteler <neteler itc.it>,
10
* Martin Landa <landa.martin gmail.com>
12
* COPYRIGHT: (C) 1994-2007 by the GRASS Development Team
14
* This program is free software under the GNU General Public
15
* License (>=v2). Read the file COPYING that comes with GRASS
18
*****************************************************************************/
22
#include <grass/gis.h>
23
#include <grass/glocale.h>
25
#include "local_proto.h"
27
int main(int argc, char *argv[])
30
struct GModule *module;
31
struct Option **parm, *p;
34
char *mapset, *location_path, **rmaps;
35
int result = EXIT_SUCCESS;
39
module = G_define_module();
40
module->keywords = _("general, map management");
42
_("Renames data base element files in the user's current mapset.");
44
parm = (struct Option **)G_calloc(nlist, sizeof(struct Option *));
46
for (n = 0; n < nlist; n++) {
49
p = parm[n] = G_define_option();
50
p->key = list[n].alias;
51
p->key_desc = "old,new";
52
p->type = TYPE_STRING;
55
G_asprintf(&str, "old,%s,%s", list[n].mainelem, list[n].maindesc);
57
G_asprintf(&str, _("%s file(s) to be renamed"), list[n].alias);
61
if (G_parser(argc, argv))
64
location_path = G__location_path();
67
for (n = 0; n < nlist; n++) {
68
if (parm[n]->answers == NULL)
71
while (parm[n]->answers[i]) {
72
old = parm[n]->answers[i++];
73
new = parm[n]->answers[i++];
74
if (!find(n, old, mapset)) {
75
G_warning(_("%s <%s> not found"), list[n].maindesc, old);
78
if (find(n, new, "") && !(module->overwrite)) {
79
G_warning(_("<%s> already exists in mapset <%s>"), new,
83
if (G_legal_filename(new) < 0) {
84
G_warning(_("<%s> is an illegal file name"), new);
87
if (G_strcasecmp(old, new) == 0) {
88
/* avoid problems on case-insensitive file systems (FAT, NTFS, ...) */
89
G_warning(_("%s=%s,%s: files could be the same, no rename possible"),
90
parm[n]->key, old, new);
94
if (G_is_reclassed_to(old, mapset, &nrmaps, &rmaps) > 0) {
96
char buf1[256], buf2[256], buf3[256], *str;
99
G_message(_("Renaming reclass maps"));
101
for (; *rmaps; rmaps++) {
102
G_message(" %s", *rmaps);
103
sprintf(buf3, "%s", *rmaps);
104
if ((str = strchr(buf3, '@'))) {
106
sprintf(buf2, "%s", str + 1);
109
sprintf(buf2, "%s", mapset);
111
sprintf(buf1, "%s/%s/cellhd/%s", location_path, buf2,
114
fp = fopen(buf1, "r");
118
fgets(buf2, 255, fp);
119
fgets(buf2, 255, fp);
120
fgets(buf2, 255, fp);
123
fseek(fp, 0L, SEEK_END);
126
str = (char *)G_malloc(l);
127
fseek(fp, ptr, SEEK_SET);
128
fread(str, l, 1, fp);
131
fp = fopen(buf1, "w");
132
fprintf(fp, "reclass\n");
133
fprintf(fp, "name: %s\n", new);
134
fprintf(fp, "mapset: %s\n", mapset);
135
fwrite(str, l, 1, fp);
140
if (do_rename(n, old, new) == 1) {
141
result = EXIT_FAILURE;