dSearchPath.cxx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. // Filename: dSearchPath.cxx
  2. // Created by: drose (01Jul00)
  3. //
  4. ////////////////////////////////////////////////////////////////////
  5. //
  6. // PANDA 3D SOFTWARE
  7. // Copyright (c) 2001 - 2004, Disney Enterprises, Inc. All rights reserved
  8. //
  9. // All use of this software is subject to the terms of the Panda 3d
  10. // Software license. You should have received a copy of this license
  11. // along with this source code; you will also find a current copy of
  12. // the license at http://etc.cmu.edu/panda3d/docs/license/ .
  13. //
  14. // To contact the maintainers of this program write to
  15. // [email protected] .
  16. //
  17. ////////////////////////////////////////////////////////////////////
  18. #include "dSearchPath.h"
  19. #include "filename.h"
  20. #include <algorithm>
  21. ////////////////////////////////////////////////////////////////////
  22. // Function: DSearchPath::Results::Constructor
  23. // Access: Public
  24. // Description:
  25. ////////////////////////////////////////////////////////////////////
  26. DSearchPath::Results::
  27. Results() {
  28. }
  29. ////////////////////////////////////////////////////////////////////
  30. // Function: DSearchPath::Results::Copy Constructor
  31. // Access: Public
  32. // Description:
  33. ////////////////////////////////////////////////////////////////////
  34. DSearchPath::Results::
  35. Results(const DSearchPath::Results &copy) :
  36. _files(copy._files)
  37. {
  38. }
  39. ////////////////////////////////////////////////////////////////////
  40. // Function: DSearchPath::Results::Copy Assignment Operator
  41. // Access: Public
  42. // Description:
  43. ////////////////////////////////////////////////////////////////////
  44. void DSearchPath::Results::
  45. operator = (const DSearchPath::Results &copy) {
  46. _files = copy._files;
  47. }
  48. ////////////////////////////////////////////////////////////////////
  49. // Function: DSearchPath::Results::Destructor
  50. // Access: Public
  51. // Description:
  52. ////////////////////////////////////////////////////////////////////
  53. DSearchPath::Results::
  54. ~Results() {
  55. }
  56. ////////////////////////////////////////////////////////////////////
  57. // Function: DSearchPath::Results::clear
  58. // Access: Public
  59. // Description: Removes all the files from the list.
  60. ////////////////////////////////////////////////////////////////////
  61. void DSearchPath::Results::
  62. clear() {
  63. _files.clear();
  64. }
  65. ////////////////////////////////////////////////////////////////////
  66. // Function: DSearchPath::Results::get_num_files
  67. // Access: Public
  68. // Description: Returns the number of files on the result list.
  69. ////////////////////////////////////////////////////////////////////
  70. int DSearchPath::Results::
  71. get_num_files() const {
  72. return _files.size();
  73. }
  74. ////////////////////////////////////////////////////////////////////
  75. // Function: DSearchPath::Results::get_file
  76. // Access: Public
  77. // Description: Returns the nth file on the result list.
  78. ////////////////////////////////////////////////////////////////////
  79. const Filename &DSearchPath::Results::
  80. get_file(int n) const {
  81. assert(n >= 0 && n < (int)_files.size());
  82. return _files[n];
  83. }
  84. ////////////////////////////////////////////////////////////////////
  85. // Function: DSearchPath::Results::add_file
  86. // Access: Public
  87. // Description: Adds a new file to the result list.
  88. ////////////////////////////////////////////////////////////////////
  89. void DSearchPath::Results::
  90. add_file(const Filename &file) {
  91. _files.push_back(file);
  92. }
  93. ////////////////////////////////////////////////////////////////////
  94. // Function: DSearchPath::Default Constructor
  95. // Access: Public
  96. // Description: Creates an empty search path.
  97. ////////////////////////////////////////////////////////////////////
  98. DSearchPath::
  99. DSearchPath() {
  100. }
  101. ////////////////////////////////////////////////////////////////////
  102. // Function: DSearchPath::Constructor
  103. // Access: Public
  104. // Description:
  105. ////////////////////////////////////////////////////////////////////
  106. DSearchPath::
  107. DSearchPath(const string &path, const string &delimiters) {
  108. append_path(path, delimiters);
  109. }
  110. ////////////////////////////////////////////////////////////////////
  111. // Function: DSearchPath::Copy Constructor
  112. // Access: Public
  113. // Description:
  114. ////////////////////////////////////////////////////////////////////
  115. DSearchPath::
  116. DSearchPath(const DSearchPath &copy) :
  117. _directories(copy._directories)
  118. {
  119. }
  120. ////////////////////////////////////////////////////////////////////
  121. // Function: DSearchPath::Copy Assignment Operator
  122. // Access: Public
  123. // Description:
  124. ////////////////////////////////////////////////////////////////////
  125. void DSearchPath::
  126. operator = (const DSearchPath &copy) {
  127. _directories = copy._directories;
  128. }
  129. ////////////////////////////////////////////////////////////////////
  130. // Function: DSearchPath::Destructor
  131. // Access: Public
  132. // Description:
  133. ////////////////////////////////////////////////////////////////////
  134. DSearchPath::
  135. ~DSearchPath() {
  136. }
  137. ////////////////////////////////////////////////////////////////////
  138. // Function: DSearchPath::clear
  139. // Access: Public
  140. // Description: Removes all the directories from the search list.
  141. ////////////////////////////////////////////////////////////////////
  142. void DSearchPath::
  143. clear() {
  144. _directories.clear();
  145. }
  146. ////////////////////////////////////////////////////////////////////
  147. // Function: DSearchPath::append_directory
  148. // Access: Public
  149. // Description: Adds a new directory to the end of the search list.
  150. ////////////////////////////////////////////////////////////////////
  151. void DSearchPath::
  152. append_directory(const Filename &directory) {
  153. _directories.push_back(directory);
  154. }
  155. ////////////////////////////////////////////////////////////////////
  156. // Function: DSearchPath::prepend_directory
  157. // Access: Public
  158. // Description: Adds a new directory to the front of the search list.
  159. ////////////////////////////////////////////////////////////////////
  160. void DSearchPath::
  161. prepend_directory(const Filename &directory) {
  162. _directories.insert(_directories.begin(), directory);
  163. }
  164. ////////////////////////////////////////////////////////////////////
  165. // Function: DSearchPath::append_path
  166. // Access: Public
  167. // Description: Adds all of the directories listed in the search path
  168. // to the end of the search list.
  169. ////////////////////////////////////////////////////////////////////
  170. void DSearchPath::
  171. append_path(const string &path, const string &delimiters) {
  172. size_t p = 0;
  173. while (p < path.length()) {
  174. size_t q = path.find_first_of(delimiters, p);
  175. if (q == string::npos) {
  176. _directories.push_back(path.substr(p));
  177. return;
  178. }
  179. if (q != p) {
  180. _directories.push_back(path.substr(p, q - p));
  181. }
  182. p = q + 1;
  183. }
  184. }
  185. ////////////////////////////////////////////////////////////////////
  186. // Function: DSearchPath::append_path
  187. // Access: Public
  188. // Description: Adds all of the directories listed in the search path
  189. // to the end of the search list.
  190. ////////////////////////////////////////////////////////////////////
  191. void DSearchPath::
  192. append_path(const DSearchPath &path) {
  193. copy(path._directories.begin(), path._directories.end(),
  194. back_inserter(_directories));
  195. }
  196. ////////////////////////////////////////////////////////////////////
  197. // Function: DSearchPath::prepend_path
  198. // Access: Public
  199. // Description: Adds all of the directories listed in the search path
  200. // to the beginning of the search list.
  201. ////////////////////////////////////////////////////////////////////
  202. void DSearchPath::
  203. prepend_path(const DSearchPath &path) {
  204. if (!path._directories.empty()) {
  205. Directories new_directories = path._directories;
  206. copy(_directories.begin(), _directories.end(),
  207. back_inserter(new_directories));
  208. _directories.swap(new_directories);
  209. }
  210. }
  211. ////////////////////////////////////////////////////////////////////
  212. // Function: DSearchPath::is_empty
  213. // Access: Public
  214. // Description: Returns true if the search list is empty, false
  215. // otherwise.
  216. ////////////////////////////////////////////////////////////////////
  217. bool DSearchPath::
  218. is_empty() const {
  219. return _directories.empty();
  220. }
  221. ////////////////////////////////////////////////////////////////////
  222. // Function: DSearchPath::get_num_directories
  223. // Access: Public
  224. // Description: Returns the number of directories on the search list.
  225. ////////////////////////////////////////////////////////////////////
  226. int DSearchPath::
  227. get_num_directories() const {
  228. return _directories.size();
  229. }
  230. ////////////////////////////////////////////////////////////////////
  231. // Function: DSearchPath::get_directory
  232. // Access: Public
  233. // Description: Returns the nth directory on the search list.
  234. ////////////////////////////////////////////////////////////////////
  235. const Filename &DSearchPath::
  236. get_directory(int n) const {
  237. assert(n >= 0 && n < (int)_directories.size());
  238. return _directories[n];
  239. }
  240. ////////////////////////////////////////////////////////////////////
  241. // Function: DSearchPath::find_file
  242. // Access: Public
  243. // Description: Searches all the directories in the search list for
  244. // the indicated file, in order. Returns the full
  245. // matching pathname of the first match if found, or the
  246. // empty string if not found.
  247. ////////////////////////////////////////////////////////////////////
  248. Filename DSearchPath::
  249. find_file(const Filename &filename) const {
  250. if (filename.is_local()) {
  251. Directories::const_iterator di;
  252. for (di = _directories.begin(); di != _directories.end(); ++di) {
  253. Filename match((*di), filename);
  254. if (match.exists()) {
  255. if ((*di) == "." && filename.is_fully_qualified()) {
  256. // A special case for the "." directory: to avoid prefixing
  257. // an endless stream of ./ in front of files, if the
  258. // filename already has a ./ prefixed
  259. // (i.e. is_fully_qualified() is true), we don't
  260. // prefix another one.
  261. return filename;
  262. } else {
  263. return match;
  264. }
  265. }
  266. }
  267. }
  268. return string();
  269. }
  270. ////////////////////////////////////////////////////////////////////
  271. // Function: DSearchPath::find_all_files
  272. // Access: Public
  273. // Description: Searches all the directories in the search list for
  274. // the indicated file, in order. Fills up the results
  275. // list with *all* of the matching filenames found, if
  276. // any. Returns the number of matches found.
  277. //
  278. // It is the responsibility of the the caller to clear
  279. // the results list first; otherwise, the newly-found
  280. // files will be appended to the list.
  281. ////////////////////////////////////////////////////////////////////
  282. int DSearchPath::
  283. find_all_files(const Filename &filename,
  284. DSearchPath::Results &results) const {
  285. int num_added = 0;
  286. if (filename.is_local()) {
  287. Directories::const_iterator di;
  288. for (di = _directories.begin(); di != _directories.end(); ++di) {
  289. Filename match((*di), filename);
  290. if (match.exists()) {
  291. if ((*di) == "." && filename.is_fully_qualified()) {
  292. // A special case for the "." directory: to avoid prefixing
  293. // an endless stream of ./ in front of files, if the
  294. // filename already has a ./ prefixed
  295. // (i.e. is_fully_qualified() is true), we don't
  296. // prefix another one.
  297. results.add_file(filename);
  298. } else {
  299. results.add_file(match);
  300. }
  301. num_added++;
  302. }
  303. }
  304. }
  305. return num_added;
  306. }
  307. ////////////////////////////////////////////////////////////////////
  308. // Function: DSearchPath::output
  309. // Access: Public
  310. // Description:
  311. ////////////////////////////////////////////////////////////////////
  312. void DSearchPath::
  313. output(ostream &out, const string &separator) const {
  314. if (!_directories.empty()) {
  315. Directories::const_iterator di = _directories.begin();
  316. out << (*di);
  317. ++di;
  318. while (di != _directories.end()) {
  319. out << separator << (*di);
  320. ++di;
  321. }
  322. }
  323. }
  324. ////////////////////////////////////////////////////////////////////
  325. // Function: DSearchPath::write
  326. // Access: Public
  327. // Description:
  328. ////////////////////////////////////////////////////////////////////
  329. void DSearchPath::
  330. write(ostream &out, int indent_level) const {
  331. Directories::const_iterator di;
  332. for (di = _directories.begin(); di != _directories.end(); ++di) {
  333. for (int i = 0; i < indent_level; i++) {
  334. out << ' ';
  335. }
  336. out << (*di) << "\n";
  337. }
  338. }