GRASS 8 Programmer's Manual 8.6.0dev(2026)-ddeab64dbf
Loading...
Searching...
No Matches
do_copy.c
Go to the documentation of this file.
1/*!
2 \file lib/manage/do_copy.c
3
4 \brief Manage Library - Copy element
5
6 (C) 2001-2011 by the GRASS Development Team
7
8 This program is free software under the GNU General Public License
9 (>=v2). Read the file COPYING that comes with GRASS for details.
10
11 \author Original author CERL
12 */
13
14#include <stdio.h>
15#include <string.h>
16#include <unistd.h>
17
18#include <grass/gis.h>
19#include <grass/glocale.h>
20#include <grass/vector.h>
21
22#include "manage_local_proto.h"
23
24/*!
25 \brief Copy element
26
27 \param n element id
28 \param old source name
29 \param mapset name of source mapset
30 \param new destination name
31
32 \return 0 on success
33 \return 1 on error
34 */
35int M_do_copy(int n, const char *old, const char *mapset, const char *new)
36{
37 int i, ret;
38 char path[GPATH_MAX], path2[GPATH_MAX];
39 int result = 0;
40 char *mname = G_fully_qualified_name(old, mapset);
41
42 G_debug(3, "Copy %s", list[n].alias);
43
44 G_message(_("Copying %s <%s> to current mapset as <%s>"), list[n].maindesc,
45 mname, new);
46
48 if (G_strcasecmp(list[n].alias, "vector") == 0) {
49 ret = Vect_copy(old, mapset, new);
50 if (ret == -1) {
51 G_warning(_("Unable to copy <%s> to current mapset as <%s>"), mname,
52 new);
53 result = 1;
54 }
55 }
56 else {
57 for (i = 0; i < list[n].nelem; i++) {
59 G_file_name(path, list[n].element[i], old, mapset);
60 if (access(path, 0) != 0) {
61 G_remove(list[n].element[i], new);
62 G_verbose_message(_("%s is missing"), list[n].desc[i]);
63
64 continue;
65 }
66 G_file_name(path2, list[n].element[i], new, G_mapset());
67 if (G_recursive_copy(path, path2) == 1) {
68 G_warning(_("Unable to copy <%s> to current mapset as <%s>"),
69 mname, new);
70 result = 1;
71 }
72 else {
73 G_verbose_message(_("%s copied"), list[n].desc[i]);
74 }
75 }
76 }
77
78 /* special case: remove (yes, remove) the secondary color table, if it
79 * exists */
80 if (G_strcasecmp(list[n].element[0], "cell") == 0) {
81 char colr2[6 + GMAPSET_MAX];
82
83 if (snprintf(colr2, 6 + GMAPSET_MAX, "colr2/%s", G_mapset()) >=
84 6 + GMAPSET_MAX)
85 G_warning(_("String for secondary color table has been truncated"));
86 G_remove(colr2, new);
87 }
90
91 return result;
92}
int G_recursive_copy(const char *, const char *)
Copy recursively source directory to destination directory.
Definition copy_dir.c:69
void G_free(void *)
Free allocated memory.
Definition gis/alloc.c:147
void G_warning(const char *,...) __attribute__((format(printf
int G_make_mapset_object_group(const char *)
Create directory for group of elements of a given type.
Definition mapset_msc.c:75
void void G_verbose_message(const char *,...) __attribute__((format(printf
char * G_file_name(char *, const char *, const char *, const char *)
Builds full path names to GIS data files.
Definition file_name.c:61
char * G_fully_qualified_name(const char *, const char *)
Get fully qualified element name.
Definition nme_in_mps.c:101
int G_remove(const char *, const char *)
Remove a database file.
Definition remove.c:43
int int G_strcasecmp(const char *, const char *)
String compare ignoring case (upper or lower)
Definition strings.c:47
void G_message(const char *,...) __attribute__((format(printf
int G_debug(int, const char *,...) __attribute__((format(printf
const char * G_mapset(void)
Get current mapset name.
Definition gis/mapset.c:33
int M__hold_signals(int)
Hold signals.
Definition sighold.c:24
int Vect_copy(const char *, const char *, const char *)
Copy vector map including attribute tables.
Definition map.c:125
int M_do_copy(int n, const char *old, const char *mapset, const char *new)
Copy element.
Definition do_copy.c:35
#define GMAPSET_MAX
Definition gis.h:197
#define GPATH_MAX
Definition gis.h:199
#define _(str)
Definition glocale.h:10
Definition manage.h:4
int nelem
Definition manage.h:9
Definition path.h:15
#define access
Definition unistd.h:7