GRASS Programmer's Manual
6.4.2(2012)
|
00001 00012 #include <sys/types.h> 00013 #include <sys/stat.h> 00014 #include <string.h> 00015 #include <dirent.h> 00016 #include <unistd.h> 00017 #include <grass/gis.h> 00018 00019 static char **mapset_name; 00020 static char **mapset_name2; 00021 static int nmapset = 0; 00022 static int nmapset2 = 0; 00023 static int new_mapset(const char *); 00024 static int get_list_of_mapsets(void); 00025 00036 char *G__mapset_name(int n) 00037 { 00038 /* 00039 * first call will detect no mapsets in list 00040 * and go look up the list 00041 */ 00042 if (nmapset == 0) 00043 get_list_of_mapsets(); 00044 /* 00045 * must not run off the bounds of the list 00046 */ 00047 if (n < 0 || n >= nmapset) 00048 return ((char *)NULL); 00049 00050 return mapset_name[n]; 00051 } 00052 00053 static int get_list_of_mapsets(void) 00054 { 00055 char name[GNAME_MAX]; 00056 FILE *fd; 00057 00058 /* 00059 * the list of mapsets is in SEARCH_PATH file in the mapset 00060 */ 00061 mapset_name = NULL; 00062 if ((fd = G_fopen_old("", "SEARCH_PATH", G_mapset()))) { 00063 while (fscanf(fd, "%s", name) == 1) 00064 if (G__mapset_permissions(name) >= 0) 00065 new_mapset(name); 00066 fclose(fd); 00067 } 00068 /* 00069 * if no list, then set the list to the current mapset followed 00070 * by PERMANENT 00071 */ 00072 if (!nmapset) { 00073 char *perm; 00074 char *cur; 00075 00076 cur = G_mapset(); 00077 perm = "PERMANENT"; 00078 00079 new_mapset(cur); 00080 if (strcmp(perm, cur) != 0 && G__mapset_permissions(perm) >= 0) 00081 new_mapset(perm); 00082 } 00083 00084 return 0; 00085 } 00086 00087 static int new_mapset(const char *name) 00088 { 00089 /* 00090 * extend mapset name list and store name 00091 * note: assumes G_realloc will become G_malloc if mapset_name == NULL 00092 */ 00093 nmapset++; 00094 mapset_name = 00095 (char **)G_realloc((char *)mapset_name, nmapset * sizeof(char *)); 00096 mapset_name[nmapset - 1] = G_store(name); 00097 00098 return 0; 00099 } 00100 00106 int G__create_alt_search_path(void) 00107 { 00108 nmapset2 = nmapset; 00109 mapset_name2 = mapset_name; 00110 00111 nmapset = 0; 00112 00113 return 0; 00114 } 00115 00121 int G__switch_search_path(void) 00122 { 00123 int n; 00124 char **names; 00125 00126 n = nmapset2; 00127 names = mapset_name2; 00128 00129 nmapset2 = nmapset; 00130 mapset_name2 = mapset_name; 00131 00132 nmapset = n; 00133 mapset_name = names; 00134 00135 return 0; 00136 } 00137 00143 int G_reset_mapsets(void) 00144 { 00145 nmapset = 0; 00146 00147 return 0; 00148 } 00149 00157 char **G_available_mapsets(void) 00158 { 00159 int i, n; 00160 static int alloc = 0; 00161 static char **mapsets = NULL; 00162 DIR *dir; 00163 struct dirent *ent; 00164 char buf[1024]; 00165 struct stat st; 00166 00167 G_debug(3, "G_available_mapsets"); 00168 00169 if (alloc == 0) { /* alloc some space, so that if something failes we can return array */ 00170 alloc = 50; 00171 mapsets = (char **)G_calloc(alloc, sizeof(char *)); 00172 } 00173 else { /* free old strings and reset pointers to NULL */ 00174 i = 0; 00175 while (mapsets[i]) { 00176 G_free(mapsets[i]); 00177 mapsets[i] = NULL; 00178 } 00179 } 00180 00181 n = 0; 00182 dir = opendir(G_location_path()); 00183 if (dir == NULL) 00184 return mapsets; 00185 00186 while ((ent = readdir(dir))) { 00187 sprintf(buf, "%s/%s/WIND", G_location_path(), ent->d_name); 00188 if (stat(buf, &st) == 0) { 00189 G_debug(4, "%s is mapset", ent->d_name); 00190 /* Realloc space if necessary */ 00191 if (n + 2 >= alloc) { 00192 alloc += 50; 00193 mapsets = (char **)G_realloc(mapsets, alloc * sizeof(char *)); 00194 for (i = n; i < alloc; i++) 00195 mapsets[i] = NULL; 00196 } 00197 /* Add to list */ 00198 mapsets[n] = G_store(ent->d_name); 00199 n++; 00200 mapsets[n] = NULL; 00201 } 00202 else { 00203 G_debug(4, "%s is not mapset", ent->d_name); 00204 } 00205 } 00206 00207 closedir(dir); 00208 00209 return mapsets; 00210 } 00211 00220 void G_add_mapset_to_search_path(const char *mapset) 00221 { 00222 if (!G_is_mapset_in_search_path(mapset)) 00223 new_mapset(mapset); 00224 } 00225 00234 int G_is_mapset_in_search_path(const char *mapset) 00235 { 00236 int i; 00237 00238 for (i = 0; i < nmapset; i++) { 00239 if (strcmp(mapset_name[i], mapset) == 0) 00240 return 1; 00241 } 00242 return 0; 00243 }