GRASS GIS 7 Programmer's Manual  7.5.svn(2018)-r72103
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 zero terminated array of available mapsets
143  */
145 {
146  char **mapsets = NULL;
147  int alloc = 50;
148  int n = 0;
149  DIR *dir;
150  struct dirent *ent;
151 
152  G_debug(3, "G_get_available_mapsets");
153 
154  mapsets = G_calloc(alloc, sizeof(char *));
155 
156  dir = opendir(G_location_path());
157  if (!dir)
158  return mapsets;
159 
160  while ((ent = readdir(dir))) {
161  char buf[GPATH_MAX];
162  struct stat st;
163 
164  sprintf(buf, "%s/%s/WIND", G_location_path(), ent->d_name);
165 
166  if (G_stat(buf, &st) != 0) {
167  G_debug(4, "%s is not mapset", ent->d_name);
168  continue;
169  }
170 
171  G_debug(4, "%s is mapset", ent->d_name);
172 
173  if (n + 2 >= alloc) {
174  alloc += 50;
175  mapsets = G_realloc(mapsets, alloc * sizeof(char *));
176  }
177 
178  mapsets[n++] = G_store(ent->d_name);
179  mapsets[n] = NULL;
180  }
181 
182  closedir(dir);
183 
184  return mapsets;
185 }
186 
187 /*!
188  \brief Add mapset to the list of mapsets in search path
189 
190  Mapset is add in memory only, not to the SEARCH_PATH file!
191  List is check first if already exists.
192 
193  \param mapset mapset name to be added to the search path
194  */
195 void G_add_mapset_to_search_path(const char *mapset)
196 {
197  if (!G_is_mapset_in_search_path(mapset))
198  new_mapset(mapset);
199 }
200 
201 /*!
202  \brief Check if given mapset is in search path
203 
204  \param mapset mapset name
205 
206  \return 1 mapset found in search path
207  \return 0 mapset not found
208 */
209 int G_is_mapset_in_search_path(const char *mapset)
210 {
211  int i;
212 
213  for (i = 0; i < st->path.count; i++) {
214  if (strcmp(st->path.names[i], mapset) == 0)
215  return 1;
216  }
217 
218  return 0;
219 }
DIR * opendir()
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:195
#define NULL
Definition: ccmath.h:32
dir_entry * readdir()
struct state * st
Definition: parser.c:103
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:209
fclose(fd)
#define GPATH_MAX
Definition: gis.h:151
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:148
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:102