00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include <sys/types.h>
00019 #include <sys/stat.h>
00020 #include <string.h>
00021 #include <dirent.h>
00022 #include <unistd.h>
00023 #include <grass/gis.h>
00024
00025 static char **mapset_name ;
00026 static char **mapset_name2 ;
00027 static int nmapset = 0;
00028 static int nmapset2 = 0;
00029 static int new_mapset(char *);
00030 static int get_list_of_mapsets(void);
00031
00032 char *G__mapset_name (int n)
00033 {
00034
00035
00036
00037
00038 if (nmapset == 0)
00039 get_list_of_mapsets();
00040
00041
00042
00043 if (n < 0 || n >= nmapset)
00044 return ( (char *) NULL);
00045
00046 return mapset_name[n];
00047 }
00048
00049 static int get_list_of_mapsets()
00050 {
00051 char name[GNAME_MAX];
00052 FILE *fd;
00053
00054
00055
00056
00057 mapset_name = NULL;
00058 if((fd = G_fopen_old ("","SEARCH_PATH",G_mapset())))
00059 {
00060 while (fscanf (fd, "%s", name) == 1)
00061 if (G__mapset_permissions (name) >= 0)
00062 new_mapset(name);
00063 fclose (fd);
00064 }
00065
00066
00067
00068
00069 if (!nmapset)
00070 {
00071 char *perm;
00072 char *cur;
00073
00074 cur = G_mapset();
00075 perm = "PERMANENT";
00076
00077 new_mapset (cur);
00078 if (strcmp(perm, cur) != 0 && G__mapset_permissions (perm) >= 0)
00079 new_mapset (perm);
00080 }
00081
00082 return 0;
00083 }
00084
00085 static int new_mapset(char *name)
00086 {
00087
00088
00089
00090
00091 nmapset++;
00092 mapset_name = (char **) G_realloc ((char *) mapset_name, nmapset * sizeof (char *));
00093 mapset_name[nmapset-1] = G_store (name);
00094
00095 return 0;
00096 }
00097
00098
00099 int G__create_alt_search_path()
00100 {
00101 nmapset2 = nmapset;
00102 mapset_name2 = mapset_name;
00103
00104 nmapset = 0;
00105
00106 return 0;
00107 get_list_of_mapsets();
00108 }
00109
00110 int G__switch_search_path()
00111 {
00112 int n;
00113 char **names;
00114
00115 n = nmapset2;
00116 names = mapset_name2;
00117
00118 nmapset2 = nmapset;
00119 mapset_name2 = mapset_name;
00120
00121 nmapset = n;
00122 mapset_name = names;
00123
00124 return 0;
00125 }
00126
00127 int G_reset_mapsets()
00128 {
00129 nmapset=0;
00130
00131 return 0;
00132 }
00133
00134
00135
00136 char **G_available_mapsets ( void )
00137 {
00138 int i, n;
00139 static int alloc = 0;
00140 static char **mapsets = NULL;
00141 DIR *dir;
00142 struct dirent *ent;
00143 char buf[1024];
00144 struct stat st;
00145
00146 G_debug (3, "G_available_mapsets");
00147
00148 if ( alloc == 0 ) {
00149 alloc = 50;
00150 mapsets = (char **) G_calloc ( alloc, sizeof (char *) );
00151 } else {
00152 i = 0;
00153 while ( mapsets[i] ) {
00154 G_free ( mapsets[i] ) ;
00155 mapsets[i] = NULL;
00156 }
00157 }
00158
00159 n = 0;
00160 dir = opendir( G_location_path() );
00161 if (dir == NULL) return mapsets;
00162
00163 while ( ( ent = readdir (dir) ) ) {
00164 sprintf ( buf, "%s/%s/WIND", G_location_path(), ent->d_name );
00165 if ( stat ( buf, &st ) == 0 ) {
00166 G_debug (4, "%s is mapset", ent->d_name);
00167
00168 if ( n + 2 >= alloc ) {
00169 alloc += 50;
00170 mapsets = (char **) G_realloc ( mapsets, alloc * sizeof (char *) );
00171 for ( i = n; i < alloc; i++ ) mapsets[i] = NULL;
00172 }
00173
00174 mapsets[n] = G_store ( ent->d_name );
00175 n++;
00176 } else {
00177 G_debug (4, "%s is not mapset", ent->d_name);
00178 }
00179 }
00180
00181 closedir ( dir );
00182
00183 return mapsets;
00184 }
00185
00186
00187
00188
00189
00190 void G_add_mapset_to_search_path ( char *mapset )
00191 {
00192 int i;
00193
00194 for ( i = 0; i < nmapset; i++ ) {
00195 if ( strcmp ( mapset_name[i], mapset) == 0 ) return;
00196 }
00197 new_mapset (mapset);
00198 }
00199