source: grass/trunk/lib/gis/mapset_nme.c@ 30718

Last change on this file since 30718 was 30718, checked in by martinl, 16 years ago

gislib (mapset_nme.c): Doxygen strings updated

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id
  • Property svn:mime-type set to text/x-csrc
File size: 4.4 KB
Line 
1/*!
2 \file mapset_nme.c
3
4 \brief GIS library - Mapset name, search path routines.
5
6 (C) 1999-2008 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 <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
19static char **mapset_name ;
20static char **mapset_name2 ;
21static int nmapset = 0;
22static int nmapset2 = 0;
23static int new_mapset(const char *);
24static int get_list_of_mapsets(void);
25
26/*!
27 \brief Get name of the n'th mapset from the mapset_name[] list.
28
29 The first call will initialize the list.
30
31 \param n mapset index
32
33 \return mapset name
34 \return NULL if mapset not found
35*/
36char *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
53static 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 {
64 while (fscanf (fd, "%s", name) == 1)
65 if (G__mapset_permissions (name) >= 0)
66 new_mapset(name);
67 fclose (fd);
68 }
69/*
70 * if no list, then set the list to the current mapset followed
71 * by PERMANENT
72 */
73 if (!nmapset)
74 {
75 char *perm;
76 char *cur;
77
78 cur = G_mapset();
79 perm = "PERMANENT";
80
81 new_mapset (cur);
82 if (strcmp(perm, cur) != 0 && G__mapset_permissions (perm) >= 0)
83 new_mapset (perm);
84 }
85
86 return 0;
87}
88
89static int new_mapset(const char *name)
90{
91/*
92 * extend mapset name list and store name
93 * note: assumes G_realloc will become G_malloc if mapset_name == NULL
94 */
95 nmapset++;
96 mapset_name = (char **) G_realloc ((char *) mapset_name, nmapset * sizeof (char *));
97 mapset_name[nmapset-1] = G_store (name);
98
99 return 0;
100}
101
102/*!
103 \brief Define alternative mapset search path
104
105 \return 0
106*/
107int G__create_alt_search_path(void)
108{
109 nmapset2 = nmapset;
110 mapset_name2 = mapset_name;
111
112 nmapset = 0;
113
114 return 0; /* ??? */
115
116 get_list_of_mapsets();
117}
118
119/*!
120 \brief Switch mapset search path
121
122 \return 0
123*/
124int G__switch_search_path(void)
125{
126 int n;
127 char **names;
128
129 n = nmapset2;
130 names = mapset_name2;
131
132 nmapset2 = nmapset;
133 mapset_name2 = mapset_name;
134
135 nmapset = n;
136 mapset_name = names;
137
138 return 0;
139}
140
141/*!
142 \brief Reset number of mapsets
143
144 \return 0
145*/
146int G_reset_mapsets(void)
147{
148 nmapset=0;
149
150 return 0;
151}
152
153/*!
154 \brief Get list of available mapsets for current location
155
156 List is updated by each call to this function
157
158 \return pointer to zero terminated array of available mapsets.
159*/
160char **G_available_mapsets ( void )
161{
162 int i, n;
163 static int alloc = 0;
164 static char **mapsets = NULL;
165 DIR *dir;
166 struct dirent *ent;
167 char buf[1024];
168 struct stat st;
169
170 G_debug (3, "G_available_mapsets");
171
172 if ( alloc == 0 ) { /* alloc some space, so that if something failes we can return array */
173 alloc = 50;
174 mapsets = (char **) G_calloc ( alloc, sizeof (char *) );
175 } else { /* free old strings and reset pointers to NULL */
176 i = 0;
177 while ( mapsets[i] ) {
178 G_free ( mapsets[i] ) ;
179 mapsets[i] = NULL;
180 }
181 }
182
183 n = 0;
184 dir = opendir( G_location_path() );
185 if (dir == NULL) return mapsets;
186
187 while ( ( ent = readdir (dir) ) ) {
188 sprintf ( buf, "%s/%s/WIND", G_location_path(), ent->d_name );
189 if ( stat ( buf, &st ) == 0 ) {
190 G_debug (4, "%s is mapset", ent->d_name);
191 /* Realloc space if necessary */
192 if ( n + 2 >= alloc ) {
193 alloc += 50;
194 mapsets = (char **) G_realloc ( mapsets, alloc * sizeof (char *) );
195 for ( i = n; i < alloc; i++ ) mapsets[i] = NULL;
196 }
197 /* Add to list */
198 mapsets[n] = G_store ( ent->d_name );
199 n++;
200 } else {
201 G_debug (4, "%s is not mapset", ent->d_name);
202 }
203 }
204
205 closedir ( dir );
206
207 return mapsets;
208}
209
210/*!
211 \brief Add mapset to the list of mapsets in search path.
212
213 Mapset is add in memory only, not to the SEARCH_PATH file!
214 List is check first if already exists.
215
216 \param mapset mapset name
217*/
218void G_add_mapset_to_search_path ( const char *mapset )
219{
220 int i;
221
222 for ( i = 0; i < nmapset; i++ ) {
223 if ( strcmp ( mapset_name[i], mapset) == 0 ) return;
224 }
225 new_mapset (mapset);
226}
227
Note: See TracBrowser for help on using the repository browser.