find_file.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*!
  2. \file find_file.c
  3. \brief GIS library - find GRASS data base files
  4. (C) 2001-2008 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 char *find_file(int misc,
  16. const char *dir,
  17. const char *element,
  18. 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. /*
  40. * reject illegal names and mapsets
  41. */
  42. if (G_legal_filename(pname) == -1)
  43. return NULL;
  44. if (pmapset && *pmapset && G_legal_filename(pmapset) == -1)
  45. return NULL;
  46. /*
  47. * if no specific mapset is to be searched
  48. * then search all mapsets in the mapset search list
  49. */
  50. if (pmapset == NULL || *pmapset == 0) {
  51. int cnt = 0;
  52. const char *pselmapset = NULL;
  53. for (n = 0; (pmapset = G__mapset_name(n)); n++) {
  54. if (misc)
  55. G__file_name_misc(path, dir, element, pname, pmapset);
  56. else
  57. G__file_name(path, element, pname, pmapset);
  58. if (access(path, 0) == 0) {
  59. if (!pselmapset)
  60. pselmapset = pmapset;
  61. else
  62. G_warning(_("'%s/%s' was found in more mapsets (also found in <%s>)"),
  63. element, pname, pmapset);
  64. cnt++;
  65. }
  66. }
  67. if (cnt > 0) {
  68. /* If the same name exists in more mapsets and print a warning */
  69. if (cnt > 1)
  70. G_warning(_("Using <%s@%s>"),
  71. pname, pselmapset);
  72. return (char *)pselmapset;
  73. }
  74. }
  75. /*
  76. * otherwise just look for the file in the specified mapset.
  77. * since the name may have been qualified, mapset may point
  78. * to the xmapset, so we must should it to
  79. * permanent storage via G_store().
  80. */
  81. else {
  82. if (misc)
  83. G__file_name_misc(path, dir, element, pname, pmapset);
  84. else
  85. G__file_name(path, element, pname, pmapset);
  86. if (access(path, 0) == 0)
  87. return G_store(pmapset);
  88. }
  89. return NULL;
  90. }
  91. static char *find_file1(int misc,
  92. const char *dir,
  93. const char *element, char *name, const char *mapset)
  94. {
  95. char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
  96. const char *pname, *pmapset;
  97. char *mp;
  98. if (G__name_is_fully_qualified(name, xname, xmapset)) {
  99. pname = xname;
  100. pmapset = xmapset;
  101. }
  102. else {
  103. pname = name;
  104. pmapset = mapset;
  105. }
  106. mp = find_file(misc, dir, element, pname, pmapset);
  107. if (mp && name != pname)
  108. strcpy(name, pname);
  109. return mp;
  110. }
  111. /*!
  112. * \brief searches for a file from the mapset search list
  113. * or in a specified mapset.
  114. * returns the mapset name where the file was found.
  115. *
  116. * notes:
  117. *
  118. * If the user specifies a fully qualified element (<i>name@mapset</i>)
  119. * which exists, then <i>G_find_file()</i> modifies <b>name</b>
  120. * by removing the "@<i>mapset</i>" part.
  121. *
  122. * Rejects all names that begin with "."
  123. *
  124. * If <b>name</b> is of the form nnn in ppp then only mapset ppp
  125. * is searched.
  126. *
  127. * \param const char *element database element (eg, "cell", "cellhd", "colr", etc)
  128. * \param char *name file name to look for
  129. * \param const char *mapset mapset to search. if mapset is ""
  130. * will search in mapset search list
  131. *
  132. * \return char * pointer to a string with name of mapset
  133. * where file was found, or NULL if not found
  134. */
  135. char *G_find_file(const char *element, char *name, const char *mapset)
  136. {
  137. return find_file1(0, NULL, element, name, mapset);
  138. }
  139. char *G_find_file_misc(const char *dir,
  140. const char *element, char *name, const char *mapset)
  141. {
  142. return find_file1(1, dir, element, name, mapset);
  143. }
  144. /*!
  145. * \brief searches for a file from the mapset search list
  146. * or in a specified mapset. (look but don't touch)
  147. * returns the mapset name where the file was found.
  148. *
  149. * Exactly the same as G_find_file() except that if <b>name</b> is in
  150. * the form "<i>name@mapset</i>", and is found, G_find_file2() will not
  151. * alter <b>name</b> by removing the "@<i>mapset</i>" part.
  152. *
  153. * note:
  154. * rejects all names that begin with "."
  155. *
  156. * \param char *element database element (eg, "cell", "cellhd", "colr", etc)
  157. * \param char *name file name to look for
  158. * \param char *mapset mapset to search. if mapset is ""
  159. * will search in mapset search list
  160. *
  161. * \return char * pointer to a string with name of mapset
  162. * where file was found, or NULL if not found
  163. */
  164. char *G_find_file2(const char *element, const char *name, const char *mapset)
  165. {
  166. return find_file(0, NULL, element, name, mapset);
  167. }
  168. char *G_find_file2_misc(const char *dir,
  169. const char *element,
  170. const char *name, const char *mapset)
  171. {
  172. return find_file(1, dir, element, name, mapset);
  173. }