123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273 |
- /*!
- \file lib/gis/find_file.c
- \brief GIS library - Find GRASS data base files
- (C) 2001-2009 by the GRASS Development Team
- This program is free software under the
- GNU General Public License (>=v2).
- Read the file COPYING that comes with GRASS
- for details.
- \author Original author CERL
- */
- #include <string.h>
- #include <unistd.h>
- #include <grass/gis.h>
- #include <grass/glocale.h>
- static const char *find_element(int misc, const char *dir, const char *element)
- {
- static const char *cell_elements[] = {
- "cellhd",
- "cell",
- "cats",
- "colr",
- "hist",
- "cell_misc",
- "fcell",
- "g3dcell",
- NULL
- };
- static const char *dig_elements[] = {
- "dig",
- "dig_att",
- "dig_plus",
- "dig_cats",
- "dig_misc",
- "reg",
- NULL
- };
- const char *search = misc ? dir : element;
- int i;
- for (i = 1; cell_elements[i]; i++)
- if (strcmp(search, cell_elements[i]) == 0)
- return cell_elements[0];
- for (i = 1; dig_elements[i]; i++)
- if (strcmp(search, dig_elements[i]) == 0)
- return dig_elements[0];
- return element;
- }
- static const char *find_file(int misc, const char *dir,
- const char *element, const char *name,
- const char *mapset)
- {
- char path[GPATH_MAX];
- char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
- const char *pname, *pmapset;
- int n;
- if (*name == 0)
- return NULL;
- *path = 0;
- /*
- * if name is in the fully qualified format, split it into
- * name, mapset (overrides what was in mapset)
- */
- if (G_name_is_fully_qualified(name, xname, xmapset)) {
- pname = xname;
- pmapset = xmapset;
- }
- else {
- pname = name;
- pmapset = mapset;
- }
- if (strcmp(element, "vector") == 0 &&
- pmapset && strcasecmp(pmapset, "ogr") == 0) {
- /* don't check for virtual OGR mapset */
- return G_store(pmapset);
- }
-
- /*
- * reject illegal names and mapsets
- */
- if (G_legal_filename(pname) == -1)
- return NULL;
-
- if (pmapset && *pmapset && G_legal_filename(pmapset) == -1)
- return NULL;
- /*
- * if no specific mapset is to be searched
- * then search all mapsets in the mapset search list
- */
- if (pmapset == NULL || *pmapset == 0) {
- int cnt = 0;
- const char *pselmapset = NULL;
- const char *pelement = find_element(misc, dir, element);
- for (n = 0; (pmapset = G_get_mapset_name(n)); n++) {
- if (misc && element == pelement)
- G_file_name_misc(path, dir, pelement, pname, pmapset);
- else
- G_file_name(path, pelement, pname, pmapset);
- if (access(path, 0) == 0) {
- if (!pselmapset)
- pselmapset = pmapset;
- else if (element == pelement)
- G_warning(_("'%s/%s' was found in more mapsets (also found in <%s>)"),
- element, pname, pmapset);
- cnt++;
- }
- }
- if (cnt > 0) {
- if (misc)
- G_file_name_misc(path, dir, element, pname, pselmapset);
- else
- G_file_name(path, element, name, pselmapset);
- if (access(path, 0) == 0) {
- /* If the same name exists in more mapsets and print a warning */
- if (cnt > 1 && element == pelement)
- G_warning(_("Using <%s@%s>"),
- pname, pselmapset);
-
- return G_store(pselmapset);
- }
- }
- }
- /*
- * otherwise just look for the file in the specified mapset.
- * since the name may have been qualified, mapset may point
- * to the xmapset, so we must should it to
- * permanent storage via G_store().
- */
- else {
- if (misc)
- G_file_name_misc(path, dir, element, pname, pmapset);
- else
- G_file_name(path, element, pname, pmapset);
-
- if (access(path, 0) == 0)
- return G_store(pmapset);
- }
-
- return NULL;
- }
- static const char *find_file1(
- int misc,
- const char *dir,
- const char *element, char *name, const char *mapset)
- {
- char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
- const char *pname, *pmapset;
- const char *mp;
- if (G_name_is_fully_qualified(name, xname, xmapset)) {
- pname = xname;
- pmapset = xmapset;
- }
- else {
- pname = name;
- pmapset = mapset;
- }
- mp = find_file(misc, dir, element, pname, pmapset);
- if (mp && name != pname)
- strcpy(name, pname);
- return mp;
- }
- /*!
- * \brief Searches for a file from the mapset search list or in a
- * specified mapset.
- *
- * Returns the mapset name where the file was found.
- *
- * If the user specifies a fully qualified element (name@mapset)
- * which exists, then G_find_file() modifies "name"
- * by removing the "@mapset" part.
- *
- * Rejects all names that begin with "."
- *
- * If <i>name</i> is of the form nnn in ppp then only mapset ppp
- * is searched.
- *
- * \param element database element (eg, "cell", "cellhd", "colr", etc)
- * \param name file name to look for
- * \param mapset mapset to search. if mapset is "" will search in mapset search list
- *
- * \return pointer to a string with name of mapset where file was
- * found, or NULL if not found
- */
- const char *G_find_file(const char *element, char *name, const char *mapset)
- {
- return find_file1(0, NULL, element, name, mapset);
- }
- /*!
- * \brief Searches for a file from the mapset search list or in a
- * specified mapset.
- *
- * Returns the mapset name where the file was found.
- *
- * \param dir file directory
- * \param element database element (eg, "cell", "cellhd", "colr", etc)
- * \param name file name to look for
- * \param mapset mapset to search. if mapset is "" will search in mapset search list
- *
- * \return pointer to a string with name of mapset where file was
- * found, or NULL if not found
- */
- const char *G_find_file_misc(const char *dir,
- const char *element, char *name, const char *mapset)
- {
- return find_file1(1, dir, element, name, mapset);
- }
- /*!
- * \brief Searches for a file from the mapset search list or in a
- * specified mapset. (look but don't touch)
- *
- * Returns the mapset name where the file was found.
- *
- * Exactly the same as G_find_file() except that if <i>name</i> is in
- * the form "<i>name@mapset</i>", and is found, G_find_file2() will
- * not alter <i>name</i> by removing the "@<i>mapset</i>" part.
- *
- * Rejects all names that begin with "."
- *
- * \param element database element (eg, "cell", "cellhd", "colr", etc)
- * \param name file name to look for
- * \param mapset mapset to search. if mapset is "" will search in mapset search list
- *
- * \return pointer to a string with name of mapset where file was
- * found, or NULL if not found
- */
- const char *G_find_file2(const char *element, const char *name, const char *mapset)
- {
- return find_file(0, NULL, element, name, mapset);
- }
- /*!
- * \brief Searches for a file from the mapset search list or in a
- * specified mapset. (look but don't touch)
- *
- * Returns the mapset name where the file was found.
- *
- *
- * \param dir file directory
- * \param element database element (eg, "cell", "cellhd", "colr", etc)
- * \param name file name to look for
- * \param mapset mapset to search. if mapset is "" will search in mapset search list
- *
- * \return pointer to a string with name of mapset where file was
- * found, or NULL if not found
- */
- const char *G_find_file2_misc(const char *dir,
- const char *element,
- const char *name, const char *mapset)
- {
- return find_file(1, dir, element, name, mapset);
- }
|