dSearchPath.cxx 12 KB

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