virtualFileSimple.cxx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // Filename: virtualFileSimple.cxx
  2. // Created by: drose (03Aug02)
  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 "virtualFileSimple.h"
  19. TypeHandle VirtualFileSimple::_type_handle;
  20. ////////////////////////////////////////////////////////////////////
  21. // Function: VirtualFileSimple::get_file_system
  22. // Access: Published, Virtual
  23. // Description: Returns the VirtualFileSystem this file is associated
  24. // with.
  25. ////////////////////////////////////////////////////////////////////
  26. VirtualFileSystem *VirtualFileSimple::
  27. get_file_system() const {
  28. return _mount->get_file_system();
  29. }
  30. ////////////////////////////////////////////////////////////////////
  31. // Function: VirtualFileSimple::get_filename
  32. // Access: Published, Virtual
  33. // Description: Returns the full pathname to this file within the
  34. // virtual file system.
  35. ////////////////////////////////////////////////////////////////////
  36. Filename VirtualFileSimple::
  37. get_filename() const {
  38. string mount_point = _mount->get_mount_point();
  39. if (_local_filename.empty()) {
  40. if (mount_point.empty()) {
  41. return "/";
  42. } else {
  43. return string("/") + mount_point;
  44. }
  45. } else {
  46. if (mount_point.empty()) {
  47. return string("/") + _local_filename.get_fullpath();
  48. } else {
  49. return string("/") + mount_point + string("/") + _local_filename.get_fullpath();
  50. }
  51. }
  52. }
  53. ////////////////////////////////////////////////////////////////////
  54. // Function: VirtualFileSimple::is_directory
  55. // Access: Published, Virtual
  56. // Description: Returns true if this file represents a directory (and
  57. // scan_directory() may be called), false otherwise.
  58. ////////////////////////////////////////////////////////////////////
  59. bool VirtualFileSimple::
  60. is_directory() const {
  61. return _mount->is_directory(_local_filename);
  62. }
  63. ////////////////////////////////////////////////////////////////////
  64. // Function: VirtualFileSimple::is_regular_file
  65. // Access: Published, Virtual
  66. // Description: Returns true if this file represents a regular file
  67. // (and read_file() may be called), false otherwise.
  68. ////////////////////////////////////////////////////////////////////
  69. bool VirtualFileSimple::
  70. is_regular_file() const {
  71. return _mount->is_regular_file(_local_filename);
  72. }
  73. ////////////////////////////////////////////////////////////////////
  74. // Function: VirtualFileSimple::open_read_file
  75. // Access: Published, Virtual
  76. // Description: Opens the file for reading. Returns a newly
  77. // allocated istream on success (which you should
  78. // eventually delete when you are done reading).
  79. // Returns NULL on failure.
  80. ////////////////////////////////////////////////////////////////////
  81. istream *VirtualFileSimple::
  82. open_read_file() const {
  83. return _mount->open_read_file(_local_filename);
  84. }
  85. ////////////////////////////////////////////////////////////////////
  86. // Function: VirtualFileSimple::scan_local_directory
  87. // Access: Protected, Virtual
  88. // Description: Fills file_list up with the list of files that are
  89. // within this directory, excluding those whose
  90. // basenames are listed in mount_points. Returns true
  91. // if successful, false if the file is not a directory
  92. // or the directory cannot be read.
  93. ////////////////////////////////////////////////////////////////////
  94. bool VirtualFileSimple::
  95. scan_local_directory(VirtualFileList *file_list,
  96. const ov_set<string> &mount_points) const {
  97. vector_string names;
  98. if (!_mount->scan_directory(names, _local_filename)) {
  99. return false;
  100. }
  101. // Now the scan above gave us a list of basenames. Turn these back
  102. // into VirtualFile pointers.
  103. // Each of the files returned by the mount will be just a simple
  104. // file within the same mount tree, unless it is shadowed by a
  105. // mount point listed in mount_points.
  106. vector_string::const_iterator ni;
  107. for (ni = names.begin(); ni != names.end(); ++ni) {
  108. const string &basename = (*ni);
  109. if (mount_points.find(basename) == mount_points.end()) {
  110. Filename filename(_local_filename, basename);
  111. VirtualFileSimple *file = new VirtualFileSimple(_mount, filename);
  112. file_list->add_file(file);
  113. }
  114. }
  115. return true;
  116. }