mapset_nme.c

Go to the documentation of this file.
00001 /*************************************************************************
00002  *
00003  *  G__mapset_name(n)
00004  *      given the index, return the char string name of the n'th mapset
00005  *      from the mapset_name[] list. The first call will initialize
00006  *      the list.
00007  *
00008  * Internal routines
00009  *
00010  *   get_list_of_mapsets()
00011  *      sets up the mapset_name[] list from the mapset
00012  *
00013  * Data structures
00014  *   mapset_name[]  list of mapset names
00015  *   nmapset        number of names in the above list
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 "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  * first call will detect no mapsets in list
00036  * and go look up the list
00037  */
00038     if (nmapset == 0)
00039         get_list_of_mapsets();
00040 /*
00041  * must not run off the bounds of the list
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[30];
00052     FILE *fd;
00053 
00054 /*
00055  * the list of mapsets is in SEARCH_PATH file in the mapset
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  * if no list, then set the list to the current mapset followed
00067  * by PERMANENT
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  * extend mapset name list and store name
00089  * note: assumes G_realloc will become G_malloc if mapset_name == NULL
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 /* Returns pointer to zero terminated array of available mapsets.
00135  * List is updated by each call to this function */
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 ) { /* alloc some space, so that if something failes we can return array */
00149         alloc = 50;
00150         mapsets = (char **) G_calloc ( alloc, sizeof (char *) );
00151     } else { /* free old strings and reset pointers to NULL */
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             /* Realloc space if necessary */
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             /* Add to list */
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 /* Add mapset to the list of mapsets in search path.
00187  * Mapset is add in memory only, not to the SEARCH_PATH file!
00188  * List is check first if already exists.
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 

Generated on Mon Jan 1 19:49:25 2007 for GRASS by  doxygen 1.5.1