Panda3D
Loading...
Searching...
No Matches
rocketFileInterface.cxx
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 rocketFileInterface.cxx
10 * @author rdb
11 * @date 2011-11-03
12 */
13
14#include "rocketFileInterface.h"
15#include "virtualFileSystem.h"
16
17/**
18 * Constructs a RocketFileInterface for the given VFS, or the default if NULL
19 * is given.
20 */
22RocketFileInterface(VirtualFileSystem *vfs) : _vfs(vfs) {
23 if (_vfs == nullptr) {
25 }
26}
27
28/**
29 *
30 */
31Rocket::Core::FileHandle RocketFileInterface::
32Open(const Rocket::Core::String& path) {
33 rocket_cat.debug() << "Opening " << path.CString() << "\n";
34
35 Filename fn = Filename::from_os_specific(path.CString());
36
37 PT(VirtualFile) file = _vfs->get_file(fn);
38 if (file == nullptr) {
39 // failed? Try model-path as a Panda-friendly fallback.
40 if (!_vfs->resolve_filename(fn, get_model_path())) {
41 rocket_cat.error() << "Could not resolve " << fn
42 << " along the model-path (currently: " << get_model_path() << ")\n";
43 return (Rocket::Core::FileHandle) nullptr;
44 }
45
46 file = _vfs->get_file(fn);
47 if (file == nullptr) {
48 rocket_cat.error() << "Failed to get " << fn << ", found on model-path\n";
49 return (Rocket::Core::FileHandle) nullptr;
50 }
51 }
52
53 std::istream *str = file->open_read_file(true);
54 if (str == nullptr) {
55 rocket_cat.error() << "Failed to open " << fn << " for reading\n";
56 return (Rocket::Core::FileHandle) nullptr;
57 }
58
59 VirtualFileHandle *handle = new VirtualFileHandle;
60 handle->_file = file;
61 handle->_stream = str;
62
63 // A FileHandle is actually just a void pointer.
64 return (Rocket::Core::FileHandle) handle;
65}
66
67/**
68 *
69 */
70void RocketFileInterface::
71Close(Rocket::Core::FileHandle file) {
72 VirtualFileHandle *handle = (VirtualFileHandle*) file;
73 if (handle == nullptr) {
74 return;
75 }
76
77 _vfs->close_read_file(handle->_stream);
78 delete handle;
79}
80
81/**
82 *
83 */
84size_t RocketFileInterface::
85Read(void* buffer, size_t size, Rocket::Core::FileHandle file) {
86 VirtualFileHandle *handle = (VirtualFileHandle*) file;
87 if (handle == nullptr) {
88 return 0;
89 }
90
91 handle->_stream->read((char*) buffer, size);
92 return handle->_stream->gcount();
93}
94
95/**
96 *
97 */
98bool RocketFileInterface::
99Seek(Rocket::Core::FileHandle file, long offset, int origin) {
100 VirtualFileHandle *handle = (VirtualFileHandle*) file;
101 if (handle == nullptr) {
102 return false;
103 }
104
105 switch(origin) {
106 case SEEK_SET:
107 handle->_stream->seekg(offset, std::ios::beg);
108 break;
109 case SEEK_CUR:
110 handle->_stream->seekg(offset, std::ios::cur);
111 break;
112 case SEEK_END:
113 handle->_stream->seekg(offset, std::ios::end);
114 };
115
116 return !handle->_stream->fail();
117}
118
119/**
120 *
121 */
122size_t RocketFileInterface::
123Tell(Rocket::Core::FileHandle file) {
124 VirtualFileHandle *handle = (VirtualFileHandle*) file;
125 if (handle == nullptr) {
126 return 0;
127 }
128
129 return handle->_stream->tellg();
130}
131
132/**
133 *
134 */
135size_t RocketFileInterface::
136Length(Rocket::Core::FileHandle file) {
137 VirtualFileHandle *handle = (VirtualFileHandle*) file;
138 if (handle == nullptr) {
139 return 0;
140 }
141
142 return handle->_file->get_file_size(handle->_stream);
143}
The name of a file, such as a texture file or an Egg file.
Definition filename.h:44
RocketFileInterface(VirtualFileSystem *vfs=nullptr)
Constructs a RocketFileInterface for the given VFS, or the default if NULL is given.
A hierarchy of directories and files that appears to be one continuous file system,...
bool resolve_filename(Filename &filename, const DSearchPath &searchpath, const std::string &default_extension=std::string()) const
Searches the given search path for the filename.
static void close_read_file(std::istream *stream)
Closes a file opened by a previous call to open_read_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.
static VirtualFileSystem * get_global_ptr()
Returns the default global VirtualFileSystem.
The abstract base class for a file or directory within the VirtualFileSystem.
Definition virtualFile.h:35
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.