find_file.c

Go to the documentation of this file.
00001 #include <string.h>
00002 #include <unistd.h>
00003 #include <grass/gis.h>
00004 
00005 static char *G__find_file (
00006     char *element,
00007     char *name,
00008     char *mapset)
00009 {
00010     char path[1000];
00011     char xname[512], xmapset[512];
00012     char *pname, *pmapset;
00013     int n;
00014 
00015     if (*name == 0)
00016         return NULL;
00017     *path = 0;
00018 
00019 /*
00020  * if name is in the fully qualified format, split it into
00021  * name, mapset (overrides what was in mapset)
00022  */
00023     if (G__name_is_fully_qualified(name, xname, xmapset))
00024     {
00025         pname = xname;
00026         pmapset = xmapset;
00027     }
00028     else
00029     {
00030         pname = name;
00031         pmapset = mapset;
00032     }
00033 
00034 /*
00035  * reject illegal names and mapsets
00036  */
00037     if (G_legal_filename (pname) == -1)
00038             return NULL;
00039 
00040     if (pmapset && *pmapset && G_legal_filename (pmapset) == -1)
00041             return NULL;
00042 
00043 /*
00044 * if no specific mapset is to be searched
00045 * then search all mapsets in the mapset search list
00046 */
00047     if (pmapset == NULL || *pmapset == 0)
00048     {
00049         int cnt = 0;
00050         char *pselmapset = NULL;
00051         for (n = 0; (pmapset = G__mapset_name(n)); n++) {
00052             if (access(G__file_name (path, element, pname, pmapset), 0) == 0) {
00053                 if ( !pselmapset )
00054                     pselmapset = pmapset;
00055                 cnt++;
00056             }
00057         }
00058         if ( cnt > 0 ) {
00059             /* If the same name exists in more mapsets and print a warning */
00060             if ( cnt > 1 ) 
00061                 G_warning ("'%s/%s' was found in more mapsets (also found in %s).", element, pname, pselmapset);
00062             return pselmapset;
00063         }
00064     }
00065 /*
00066  * otherwise just look for the file in the specified mapset.
00067  * since the name may have been qualified, mapset may point
00068  * to the xmapset, so we must should it to
00069  * permanent storage via G_store().
00070  */
00071     else
00072     {
00073         if (access(G__file_name (path, element, pname, pmapset),0) == 0)
00074                 return G_store (pmapset);
00075     }
00076     return NULL;
00077 }
00078 
00103 char *G_find_file (
00104     char *element,
00105     char *name,
00106     char *mapset)
00107 {
00108     char *mp;
00109     char xname[512], xmapset[512];
00110 
00111     mp = G__find_file (element, name, mapset);
00112     if (mp)
00113     {
00114         if (G__name_is_fully_qualified(name, xname, xmapset))
00115             strcpy (name, xname);
00116     }
00117 
00118     return mp;
00119 }
00120 
00143 char *G_find_file2 (
00144     char *element,
00145     char *name,
00146     char *mapset)
00147 {
00148     return G__find_file (element, name, mapset);
00149 }

Generated on Fri Nov 21 11:02:17 2008 for GRASS by  doxygen 1.5.1