virtualFileSystem.I 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // Filename: virtualFileSystem.I
  2. // Created by: drose (03Aug02)
  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: VirtualFileSystem::exists
  20. // Access: Published
  21. // Description: Convenience function; returns true if the named file
  22. // exists.
  23. ////////////////////////////////////////////////////////////////////
  24. INLINE bool VirtualFileSystem::
  25. exists(const Filename &filename) const {
  26. return get_file(filename) != (VirtualFile *)NULL;
  27. }
  28. ////////////////////////////////////////////////////////////////////
  29. // Function: VirtualFileSystem::is_directory
  30. // Access: Published
  31. // Description: Convenience function; returns true if the named file
  32. // exists and is a directory.
  33. ////////////////////////////////////////////////////////////////////
  34. INLINE bool VirtualFileSystem::
  35. is_directory(const Filename &filename) const {
  36. PT(VirtualFile) file = get_file(filename);
  37. return (file != (VirtualFile *)NULL && file->is_directory());
  38. }
  39. ////////////////////////////////////////////////////////////////////
  40. // Function: VirtualFileSystem::is_regular_file
  41. // Access: Published
  42. // Description: Convenience function; returns true if the named file
  43. // exists and is a regular file.
  44. ////////////////////////////////////////////////////////////////////
  45. INLINE bool VirtualFileSystem::
  46. is_regular_file(const Filename &filename) const {
  47. PT(VirtualFile) file = get_file(filename);
  48. return (file != (VirtualFile *)NULL && file->is_regular_file());
  49. }
  50. ////////////////////////////////////////////////////////////////////
  51. // Function: VirtualFileSystem::ls
  52. // Access: Published
  53. // Description: Convenience function; lists the files within the
  54. // indicated directory. This accepts a string instead
  55. // of a Filename purely for programmer convenience at
  56. // the Python prompt.
  57. ////////////////////////////////////////////////////////////////////
  58. INLINE void VirtualFileSystem::
  59. ls(const string &filename) const {
  60. PT(VirtualFile) file = get_file(filename);
  61. if (file == (VirtualFile *)NULL) {
  62. express_cat.info()
  63. << "Not found: " << filename << "\n";
  64. } else {
  65. file->ls();
  66. }
  67. }
  68. ////////////////////////////////////////////////////////////////////
  69. // Function: VirtualFileSystem::ls_all
  70. // Access: Published
  71. // Description: Convenience function; lists the files within the
  72. // indicated directory, and all files below,
  73. // recursively. This accepts a string instead of a
  74. // Filename purely for programmer convenience at the
  75. // Python prompt.
  76. ////////////////////////////////////////////////////////////////////
  77. INLINE void VirtualFileSystem::
  78. ls_all(const string &filename) const {
  79. PT(VirtualFile) file = get_file(filename);
  80. if (file == (VirtualFile *)NULL) {
  81. express_cat.info()
  82. << "Not found: " << filename << "\n";
  83. } else {
  84. file->ls_all();
  85. }
  86. }
  87. ////////////////////////////////////////////////////////////////////
  88. // Function: VirtualFileSystem::read_file
  89. // Access: Published
  90. // Description: Convenience function; returns the entire contents of
  91. // the indicated file as a string.
  92. //
  93. // If auto_unwrap is true, an explicitly-named .pz file
  94. // is automatically decompressed and the decompressed
  95. // contents are returned. This is different than
  96. // vfs-implicit-pz, which will automatically decompress
  97. // a file if the extension .pz is *not* given.
  98. ////////////////////////////////////////////////////////////////////
  99. INLINE string VirtualFileSystem::
  100. read_file(const Filename &filename, bool auto_unwrap) const {
  101. string result;
  102. bool okflag = read_file(filename, result, auto_unwrap);
  103. nassertr(okflag, string());
  104. return result;
  105. }
  106. ////////////////////////////////////////////////////////////////////
  107. // Function: VirtualFileSystem::open_read_file
  108. // Access: Published
  109. // Description: Convenience function; returns a newly allocated
  110. // istream if the file exists and can be read, or NULL
  111. // otherwise. Does not return an invalid istream.
  112. //
  113. // If auto_unwrap is true, an explicitly-named .pz file
  114. // is automatically decompressed and the decompressed
  115. // contents are returned. This is different than
  116. // vfs-implicit-pz, which will automatically decompress
  117. // a file if the extension .pz is *not* given.
  118. ////////////////////////////////////////////////////////////////////
  119. INLINE istream *VirtualFileSystem::
  120. open_read_file(const Filename &filename, bool auto_unwrap) const {
  121. PT(VirtualFile) file = get_file(filename);
  122. if (file == (VirtualFile *)NULL) {
  123. return NULL;
  124. }
  125. istream *str = file->open_read_file(auto_unwrap);
  126. if (str != (istream *)NULL && str->fail()) {
  127. delete str;
  128. str = (istream *)NULL;
  129. }
  130. return str;
  131. }
  132. ////////////////////////////////////////////////////////////////////
  133. // Function: VirtualFileSystem::read_file
  134. // Access: Public
  135. // Description: Convenience function; fills the string up with the
  136. // data from the indicated file, if it exists and can be
  137. // read. Returns true on success, false otherwise.
  138. //
  139. // If auto_unwrap is true, an explicitly-named .pz file
  140. // is automatically decompressed and the decompressed
  141. // contents are returned. This is different than
  142. // vfs-implicit-pz, which will automatically decompress
  143. // a file if the extension .pz is *not* given.
  144. ////////////////////////////////////////////////////////////////////
  145. INLINE bool VirtualFileSystem::
  146. read_file(const Filename &filename, string &result, bool auto_unwrap) const {
  147. PT(VirtualFile) file = get_file(filename);
  148. return (file != (VirtualFile *)NULL && file->read_file(result, auto_unwrap));
  149. }