GRASS 8 Programmer's Manual 8.6.0dev(2026)-55de52a352
Loading...
Searching...
No Matches
file_name.c
Go to the documentation of this file.
1/*!
2 \file lib/gis/file_name.c
3
4 \brief GIS library - Determine GRASS data base file name
5
6 SPDX-FileCopyrightText: 2001-2015 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 <stdlib.h>
14#include <grass/gis.h>
15
16#include "gis_local_proto.h"
17
18static char *file_name(char *, const char *, const char *, const char *,
19 const char *, const char *);
20static void append_char(char *, char);
21
22/*!
23 \brief Builds full path names to GIS data files
24
25 If <i>name</i> is of the form "nnn@ppp" then path is set as if name
26 had been "nnn" and mapset had been "ppp" (mapset parameter itself is
27 ignored in this case).
28
29 Paths to files are currently in form:
30 /path/to/location/mapset/element/name
31
32 path input buffer memory must be allocated by caller.
33
34 C:
35 @code{.c}
36 char path[GPATH_MAX];
37 G_file_name(path, "fcell", "my_raster", "my_mapset");
38 // path now is "/full/path/to/my_mapset/fcell/my_raster"
39 @endcode
40 Python:
41 @code{.py}
42 import ctypes
43 from grass.pygrass.utils import decode
44 from grass.lib.gis import G_file_name, GPATH_MAX
45
46 path = ctypes.create_string_buffer(GPATH_MAX)
47 path_str = decode(G_file_name(path, "elem", "name", "mapset"))
48 print(path_str)
49 >>> /full/path/to/mapset/elem/name
50 @endcode
51
52 \param[out] path allocated buffer to hold resultant full path to file
53 \param element database element (eg, "cell", "cellhd", "vector", etc)
54 \param name name of file to build path to (fully qualified names allowed)
55 \param mapset mapset name
56
57 \return pointer to <i>path</i> buffer
58 */
59char *G_file_name(char *path, const char *element, const char *name,
60 const char *mapset)
61{
62 return file_name(path, NULL, element, name, mapset, NULL);
63}
64
65/*!
66 \brief Builds full path names to GIS misc data files
67
68 Paths to misc files are currently in form:
69 /path/to/location/mapset/dir/name/element
70
71 path input buffer memory must be allocated by caller.
72
73 C:
74 @code{.c}
75 char path[GPATH_MAX];
76 G_file_name_misc(path, "cell_misc", "history", "my_raster", "my_mapset");
77 // path now contains "/full/path/to/my_mapset/cell_misc/my_raster/history"
78 @endcode
79 Python:
80 @code{.py}
81 import ctypes
82 from grass.pygrass.utils import decode
83 from grass.lib.gis import G_file_name_misc, GPATH_MAX
84
85 path = ctypes.create_string_buffer(GPATH_MAX)
86 path_str = decode(G_file_name_misc(path, "dir", "elem", "name", "mapset"))
87 print(path_str)
88 >>> /full/path/to/mapset/dir/name/elem
89 @endcode
90
91 \param[out] path allocated buffer to hold resultant full path to file
92 \param dir misc directory (e.g., "cell_misc", "group")
93 \param element database element (in this case – file to build path to e.g.,
94 "history", "REF") \param name name of object (raster, group; fully qualified
95 names allowed e.g., "my_raster@PERMANENT") \param mapset mapset name
96
97 \return pointer to <i>path</i> buffer
98 */
99char *G_file_name_misc(char *path, const char *dir, const char *element,
100 const char *name, const char *mapset)
101{
102 return file_name(path, dir, element, name, mapset, NULL);
103}
104
105/*!
106 \brief Builds full path names to GIS data files in temporary directory (for
107 internal use only)
108
109 By default temporary directory is located
110 $LOCATION/$MAPSET/.tmp/$HOSTNAME. If GRASS_VECTOR_TMPDIR_MAPSET is
111 set to "0", the temporary directory is located in TMPDIR
112 (environmental variable defined by the user or GRASS initialization
113 script if not given). Note that GRASS_VECTOR_TMPDIR_MAPSET variable
114 is currently used only by vector library.
115
116 \param[out] path buffer to hold resultant full path to file
117 \param element database element (eg, "cell", "cellhd", "vector", etc)
118 \param name name of file to build path to (fully qualified names allowed)
119 \param mapset mapset name
120
121 \return pointer to <i>path</i> buffer
122 */
123char *G_file_name_tmp(char *path, const char *element, const char *name,
124 const char *mapset)
125{
126 const char *env;
127 char tmp_path[GPATH_MAX] = {0};
128
129 env = getenv("GRASS_VECTOR_TMPDIR_MAPSET");
130 if (env && strcmp(env, "0") == 0) {
131 snprintf(tmp_path, GPATH_MAX, "%s", getenv("TMPDIR"));
132 }
133
134 return file_name(path, NULL, element, name, mapset, tmp_path);
135}
136
137/*!
138 \brief Builds full path names to GIS data files in temporary directory (for
139 internal use only)
140
141 By default the GRASS temporary directory is located at
142 $LOCATION/$MAPSET/.tmp/$HOSTNAME/. If basedir is provided, the
143 temporary directory is located at <basedir>/.tmp/$HOSTNAME/.
144
145 \param[out] path buffer to hold resultant full path to file
146 \param element database element (eg, "cell", "cellhd", "vector", etc)
147 \param name name of file to build path to (fully qualified names allowed)
148 \param mapset mapset name
149 \param basedir
150
151 \return pointer to <i>path</i> buffer
152 */
153char *G_file_name_basedir(char *path, const char *element, const char *name,
154 const char *mapset, const char *basedir)
155{
156 return file_name(path, NULL, element, name, mapset, basedir);
157}
158
159char *file_name(char *path, const char *dir, const char *element,
160 const char *name, const char *mapset, const char *base)
161{
162 const char *pname = name;
163 char xname[GNAME_MAX] = {'\0'};
164
165 if (base && *base) {
166 sprintf(path, "%s", base);
167 }
168 else {
169 char xmapset[GMAPSET_MAX] = {'\0'};
170 char *location = G__location_path();
171
172 /*
173 * if a name is given, build a file name
174 * must split the name into name, mapset if it is
175 * in the name@mapset format
176 */
178 pname = xname;
179 sprintf(path, "%s%c%s", location, HOST_DIRSEP, xmapset);
180 }
181 else if (mapset && *mapset)
182 sprintf(path, "%s%c%s", location, HOST_DIRSEP, mapset);
183 else
184 sprintf(path, "%s%c%s", location, HOST_DIRSEP, G_mapset());
185 G_free(location);
186 }
187
188 if (dir && *dir) { /* misc element */
189 append_char(path, HOST_DIRSEP);
190 strcat(path, dir);
191
192 if (pname && *pname) {
193 append_char(path, HOST_DIRSEP);
194 strcat(path, pname);
195 }
196
197 if (element && *element) {
198 append_char(path, HOST_DIRSEP);
200 }
201 }
202 else {
203 if (element && *element) {
204 append_char(path, HOST_DIRSEP);
206 }
207
208 if (pname && *pname) {
209 append_char(path, HOST_DIRSEP);
210 strcat(path, pname);
211 }
212 }
213
214 G_debug(2, "G_file_name(): path = %s", path);
215
216 return path;
217}
218
219void append_char(char *s, char c)
220{
221 int len = strlen(s);
222
223 s[len] = c;
224 s[len + 1] = '\0';
225}
#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
void G_free(void *)
Free allocated memory.
Definition gis/alloc.c:145
int G_debug(int, const char *,...) __attribute__((format(printf
const char * G_mapset(void)
Get current mapset name.
Definition gis/mapset.c:31
char * G_file_name(char *path, const char *element, const char *name, const char *mapset)
Builds full path names to GIS data files.
Definition file_name.c:59
char * G_file_name_basedir(char *path, const char *element, const char *name, const char *mapset, const char *basedir)
Builds full path names to GIS data files in temporary directory (for internal use only)
Definition file_name.c:153
char * G_file_name_misc(char *path, const char *dir, const char *element, const char *name, const char *mapset)
Builds full path names to GIS misc data files.
Definition file_name.c:99
char * G_file_name_tmp(char *path, const char *element, const char *name, const char *mapset)
Builds full path names to GIS data files in temporary directory (for internal use only)
Definition file_name.c:123
#define GMAPSET_MAX
Definition gis.h:194
#define GPATH_MAX
Definition gis.h:196
#define GNAME_MAX
Definition gis.h:193
#define HOST_DIRSEP
Definition gis.h:237
char * G__location_path(void)
Get current location UNIX-like path (internal use only)
Definition location.c:75
const char * name
Definition named_colr.c:6
Definition path.h:15