GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-d6dec75dd4
nme_in_mps.c
Go to the documentation of this file.
1 /*!
2  \file lib/gis/nme_in_mps.c
3 
4  \brief GIS Library - check map name
5 
6  (C) 2001-2009, 2013 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 <string.h>
15 #include <grass/gis.h>
16 
17 /*!
18  \brief Check if map name is fully qualified (map @ mapset)
19 
20  Returns a fully qualified name for the file <i>name</i> in
21  <i>mapset</i>. Currently this string is in the form
22  <i>name@mapset</i>, but the programmer should pretend not to know this
23  and always call this routine to get the fully qualified name.
24 
25  Note:
26  - <i>name</i> is char array of size GNAME_MAX
27  - <i>mapset</i> is char array of size GMAPSET_MAX
28 
29  \param fullname full map name
30  \param[out] name map name
31  \param[out] mapset mapset name
32 
33  \return 1 if input map name is fully qualified
34  \return 0 if input map name is not fully qualified
35  */
36 int G_name_is_fully_qualified(const char *fullname, char *name, char *mapset)
37 {
38  const char *p;
39  char *q;
40 
41  /* search for name@mapset */
42 
43  *name = *mapset = 0;
44 
45  for (p = fullname; *p; p++)
46  if (*p == '@')
47  break;
48 
49  if (*p == 0)
50  return 0;
51 
52  /* copy the name part */
53  q = name;
54  while (fullname != p)
55  *q++ = *fullname++;
56  *q = 0;
57 
58  /* copy the mapset part */
59  p++; /* skip the @ */
60  q = mapset;
61  while ((*q++ = *p++))
62  ;
63 
64  return (*name && *mapset);
65 }
66 
67 /*!
68  \brief Get fully qualified element name
69 
70  Returns a fully qualified name for GIS element <i>name</i> in
71  <i>mapset</i>. Currently this string is in the form
72  <b>name@mapset</b>, but the programmer should pretend not to know
73  this and always call this routine to get the fully qualified name.
74 
75  String is allocated by G_store().
76 
77  \code
78  #include <grass/gis.h>
79  int main(char *argc, char **argv)
80  {
81  char name[GNAME_MAX], *mapset, *fqn;
82  char command[1024];
83 
84  G_gisinit(argv[0]);
85  mapset = G_find_rast(name, "");
86  if (mapset == NULL)
87  exit(EXIT_SUCCESS);
88 
89  fqn = G_fully_qualified_name (name, mapset);
90  printf (stdout, "map='%s'", fqn);
91 
92  exit(EXIT_SUCCESS);
93  }
94  \endcode
95 
96  \param name element name
97  \param mapset mapset name
98 
99  \return pointer to full element name (map@mapset)
100  */
101 char *G_fully_qualified_name(const char *name, const char *mapset)
102 {
103  char fullname[GNAME_MAX + GMAPSET_MAX];
104 
105  if (strchr(name, '@') || strlen(mapset) < 1) {
106  sprintf(fullname, "%s", name);
107  }
108  else {
109  sprintf(fullname, "%s@%s", name, mapset);
110  }
111 
112  return G_store(fullname);
113 }
114 
115 /*!
116  \brief Returns unqualified map name (without @ mapset)
117 
118  Returns an unqualified name for the file <i>name</i> in
119  <i>mapset</i>.
120 
121  Note:
122  - <i>name, xname</i> are char array of size GNAME_MAX
123  - <i>mapset, xmapset</i> are char array of size GMAPSET_MAX
124 
125  \param name map name
126  \param mapset mapset to check or NULL
127  \param[out] xname map name
128  \param[out] xmapset mapset name
129 
130  \return 1 if input map name is fully qualified
131  \return 0 if name is not fully qualified
132  \return -1 if input mapset invalid (mapset != xmapset)
133  */
134 int G_unqualified_name(const char *name, const char *mapset, char *xname,
135  char *xmapset)
136 {
137  if (G_name_is_fully_qualified(name, xname, xmapset)) {
138  /* name is fully qualified */
139  if (mapset && *mapset && strcmp(mapset, xmapset) != 0)
140  return -1;
141  return 1;
142  }
143 
144  /* name is not fully qualified */
145  strcpy(xname, name);
146  if (mapset)
147  strcpy(xmapset, mapset);
148  else
149  xmapset[0] = '\0';
150 
151  return 0;
152 }
char * G_store(const char *)
Copy string to allocated memory.
Definition: strings.c:87
#define GMAPSET_MAX
Definition: gis.h:192
#define GNAME_MAX
Definition: gis.h:191
const char * name
Definition: named_colr.c:6
char * G_fully_qualified_name(const char *name, const char *mapset)
Get fully qualified element name.
Definition: nme_in_mps.c:101
int G_name_is_fully_qualified(const char *fullname, char *name, char *mapset)
Check if map name is fully qualified (map @ mapset)
Definition: nme_in_mps.c:36
int G_unqualified_name(const char *name, const char *mapset, char *xname, char *xmapset)
Returns unqualified map name (without @ mapset)
Definition: nme_in_mps.c:134
#define strcpy
Definition: parson.c:62