GRASS Programmer's Manual  6.4.1(2011)
find_file.c
Go to the documentation of this file.
00001 
00016 #include <string.h>
00017 #include <unistd.h>
00018 #include <grass/gis.h>
00019 #include <grass/glocale.h>
00020 
00021 static char *find_file(int misc,
00022                        const char *dir,
00023                        const char *element,
00024                        const char *name, const char *mapset)
00025 {
00026     char path[GPATH_MAX];
00027     char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
00028     const char *pname, *pmapset;
00029     int n;
00030 
00031     if (*name == 0)
00032         return NULL;
00033     *path = 0;
00034 
00035     /*
00036      * if name is in the fully qualified format, split it into
00037      * name, mapset (overrides what was in mapset)
00038      */
00039     if (G__name_is_fully_qualified(name, xname, xmapset)) {
00040         pname = xname;
00041         pmapset = xmapset;
00042     }
00043     else {
00044         pname = name;
00045         pmapset = mapset;
00046     }
00047 
00048     /*
00049      * reject illegal names and mapsets
00050      */
00051     if (G_legal_filename(pname) == -1)
00052         return NULL;
00053 
00054     if (pmapset && *pmapset && G_legal_filename(pmapset) == -1)
00055         return NULL;
00056 
00057     /*
00058      * if no specific mapset is to be searched
00059      * then search all mapsets in the mapset search list
00060      */
00061     if (pmapset == NULL || *pmapset == 0) {
00062         int cnt = 0;
00063         const char *pselmapset = NULL;
00064 
00065         for (n = 0; (pmapset = G__mapset_name(n)); n++) {
00066             if (misc)
00067                 G__file_name_misc(path, dir, element, pname, pmapset);
00068             else
00069                 G__file_name(path, element, pname, pmapset);
00070             if (access(path, 0) == 0) {
00071                 if (!pselmapset)
00072                     pselmapset = pmapset;
00073                 else
00074                     G_warning(_("'%s/%s' was found in more mapsets (also found in <%s>)"),
00075                               element, pname, pmapset);
00076                 cnt++;
00077             }
00078         }
00079         if (cnt > 0) {
00080             /* If the same name exists in more mapsets and print a warning */
00081             if (cnt > 1)
00082                 G_warning(_("Using <%s@%s>"),
00083                           pname, pselmapset);
00084             
00085             return (char *)pselmapset;
00086         }
00087     }
00088     /*
00089      * otherwise just look for the file in the specified mapset.
00090      * since the name may have been qualified, mapset may point
00091      * to the xmapset, so we must should it to
00092      * permanent storage via G_store().
00093      */
00094     else {
00095         if (misc)
00096             G__file_name_misc(path, dir, element, pname, pmapset);
00097         else
00098             G__file_name(path, element, pname, pmapset);
00099             
00100         if (access(path, 0) == 0)
00101             return G_store(pmapset);
00102     }
00103     
00104     return NULL;
00105 }
00106 
00107 
00108 
00109 static char *find_file1(int misc,
00110                         const char *dir,
00111                         const char *element, char *name, const char *mapset)
00112 {
00113     char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
00114     const char *pname, *pmapset;
00115     char *mp;
00116 
00117     if (G__name_is_fully_qualified(name, xname, xmapset)) {
00118         pname = xname;
00119         pmapset = xmapset;
00120     }
00121     else {
00122         pname = name;
00123         pmapset = mapset;
00124     }
00125 
00126     mp = find_file(misc, dir, element, pname, pmapset);
00127 
00128     if (mp && name != pname)
00129         strcpy(name, pname);
00130 
00131     return mp;
00132 }
00133 
00159 char *G_find_file(const char *element, char *name, const char *mapset)
00160 {
00161     return find_file1(0, NULL, element, name, mapset);
00162 }
00163 
00164 char *G_find_file_misc(const char *dir,
00165                        const char *element, char *name, const char *mapset)
00166 {
00167     return find_file1(1, dir, element, name, mapset);
00168 }
00169 
00170 
00191 char *G_find_file2(const char *element, const char *name, const char *mapset)
00192 {
00193     return find_file(0, NULL, element, name, mapset);
00194 }
00195 
00196 char *G_find_file2_misc(const char *dir,
00197                         const char *element,
00198                         const char *name, const char *mapset)
00199 {
00200     return find_file(1, dir, element, name, mapset);
00201 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines