~ubuntu-branches/ubuntu/wily/grass/wily

« back to all changes in this revision

Viewing changes to general/manage/cmd/rename.c

Tags: 7.0.0~rc1+ds1-1~exp1
* New upstream release candidate.
* Repack upstream tarball, remove precompiled Python objects.
* Add upstream metadata.
* Update gbp.conf and Vcs-Git URL to use the experimental branch.
* Update watch file for GRASS 7.0.
* Drop build dependencies for Tcl/Tk, add build dependencies:
  python-numpy, libnetcdf-dev, netcdf-bin, libblas-dev, liblapack-dev
* Update Vcs-Browser URL to use cgit instead of gitweb.
* Update paths to use grass70.
* Add configure options: --with-netcdf, --with-blas, --with-lapack,
  remove --with-tcltk-includes.
* Update patches for GRASS 7.
* Update copyright file, changes:
  - Update copyright years
  - Group files by license
  - Remove unused license sections
* Add patches for various typos.
* Fix desktop file with patch instead of d/rules.
* Use minimal dh rules.
* Bump Standards-Version to 3.9.6, no changes.
* Use dpkg-maintscript-helper to replace directories with symlinks.
  (closes: #776349)
* Update my email to use @debian.org address.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
 
2
 
/****************************************************************************
3
 
 *
4
 
 * MODULE:       g.rename cmd
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>
11
 
 * PURPOSE:      
12
 
 * COPYRIGHT:    (C) 1994-2007 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
 
#define MAIN
20
 
#include <stdlib.h>
21
 
#include <string.h>
22
 
#include <grass/gis.h>
23
 
#include <grass/glocale.h>
24
 
#include "list.h"
25
 
#include "local_proto.h"
26
 
 
27
 
int main(int argc, char *argv[])
28
 
{
29
 
    int i, n;
30
 
    struct GModule *module;
31
 
    struct Option **parm, *p;
32
 
    char *old, *new;
33
 
    int nrmaps;
34
 
    char *mapset, *location_path, **rmaps;
35
 
    int result = EXIT_SUCCESS;
36
 
 
37
 
    init(argv[0]);
38
 
 
39
 
    module = G_define_module();
40
 
    module->keywords = _("general, map management");
41
 
    module->description =
42
 
        _("Renames data base element files in the user's current mapset.");
43
 
 
44
 
    parm = (struct Option **)G_calloc(nlist, sizeof(struct Option *));
45
 
 
46
 
    for (n = 0; n < nlist; n++) {
47
 
        char *str;
48
 
 
49
 
        p = parm[n] = G_define_option();
50
 
        p->key = list[n].alias;
51
 
        p->key_desc = "old,new";
52
 
        p->type = TYPE_STRING;
53
 
        p->required = NO;
54
 
        p->multiple = NO;
55
 
        G_asprintf(&str, "old,%s,%s", list[n].mainelem, list[n].maindesc);
56
 
        p->gisprompt = str;
57
 
        G_asprintf(&str, _("%s file(s) to be renamed"), list[n].alias);
58
 
        p->description = str;
59
 
    }
60
 
 
61
 
    if (G_parser(argc, argv))
62
 
        exit(EXIT_FAILURE);
63
 
 
64
 
    location_path = G__location_path();
65
 
    mapset = G_mapset();
66
 
 
67
 
    for (n = 0; n < nlist; n++) {
68
 
        if (parm[n]->answers == NULL)
69
 
            continue;
70
 
        i = 0;
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);
76
 
                continue;
77
 
            }
78
 
            if (find(n, new, "") && !(module->overwrite)) {
79
 
                G_warning(_("<%s> already exists in mapset <%s>"), new,
80
 
                          find(n, new, ""));
81
 
                continue;
82
 
            }
83
 
            if (G_legal_filename(new) < 0) {
84
 
                G_warning(_("<%s> is an illegal file name"), new);
85
 
                continue;
86
 
            }
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);
91
 
                continue;
92
 
            }
93
 
 
94
 
            if (G_is_reclassed_to(old, mapset, &nrmaps, &rmaps) > 0) {
95
 
                int ptr, l;
96
 
                char buf1[256], buf2[256], buf3[256], *str;
97
 
                FILE *fp;
98
 
 
99
 
                G_message(_("Renaming reclass maps"));
100
 
 
101
 
                for (; *rmaps; rmaps++) {
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
 
                    sprintf(buf1, "%s/%s/cellhd/%s", location_path, buf2,
112
 
                            buf3);
113
 
 
114
 
                    fp = fopen(buf1, "r");
115
 
                    if (fp == NULL)
116
 
                        continue;
117
 
 
118
 
                    fgets(buf2, 255, fp);
119
 
                    fgets(buf2, 255, fp);
120
 
                    fgets(buf2, 255, fp);
121
 
 
122
 
                    ptr = ftell(fp);
123
 
                    fseek(fp, 0L, SEEK_END);
124
 
                    l = ftell(fp) - ptr;
125
 
 
126
 
                    str = (char *)G_malloc(l);
127
 
                    fseek(fp, ptr, SEEK_SET);
128
 
                    fread(str, l, 1, fp);
129
 
                    fclose(fp);
130
 
 
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);
136
 
                    G_free(str);
137
 
                    fclose(fp);
138
 
                }
139
 
            }
140
 
            if (do_rename(n, old, new) == 1) {
141
 
                result = EXIT_FAILURE;
142
 
            }
143
 
        }
144
 
    }
145
 
    exit(result);
146
 
}