GRASS 8 Programmer's Manual
8.5.0(2026)-8d6ceba290
Toggle main menu visibility
Loading...
Searching...
No Matches
mapset_nme.c
Go to the documentation of this file.
1
/*!
2
\file lib/gis/mapset_nme.c
3
4
\brief GIS library - Mapset name, search path routines.
5
6
(C) 1999-2014 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
12
#include <grass/config.h>
13
#include <sys/types.h>
14
#include <sys/stat.h>
15
#include <string.h>
16
#include <dirent.h>
17
#include <unistd.h>
18
#include <grass/gis.h>
19
20
#include "gis_local_proto.h"
21
22
static
struct
state {
23
struct
list {
24
char
**names;
25
int
count;
26
int
size;
27
} path, path2;
28
} state;
29
30
static
struct
state *
st
= &state;
31
32
static
void
new_mapset(
const
char
*);
33
34
/*!
35
\brief Get name of the n'th mapset from the current mapset search path.
36
37
The first call will initialize the list.
38
39
\param n mapset index
40
41
\return mapset name
42
\return NULL if mapset not found
43
*/
44
const
char
*
G_get_mapset_name
(
int
n)
45
{
46
G__get_list_of_mapsets
();
47
48
if
(n < 0 || n >=
st
->path.count)
49
return
NULL
;
50
51
return
st
->path.names[n];
52
}
53
54
/*!
55
\brief Fill list of mapsets from search path (internal use only)
56
*/
57
void
G__get_list_of_mapsets
(
void
)
58
{
59
FILE *fp;
60
const
char
*cur;
61
62
if
(
st
->path.count > 0)
63
return
;
64
65
st
->path.count = 0;
66
st
->path.size = 0;
67
st
->path.names =
NULL
;
68
69
cur =
G_mapset
();
70
new_mapset(cur);
71
72
fp =
G_fopen_old
(
""
,
"SEARCH_PATH"
,
G_mapset
());
73
if
(fp) {
74
char
name[GNAME_MAX];
75
76
while
(fscanf(fp,
"%s"
, name) == 1) {
77
if
(strcmp(name, cur) == 0)
78
continue
;
79
if
(
G_mapset_permissions
(name) >= 0)
80
new_mapset(name);
81
}
82
fclose(fp);
83
}
84
else
{
85
static
const
char
perm[] =
"PERMANENT"
;
86
87
if
(strcmp(perm, cur) != 0 &&
G_mapset_permissions
(perm) >= 0)
88
new_mapset(perm);
89
}
90
}
91
92
void
new_mapset(
const
char
*name)
93
{
94
if
(
st
->path.count >=
st
->path.size) {
95
st
->path.size += 10;
96
st
->path.names =
97
G_realloc(
st
->path.names,
st
->path.size *
sizeof
(
char
*));
98
}
99
100
st
->path.names[
st
->path.count++] =
G_store
(name);
101
}
102
103
/*!
104
\brief Define alternative mapset search path
105
*/
106
void
G_create_alt_search_path
(
void
)
107
{
108
st
->path2.count =
st
->path.count;
109
st
->path2.names =
st
->path.names;
110
111
st
->path.count = 0;
112
}
113
114
/*!
115
\brief Switch mapset search path
116
*/
117
void
G_switch_search_path
(
void
)
118
{
119
int
count
;
120
char
**names;
121
122
count
=
st
->path2.count;
123
names =
st
->path2.names;
124
125
st
->path2.count =
st
->path.count;
126
st
->path2.names =
st
->path.names;
127
128
st
->path.count =
count
;
129
st
->path.names = names;
130
}
131
132
/*!
133
\brief Reset number of mapsets
134
*/
135
void
G_reset_mapsets
(
void
)
136
{
137
st
->path.count = 0;
138
}
139
140
/*!
141
\brief Get list of available mapsets for current location
142
143
List is updated by each call to this function.
144
145
\return pointer to NULL terminated array of available mapsets
146
*/
147
char
**
G_get_available_mapsets
(
void
)
148
{
149
char
*location;
150
char
**mapsets =
NULL
;
151
int
alloc = 50;
152
int
n = 0;
153
DIR *dir;
154
struct
dirent *ent;
155
156
G_debug
(3,
"G_get_available_mapsets"
);
157
158
mapsets = G_calloc(alloc,
sizeof
(
char
*));
159
160
location =
G_location_path
();
161
dir = opendir(location);
162
if
(!dir) {
163
G_free
(location);
164
return
mapsets;
165
}
166
167
while
((ent = readdir(dir))) {
168
char
buf[GPATH_MAX];
169
struct
stat
st
;
170
171
snprintf(buf,
sizeof
(buf),
"%s/%s/WIND"
, location, ent->d_name);
172
173
if
(
G_stat
(buf, &
st
) != 0) {
174
G_debug
(4,
"%s is not mapset"
, ent->d_name);
175
continue
;
176
}
177
178
G_debug
(4,
"%s is mapset"
, ent->d_name);
179
180
if
(n + 2 >= alloc) {
181
alloc += 50;
182
mapsets = G_realloc(mapsets, alloc *
sizeof
(
char
*));
183
}
184
185
mapsets[n++] =
G_store
(ent->d_name);
186
}
187
mapsets[n] =
NULL
;
188
189
closedir(dir);
190
G_free
(location);
191
192
return
mapsets;
193
}
194
195
/*!
196
\brief Add mapset to the list of mapsets in search path
197
198
Mapset is add in memory only, not to the SEARCH_PATH file!
199
List is check first if already exists.
200
201
\param mapset mapset name to be added to the search path
202
*/
203
void
G_add_mapset_to_search_path
(
const
char
*mapset)
204
{
205
if
(!
G_is_mapset_in_search_path
(mapset))
206
new_mapset(mapset);
207
}
208
209
/*!
210
\brief Check if given mapset is in search path
211
212
\param mapset mapset name
213
214
\return 1 mapset found in search path
215
\return 0 mapset not found
216
*/
217
int
G_is_mapset_in_search_path
(
const
char
*mapset)
218
{
219
int
i;
220
221
for
(i = 0; i <
st
->path.count; i++) {
222
if
(strcmp(
st
->path.names[i], mapset) == 0)
223
return
1;
224
}
225
226
return
0;
227
}
G_free
void G_free(void *buf)
Free allocated memory.
Definition
alloc.c:147
NULL
#define NULL
Definition
ccmath.h:32
G_debug
int G_debug(int level, const char *msg,...)
Print debugging message.
Definition
debug.c:66
G_fopen_old
FILE * G_fopen_old(const char *element, const char *name, const char *mapset)
Open a database file for reading.
Definition
gis/open.c:253
count
int count
G_location_path
char * G_location_path(void)
Get current location UNIX-like path.
Definition
location.c:54
G_mapset
const char * G_mapset(void)
Get current mapset name.
Definition
mapset.c:33
G_mapset_permissions
int G_mapset_permissions(const char *mapset)
Check for user mapset permission.
Definition
mapset_msc.c:292
G_add_mapset_to_search_path
void G_add_mapset_to_search_path(const char *mapset)
Add mapset to the list of mapsets in search path.
Definition
mapset_nme.c:203
G_reset_mapsets
void G_reset_mapsets(void)
Reset number of mapsets.
Definition
mapset_nme.c:135
G_get_mapset_name
const char * G_get_mapset_name(int n)
Get name of the n'th mapset from the current mapset search path.
Definition
mapset_nme.c:44
G__get_list_of_mapsets
void G__get_list_of_mapsets(void)
Fill list of mapsets from search path (internal use only).
Definition
mapset_nme.c:57
G_create_alt_search_path
void G_create_alt_search_path(void)
Define alternative mapset search path.
Definition
mapset_nme.c:106
G_switch_search_path
void G_switch_search_path(void)
Switch mapset search path.
Definition
mapset_nme.c:117
G_is_mapset_in_search_path
int G_is_mapset_in_search_path(const char *mapset)
Check if given mapset is in search path.
Definition
mapset_nme.c:217
G_get_available_mapsets
char ** G_get_available_mapsets(void)
Get list of available mapsets for current location.
Definition
mapset_nme.c:147
st
struct state * st
Definition
parser.c:104
G_stat
int G_stat(const char *file_name, struct stat *buf)
Get file status.
Definition
paths.c:128
G_store
char * G_store(const char *s)
Copy string to allocated memory.
Definition
strings.c:87
gis
mapset_nme.c
Generated on
for GRASS 8 Programmer's Manual by
1.18.0