find_file.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*!
  2. \file lib/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_element(int misc, const char *dir, const char *element)
  16. {
  17. static const char *cell_elements[] = {
  18. "cellhd",
  19. "cell",
  20. "cats",
  21. "colr",
  22. "hist",
  23. "cell_misc",
  24. "fcell",
  25. "g3dcell",
  26. NULL
  27. };
  28. static const char *dig_elements[] = {
  29. "dig",
  30. "dig_att",
  31. "dig_plus",
  32. "dig_cats",
  33. "dig_misc",
  34. "reg",
  35. NULL
  36. };
  37. const char *search = misc ? dir : element;
  38. int i;
  39. for (i = 1; cell_elements[i]; i++)
  40. if (strcmp(search, cell_elements[i]) == 0)
  41. return cell_elements[0];
  42. for (i = 1; dig_elements[i]; i++)
  43. if (strcmp(search, dig_elements[i]) == 0)
  44. return dig_elements[0];
  45. return element;
  46. }
  47. static const char *find_file(int misc, const char *dir,
  48. const char *element, const char *name,
  49. const char *mapset)
  50. {
  51. char path[GPATH_MAX];
  52. char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
  53. const char *pname, *pmapset;
  54. int n;
  55. if (*name == 0)
  56. return NULL;
  57. *path = 0;
  58. /*
  59. * if name is in the fully qualified format, split it into
  60. * name, mapset (overrides what was in mapset)
  61. */
  62. if (G_name_is_fully_qualified(name, xname, xmapset)) {
  63. pname = xname;
  64. pmapset = xmapset;
  65. }
  66. else {
  67. pname = name;
  68. pmapset = mapset;
  69. }
  70. if (strcmp(element, "vector") == 0 &&
  71. pmapset && strcasecmp(pmapset, "ogr") == 0) {
  72. /* don't check for virtual OGR mapset */
  73. return G_store(pmapset);
  74. }
  75. /*
  76. * reject illegal names and mapsets
  77. */
  78. if (G_legal_filename(pname) == -1)
  79. return NULL;
  80. if (pmapset && *pmapset && G_legal_filename(pmapset) == -1)
  81. return NULL;
  82. /*
  83. * if no specific mapset is to be searched
  84. * then search all mapsets in the mapset search list
  85. */
  86. if (pmapset == NULL || *pmapset == 0) {
  87. int cnt = 0;
  88. const char *pselmapset = NULL;
  89. const char *pelement = find_element(misc, dir, element);
  90. for (n = 0; (pmapset = G_get_mapset_name(n)); n++) {
  91. if (misc && element == pelement)
  92. G_file_name_misc(path, dir, pelement, pname, pmapset);
  93. else
  94. G_file_name(path, pelement, pname, pmapset);
  95. if (access(path, 0) == 0) {
  96. if (!pselmapset)
  97. pselmapset = pmapset;
  98. else if (element == pelement)
  99. G_warning(_("'%s/%s' was found in more mapsets (also found in <%s>)"),
  100. element, pname, pmapset);
  101. cnt++;
  102. }
  103. }
  104. if (cnt > 0) {
  105. if (misc)
  106. G_file_name_misc(path, dir, element, pname, pselmapset);
  107. else
  108. G_file_name(path, element, name, pselmapset);
  109. if (access(path, 0) == 0) {
  110. /* If the same name exists in more mapsets and print a warning */
  111. if (cnt > 1 && element == pelement)
  112. G_warning(_("Using <%s@%s>"),
  113. pname, pselmapset);
  114. return G_store(pselmapset);
  115. }
  116. }
  117. }
  118. /*
  119. * otherwise just look for the file in the specified mapset.
  120. * since the name may have been qualified, mapset may point
  121. * to the xmapset, so we must should it to
  122. * permanent storage via G_store().
  123. */
  124. else {
  125. if (misc)
  126. G_file_name_misc(path, dir, element, pname, pmapset);
  127. else
  128. G_file_name(path, element, pname, pmapset);
  129. if (access(path, 0) == 0)
  130. return G_store(pmapset);
  131. }
  132. return NULL;
  133. }
  134. static const char *find_file1(
  135. int misc,
  136. const char *dir,
  137. const char *element, char *name, const char *mapset)
  138. {
  139. char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
  140. const char *pname, *pmapset;
  141. const char *mp;
  142. if (G_name_is_fully_qualified(name, xname, xmapset)) {
  143. pname = xname;
  144. pmapset = xmapset;
  145. }
  146. else {
  147. pname = name;
  148. pmapset = mapset;
  149. }
  150. mp = find_file(misc, dir, element, pname, pmapset);
  151. if (mp && name != pname)
  152. strcpy(name, pname);
  153. return mp;
  154. }
  155. /*!
  156. * \brief Searches for a file from the mapset search list or in a
  157. * specified mapset.
  158. *
  159. * Returns the mapset name where the file was found.
  160. *
  161. * If the user specifies a fully qualified element (name@mapset)
  162. * which exists, then G_find_file() modifies "name"
  163. * by removing the "@mapset" part.
  164. *
  165. * Rejects all names that begin with "."
  166. *
  167. * If <i>name</i> is of the form nnn in ppp then only mapset ppp
  168. * is searched.
  169. *
  170. * \param element database element (eg, "cell", "cellhd", "colr", etc)
  171. * \param name file name to look for
  172. * \param mapset mapset to search. if mapset is "" will search in mapset search list
  173. *
  174. * \return pointer to a string with name of mapset where file was
  175. * found, or NULL if not found
  176. */
  177. const char *G_find_file(const char *element, char *name, const char *mapset)
  178. {
  179. return find_file1(0, NULL, element, name, mapset);
  180. }
  181. /*!
  182. * \brief Searches for a file from the mapset search list or in a
  183. * specified mapset.
  184. *
  185. * Returns the mapset name where the file was found.
  186. *
  187. * \param dir file directory
  188. * \param element database element (eg, "cell", "cellhd", "colr", etc)
  189. * \param name file name to look for
  190. * \param mapset mapset to search. if mapset is "" will search in mapset search list
  191. *
  192. * \return pointer to a string with name of mapset where file was
  193. * found, or NULL if not found
  194. */
  195. const char *G_find_file_misc(const char *dir,
  196. const char *element, char *name, const char *mapset)
  197. {
  198. return find_file1(1, dir, element, name, mapset);
  199. }
  200. /*!
  201. * \brief Searches for a file from the mapset search list or in a
  202. * specified mapset. (look but don't touch)
  203. *
  204. * Returns the mapset name where the file was found.
  205. *
  206. * Exactly the same as G_find_file() except that if <i>name</i> is in
  207. * the form "<i>name@mapset</i>", and is found, G_find_file2() will
  208. * not alter <i>name</i> by removing the "@<i>mapset</i>" part.
  209. *
  210. * Rejects all names that begin with "."
  211. *
  212. * \param element database element (eg, "cell", "cellhd", "colr", etc)
  213. * \param name file name to look for
  214. * \param mapset mapset to search. if mapset is "" will search in mapset search list
  215. *
  216. * \return pointer to a string with name of mapset where file was
  217. * found, or NULL if not found
  218. */
  219. const char *G_find_file2(const char *element, const char *name, const char *mapset)
  220. {
  221. return find_file(0, NULL, element, name, mapset);
  222. }
  223. /*!
  224. * \brief Searches for a file from the mapset search list or in a
  225. * specified mapset. (look but don't touch)
  226. *
  227. * Returns the mapset name where the file was found.
  228. *
  229. *
  230. * \param dir file directory
  231. * \param element database element (eg, "cell", "cellhd", "colr", etc)
  232. * \param name file name to look for
  233. * \param mapset mapset to search. if mapset is "" will search in mapset search list
  234. *
  235. * \return pointer to a string with name of mapset where file was
  236. * found, or NULL if not found
  237. */
  238. const char *G_find_file2_misc(const char *dir,
  239. const char *element,
  240. const char *name, const char *mapset)
  241. {
  242. return find_file(1, dir, element, name, mapset);
  243. }