find_file.c

Go to the documentation of this file.
00001 /*
00002  **********************************************************************
00003  *  char *
00004  *  G_find_file (element, name, mapset)
00005  *        char *element    database element (eg, "cell", "cellhd", etc)
00006  *        char *name       file name to look for
00007  *        char *mapset     mapset to search. if mapset is ""
00008  *                         will search in mapset search list
00009  *
00010  *      searches for a file from the mapset search list
00011  *      or in a specified mapset.
00012  *      returns the mapset name where the file was found.
00013  *
00014  *  returns:
00015  *      char *  pointer to a string with name of mapset
00016  *              where file was found, or NULL if not found
00017  *  note:
00018  *      rejects all names that begin with .
00019  *
00020  *      if name is of the form nnn in ppp then only mapset ppp
00021  *      is searched
00022  *
00023  *  G_find_file2 (element, name, mapset)
00024  *
00025  *      exactly the same as G_find_file() except that if name is in the
00026  *      form nnn in ppp, and is found, name is changed to nnn by G_find_file().
00027  **********************************************************************/
00028 
00029 #include <string.h>
00030 #include <unistd.h>
00031 #include "gis.h"
00032 
00033 static char *G__find_file (
00034     char *element,
00035     char *name,
00036     char *mapset)
00037 {
00038     char path[1000];
00039     char xname[512], xmapset[512];
00040     char *pname, *pmapset;
00041     int n;
00042 
00043     if (*name == 0)
00044         return NULL;
00045     *path = 0;
00046 
00047 /*
00048  * if name is in the fully qualified format, split it into
00049  * name, mapset (overrides what was in mapset)
00050  */
00051     if (G__name_is_fully_qualified(name, xname, xmapset))
00052     {
00053         pname = xname;
00054         pmapset = xmapset;
00055     }
00056     else
00057     {
00058         pname = name;
00059         pmapset = mapset;
00060     }
00061 
00062 /*
00063  * reject illegal names and mapsets
00064  */
00065     if (G_legal_filename (pname) == -1)
00066             return NULL;
00067 
00068     if (pmapset && *pmapset && G_legal_filename (pmapset) == -1)
00069             return NULL;
00070 
00071 /*
00072 * if no specific mapset is to be searched
00073 * then search all mapsets in the mapset search list
00074 */
00075     if (pmapset == NULL || *pmapset == 0)
00076     {
00077         int cnt = 0;
00078         char *pselmapset = NULL;
00079         for (n = 0; (pmapset = G__mapset_name(n)); n++) {
00080             if (access(G__file_name (path, element, pname, pmapset), 0) == 0) {
00081                 if ( !pselmapset )
00082                     pselmapset = pmapset;
00083                 cnt++;
00084             }
00085         }
00086         if ( cnt > 0 ) {
00087             /* If the same name exists in more mapsets and print a warning */
00088             if ( cnt > 1 ) 
00089                 G_warning ("'%s/%s' was found in more mapsets (also found in %s).", element, pname, pselmapset);
00090             return pselmapset;
00091         }
00092     }
00093 /*
00094  * otherwise just look for the file in the specified mapset.
00095  * since the name may have been qualified, mapset may point
00096  * to the xmapset, so we must should it to
00097  * permanent storage via G_store().
00098  */
00099     else
00100     {
00101         if (access(G__file_name (path, element, pname, pmapset),0) == 0)
00102                 return G_store (pmapset);
00103     }
00104     return NULL;
00105 }
00106 
00107 char *G_find_file (
00108     char *element,
00109     char *name,
00110     char *mapset)
00111 {
00112     char *mp;
00113     char xname[512], xmapset[512];
00114 
00115     mp = G__find_file (element, name, mapset);
00116     if (mp)
00117     {
00118         if (G__name_is_fully_qualified(name, xname, xmapset))
00119             strcpy (name, xname);
00120     }
00121 
00122     return mp;
00123 }
00124 char *G_find_file2 (
00125     char *element,
00126     char *name,
00127     char *mapset)
00128 {
00129     return G__find_file (element, name, mapset);
00130 }

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