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  */
17 INLINE bool VirtualFileSystem::
18 exists(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  */
26 INLINE bool VirtualFileSystem::
27 is_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  */
36 INLINE bool VirtualFileSystem::
37 is_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  */
48 INLINE PT(VirtualFileList) VirtualFileSystem::
49 scan_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  */
61 INLINE void VirtualFileSystem::
62 ls(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  */
76 INLINE void VirtualFileSystem::
77 ls_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  */
96 INLINE std::string VirtualFileSystem::
97 read_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  */
111 INLINE bool VirtualFileSystem::
112 write_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  */
126 INLINE bool VirtualFileSystem::
127 read_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  */
142 INLINE bool VirtualFileSystem::
143 read_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  */
155 INLINE bool VirtualFileSystem::
156 write_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 abstract base class for a file or directory within the VirtualFileSystem.
Definition: virtualFile.h:35
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:39
PT(VirtualFileList) VirtualFileSystem
If the file represents a directory (that is, is_directory() returns true), this returns the list of f...
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.