virtualFile.cxx 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. // Filename: virtualFile.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 "virtualFile.h"
  19. #include "virtualFileSystem.h"
  20. #include "virtualFileList.h"
  21. #include "config_express.h"
  22. TypeHandle VirtualFile::_type_handle;
  23. ////////////////////////////////////////////////////////////////////
  24. // Function: VirtualFile::is_directory
  25. // Access: Published, Virtual
  26. // Description: Returns true if this file represents a directory (and
  27. // scan_directory() may be called), false otherwise.
  28. ////////////////////////////////////////////////////////////////////
  29. bool VirtualFile::
  30. is_directory() const {
  31. return false;
  32. }
  33. ////////////////////////////////////////////////////////////////////
  34. // Function: VirtualFile::is_regular_file
  35. // Access: Published, Virtual
  36. // Description: Returns true if this file represents a regular file
  37. // (and read_file() may be called), false otherwise.
  38. ////////////////////////////////////////////////////////////////////
  39. bool VirtualFile::
  40. is_regular_file() const {
  41. return false;
  42. }
  43. ////////////////////////////////////////////////////////////////////
  44. // Function: VirtualFile::scan_directory
  45. // Access: Published
  46. // Description: If the file represents a directory (that is,
  47. // is_directory() returns true), this returns the list
  48. // of files within the directory at the current time.
  49. // Returns NULL if the file is not a directory or if the
  50. // directory cannot be read.
  51. ////////////////////////////////////////////////////////////////////
  52. PT(VirtualFileList) VirtualFile::
  53. scan_directory() const {
  54. // First, we have to make sure there aren't any mount points attached
  55. // under this directory. These will override any local filenames.
  56. VirtualFileSystem *file_system = get_file_system();
  57. Filename this_filename = get_filename();
  58. vector_string mount_points_flat;
  59. file_system->scan_mount_points(mount_points_flat, this_filename);
  60. // Copy the set of nested mount points to a sorted list so we can
  61. // search it quickly.
  62. ov_set<string> mount_points;
  63. copy(mount_points_flat.begin(), mount_points_flat.end(),
  64. back_inserter(mount_points));
  65. mount_points.sort();
  66. PT(VirtualFileList) file_list = new VirtualFileList;
  67. // Each of those mount points maps to a directory root or something
  68. // from the file system.
  69. ov_set<string>::const_iterator mi;
  70. for (mi = mount_points.begin(); mi != mount_points.end(); ++mi) {
  71. const string &basename = (*mi);
  72. Filename filename(this_filename, basename);
  73. PT(VirtualFile) file = file_system->get_file(filename);
  74. file_list->add_file(file);
  75. }
  76. // Now, get the actual local files in this directory.
  77. vector_string names;
  78. if (!scan_local_directory(file_list, mount_points)) {
  79. // Not a directory, or unable to read directory.
  80. if (file_list->get_num_files() == 0) {
  81. return NULL;
  82. }
  83. // We couldn't read the physical directory, but we do have some
  84. // mounted files to return.
  85. return file_list;
  86. }
  87. return file_list;
  88. }
  89. ////////////////////////////////////////////////////////////////////
  90. // Function: VirtualFile::output
  91. // Access: Published
  92. // Description:
  93. ////////////////////////////////////////////////////////////////////
  94. void VirtualFile::
  95. output(ostream &out) const {
  96. out << get_filename();
  97. }
  98. ////////////////////////////////////////////////////////////////////
  99. // Function: VirtualFile::ls
  100. // Access: Published
  101. // Description: If the file represents a directory, lists its
  102. // contents.
  103. ////////////////////////////////////////////////////////////////////
  104. void VirtualFile::
  105. ls(ostream &out) const {
  106. CPT(VirtualFileList) contents = scan_directory();
  107. if (contents == NULL) {
  108. if (!is_directory()) {
  109. out << get_filename() << "\n";
  110. } else {
  111. out << get_filename() << " cannot be read.\n";
  112. }
  113. return;
  114. }
  115. int num_files = contents->get_num_files();
  116. for (int i = 0; i < num_files; i++) {
  117. VirtualFile *file = contents->get_file(i);
  118. out << file->get_filename().get_basename() << "\n";
  119. }
  120. }
  121. ////////////////////////////////////////////////////////////////////
  122. // Function: VirtualFile::ls_all
  123. // Access: Published
  124. // Description: If the file represents a directory, recursively lists
  125. // its contents and those of all subdirectories.
  126. ////////////////////////////////////////////////////////////////////
  127. void VirtualFile::
  128. ls_all(ostream &out) const {
  129. if (!is_directory()) {
  130. out << get_filename() << " is not a directory.\n";
  131. } else {
  132. r_ls_all(out, get_filename());
  133. }
  134. }
  135. ////////////////////////////////////////////////////////////////////
  136. // Function: VirtualFile::open_read_file
  137. // Access: Published, Virtual
  138. // Description: Opens the file for reading. Returns a newly
  139. // allocated istream on success (which you should
  140. // eventually delete when you are done reading).
  141. // Returns NULL on failure.
  142. ////////////////////////////////////////////////////////////////////
  143. istream *VirtualFile::
  144. open_read_file() const {
  145. return NULL;
  146. }
  147. ////////////////////////////////////////////////////////////////////
  148. // Function: VirtualFile::read_file
  149. // Access: Public
  150. // Description: Fills up the indicated string with the contents of
  151. // the file, if it is a regular file. Returns true on
  152. // success, false otherwise.
  153. ////////////////////////////////////////////////////////////////////
  154. bool VirtualFile::
  155. read_file(string &result) const {
  156. result = string();
  157. istream *in = open_read_file();
  158. if (in == (istream *)NULL) {
  159. express_cat.info()
  160. << "Unable to read " << get_filename() << "\n";
  161. return false;
  162. }
  163. int byte = in->get();
  164. while (!in->eof() && !in->fail()) {
  165. result += (char)byte;
  166. byte = in->get();
  167. }
  168. bool failed = in->fail() && !in->eof();
  169. delete in;
  170. if (failed) {
  171. express_cat.info()
  172. << "Error while reading " << get_filename() << "\n";
  173. }
  174. return !failed;
  175. }
  176. ////////////////////////////////////////////////////////////////////
  177. // Function: VirtualFile::scan_local_directory
  178. // Access: Protected, Virtual
  179. // Description: Fills file_list up with the list of files that are
  180. // within this directory, excluding those whose
  181. // basenames are listed in mount_points. Returns true
  182. // if successful, false if the file is not a directory
  183. // or the directory cannot be read.
  184. ////////////////////////////////////////////////////////////////////
  185. bool VirtualFile::
  186. scan_local_directory(VirtualFileList *, const ov_set<string> &) const {
  187. return false;
  188. }
  189. ////////////////////////////////////////////////////////////////////
  190. // Function: VirtualFile::r_ls_all
  191. // Access: Private
  192. // Description: The recursive implementation of ls_all().
  193. ////////////////////////////////////////////////////////////////////
  194. void VirtualFile::
  195. r_ls_all(ostream &out, const Filename &root) const {
  196. CPT(VirtualFileList) contents = scan_directory();
  197. if (contents == NULL) {
  198. return;
  199. }
  200. int num_files = contents->get_num_files();
  201. for (int i = 0; i < num_files; i++) {
  202. VirtualFile *file = contents->get_file(i);
  203. Filename filename = file->get_filename();
  204. filename.make_relative_to(root);
  205. out << filename << "\n";
  206. if (file->is_directory()) {
  207. file->r_ls_all(out, root);
  208. }
  209. }
  210. }