Panda3D

Loading...
Searching...
No Matches
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 in the virtual
16 * file system hierarchy.
17 */
18INLINE bool VirtualFileSystem::
19 exists(const Filename &filename) const {
20 return get_file(filename, true) != nullptr;
21}
22
23/**
24 * Convenience function; returns true if the named file exists as a directory in
25 * the virtual file system hierarchy.
26 */
27INLINE bool VirtualFileSystem::
28 is_directory(const Filename &filename) const {
29 PT(VirtualFile) file = get_file(filename, true);
30 return (file != nullptr && file->is_directory());
31}
32
33/**
34 * Convenience function; returns true if the named file exists as a regular file
35 * in the virtual file system hierarchy.
36 */
37INLINE bool VirtualFileSystem::
38 is_regular_file(const Filename &filename) const {
39 PT(VirtualFile) file = get_file(filename, true);
40 return (file != nullptr && file->is_regular_file());
41}
42
43/**
44 * If the file represents a directory (that is, is_directory() returns true),
45 * this returns the list of files within the directory at the current time.
46 * Returns NULL if the file is not a directory or if the directory cannot be
47 * read.
48 */
49INLINE PT(VirtualFileList) VirtualFileSystem::
50scan_directory(const Filename &filename) const {
51 PT(VirtualFile) file = get_file(filename, true);
52 if (file == nullptr) {
53 return nullptr;
54 }
55
56 return file->scan_directory();
57}
58
59/**
60 * Convenience function; lists the files within the indicated directory.
61 */
62INLINE void VirtualFileSystem::
63ls(const Filename &filename) const {
64 PT(VirtualFile) file = get_file(filename, true);
65 if (file == nullptr) {
66 express_cat.info()
67 << "Not found: " << filename << "\n";
68 } else {
69 file->ls();
70 }
71}
72
73/**
74 * Convenience function; lists the files within the indicated directory, and
75 * all files below, recursively.
76 */
77INLINE void VirtualFileSystem::
78ls_all(const Filename &filename) const {
79 PT(VirtualFile) file = get_file(filename, true);
80 if (file == nullptr) {
81 express_cat.info()
82 << "Not found: " << filename << "\n";
83 } else {
84 file->ls_all();
85 }
86}
87
88/**
89 * Convenience function; returns the entire contents of the indicated file as
90 * a string.
91 *
92 * If auto_unwrap is true, an explicitly-named .pz/.gz file is automatically
93 * decompressed and the decompressed contents are returned. This is different
94 * than vfs-implicit-pz, which will automatically decompress a file if the
95 * extension .pz is *not* given.
96 */
97INLINE std::string VirtualFileSystem::
98read_file(const Filename &filename, bool auto_unwrap) const {
99 std::string result;
100 bool okflag = read_file(filename, result, auto_unwrap);
101 nassertr(okflag, std::string());
102 return result;
103}
104
105/**
106 * Convenience function; writes the entire contents of the indicated file as a
107 * string.
108 *
109 * If auto_wrap is true, an explicitly-named .pz file is automatically
110 * compressed while writing.
111 */
112INLINE bool VirtualFileSystem::
113write_file(const Filename &filename, const std::string &data, bool auto_wrap) {
114 return write_file(filename, (const unsigned char *)data.data(), data.size(), auto_wrap);
115}
116
117/**
118 * Convenience function; fills the string up with the data from the indicated
119 * file, if it exists and can be read. Returns true on success, false
120 * otherwise.
121 *
122 * If auto_unwrap is true, an explicitly-named .pz/.gz file is automatically
123 * decompressed and the decompressed contents are returned. This is different
124 * than vfs-implicit-pz, which will automatically decompress a file if the
125 * extension .pz is *not* given.
126 */
127INLINE bool VirtualFileSystem::
128read_file(const Filename &filename, std::string &result, bool auto_unwrap) const {
129 PT(VirtualFile) file = get_file(filename, false);
130 return (file != nullptr && file->read_file(result, auto_unwrap));
131}
132
133/**
134 * Convenience function; fills the pvector up with the data from the indicated
135 * file, if it exists and can be read. Returns true on success, false
136 * otherwise.
137 *
138 * If auto_unwrap is true, an explicitly-named .pz/.gz file is automatically
139 * decompressed and the decompressed contents are returned. This is different
140 * than vfs-implicit-pz, which will automatically decompress a file if the
141 * extension .pz is *not* given.
142 */
143INLINE bool VirtualFileSystem::
144read_file(const Filename &filename, vector_uchar &result, bool auto_unwrap) const {
145 PT(VirtualFile) file = get_file(filename, false);
146 return (file != nullptr && file->read_file(result, auto_unwrap));
147}
148
149/**
150 * Convenience function; writes the entire contents of the indicated file as a
151 * block of data.
152 *
153 * If auto_wrap is true, an explicitly-named .pz file is automatically
154 * compressed while writing.
155 */
156INLINE bool VirtualFileSystem::
157write_file(const Filename &filename, const unsigned char *data, size_t data_size, bool auto_wrap) {
158 PT(VirtualFile) file = create_file(filename);
159 return (file != nullptr && file->write_file(data, data_size, auto_wrap));
160}
Filename
The name of a file, such as a texture file or an Egg file.
Definition filename.h:44
VirtualFileList
A list of VirtualFiles, as returned by VirtualFile::scan_directory().
Definition virtualFileList.h:25
VirtualFileSystem::exists
bool exists(const Filename &filename) const
Convenience function; returns true if the named file exists in the virtual file system hierarchy.
Definition virtualFileSystem.I:19
VirtualFileSystem::is_regular_file
bool is_regular_file(const Filename &filename) const
Convenience function; returns true if the named file exists as a regular file in the virtual file sys...
Definition virtualFileSystem.I:38
VirtualFileSystem::create_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.
Definition virtualFileSystem.cxx:530
VirtualFileSystem::is_directory
bool is_directory(const Filename &filename) const
Convenience function; returns true if the named file exists as a directory in the virtual file system...
Definition virtualFileSystem.I:28
VirtualFileSystem::get_file
PointerTo< VirtualFile > get_file(const Filename &filename, bool status_only=false) const
Looks up the file by the indicated name in the file system.
Definition virtualFileSystem.cxx:515
VirtualFile
The abstract base class for a file or directory within the VirtualFileSystem.
Definition virtualFile.h:35