GRASS 8 Programmer's Manual 8.6.0dev(2026)-1878fdfec5
Loading...
Searching...
No Matches
find_file.c
Go to the documentation of this file.
1/*!
2 \file lib/gis/find_file.c
3
4 \brief GIS library - Find GRASS data base files
5
6 SPDX-FileCopyrightText: 2001-2009 GRASS Development Team
7 SPDX-License-Identifier: GPL-2.0-or-later
8
9 \author Original author CERL
10 */
11
12#include <string.h>
13#include <unistd.h>
14#include <grass/gis.h>
15#include <grass/glocale.h>
16
17static const char *find_element(int misc, const char *dir, const char *element)
18{
19 static const char *cell_elements[] = {"cellhd", "cell", "cats",
20 "colr", "hist", "cell_misc",
21 "fcell", "g3dcell", NULL};
22 static const char *dig_elements[] = {
23 "dig", "dig_att", "dig_plus", "dig_cats", "dig_misc", "reg", NULL};
24 const char *search = misc ? dir : element;
25 int i;
26
27 for (i = 1; cell_elements[i]; i++)
28 if (strcmp(search, cell_elements[i]) == 0)
29 return cell_elements[0];
30 for (i = 1; dig_elements[i]; i++)
31 if (strcmp(search, dig_elements[i]) == 0)
32 return dig_elements[0];
33 return element;
34}
35
36static const char *find_file(int misc, const char *dir, const char *element,
37 const char *name, const char *mapset)
38{
39 char path[GPATH_MAX];
41 const char *pname, *pmapset;
42 int n;
43
44 if (*name == 0)
45 return NULL;
46 *path = 0;
47
48 /*
49 * if name is in the fully qualified format, split it into
50 * name, mapset (overrides what was in mapset)
51 */
53 pname = xname;
55 }
56 else {
57 pname = name;
58 pmapset = mapset;
59 }
60
61 if (strcmp(element, "vector") == 0 && pmapset &&
62 strcasecmp(pmapset, "ogr") == 0) {
63 /* don't check for virtual OGR mapset */
64 return G_store(pmapset);
65 }
66
67 /*
68 * reject illegal names and mapsets
69 */
70 if (G_legal_filename(pname) == -1)
71 return NULL;
72
73 if (pmapset && *pmapset && G_legal_filename(pmapset) == -1)
74 return NULL;
75
76 /*
77 * if no specific mapset is to be searched
78 * then search all mapsets in the mapset search list
79 */
80 if (pmapset == NULL || *pmapset == 0) {
81 int cnt = 0;
82 const char *pselmapset = NULL;
83 const char *pelement = find_element(misc, dir, element);
84
85 for (n = 0; (pmapset = G_get_mapset_name(n)); n++) {
86 if (misc && element == pelement)
88 else
90 if (access(path, 0) == 0) {
91 if (!pselmapset)
93 else if (element == pelement)
94 G_important_message(_("Data element '%s/%s' was found in "
95 "more mapsets (also found in <%s>)"),
97 cnt++;
98 }
99 }
100 if (cnt > 0) {
101 if (misc)
103 else
105 if (access(path, 0) == 0) {
106 /* If the same name exists in more mapsets and print a warning
107 */
108 if (cnt > 1 && element == pelement)
109 G_important_message(_("Using <%s@%s>..."), pname,
110 pselmapset);
111
112 return G_store(pselmapset);
113 }
114 }
115 }
116 /*
117 * otherwise just look for the file in the specified mapset.
118 * since the name may have been qualified, mapset may point
119 * to the xmapset, so we must should it to
120 * permanent storage via G_store().
121 */
122 else {
123 if (misc)
125 else
127
128 if (access(path, 0) == 0)
129 return G_store(pmapset);
130 }
131
132 return NULL;
133}
134
135static const char *find_file1(int misc, const char *dir, const char *element,
136 char *name, const char *mapset)
137{
139 const char *pname, *pmapset;
140 const char *mp;
141
143 pname = xname;
145 }
146 else {
147 pname = name;
148 pmapset = mapset;
149 }
150
151 mp = find_file(misc, dir, element, pname, pmapset);
152
153 if (mp && name != pname)
154 strcpy(name, pname);
155
156 return mp;
157}
158
159/*!
160 * \brief Searches for a file from the mapset search list or in a
161 * specified mapset.
162 *
163 * Returns the mapset name where the file was found.
164 *
165 * If the user specifies a fully qualified element (name\@mapset)
166 * which exists, then G_find_file() modifies "name"
167 * by removing the "@mapset" part.
168 *
169 * Rejects all names that begin with "."
170 *
171 * If <i>name</i> is of the form nnn in ppp then only mapset ppp
172 * is searched.
173 *
174 * \param element database element (eg, "cell", "cellhd", "colr", etc)
175 * \param name file name to look for
176 * \param mapset mapset to search. if mapset is "" will search in mapset search
177 * list
178 *
179 * \return pointer to a string with name of mapset where file was
180 * found, or NULL if not found
181 */
182const char *G_find_file(const char *element, char *name, const char *mapset)
183{
184 return find_file1(0, NULL, element, name, mapset);
185}
186
187/*!
188 * \brief Searches for a misc file from the mapset search list or in a
189 * specified mapset.
190 *
191 * Returns the mapset name where the misc file was found.
192 * Paths to misc files currently follow structure:
193 * mapset/dir/name/element
194 *
195 * \param dir file directory
196 * \param element database element (eg, "cell", "cellhd", "colr", etc)
197 * \param name file name to look for
198 * \param mapset mapset to search. if mapset is "" will search in mapset search
199 * list
200 *
201 * \return pointer to a string with name of mapset where file was
202 * found, or NULL if not found
203 */
204const char *G_find_file_misc(const char *dir, const char *element, char *name,
205 const char *mapset)
206{
207 return find_file1(1, dir, element, name, mapset);
208}
209
210/*!
211 * \brief Searches for a file from the mapset search list or in a
212 * specified mapset. (look but don't touch)
213 *
214 * Returns the mapset name where the file was found.
215 *
216 * Exactly the same as G_find_file() except that if <i>name</i> is in
217 * the form "<i>name@mapset</i>", and is found, G_find_file2() will
218 * not alter <i>name</i> by removing the "@<i>mapset</i>" part.
219 *
220 * Rejects all names that begin with "."
221 *
222 * \param element database element (eg, "cell", "cellhd", "colr", etc)
223 * \param name file name to look for
224 * \param mapset mapset to search. if mapset is "" will search in mapset
225 * search list
226 *
227 * \return pointer to a string with name of mapset where file was
228 * found, or NULL if not found
229 */
230const char *G_find_file2(const char *element, const char *name,
231 const char *mapset)
232{
233 return find_file(0, NULL, element, name, mapset);
234}
235
236/*!
237 * \brief Searches for a misc file from the mapset search list or in a
238 * specified mapset. (look but don't touch)
239 *
240 * Returns the mapset name where the misc file was found.
241 * Paths to misc files currently follow structure:
242 * mapset/dir/name/element
243 *
244 * \param dir file directory
245 * \param element database element (eg, "cell", "cellhd", "colr", etc)
246 * \param name file name to look for
247 * \param mapset mapset to search. if mapset is "" will search in mapset
248 * search list
249 *
250 * \return pointer to a string with name of mapset where file was
251 * found, or NULL if not found
252 */
253const char *G_find_file2_misc(const char *dir, const char *element,
254 const char *name, const char *mapset)
255{
256 return find_file(1, dir, element, name, mapset);
257}
#define NULL
Definition ccmath.h:32
int G_name_is_fully_qualified(const char *, char *, char *)
Check if map name is fully qualified (map @ mapset)
Definition nme_in_mps.c:34
int G_legal_filename(const char *)
Check for legal database file name.
Definition legal_name.c:32
char * G_file_name_misc(char *, const char *, const char *, const char *, const char *)
Builds full path names to GIS misc data files.
Definition file_name.c:99
char * G_file_name(char *, const char *, const char *, const char *)
Builds full path names to GIS data files.
Definition file_name.c:59
void void void G_important_message(const char *,...) __attribute__((format(printf
const char * G_get_mapset_name(int)
Get name of the n'th mapset from the current mapset search path.
Definition mapset_nme.c:42
char * G_store(const char *)
Copy string to allocated memory.
Definition strings.c:85
const char * G_find_file2(const char *element, const char *name, const char *mapset)
Searches for a file from the mapset search list or in a specified mapset. (look but don't touch)
Definition find_file.c:230
const char * G_find_file_misc(const char *dir, const char *element, char *name, const char *mapset)
Searches for a misc file from the mapset search list or in a specified mapset.
Definition find_file.c:204
const char * G_find_file2_misc(const char *dir, const char *element, const char *name, const char *mapset)
Searches for a misc file from the mapset search list or in a specified mapset. (look but don't touch)
Definition find_file.c:253
const char * G_find_file(const char *element, char *name, const char *mapset)
Searches for a file from the mapset search list or in a specified mapset.
Definition find_file.c:182
#define GMAPSET_MAX
Definition gis.h:194
#define GPATH_MAX
Definition gis.h:196
#define GNAME_MAX
Definition gis.h:193
#define _(str)
Definition glocale.h:10
const char * name
Definition named_colr.c:6
#define strcpy
Definition parson.c:66
#define strcasecmp
Definition strings.h:8
Definition path.h:15
#define access
Definition unistd.h:7