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