GRASS 8 Programmer's Manual 8.6.0dev(2026)-4bb960b182
Loading...
Searching...
No Matches
do_rename.c
Go to the documentation of this file.
1/*!
2 \file lib/manage/do_rename.c
3
4 \brief Manage Library - Rename elements
5
6 SPDX-FileCopyrightText: 2001-2011 GRASS Development Team
7 SPDX-License-Identifier: GPL-2.0-or-later
8
9 \author Original author CERL
10 */
11
12#include <stdio.h>
13#include <stdlib.h>
14#include <string.h>
15
16#include <grass/gis.h>
17#include <grass/vector.h>
18#include <grass/raster3d.h>
19#include <grass/glocale.h>
20
21#include "manage_local_proto.h"
22
23/*!
24 \brief Rename element
25
26 \param n element id
27 \param old source name
28 \param new destination name
29
30 \return 0 on success
31 \return 1 on error
32 */
33int M_do_rename(int n, const char *old, const char *new)
34{
35 int i, ret;
36 const char *mapset;
37 int result = 0;
38 int renamed = 0;
39
40 G_message(_("Rename %s <%s> to <%s>"), list[n].maindesc, old, new);
41
42 if (G_strcasecmp(old, new) == 0)
43 return 1;
44
46
47 if (G_strcasecmp(list[n].alias, "vector") == 0) {
48 if ((mapset = G_find_vector2(old, "")) == NULL) {
49 G_warning(_("Vector map <%s> not found"), old);
50 }
51 else {
52 ret = Vect_rename(old, new);
53 if (ret != -1) {
54 renamed = 1;
55 }
56 else {
57 G_warning(_("Unable to rename vector map <%s> to <%s>"), old,
58 new);
59 result = 1;
60 }
61 }
62 }
63 else {
64 if (G_strcasecmp(list[n].alias, "raster") == 0) {
65 if ((mapset = G_find_raster2(old, "")) == NULL)
66 G_warning(_("Raster map <%s> not found"), old);
67 }
68
69 if (G_strcasecmp(list[n].alias, "raster_3d") == 0) {
70 if ((mapset = G_find_raster3d(old, "")) == NULL)
71 G_warning(_("3D raster map <%s> not found"), old);
72 }
73
74 for (i = 0; i < list[n].nelem; i++) {
75 G_remove(list[n].element[i], new);
76 switch (G_rename(list[n].element[i], old, new)) {
77 case -1:
78 G_warning(_("Unable to rename %s"), list[n].desc[i]);
79 result = 1;
80 break;
81 case 0:
82 G_verbose_message(_("%s is missing"), list[n].desc[i]);
83 break;
84 case 1:
85 G_verbose_message(_("%s renamed"), list[n].desc[i]);
86 renamed = 1;
87 break;
88 }
89 }
90
91 if (G_strcasecmp(list[n].element[0], "cell") == 0) {
92 char colr2[6 + GMAPSET_MAX];
93
94 if (snprintf(colr2, 6 + GMAPSET_MAX, "colr2/%s", G_mapset()) >=
95 6 + GMAPSET_MAX)
97 _("String for secondary color table has been truncated"));
98 G_remove(colr2, new);
99 switch (G_rename(colr2, old, new)) {
100 case -1:
101 G_warning(_("Unable to rename %s"), colr2);
102 result = 1;
103 break;
104 case 0:
105 G_verbose_message(_("%s is missing"), colr2);
106 break;
107 case 1:
108 G_verbose_message(_("%s renamed"), colr2);
109 renamed = 1;
110 break;
111 }
112 }
113 }
115
116 if (!renamed)
117 G_warning(_("<%s> nothing renamed"), old);
118
119 return result;
120}
#define NULL
Definition ccmath.h:32
const char * G_find_raster3d(const char *, const char *)
Search for a 3D raster map in current search path or in a specified mapset.
Definition find_rast3d.c:26
void G_warning(const char *,...) __attribute__((format(printf
void void G_verbose_message(const char *,...) __attribute__((format(printf
int G_remove(const char *, const char *)
Remove a database file.
Definition remove.c:41
int int G_strcasecmp(const char *, const char *)
String compare ignoring case (upper or lower)
Definition strings.c:45
void G_message(const char *,...) __attribute__((format(printf
const char * G_find_raster2(const char *, const char *)
Find a raster map (look but don't touch)
Definition find_rast.c:73
const char * G_find_vector2(const char *, const char *)
Find a vector map (look but don't touch)
Definition find_vect.c:60
const char * G_mapset(void)
Get current mapset name.
Definition gis/mapset.c:31
int G_rename(const char *, const char *, const char *)
Rename a database file.
Definition rename.c:67
int M__hold_signals(int)
Hold signals.
Definition sighold.c:22
int Vect_rename(const char *, const char *)
Rename existing vector map (in the current mapset).
Definition map.c:231
int M_do_rename(int n, const char *old, const char *new)
Rename element.
Definition do_rename.c:33
#define GMAPSET_MAX
Definition gis.h:194
#define _(str)
Definition glocale.h:10
Definition manage.h:4
int nelem
Definition manage.h:9