GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
mapset_nme.c
Go to the documentation of this file.
1 
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <string.h>
15 #include <dirent.h>
16 #include <unistd.h>
17 #include <grass/gis.h>
18 
19 static char **mapset_name;
20 static char **mapset_name2;
21 static int nmapset = 0;
22 static int nmapset2 = 0;
23 static int new_mapset(const char *);
24 static int get_list_of_mapsets(void);
25 
36 char *G__mapset_name(int n)
37 {
38  /*
39  * first call will detect no mapsets in list
40  * and go look up the list
41  */
42  if (nmapset == 0)
43  get_list_of_mapsets();
44  /*
45  * must not run off the bounds of the list
46  */
47  if (n < 0 || n >= nmapset)
48  return ((char *)NULL);
49 
50  return mapset_name[n];
51 }
52 
53 static int get_list_of_mapsets(void)
54 {
55  char name[GNAME_MAX];
56  FILE *fd;
57 
58  /*
59  * the list of mapsets is in SEARCH_PATH file in the mapset
60  */
61  mapset_name = NULL;
62  if ((fd = G_fopen_old("", "SEARCH_PATH", G_mapset()))) {
63  while (fscanf(fd, "%s", name) == 1)
64  if (G__mapset_permissions(name) >= 0)
65  new_mapset(name);
66  fclose(fd);
67  }
68  /*
69  * if no list, then set the list to the current mapset followed
70  * by PERMANENT
71  */
72  if (!nmapset) {
73  char *perm;
74  char *cur;
75 
76  cur = G_mapset();
77  perm = "PERMANENT";
78 
79  new_mapset(cur);
80  if (strcmp(perm, cur) != 0 && G__mapset_permissions(perm) >= 0)
81  new_mapset(perm);
82  }
83 
84  return 0;
85 }
86 
87 static int new_mapset(const char *name)
88 {
89  /*
90  * extend mapset name list and store name
91  * note: assumes G_realloc will become G_malloc if mapset_name == NULL
92  */
93  nmapset++;
94  mapset_name =
95  (char **)G_realloc((char *)mapset_name, nmapset * sizeof(char *));
96  mapset_name[nmapset - 1] = G_store(name);
97 
98  return 0;
99 }
100 
107 {
108  nmapset2 = nmapset;
109  mapset_name2 = mapset_name;
110 
111  nmapset = 0;
112 
113  return 0;
114 }
115 
122 {
123  int n;
124  char **names;
125 
126  n = nmapset2;
127  names = mapset_name2;
128 
129  nmapset2 = nmapset;
130  mapset_name2 = mapset_name;
131 
132  nmapset = n;
133  mapset_name = names;
134 
135  return 0;
136 }
137 
144 {
145  nmapset = 0;
146 
147  return 0;
148 }
149 
158 {
159  int i, n;
160  static int alloc = 0;
161  static char **mapsets = NULL;
162  DIR *dir;
163  struct dirent *ent;
164  char buf[1024];
165  struct stat st;
166 
167  G_debug(3, "G_available_mapsets");
168 
169  if (alloc == 0) { /* alloc some space, so that if something failes we can return array */
170  alloc = 50;
171  mapsets = (char **)G_calloc(alloc, sizeof(char *));
172  }
173  else { /* free old strings and reset pointers to NULL */
174  i = 0;
175  while (mapsets[i]) {
176  G_free(mapsets[i]);
177  mapsets[i] = NULL;
178  }
179  }
180 
181  n = 0;
182  dir = opendir(G_location_path());
183  if (dir == NULL)
184  return mapsets;
185 
186  while ((ent = readdir(dir))) {
187  sprintf(buf, "%s/%s/WIND", G_location_path(), ent->d_name);
188  if (stat(buf, &st) == 0) {
189  G_debug(4, "%s is mapset", ent->d_name);
190  /* Realloc space if necessary */
191  if (n + 2 >= alloc) {
192  alloc += 50;
193  mapsets = (char **)G_realloc(mapsets, alloc * sizeof(char *));
194  for (i = n; i < alloc; i++)
195  mapsets[i] = NULL;
196  }
197  /* Add to list */
198  mapsets[n] = G_store(ent->d_name);
199  n++;
200  mapsets[n] = NULL;
201  }
202  else {
203  G_debug(4, "%s is not mapset", ent->d_name);
204  }
205  }
206 
207  closedir(dir);
208 
209  return mapsets;
210 }
211 
220 void G_add_mapset_to_search_path(const char *mapset)
221 {
222  if (!G_is_mapset_in_search_path(mapset))
223  new_mapset(mapset);
224 }
225 
234 int G_is_mapset_in_search_path(const char *mapset)
235 {
236  int i;
237 
238  for (i = 0; i < nmapset; i++) {
239  if (strcmp(mapset_name[i], mapset) == 0)
240  return 1;
241  }
242  return 0;
243 }
char * G_mapset(void)
current mapset name
Definition: mapset.c:31
sprintf(buf2,"%s", G3D_CATS_ELEMENT)
int G__mapset_permissions(const char *mapset)
Check for user mapset permission.
Definition: mapset_msc.c:115
DIR * opendir()
void G_free(void *buf)
Free allocated memory.
Definition: gis/alloc.c:142
char * G_store(const char *s)
Copy string to allocated memory.
Definition: store.c:32
FILE * fd
Definition: g3dcolor.c:368
string name
Definition: render.py:1314
void G_add_mapset_to_search_path(const char *mapset)
Add mapset to the list of mapsets in search path.
Definition: mapset_nme.c:220
int G__create_alt_search_path(void)
Define alternative mapset search path.
Definition: mapset_nme.c:106
int G__switch_search_path(void)
Switch mapset search path.
Definition: mapset_nme.c:121
dir_entry * readdir()
int stat
Definition: g3dcolor.c:369
char ** G_available_mapsets(void)
Get list of available mapsets for current location.
Definition: mapset_nme.c:157
char * G_location_path(void)
Get current location directory.
Definition: location.c:37
char * G__mapset_name(int n)
Get name of the n&#39;th mapset from the mapset_name[] list.
Definition: mapset_nme.c:36
int G_is_mapset_in_search_path(const char *mapset)
Check if given mapset is in search path.
Definition: mapset_nme.c:234
int G_reset_mapsets(void)
Reset number of mapsets.
Definition: mapset_nme.c:143
def mapsets
List available mapsets.
Definition: core.py:1045
char buf[GNAME_MAX+sizeof(G3D_DIRECTORY)+2]
Definition: g3drange.c:62
return NULL
Definition: dbfopen.c:1394
int G_debug(int level, const char *msg,...)
Print debugging message.
Definition: gis/debug.c:51
fclose(fd)
FILE * G_fopen_old(const char *element, const char *name, const char *mapset)
Open a database file for reading.
Definition: gis/open.c:226
int n
Definition: dataquad.c:291