GRASS GIS 7 Programmer's Manual  7.0.5(2016)-r00000
mapset_nme.c
Go to the documentation of this file.
1 
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 "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 
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 
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 
104 {
105  st->path2.count = st->path.count;
106  st->path2.names = st->path.names;
107 
108  st->path.count = 0;
109 }
110 
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 
132 void G_reset_mapsets(void)
133 {
134  st->path.count = 0;
135 }
136 
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 
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 
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 }
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
const char * G_mapset(void)
Get current mapset name.
Definition: mapset.c:33
int G_mapset_permissions(const char *mapset)
Check for user mapset permission.
Definition: mapset_msc.c:116
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
struct state * st
Definition: parser.c:101
char * G_location_path(void)
Get current location UNIX-like path.
Definition: location.c:54
struct list * list
Definition: read_list.c:24
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
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: path.h:16
FILE * G_fopen_old(const char *element, const char *name, const char *mapset)
Open a database file for reading.
Definition: gis/open.c:240
const char * name
Definition: named_colr.c:7
struct state state
Definition: parser.c:100