filename.I 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. // Filename: filename.I
  2. // Created by: drose (18Jan99)
  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. ////////////////////////////////////////////////////////////////////
  19. // Function: Filename::Constructor
  20. // Access: Public
  21. // Description:
  22. ////////////////////////////////////////////////////////////////////
  23. INLINE Filename::
  24. Filename(const string &filename) {
  25. (*this) = filename;
  26. _flags = 0;
  27. }
  28. ////////////////////////////////////////////////////////////////////
  29. // Function: Filename::Constructor
  30. // Access: Public
  31. // Description:
  32. ////////////////////////////////////////////////////////////////////
  33. INLINE Filename::
  34. Filename(const char *filename) {
  35. (*this) = filename;
  36. _flags = 0;
  37. }
  38. ////////////////////////////////////////////////////////////////////
  39. // Function: Filename::Copy Constructor
  40. // Access: Public
  41. // Description:
  42. ////////////////////////////////////////////////////////////////////
  43. INLINE Filename::
  44. Filename(const Filename &copy)
  45. : _filename(copy._filename),
  46. _dirname_end(copy._dirname_end),
  47. _basename_start(copy._basename_start),
  48. _basename_end(copy._basename_end),
  49. _extension_start(copy._extension_start),
  50. _flags(copy._flags)
  51. {
  52. }
  53. ////////////////////////////////////////////////////////////////////
  54. // Function: Filename::text_filename named constructor
  55. // Access: Public
  56. // Description:
  57. ////////////////////////////////////////////////////////////////////
  58. INLINE Filename Filename::
  59. text_filename(const string &filename) {
  60. Filename result(filename);
  61. result.set_text();
  62. return result;
  63. }
  64. ////////////////////////////////////////////////////////////////////
  65. // Function: Filename::binary_filename named constructor
  66. // Access: Public
  67. // Description:
  68. ////////////////////////////////////////////////////////////////////
  69. INLINE Filename Filename::
  70. binary_filename(const string &filename) {
  71. Filename result(filename);
  72. result.set_binary();
  73. return result;
  74. }
  75. ////////////////////////////////////////////////////////////////////
  76. // Function: Filename::dso_filename named constructor
  77. // Access: Public
  78. // Description:
  79. ////////////////////////////////////////////////////////////////////
  80. INLINE Filename Filename::
  81. dso_filename(const string &filename) {
  82. Filename result(filename);
  83. result.set_type(T_dso);
  84. return result;
  85. }
  86. ////////////////////////////////////////////////////////////////////
  87. // Function: Filename::executable_filename named constructor
  88. // Access: Public
  89. // Description:
  90. ////////////////////////////////////////////////////////////////////
  91. INLINE Filename Filename::
  92. executable_filename(const string &filename) {
  93. Filename result(filename);
  94. result.set_type(T_executable);
  95. return result;
  96. }
  97. ////////////////////////////////////////////////////////////////////
  98. // Function: Filename::Destructor
  99. // Access: Public
  100. // Description:
  101. ////////////////////////////////////////////////////////////////////
  102. INLINE Filename::
  103. ~Filename() {
  104. }
  105. ////////////////////////////////////////////////////////////////////
  106. // Function: Filename::Assignment operator
  107. // Access: Public
  108. // Description:
  109. ////////////////////////////////////////////////////////////////////
  110. INLINE Filename &Filename::
  111. operator = (const string &filename) {
  112. _filename = filename;
  113. locate_basename();
  114. locate_extension();
  115. return *this;
  116. }
  117. ////////////////////////////////////////////////////////////////////
  118. // Function: Filename::Assignment operator
  119. // Access: Public
  120. // Description:
  121. ////////////////////////////////////////////////////////////////////
  122. INLINE Filename &Filename::
  123. operator = (const char *filename) {
  124. assert(filename != NULL);
  125. return (*this) = string(filename);
  126. }
  127. ////////////////////////////////////////////////////////////////////
  128. // Function: Filename::Copy assignment operator
  129. // Access: Public
  130. // Description:
  131. ////////////////////////////////////////////////////////////////////
  132. INLINE Filename &Filename::
  133. operator = (const Filename &copy) {
  134. _filename = copy._filename;
  135. _dirname_end = copy._dirname_end;
  136. _basename_start = copy._basename_start;
  137. _basename_end = copy._basename_end;
  138. _extension_start = copy._extension_start;
  139. _flags = copy._flags;
  140. return *this;
  141. }
  142. ////////////////////////////////////////////////////////////////////
  143. // Function: Filename::string typecast operator
  144. // Access: Public
  145. // Description:
  146. ////////////////////////////////////////////////////////////////////
  147. INLINE Filename::
  148. operator const string & () const {
  149. return _filename;
  150. }
  151. ////////////////////////////////////////////////////////////////////
  152. // Function: Filename::c_str
  153. // Access: Public
  154. // Description:
  155. ////////////////////////////////////////////////////////////////////
  156. INLINE const char *Filename::
  157. c_str() const {
  158. return _filename.c_str();
  159. }
  160. ////////////////////////////////////////////////////////////////////
  161. // Function: Filename::empty
  162. // Access: Public
  163. // Description:
  164. ////////////////////////////////////////////////////////////////////
  165. INLINE bool Filename::
  166. empty() const {
  167. return _filename.empty();
  168. }
  169. ////////////////////////////////////////////////////////////////////
  170. // Function: Filename::length
  171. // Access: Public
  172. // Description:
  173. ////////////////////////////////////////////////////////////////////
  174. INLINE size_t Filename::
  175. length() const {
  176. return _filename.length();
  177. }
  178. ////////////////////////////////////////////////////////////////////
  179. // Function: Filename::Indexing operator
  180. // Access: Public
  181. // Description:
  182. ////////////////////////////////////////////////////////////////////
  183. INLINE char Filename::
  184. operator [] (int n) const {
  185. assert(n >= 0 && n < (int)_filename.length());
  186. return _filename[n];
  187. }
  188. ////////////////////////////////////////////////////////////////////
  189. // Function: Filename::get_fullpath
  190. // Access: Public
  191. // Description: Returns the entire filename: directory, basename,
  192. // extension. This is the same thing returned by the
  193. // string typecast operator, so this function is a
  194. // little redundant.
  195. ////////////////////////////////////////////////////////////////////
  196. INLINE string Filename::
  197. get_fullpath() const {
  198. return _filename;
  199. }
  200. ////////////////////////////////////////////////////////////////////
  201. // Function: Filename::get_dirname
  202. // Access: Public
  203. // Description: Returns the directory part of the filename. This is
  204. // everything in the filename up to, but not including
  205. // the rightmost slash.
  206. ////////////////////////////////////////////////////////////////////
  207. INLINE string Filename::
  208. get_dirname() const {
  209. return _filename.substr(0, _dirname_end);
  210. }
  211. ////////////////////////////////////////////////////////////////////
  212. // Function: Filename::get_basename
  213. // Access: Public
  214. // Description: Returns the basename part of the filename. This is
  215. // everything in the filename after the rightmost slash,
  216. // including any extensions.
  217. ////////////////////////////////////////////////////////////////////
  218. INLINE string Filename::
  219. get_basename() const {
  220. return _filename.substr(_basename_start);
  221. }
  222. ////////////////////////////////////////////////////////////////////
  223. // Function: Filename::get_fullpath_wo_extension
  224. // Access: Public
  225. // Description: Returns the full filename--directory and basename
  226. // parts--except for the extension.
  227. ////////////////////////////////////////////////////////////////////
  228. INLINE string Filename::
  229. get_fullpath_wo_extension() const {
  230. return _filename.substr(0, _basename_end);
  231. }
  232. ////////////////////////////////////////////////////////////////////
  233. // Function: Filename::get_basename_wo_extension
  234. // Access: Public
  235. // Description: Returns the basename part of the filename, without
  236. // the file extension.
  237. ////////////////////////////////////////////////////////////////////
  238. INLINE string Filename::
  239. get_basename_wo_extension() const {
  240. if (_basename_end == string::npos) {
  241. return _filename.substr(_basename_start);
  242. } else {
  243. return _filename.substr(_basename_start, _basename_end - _basename_start);
  244. }
  245. }
  246. ////////////////////////////////////////////////////////////////////
  247. // Function: Filename::get_extension
  248. // Access: Public
  249. // Description: Returns the file extension. This is everything after
  250. // the rightmost dot, if there is one, or the empty
  251. // string if there is not.
  252. ////////////////////////////////////////////////////////////////////
  253. INLINE string Filename::
  254. get_extension() const {
  255. if (_extension_start == string::npos) {
  256. return string();
  257. } else {
  258. return _filename.substr(_extension_start);
  259. }
  260. }
  261. ////////////////////////////////////////////////////////////////////
  262. // Function: Filename::set_binary
  263. // Access: Public
  264. // Description: Indicates that the filename represents a binary file.
  265. // This is primarily relevant to the read_file() and
  266. // write_file() methods, so they can set the appropriate
  267. // flags to the OS.
  268. ////////////////////////////////////////////////////////////////////
  269. INLINE void Filename::
  270. set_binary() {
  271. _flags = (_flags & ~F_text) | F_binary;
  272. }
  273. ////////////////////////////////////////////////////////////////////
  274. // Function: Filename::set_text
  275. // Access: Public
  276. // Description: Indicates that the filename represents a text file.
  277. // This is primarily relevant to the read_file() and
  278. // write_file() methods, so they can set the appropriate
  279. // flags to the OS.
  280. ////////////////////////////////////////////////////////////////////
  281. INLINE void Filename::
  282. set_text() {
  283. _flags = (_flags & ~F_binary) | F_text;
  284. }
  285. ////////////////////////////////////////////////////////////////////
  286. // Function: Filename::is_binary
  287. // Access: Public
  288. // Description: Returns true if the Filename has been indicated to
  289. // represent a binary file via a previous call to
  290. // set_binary(). It is possible that neither
  291. // is_binary() nor is_text() will be true, if neither
  292. // set_binary() nor set_text() was ever called.
  293. ////////////////////////////////////////////////////////////////////
  294. INLINE bool Filename::
  295. is_binary() const {
  296. return ((_flags & F_binary) != 0);
  297. }
  298. ////////////////////////////////////////////////////////////////////
  299. // Function: Filename::is_text
  300. // Access: Public
  301. // Description: Returns true if the Filename has been indicated to
  302. // represent a text file via a previous call to
  303. // set_text(). It is possible that neither is_binary()
  304. // nor is_text() will be true, if neither set_binary()
  305. // nor set_text() was ever called.
  306. ////////////////////////////////////////////////////////////////////
  307. INLINE bool Filename::
  308. is_text() const {
  309. return ((_flags & F_text) != 0);
  310. }
  311. ////////////////////////////////////////////////////////////////////
  312. // Function: Filename::set_type
  313. // Access: Public
  314. // Description: Sets the type of the file represented by the
  315. // filename. This is useful for to_os_specific(),
  316. // resolve_filename(), test_existence(), and all such
  317. // real-world access functions. It helps the Filename
  318. // know how to map the internal filename to the
  319. // OS-specific filename (for instance, maybe executables
  320. // should have an .exe extension).
  321. ////////////////////////////////////////////////////////////////////
  322. INLINE void Filename::
  323. set_type(Filename::Type type) {
  324. _flags = (_flags & ~F_type) | type;
  325. switch (type) {
  326. case T_dso:
  327. case T_executable:
  328. set_binary();
  329. case T_general:
  330. break;
  331. }
  332. }
  333. ////////////////////////////////////////////////////////////////////
  334. // Function: Filename::get_type
  335. // Access: Public
  336. // Description: Returns the type of the file represented by the
  337. // filename, as previously set by set_type().
  338. ////////////////////////////////////////////////////////////////////
  339. INLINE Filename::Type Filename::
  340. get_type() const {
  341. return (Type)(_flags & (int)F_type);
  342. }
  343. ////////////////////////////////////////////////////////////////////
  344. // Function: Filename::is_local
  345. // Access: Public
  346. // Description: Returns true if the filename is local, e.g. does not
  347. // begin with a slash, or false if the filename is fully
  348. // specified from the root.
  349. ////////////////////////////////////////////////////////////////////
  350. INLINE bool Filename::
  351. is_local() const {
  352. return _filename.empty() || _filename[0] != '/';
  353. }
  354. ////////////////////////////////////////////////////////////////////
  355. // Function: Filename::is_fully_qualified
  356. // Access: Public
  357. // Description: Returns true if the filename is fully qualified,
  358. // e.g. begins with a slash. This is almost, but not
  359. // quite, the same thing as !is_local(). It's not
  360. // exactly the same because a special case is made for
  361. // filenames that begin with a single dot followed by a
  362. // slash--these are considered to be fully qualified
  363. // (they are explicitly relative to the current
  364. // directory, and do not refer to a filename on a search
  365. // path somewhere).
  366. ////////////////////////////////////////////////////////////////////
  367. INLINE bool Filename::
  368. is_fully_qualified() const {
  369. return
  370. (_filename.size() > 2 && _filename[0] == '.' && _filename[1] == '/') ||
  371. (!_filename.empty() && _filename[0] == '/');
  372. }
  373. ////////////////////////////////////////////////////////////////////
  374. // Function: Filename::Equality operator
  375. // Access: Public
  376. // Description:
  377. ////////////////////////////////////////////////////////////////////
  378. INLINE bool Filename::
  379. operator == (const string &other) const {
  380. return (*(string *)this) == other;
  381. }
  382. ////////////////////////////////////////////////////////////////////
  383. // Function: Filename::Inequality operator
  384. // Access: Public
  385. // Description:
  386. ////////////////////////////////////////////////////////////////////
  387. INLINE bool Filename::
  388. operator != (const string &other) const {
  389. return (*(string *)this) != other;
  390. }
  391. ////////////////////////////////////////////////////////////////////
  392. // Function: Filename::Ordering operator
  393. // Access: Public
  394. // Description:
  395. ////////////////////////////////////////////////////////////////////
  396. INLINE bool Filename::
  397. operator < (const string &other) const {
  398. return (*(string *)this) < other;
  399. }
  400. ////////////////////////////////////////////////////////////////////
  401. // Function: Filename::output
  402. // Access: Public
  403. // Description:
  404. ////////////////////////////////////////////////////////////////////
  405. INLINE void Filename::
  406. output(ostream &out) const {
  407. out << _filename;
  408. }