GRASS GIS 7 Programmer's Manual  7.9.dev(2020)-547ded6c0
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 {
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 */
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  while (fscanf(fp, "%s", name) == 1) {
76  if (strcmp(name, cur) == 0)
77  continue;
78  if (G_mapset_permissions(name) >= 0)
79  new_mapset(name);
80  }
81  fclose(fp);
82  }
83  else {
84  static const char perm[] = "PERMANENT";
85  if (strcmp(perm, cur) != 0 && G_mapset_permissions(perm) >= 0)
86  new_mapset(perm);
87  }
88 }
89 
90 void new_mapset(const char *name)
91 {
92  if (st->path.count >= st->path.size) {
93  st->path.size += 10;
94  st->path.names = G_realloc(st->path.names, st->path.size * sizeof(char *));
95  }
96 
97  st->path.names[st->path.count++] = G_store(name);
98 }
99 
100 /*!
101  \brief Define alternative mapset search path
102  */
104 {
105  st->path2.count = st->path.count;
106  st->path2.names = st->path.names;
107 
108  st->path.count = 0;
109 }
110 
111 /*!
112  \brief Switch mapset search path
113  */
115 {
116  int count;
117  char **names;
118 
119  count = st->path2.count;
120  names = st->path2.names;
121 
122  st->path2.count = st->path.count;
123  st->path2.names = st->path.names;
124 
125  st->path.count = count;
126  st->path.names = names;
127 }
128 
129 /*!
130  \brief Reset number of mapsets
131  */
132 void G_reset_mapsets(void)
133 {
134  st->path.count = 0;
135 }
136 
137 /*!
138  \brief Get list of available mapsets for current location
139 
140  List is updated by each call to this function.
141 
142  \return pointer to NULL terminated array of available mapsets
143  */
145 {
146  char *location;
147  char **mapsets = NULL;
148  int alloc = 50;
149  int n = 0;
150  DIR *dir;
151  struct dirent *ent;
152 
153  G_debug(3, "G_get_available_mapsets");
154 
155  mapsets = G_calloc(alloc, sizeof(char *));
156 
157  location = G_location_path();
158  dir = opendir(location);
159  if (!dir) {
160  G_free(location);
161  return mapsets;
162  }
163 
164  while ((ent = readdir(dir))) {
165  char buf[GPATH_MAX];
166  struct stat st;
167 
168  sprintf(buf, "%s/%s/WIND", location, ent->d_name);
169 
170  if (G_stat(buf, &st) != 0) {
171  G_debug(4, "%s is not mapset", ent->d_name);
172  continue;
173  }
174 
175  G_debug(4, "%s is mapset", ent->d_name);
176 
177  if (n + 2 >= alloc) {
178  alloc += 50;
179  mapsets = G_realloc(mapsets, alloc * sizeof(char *));
180  }
181 
182  mapsets[n++] = G_store(ent->d_name);
183  }
184  mapsets[n] = NULL;
185 
186  closedir(dir);
187  G_free(location);
188 
189  return mapsets;
190 }
191 
192 /*!
193  \brief Add mapset to the list of mapsets in search path
194 
195  Mapset is add in memory only, not to the SEARCH_PATH file!
196  List is check first if already exists.
197 
198  \param mapset mapset name to be added to the search path
199  */
200 void G_add_mapset_to_search_path(const char *mapset)
201 {
202  if (!G_is_mapset_in_search_path(mapset))
203  new_mapset(mapset);
204 }
205 
206 /*!
207  \brief Check if given mapset is in search path
208 
209  \param mapset mapset name
210 
211  \return 1 mapset found in search path
212  \return 0 mapset not found
213 */
214 int G_is_mapset_in_search_path(const char *mapset)
215 {
216  int i;
217 
218  for (i = 0; i < st->path.count; i++) {
219  if (strcmp(st->path.names[i], mapset) == 0)
220  return 1;
221  }
222 
223  return 0;
224 }
DIR * opendir()
void G_free(void *buf)
Free allocated memory.
Definition: gis/alloc.c:149
void G_create_alt_search_path(void)
Define alternative mapset search path.
Definition: mapset_nme.c:103
char ** G_get_available_mapsets(void)
Get list of available mapsets for current location.
Definition: mapset_nme.c:144
int G_stat(const char *file_name, struct stat *buf)
Get file status.
Definition: paths.c:128
int G_mapset_permissions(const char *mapset)
Check for user mapset permission.
Definition: mapset_msc.c:145
void G_reset_mapsets(void)
Reset number of mapsets.
Definition: mapset_nme.c:132
int count
char * G_store(const char *s)
Copy string to allocated memory.
Definition: strings.c:86
void G_add_mapset_to_search_path(const char *mapset)
Add mapset to the list of mapsets in search path.
Definition: mapset_nme.c:200
#define NULL
Definition: ccmath.h:32
dir_entry * readdir()
struct state * st
Definition: parser.c:102
char * G_location_path(void)
Get current location UNIX-like path.
Definition: location.c:54
void G__get_list_of_mapsets(void)
Fill list of mapsets from search path (internal use only)
Definition: mapset_nme.c:57
int G_debug(int level, const char *msg,...)
Print debugging message.
Definition: debug.c:65
int G_is_mapset_in_search_path(const char *mapset)
Check if given mapset is in search path.
Definition: mapset_nme.c:214
#define GPATH_MAX
Definition: gis.h:167
const char * G_get_mapset_name(int n)
Get name of the n&#39;th mapset from the current mapset search path.
Definition: mapset_nme.c:44
void G_switch_search_path(void)
Switch mapset search path.
Definition: mapset_nme.c:114
Definition: manage.h:4
#define GNAME_MAX
Definition: gis.h:164
Definition: path.h:16
const char * G_mapset(void)
Get current mapset name.
Definition: gis/mapset.c:33
FILE * G_fopen_old(const char *element, const char *name, const char *mapset)
Open a database file for reading.
Definition: gis/open.c:253
const char * name
Definition: named_colr.c:7
struct state state
Definition: parser.c:101