find_file.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*!
  2. \file gis/find_file.c
  3. \brief GIS library - Find GRASS data base files
  4. (C) 2001-2009 by the GRASS Development Team
  5. This program is free software under the
  6. GNU General Public License (>=v2).
  7. Read the file COPYING that comes with GRASS
  8. for details.
  9. \author Original author CERL
  10. */
  11. #include <string.h>
  12. #include <unistd.h>
  13. #include <grass/gis.h>
  14. #include <grass/glocale.h>
  15. static const char *find_file(
  16. int misc,
  17. const char *dir,
  18. const char *element, const char *name, const char *mapset)
  19. {
  20. char path[GPATH_MAX];
  21. char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
  22. const char *pname, *pmapset;
  23. int n;
  24. if (*name == 0)
  25. return NULL;
  26. *path = 0;
  27. /*
  28. * if name is in the fully qualified format, split it into
  29. * name, mapset (overrides what was in mapset)
  30. */
  31. if (G_name_is_fully_qualified(name, xname, xmapset)) {
  32. pname = xname;
  33. pmapset = xmapset;
  34. }
  35. else {
  36. pname = name;
  37. pmapset = mapset;
  38. }
  39. if (strcmp(element, "vector") == 0 &&
  40. strcasecmp(pmapset, "ogr") == 0) {
  41. /* don't check for virtual OGR mapset */
  42. return G_store(pmapset);
  43. }
  44. /*
  45. * reject illegal names and mapsets
  46. */
  47. if (G_legal_filename(pname) == -1)
  48. return NULL;
  49. if (pmapset && *pmapset && G_legal_filename(pmapset) == -1)
  50. return NULL;
  51. /*
  52. * if no specific mapset is to be searched
  53. * then search all mapsets in the mapset search list
  54. */
  55. if (pmapset == NULL || *pmapset == 0) {
  56. int cnt = 0;
  57. const char *pselmapset = NULL;
  58. for (n = 0; (pmapset = G__mapset_name(n)); n++) {
  59. if (misc)
  60. G__file_name_misc(path, dir, element, pname, pmapset);
  61. else
  62. G__file_name(path, element, pname, pmapset);
  63. if (access(path, 0) == 0) {
  64. if (!pselmapset)
  65. pselmapset = pmapset;
  66. else
  67. G_warning(_("'%s/%s' was found in more mapsets (also found in <%s>)"),
  68. element, pname, pmapset);
  69. cnt++;
  70. }
  71. }
  72. if (cnt > 0) {
  73. /* If the same name exists in more mapsets and print a warning */
  74. if (cnt > 1)
  75. G_warning(_("Using <%s@%s>"),
  76. pname, pselmapset);
  77. return G_store(pselmapset);
  78. }
  79. }
  80. /*
  81. * otherwise just look for the file in the specified mapset.
  82. * since the name may have been qualified, mapset may point
  83. * to the xmapset, so we must should it to
  84. * permanent storage via G_store().
  85. */
  86. else {
  87. if (misc)
  88. G__file_name_misc(path, dir, element, pname, pmapset);
  89. else
  90. G__file_name(path, element, pname, pmapset);
  91. if (access(path, 0) == 0)
  92. return G_store(pmapset);
  93. }
  94. return NULL;
  95. }
  96. static const char *find_file1(
  97. int misc,
  98. const char *dir,
  99. const char *element, char *name, const char *mapset)
  100. {
  101. char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
  102. const char *pname, *pmapset;
  103. const char *mp;
  104. if (G_name_is_fully_qualified(name, xname, xmapset)) {
  105. pname = xname;
  106. pmapset = xmapset;
  107. }
  108. else {
  109. pname = name;
  110. pmapset = mapset;
  111. }
  112. mp = find_file(misc, dir, element, pname, pmapset);
  113. if (mp && name != pname)
  114. strcpy(name, pname);
  115. return mp;
  116. }
  117. /*!
  118. * \brief Searches for a file from the mapset search list or in a
  119. * specified mapset.
  120. *
  121. * Returns the mapset name where the file was found.
  122. *
  123. * If the user specifies a fully qualified element (<i>name@mapset</i>)
  124. * which exists, then G_find_file() modifies <i>name</i>
  125. * by removing the "@<i>mapset</i>" part.
  126. *
  127. * Rejects all names that begin with "."
  128. *
  129. * If <i>name</i> is of the form nnn in ppp then only mapset ppp
  130. * is searched.
  131. *
  132. * \param element database element (eg, "cell", "cellhd", "colr", etc)
  133. * \param name file name to look for
  134. * \param mapset mapset to search. if mapset is "" will search in mapset search list
  135. *
  136. * \return pointer to a string with name of mapset where file was
  137. * found, or NULL if not found
  138. */
  139. const char *G_find_file(const char *element, char *name, const char *mapset)
  140. {
  141. return find_file1(0, NULL, element, name, mapset);
  142. }
  143. /*!
  144. * \brief Searches for a file from the mapset search list or in a
  145. * specified mapset.
  146. *
  147. * Returns the mapset name where the file was found.
  148. *
  149. * \param dir file directory
  150. * \param element database element (eg, "cell", "cellhd", "colr", etc)
  151. * \param name file name to look for
  152. * \param mapset mapset to search. if mapset is "" will search in mapset search list
  153. *
  154. * \return pointer to a string with name of mapset where file was
  155. * found, or NULL if not found
  156. */
  157. const char *G_find_file_misc(const char *dir,
  158. const char *element, char *name, const char *mapset)
  159. {
  160. return find_file1(1, dir, element, name, mapset);
  161. }
  162. /*!
  163. * \brief Searches for a file from the mapset search list or in a
  164. * specified mapset. (look but don't touch)
  165. *
  166. * Returns the mapset name where the file was found.
  167. *
  168. * Exactly the same as G_find_file() except that if <i>name</i> is in
  169. * the form "<i>name@mapset</i>", and is found, G_find_file2() will
  170. * not alter <i>name</i> by removing the "@<i>mapset</i>" part.
  171. *
  172. * Rejects all names that begin with "."
  173. *
  174. * \param element database element (eg, "cell", "cellhd", "colr", etc)
  175. * \param name file name to look for
  176. * \param mapset mapset to search. if mapset is "" will search in mapset search list
  177. *
  178. * \return pointer to a string with name of mapset where file was
  179. * found, or NULL if not found
  180. */
  181. const char *G_find_file2(const char *element, const char *name, const char *mapset)
  182. {
  183. return find_file(0, NULL, element, name, mapset);
  184. }
  185. /*!
  186. * \brief Searches for a file from the mapset search list or in a
  187. * specified mapset. (look but don't touch)
  188. *
  189. * Returns the mapset name where the file was found.
  190. *
  191. *
  192. * \param dir file directory
  193. * \param element database element (eg, "cell", "cellhd", "colr", etc)
  194. * \param name file name to look for
  195. * \param mapset mapset to search. if mapset is "" will search in mapset search list
  196. *
  197. * \return pointer to a string with name of mapset where file was
  198. * found, or NULL if not found
  199. */
  200. const char *G_find_file2_misc(const char *dir,
  201. const char *element,
  202. const char *name, const char *mapset)
  203. {
  204. return find_file(1, dir, element, name, mapset);
  205. }