GRASS GIS 7 Programmer's Manual  7.9.dev(2021)-e5379bbd7
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  (C) 2001-2015 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 <stdlib.h>
16 #include <grass/gis.h>
17 
18 #include "gis_local_proto.h"
19 
20 static char *file_name(char *, const char *, const char *,
21  const char *, const char *, const char *);
22 static void append_char(char*, char);
23 
24 /*!
25  \brief Builds full path names to GIS data files
26 
27  If <i>name</i> is of the form "nnn@ppp" then path is set as if name
28  had been "nnn" and mapset had been "ppp" (mapset parameter itself is
29  ignored in this case).
30 
31  \param[out] path buffer to hold resultant full path to file
32  \param element database element (eg, "cell", "cellhd", "vector", etc)
33  \param name name of file to build path to (fully qualified names allowed)
34  \param mapset mapset name
35 
36  \return pointer to <i>path</i> buffer
37 */
38 char *G_file_name(char *path,
39  const char *element, const char *name, const char *mapset)
40 {
41  return file_name(path, NULL, element, name, mapset, NULL);
42 }
43 
44 /*!
45  \brief Builds full path names to GIS misc data files
46 
47  \param[out] path buffer to hold resultant full path to file
48  \param dir misc directory
49  \param element database element (eg, "cell", "cellhd", "vector", etc)
50  \param name name of file to build path to (fully qualified names allowed)
51  \param mapset mapset name
52 
53  \return pointer to <i>path</i> buffer
54 */
55 char *G_file_name_misc(char *path,
56  const char *dir,
57  const char *element,
58  const char *name, const char *mapset)
59 {
60  return file_name(path, dir, element, name, mapset, NULL);
61 }
62 
63 /*!
64  \brief Builds full path names to GIS data files in temporary directory (for internal use only)
65 
66  By default temporary directory is located
67  $LOCATION/$MAPSET/.tmp/$HOSTNAME. If GRASS_VECTOR_TMPDIR_MAPSET is
68  set to "0", the temporary directory is located in TMPDIR
69  (environmental variable defined by the user or GRASS initialization
70  script if not given). Note that GRASS_VECTOR_TMPDIR_MAPSET variable
71  is currently used only by vector library.
72 
73  \param[out] path buffer to hold resultant full path to file
74  \param element database element (eg, "cell", "cellhd", "vector", etc)
75  \param name name of file to build path to (fully qualified names allowed)
76  \param mapset mapset name
77 
78  \return pointer to <i>path</i> buffer
79 */
80 char *G_file_name_tmp(char *path,
81  const char *element,
82  const char *name, const char *mapset)
83 {
84  const char *env, *tmp_path;
85 
86  tmp_path = NULL;
87  env = getenv("GRASS_VECTOR_TMPDIR_MAPSET");
88  if (env && strcmp(env, "0") == 0) {
89  tmp_path = getenv("TMPDIR");
90  }
91 
92  return file_name(path, NULL, element, name, mapset, tmp_path);
93 }
94 
95 char *file_name(char *path,
96  const char *dir, const char *element, const char *name,
97  const char *mapset, const char *base)
98 {
99  const char *pname = name;
100 
101  if (base && *base) {
102  sprintf(path, "%s", base);
103  }
104  else {
105  char xname[GNAME_MAX];
106  char xmapset[GMAPSET_MAX];
107  char *location = G__location_path();
108 
109  /*
110  * if a name is given, build a file name
111  * must split the name into name, mapset if it is
112  * in the name@mapset format
113  */
114  if (name && *name && G_name_is_fully_qualified(name, xname, xmapset)) {
115  pname = xname;
116  sprintf(path, "%s%c%s", location, HOST_DIRSEP, xmapset);
117  }
118  else if (mapset && *mapset)
119  sprintf(path, "%s%c%s", location, HOST_DIRSEP, mapset);
120  else
121  sprintf(path, "%s%c%s", location, HOST_DIRSEP, G_mapset());
122  G_free(location);
123  }
124 
125  if (dir && *dir) { /* misc element */
126  append_char(path, HOST_DIRSEP);
127  strcat(path, dir);
128 
129  if (pname && *pname) {
130  append_char(path, HOST_DIRSEP);
131  strcat(path, pname);
132  }
133 
134  if (element && *element) {
135  append_char(path, HOST_DIRSEP);
136  strcat(path, element);
137  }
138  }
139  else {
140  if (element && *element) {
141  append_char(path, HOST_DIRSEP);
142  strcat(path, element);
143  }
144 
145  if (pname && *pname) {
146  append_char(path, HOST_DIRSEP);
147  strcat(path, pname);
148  }
149  }
150 
151  G_debug(2, "G_file_name(): path = %s", path);
152 
153  return path;
154 }
155 
156 void append_char(char* s, char c)
157 {
158  int len = strlen(s);
159  s[len] = c;
160  s[len+1] = '\0';
161 }
#define GMAPSET_MAX
Definition: gis.h:168
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:80
void G_free(void *)
Free allocated memory.
Definition: gis/alloc.c:149
char * G__location_path(void)
Get current location UNIX-like path (internal use only)
Definition: location.c:78
#define NULL
Definition: ccmath.h:32
Definition: lidar.h:86
#define HOST_DIRSEP
Definition: gis.h:213
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:38
const char * G_mapset(void)
Get current mapset name.
Definition: gis/mapset.c:33
#define GNAME_MAX
Definition: gis.h:167
Definition: path.h:16
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:55
const char * name
Definition: named_colr.c:7
char * getenv()
int G_name_is_fully_qualified(const char *, char *, char *)
Check if map name is fully qualified (map @ mapset)
Definition: nme_in_mps.c:36
int G_debug(int, const char *,...) __attribute__((format(printf