Panda3D
virtualFileSystem.I
1 // Filename: virtualFileSystem.I
2 // Created by: drose (03Aug02)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 
16 ////////////////////////////////////////////////////////////////////
17 // Function: VirtualFileSystem::exists
18 // Access: Published
19 // Description: Convenience function; returns true if the named file
20 // exists.
21 ////////////////////////////////////////////////////////////////////
22 INLINE bool VirtualFileSystem::
23 exists(const Filename &filename) const {
24  return get_file(filename, true) != (VirtualFile *)NULL;
25 }
26 
27 ////////////////////////////////////////////////////////////////////
28 // Function: VirtualFileSystem::is_directory
29 // Access: Published
30 // Description: Convenience function; returns true if the named file
31 // exists and is a directory.
32 ////////////////////////////////////////////////////////////////////
33 INLINE bool VirtualFileSystem::
34 is_directory(const Filename &filename) const {
35  PT(VirtualFile) file = get_file(filename, true);
36  return (file != (VirtualFile *)NULL && file->is_directory());
37 }
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, true);
48  return (file != (VirtualFile *)NULL && file->is_regular_file());
49 }
50 
51 ////////////////////////////////////////////////////////////////////
52 // Function: VirtualFileSystem::scan_directory
53 // Access: Published
54 // Description: If the file represents a directory (that is,
55 // is_directory() returns true), this returns the list
56 // of files within the directory at the current time.
57 // Returns NULL if the file is not a directory or if the
58 // directory cannot be read.
59 ////////////////////////////////////////////////////////////////////
60 INLINE PT(VirtualFileList) VirtualFileSystem::
61 scan_directory(const Filename &filename) const {
62  PT(VirtualFile) file = get_file(filename, true);
63  if (file == (VirtualFile *)NULL) {
64  return NULL;
65  }
66 
67  return file->scan_directory();
68 }
69 
70 ////////////////////////////////////////////////////////////////////
71 // Function: VirtualFileSystem::ls
72 // Access: Published
73 // Description: Convenience function; lists the files within the
74 // indicated directory.
75 ////////////////////////////////////////////////////////////////////
76 INLINE void VirtualFileSystem::
77 ls(const Filename &filename) const {
78  PT(VirtualFile) file = get_file(filename, true);
79  if (file == (VirtualFile *)NULL) {
80  express_cat.info()
81  << "Not found: " << filename << "\n";
82  } else {
83  file->ls();
84  }
85 }
86 
87 ////////////////////////////////////////////////////////////////////
88 // Function: VirtualFileSystem::ls_all
89 // Access: Published
90 // Description: Convenience function; lists the files within the
91 // indicated directory, and all files below,
92 // recursively.
93 ////////////////////////////////////////////////////////////////////
94 INLINE void VirtualFileSystem::
95 ls_all(const Filename &filename) const {
96  PT(VirtualFile) file = get_file(filename, true);
97  if (file == (VirtualFile *)NULL) {
98  express_cat.info()
99  << "Not found: " << filename << "\n";
100  } else {
101  file->ls_all();
102  }
103 }
104 
105 ////////////////////////////////////////////////////////////////////
106 // Function: VirtualFileSystem::read_file
107 // Access: Published
108 // Description: Convenience function; returns the entire contents of
109 // the indicated file as a string.
110 //
111 // If auto_unwrap is true, an explicitly-named .pz file
112 // is automatically decompressed and the decompressed
113 // contents are returned. This is different than
114 // vfs-implicit-pz, which will automatically decompress
115 // a file if the extension .pz is *not* given.
116 ////////////////////////////////////////////////////////////////////
117 INLINE string VirtualFileSystem::
118 read_file(const Filename &filename, bool auto_unwrap) const {
119  string result;
120  bool okflag = read_file(filename, result, auto_unwrap);
121  nassertr(okflag, string());
122  return result;
123 }
124 
125 ////////////////////////////////////////////////////////////////////
126 // Function: VirtualFileSystem::write_file
127 // Access: Published
128 // Description: Convenience function; writes the entire contents of
129 // the indicated file as a string.
130 //
131 // If auto_wrap is true, an explicitly-named .pz file
132 // is automatically compressed while writing.
133 ////////////////////////////////////////////////////////////////////
134 INLINE bool VirtualFileSystem::
135 write_file(const Filename &filename, const string &data, bool auto_wrap) {
136  return write_file(filename, (const unsigned char *)data.data(), data.size(), auto_wrap);
137 }
138 
139 ////////////////////////////////////////////////////////////////////
140 // Function: VirtualFileSystem::read_file
141 // Access: Public
142 // Description: Convenience function; fills the string up with the
143 // data from the indicated file, if it exists and can be
144 // read. Returns true on success, false otherwise.
145 //
146 // If auto_unwrap is true, an explicitly-named .pz file
147 // is automatically decompressed and the decompressed
148 // contents are returned. This is different than
149 // vfs-implicit-pz, which will automatically decompress
150 // a file if the extension .pz is *not* given.
151 ////////////////////////////////////////////////////////////////////
152 INLINE bool VirtualFileSystem::
153 read_file(const Filename &filename, string &result, bool auto_unwrap) const {
154  PT(VirtualFile) file = get_file(filename, false);
155  return (file != (VirtualFile *)NULL && file->read_file(result, auto_unwrap));
156 }
157 
158 ////////////////////////////////////////////////////////////////////
159 // Function: VirtualFileSystem::read_file
160 // Access: Public
161 // Description: Convenience function; fills the pvector up with the
162 // data from the indicated file, if it exists and can be
163 // read. Returns true on success, false otherwise.
164 //
165 // If auto_unwrap is true, an explicitly-named .pz file
166 // is automatically decompressed and the decompressed
167 // contents are returned. This is different than
168 // vfs-implicit-pz, which will automatically decompress
169 // a file if the extension .pz is *not* given.
170 ////////////////////////////////////////////////////////////////////
171 INLINE bool VirtualFileSystem::
172 read_file(const Filename &filename, pvector<unsigned char> &result, bool auto_unwrap) const {
173  PT(VirtualFile) file = get_file(filename, false);
174  return (file != (VirtualFile *)NULL && file->read_file(result, auto_unwrap));
175 }
176 
177 ////////////////////////////////////////////////////////////////////
178 // Function: VirtualFileSystem::write_file
179 // Access: Public
180 // Description: Convenience function; writes the entire contents of
181 // the indicated file as a block of data.
182 //
183 // If auto_wrap is true, an explicitly-named .pz file
184 // is automatically compressed while writing.
185 ////////////////////////////////////////////////////////////////////
186 INLINE bool VirtualFileSystem::
187 write_file(const Filename &filename, const unsigned char *data, size_t data_size, bool auto_wrap) {
188  PT(VirtualFile) file = create_file(filename);
189  return (file != (VirtualFile *)NULL && file->write_file(data, data_size, auto_wrap));
190 }
PointerTo< VirtualFileList > scan_directory() const
If the file represents a directory (that is, is_directory() returns true), this returns the list of f...
The abstract base class for a file or directory within the VirtualFileSystem.
Definition: virtualFile.h:37
PointerTo< VirtualFile > create_file(const Filename &filename)
Attempts to create a file by the indicated name in the filesystem, if possible, and returns it...
A list of VirtualFiles, as returned by VirtualFile::scan_directory().
bool exists(const Filename &filename) const
Convenience function; returns true if the named file exists.
bool is_directory(const Filename &filename) const
Convenience function; returns true if the named file exists and is a directory.
The name of a file, such as a texture file or an Egg file.
Definition: filename.h:44
PointerTo< VirtualFile > get_file(const Filename &filename, bool status_only=false) const
Looks up the file by the indicated name in the file system.
bool is_regular_file(const Filename &filename) const
Convenience function; returns true if the named file exists and is a regular file.