Panda3D
Loading...
Searching...
No Matches
virtualFile.h
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 virtualFile.h
10 * @author drose
11 * @date 2002-08-03
12 */
13
14#ifndef VIRTUALFILE_H
15#define VIRTUALFILE_H
16
17#include "pandabase.h"
18
19#include "filename.h"
20#include "subfileInfo.h"
21#include "pointerTo.h"
22#include "typedReferenceCount.h"
23#include "ordered_vector.h"
24#include "vector_uchar.h"
25
27class VirtualFileList;
29
30
31/**
32 * The abstract base class for a file or directory within the
33 * VirtualFileSystem.
34 */
35class EXPCL_PANDA_EXPRESS VirtualFile : public TypedReferenceCount {
36public:
37 INLINE VirtualFile();
38
39PUBLISHED:
40 virtual VirtualFileSystem *get_file_system() const=0;
41 virtual Filename get_filename() const=0;
42 INLINE const Filename &get_original_filename() const;
43
44 virtual bool has_file() const;
45 virtual bool is_directory() const;
46 virtual bool is_regular_file() const;
47 virtual bool is_writable() const;
48
49 BLOCKING virtual bool delete_file();
50 BLOCKING virtual bool rename_file(VirtualFile *new_file);
51 BLOCKING virtual bool copy_file(VirtualFile *new_file);
52
53 BLOCKING PT(VirtualFileList) scan_directory() const;
54
55 void output(std::ostream &out) const;
56 BLOCKING void ls(std::ostream &out = std::cout) const;
57 BLOCKING void ls_all(std::ostream &out = std::cout) const;
58
59 EXTENSION(PyObject *read_file(bool auto_unwrap) const);
60 BLOCKING virtual std::istream *open_read_file(bool auto_unwrap) const;
61 BLOCKING virtual void close_read_file(std::istream *stream) const;
62 virtual bool was_read_successful() const;
63
64 EXTENSION(PyObject *write_file(PyObject *data, bool auto_wrap));
65 BLOCKING virtual std::ostream *open_write_file(bool auto_wrap, bool truncate);
66 BLOCKING virtual std::ostream *open_append_file();
67 BLOCKING virtual void close_write_file(std::ostream *stream);
68
69 BLOCKING virtual std::iostream *open_read_write_file(bool truncate);
70 BLOCKING virtual std::iostream *open_read_append_file();
71 BLOCKING virtual void close_read_write_file(std::iostream *stream);
72
73 BLOCKING virtual std::streamsize get_file_size(std::istream *stream) const;
74 BLOCKING virtual std::streamsize get_file_size() const;
75 BLOCKING virtual time_t get_timestamp() const;
76
77 virtual bool get_system_info(SubfileInfo &info);
78
79public:
80 virtual bool atomic_compare_and_exchange_contents(std::string &orig_contents, const std::string &old_contents, const std::string &new_contents);
81 virtual bool atomic_read_contents(std::string &contents) const;
82
83 INLINE std::string read_file(bool auto_unwrap) const;
84 INLINE bool write_file(const std::string &data, bool auto_wrap);
85
86 INLINE void set_original_filename(const Filename &filename);
87 bool read_file(std::string &result, bool auto_unwrap) const;
88 virtual bool read_file(vector_uchar &result, bool auto_unwrap) const;
89 virtual bool write_file(const unsigned char *data, size_t data_size, bool auto_wrap);
90
91 static bool simple_read_file(std::istream *stream, vector_uchar &result);
92 static bool simple_read_file(std::istream *stream, vector_uchar &result, size_t max_bytes);
93
94protected:
95 virtual bool scan_local_directory(VirtualFileList *file_list,
96 const ov_set<std::string> &mount_points) const;
97
98private:
99 void r_ls_all(std::ostream &out, const Filename &root) const;
100
101 Filename _original_filename;
102
103
104public:
105 virtual TypeHandle get_type() const {
106 return get_class_type();
107 }
108 virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
109 static TypeHandle get_class_type() {
110 return _type_handle;
111 }
112 static void init_type() {
113 TypedReferenceCount::init_type();
114 register_type(_type_handle, "VirtualFile",
115 TypedReferenceCount::get_class_type());
116 }
117
118private:
119 static TypeHandle _type_handle;
120
121 friend class VirtualFileComposite;
122};
123
124INLINE std::ostream &operator << (std::ostream &out, const VirtualFile &file);
125
126
127#include "virtualFile.I"
128
129#endif
The name of a file, such as a texture file or an Egg file.
Definition filename.h:44
This class records a particular byte sub-range within an existing file on disk.
Definition subfileInfo.h:26
TypeHandle is the identifier used to differentiate C++ class types.
Definition typeHandle.h:81
A list of VirtualFiles, as returned by VirtualFile::scan_directory().
The abstract base class for a mount definition used within a VirtualFileSystem.
A hierarchy of directories and files that appears to be one continuous file system,...
The abstract base class for a file or directory within the VirtualFileSystem.
Definition virtualFile.h:35
virtual std::iostream * open_read_append_file()
Works like open_read_write_file(), but the file is opened in append mode.
virtual void close_read_file(std::istream *stream) const
Closes a file opened by a previous call to open_read_file().
virtual bool rename_file(VirtualFile *new_file)
Attempts to move or rename this file or directory.
virtual std::istream * open_read_file(bool auto_unwrap) const
Opens the file for reading.
virtual bool is_regular_file() const
Returns true if this file represents a regular file (and read_file() may be called),...
virtual bool copy_file(VirtualFile *new_file)
Attempts to copy the contents of this file to the indicated file.
virtual bool get_system_info(SubfileInfo &info)
Populates the SubfileInfo structure with the data representing where the file actually resides on dis...
PointerTo< VirtualFileList > scan_directory() const
If the file represents a directory (that is, is_directory() returns true), this returns the list of f...
virtual bool delete_file()
Attempts to delete this file or directory.
const Filename & get_original_filename() const
Returns the original filename as it was used to locate this VirtualFile.
Definition virtualFile.I:27
virtual std::ostream * open_write_file(bool auto_wrap, bool truncate)
Opens the file for writing.
void ls_all(std::ostream &out=std::cout) const
If the file represents a directory, recursively lists its contents and those of all subdirectories.
virtual std::streamsize get_file_size(std::istream *stream) const
Returns the current size on disk (or wherever it is) of the already-open file.
static bool simple_read_file(std::istream *stream, vector_uchar &result)
Fills up the indicated pvector with the contents of the just-opened file.
virtual bool is_writable() const
Returns true if this file may be written to, which implies write_file() may be called (unless it is a...
virtual bool has_file() const
Returns true if this file exists, false otherwise.
virtual bool is_directory() const
Returns true if this file represents a directory (and scan_directory() may be called),...
virtual void close_read_write_file(std::iostream *stream)
Closes a file opened by a previous call to open_read_write_file().
virtual bool atomic_read_contents(std::string &contents) const
See Filename::atomic_read_contents().
virtual bool was_read_successful() const
Call this method after a reading the istream returned by open_read_file() to completion.
virtual bool atomic_compare_and_exchange_contents(std::string &orig_contents, const std::string &old_contents, const std::string &new_contents)
See Filename::atomic_compare_and_exchange_contents().
std::string read_file(bool auto_unwrap) const
Returns the entire contents of the file as a string.
Definition virtualFile.I:35
void ls(std::ostream &out=std::cout) const
If the file represents a directory, lists its contents.
virtual std::ostream * open_append_file()
Works like open_write_file(), but the file is opened in append mode.
virtual time_t get_timestamp() const
Returns a time_t value that represents the time the file was last modified, to within whatever precis...
void set_original_filename(const Filename &filename)
Stores the original filename that was used to locate this VirtualFile.
Definition virtualFile.I:55
virtual std::iostream * open_read_write_file(bool truncate)
Opens the file for writing.
virtual void close_write_file(std::ostream *stream)
Closes a file opened by a previous call to open_write_file().
bool write_file(const std::string &data, bool auto_wrap)
Writes the entire contents of the file as a string, if it is writable.
Definition virtualFile.I:45
A specialization of ordered_vector that emulates a standard STL set: one copy of each element is allo...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void register_type(TypeHandle &type_handle, const std::string &name)
This inline function is just a convenient way to call TypeRegistry::register_type(),...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.