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