dSearchPath.cxx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. // Filename: dSearchPath.cxx
  2. // Created by: drose (01Jul00)
  3. //
  4. ////////////////////////////////////////////////////////////////////
  5. //
  6. // PANDA 3D SOFTWARE
  7. // Copyright (c) 2001, 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://www.panda3d.org/license.txt .
  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. 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::Default Constructor
  86. // Access: Public
  87. // Description: Creates an empty search path.
  88. ////////////////////////////////////////////////////////////////////
  89. DSearchPath::
  90. DSearchPath() {
  91. }
  92. ////////////////////////////////////////////////////////////////////
  93. // Function: DSearchPath::Constructor
  94. // Access: Public
  95. // Description:
  96. ////////////////////////////////////////////////////////////////////
  97. DSearchPath::
  98. DSearchPath(const string &path, const string &delimiters) {
  99. append_path(path, delimiters);
  100. }
  101. ////////////////////////////////////////////////////////////////////
  102. // Function: DSearchPath::Copy Constructor
  103. // Access: Public
  104. // Description:
  105. ////////////////////////////////////////////////////////////////////
  106. DSearchPath::
  107. DSearchPath(const DSearchPath &copy) :
  108. _directories(copy._directories)
  109. {
  110. }
  111. ////////////////////////////////////////////////////////////////////
  112. // Function: DSearchPath::Copy Assignment Operator
  113. // Access: Public
  114. // Description:
  115. ////////////////////////////////////////////////////////////////////
  116. void DSearchPath::
  117. operator = (const DSearchPath &copy) {
  118. _directories = copy._directories;
  119. }
  120. ////////////////////////////////////////////////////////////////////
  121. // Function: DSearchPath::Destructor
  122. // Access: Public
  123. // Description:
  124. ////////////////////////////////////////////////////////////////////
  125. DSearchPath::
  126. ~DSearchPath() {
  127. }
  128. ////////////////////////////////////////////////////////////////////
  129. // Function: DSearchPath::clear
  130. // Access: Public
  131. // Description: Removes all the directories from the search list.
  132. ////////////////////////////////////////////////////////////////////
  133. void DSearchPath::
  134. clear() {
  135. _directories.clear();
  136. }
  137. ////////////////////////////////////////////////////////////////////
  138. // Function: DSearchPath::append_directory
  139. // Access: Public
  140. // Description: Adds a new directory to the end of the search list.
  141. ////////////////////////////////////////////////////////////////////
  142. void DSearchPath::
  143. append_directory(const Filename &directory) {
  144. _directories.push_back(directory);
  145. }
  146. ////////////////////////////////////////////////////////////////////
  147. // Function: DSearchPath::prepend_directory
  148. // Access: Public
  149. // Description: Adds a new directory to the front of the search list.
  150. ////////////////////////////////////////////////////////////////////
  151. void DSearchPath::
  152. prepend_directory(const Filename &directory) {
  153. _directories.insert(_directories.begin(), directory);
  154. }
  155. ////////////////////////////////////////////////////////////////////
  156. // Function: DSearchPath::append_path
  157. // Access: Public
  158. // Description: Adds all of the directories listed in the search path
  159. // to the end of the search list.
  160. ////////////////////////////////////////////////////////////////////
  161. void DSearchPath::
  162. append_path(const string &path, const string &delimiters) {
  163. size_t p = 0;
  164. while (p < path.length()) {
  165. size_t q = path.find_first_of(delimiters, p);
  166. if (q == string::npos) {
  167. _directories.push_back(path.substr(p));
  168. return;
  169. }
  170. if (q != p) {
  171. _directories.push_back(path.substr(p, q - p));
  172. }
  173. p = q + 1;
  174. }
  175. }
  176. ////////////////////////////////////////////////////////////////////
  177. // Function: DSearchPath::append_path
  178. // Access: Public
  179. // Description: Adds all of the directories listed in the search path
  180. // to the end of the search list.
  181. ////////////////////////////////////////////////////////////////////
  182. void DSearchPath::
  183. append_path(const DSearchPath &path) {
  184. copy(path._directories.begin(), path._directories.end(),
  185. back_inserter(_directories));
  186. }
  187. ////////////////////////////////////////////////////////////////////
  188. // Function: DSearchPath::prepend_path
  189. // Access: Public
  190. // Description: Adds all of the directories listed in the search path
  191. // to the beginning of the search list.
  192. ////////////////////////////////////////////////////////////////////
  193. void DSearchPath::
  194. prepend_path(const DSearchPath &path) {
  195. if (!path._directories.empty()) {
  196. Directories new_directories = path._directories;
  197. copy(_directories.begin(), _directories.end(),
  198. back_inserter(new_directories));
  199. _directories.swap(new_directories);
  200. }
  201. }
  202. ////////////////////////////////////////////////////////////////////
  203. // Function: DSearchPath::is_empty
  204. // Access: Public
  205. // Description: Returns true if the search list is empty, false
  206. // otherwise.
  207. ////////////////////////////////////////////////////////////////////
  208. bool DSearchPath::
  209. is_empty() const {
  210. return _directories.empty();
  211. }
  212. ////////////////////////////////////////////////////////////////////
  213. // Function: DSearchPath::get_num_directories
  214. // Access: Public
  215. // Description: Returns the number of directories on the search list.
  216. ////////////////////////////////////////////////////////////////////
  217. int DSearchPath::
  218. get_num_directories() const {
  219. return _directories.size();
  220. }
  221. ////////////////////////////////////////////////////////////////////
  222. // Function: DSearchPath::get_directory
  223. // Access: Public
  224. // Description: Returns the nth directory on the search list.
  225. ////////////////////////////////////////////////////////////////////
  226. Filename DSearchPath::
  227. get_directory(int n) const {
  228. assert(n >= 0 && n < (int)_directories.size());
  229. return _directories[n];
  230. }
  231. ////////////////////////////////////////////////////////////////////
  232. // Function: DSearchPath::find_file
  233. // Access: Public
  234. // Description: Searches all the directories in the search list for
  235. // the indicated file, in order. Returns the full
  236. // matching pathname of the first match if found, or the
  237. // empty string if not found.
  238. ////////////////////////////////////////////////////////////////////
  239. Filename DSearchPath::
  240. find_file(const Filename &filename) const {
  241. Directories::const_iterator di;
  242. for (di = _directories.begin(); di != _directories.end(); ++di) {
  243. Filename match((*di), filename);
  244. if (match.exists()) {
  245. return match;
  246. }
  247. }
  248. return string();
  249. }
  250. ////////////////////////////////////////////////////////////////////
  251. // Function: DSearchPath::find_all_files
  252. // Access: Public
  253. // Description: Searches all the directories in the search list for
  254. // the indicated file, in order. Fills up the results
  255. // list with *all* of the matching filenames found, if
  256. // any. Returns the number of matches found.
  257. ////////////////////////////////////////////////////////////////////
  258. int DSearchPath::
  259. find_all_files(const Filename &filename,
  260. DSearchPath::Results &results) const {
  261. results._files.clear();
  262. Directories::const_iterator di;
  263. for (di = _directories.begin(); di != _directories.end(); ++di) {
  264. Filename match((*di), filename);
  265. if (match.exists()) {
  266. results._files.push_back(match);
  267. }
  268. }
  269. return results._files.size();
  270. }
  271. ////////////////////////////////////////////////////////////////////
  272. // Function: DSearchPath::output
  273. // Access: Public
  274. // Description:
  275. ////////////////////////////////////////////////////////////////////
  276. void DSearchPath::
  277. output(ostream &out, const string &separator) const {
  278. if (!_directories.empty()) {
  279. Directories::const_iterator di = _directories.begin();
  280. out << (*di);
  281. ++di;
  282. while (di != _directories.end()) {
  283. out << separator << (*di);
  284. ++di;
  285. }
  286. }
  287. }
  288. ////////////////////////////////////////////////////////////////////
  289. // Function: DSearchPath::write
  290. // Access: Public
  291. // Description:
  292. ////////////////////////////////////////////////////////////////////
  293. void DSearchPath::
  294. write(ostream &out, int indent_level) const {
  295. Directories::const_iterator di;
  296. for (di = _directories.begin(); di != _directories.end(); ++di) {
  297. for (int i = 0; i < indent_level; i++) {
  298. out << ' ';
  299. }
  300. out << (*di) << "\n";
  301. }
  302. }