find_file.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. #include <string.h>
  2. #include <unistd.h>
  3. #include <grass/gis.h>
  4. static char *find_file(int misc,
  5. const char *dir,
  6. const char *element,
  7. const char *name, const char *mapset)
  8. {
  9. char path[GPATH_MAX];
  10. char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
  11. const char *pname, *pmapset;
  12. int n;
  13. if (*name == 0)
  14. return NULL;
  15. *path = 0;
  16. /*
  17. * if name is in the fully qualified format, split it into
  18. * name, mapset (overrides what was in mapset)
  19. */
  20. if (G__name_is_fully_qualified(name, xname, xmapset)) {
  21. pname = xname;
  22. pmapset = xmapset;
  23. }
  24. else {
  25. pname = name;
  26. pmapset = mapset;
  27. }
  28. /*
  29. * reject illegal names and mapsets
  30. */
  31. if (G_legal_filename(pname) == -1)
  32. return NULL;
  33. if (pmapset && *pmapset && G_legal_filename(pmapset) == -1)
  34. return NULL;
  35. /*
  36. * if no specific mapset is to be searched
  37. * then search all mapsets in the mapset search list
  38. */
  39. if (pmapset == NULL || *pmapset == 0) {
  40. int cnt = 0;
  41. const char *pselmapset = NULL;
  42. for (n = 0; (pmapset = G__mapset_name(n)); n++) {
  43. if (misc)
  44. G__file_name_misc(path, dir, element, pname, pmapset);
  45. else
  46. G__file_name(path, element, pname, pmapset);
  47. if (access(path, 0) == 0) {
  48. if (!pselmapset)
  49. pselmapset = pmapset;
  50. else
  51. G_warning
  52. ("'%s/%s' was found in more mapsets (also found in %s).",
  53. element, pname, pmapset);
  54. cnt++;
  55. }
  56. }
  57. if (cnt > 0) {
  58. /* If the same name exists in more mapsets and print a warning */
  59. if (cnt > 1)
  60. G_warning("using '%s@%s'.", pname, pselmapset);
  61. return (char *)pselmapset;
  62. }
  63. }
  64. /*
  65. * otherwise just look for the file in the specified mapset.
  66. * since the name may have been qualified, mapset may point
  67. * to the xmapset, so we must should it to
  68. * permanent storage via G_store().
  69. */
  70. else {
  71. if (misc)
  72. G__file_name_misc(path, dir, element, pname, pmapset);
  73. else
  74. G__file_name(path, element, pname, pmapset);
  75. if (access(path, 0) == 0)
  76. return G_store(pmapset);
  77. }
  78. return NULL;
  79. }
  80. static char *find_file1(int misc,
  81. const char *dir,
  82. const char *element, char *name, const char *mapset)
  83. {
  84. char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
  85. const char *pname, *pmapset;
  86. char *mp;
  87. if (G__name_is_fully_qualified(name, xname, xmapset)) {
  88. pname = xname;
  89. pmapset = xmapset;
  90. }
  91. else {
  92. pname = name;
  93. pmapset = mapset;
  94. }
  95. mp = find_file(misc, dir, element, pname, pmapset);
  96. if (mp && name != pname)
  97. strcpy(name, pname);
  98. return mp;
  99. }
  100. /*!
  101. * \brief searches for a file from the mapset search list
  102. * or in a specified mapset.
  103. * returns the mapset name where the file was found.
  104. *
  105. * notes:
  106. *
  107. * If the user specifies a fully qualified element (<i>name@mapset</i>)
  108. * which exists, then <i>G_find_file()</i> modifies <b>name</b>
  109. * by removing the "@<i>mapset</i>" part.
  110. *
  111. * Rejects all names that begin with "."
  112. *
  113. * If <b>name</b> is of the form nnn in ppp then only mapset ppp
  114. * is searched.
  115. *
  116. * \param const char *element database element (eg, "cell", "cellhd", "colr", etc)
  117. * \param char *name file name to look for
  118. * \param const char *mapset mapset to search. if mapset is ""
  119. * will search in mapset search list
  120. *
  121. * \return char * pointer to a string with name of mapset
  122. * where file was found, or NULL if not found
  123. */
  124. char *G_find_file(const char *element, char *name, const char *mapset)
  125. {
  126. return find_file1(0, NULL, element, name, mapset);
  127. }
  128. char *G_find_file_misc(const char *dir,
  129. const char *element, char *name, const char *mapset)
  130. {
  131. return find_file1(1, dir, element, name, mapset);
  132. }
  133. /*!
  134. * \brief searches for a file from the mapset search list
  135. * or in a specified mapset. (look but don't touch)
  136. * returns the mapset name where the file was found.
  137. *
  138. * Exactly the same as G_find_file() except that if <b>name</b> is in
  139. * the form "<i>name@mapset</i>", and is found, G_find_file2() will not
  140. * alter <b>name</b> by removing the "@<i>mapset</i>" part.
  141. *
  142. * note:
  143. * rejects all names that begin with "."
  144. *
  145. * \param char *element database element (eg, "cell", "cellhd", "colr", etc)
  146. * \param char *name file name to look for
  147. * \param char *mapset mapset to search. if mapset is ""
  148. * will search in mapset search list
  149. *
  150. * \return char * pointer to a string with name of mapset
  151. * where file was found, or NULL if not found
  152. */
  153. char *G_find_file2(const char *element, const char *name, const char *mapset)
  154. {
  155. return find_file(0, NULL, element, name, mapset);
  156. }
  157. char *G_find_file2_misc(const char *dir,
  158. const char *element,
  159. const char *name, const char *mapset)
  160. {
  161. return find_file(1, dir, element, name, mapset);
  162. }