Panda3D
rocketFileInterface.cxx
1 // Filename: rocketFileInterface.cxx
2 // Created by: rdb (03Nov11)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #include "rocketFileInterface.h"
16 #include "virtualFileSystem.h"
17 
18 ////////////////////////////////////////////////////////////////////
19 // Function: RocketFileInterface::Constructor
20 // Access: Public
21 // Description: Constructs a RocketFileInterface for the given
22 // VFS, or the default if NULL is given.
23 ////////////////////////////////////////////////////////////////////
26  if (_vfs == NULL) {
28  }
29 }
30 
31 ////////////////////////////////////////////////////////////////////
32 // Function: RocketFileInterface::Open
33 // Access: Public
34 // Description:
35 ////////////////////////////////////////////////////////////////////
36 Rocket::Core::FileHandle RocketFileInterface::
37 Open(const Rocket::Core::String& path) {
38  rocket_cat.debug() << "Opening " << path.CString() << "\n";
39 
40  Filename fn = Filename::from_os_specific(path.CString());
41 
42  PT(VirtualFile) file = _vfs->get_file(fn);
43  if (file == NULL) {
44  // failed? Try model-path as a Panda-friendly fallback.
45  if (!_vfs->resolve_filename(fn, get_model_path())) {
46  rocket_cat.error() << "Could not resolve " << fn
47  << " along the model-path (currently: " << get_model_path() << ")\n";
48  return (Rocket::Core::FileHandle) NULL;
49  }
50 
51  file = _vfs->get_file(fn);
52  if (file == NULL) {
53  rocket_cat.error() << "Failed to get " << fn << ", found on model-path\n";
54  return (Rocket::Core::FileHandle) NULL;
55  }
56  }
57 
58  istream *str = file->open_read_file(true);
59  if (str == NULL) {
60  rocket_cat.error() << "Failed to open " << fn << " for reading\n";
61  return (Rocket::Core::FileHandle) NULL;
62  }
63 
64  VirtualFileHandle *handle = new VirtualFileHandle;
65  handle->_file = file;
66  handle->_stream = str;
67 
68  // A FileHandle is actually just a void pointer.
69  return (Rocket::Core::FileHandle) handle;
70 }
71 
72 ////////////////////////////////////////////////////////////////////
73 // Function: RocketFileInterface::Close
74 // Access: Public
75 // Description:
76 ////////////////////////////////////////////////////////////////////
77 void RocketFileInterface::
78 Close(Rocket::Core::FileHandle file) {
79  VirtualFileHandle *handle = (VirtualFileHandle*) file;
80  if (handle == NULL) {
81  return;
82  }
83 
84  _vfs->close_read_file(handle->_stream);
85  delete handle;
86 }
87 
88 ////////////////////////////////////////////////////////////////////
89 // Function: RocketFileInterface::Read
90 // Access: Public
91 // Description:
92 ////////////////////////////////////////////////////////////////////
93 size_t RocketFileInterface::
94 Read(void* buffer, size_t size, Rocket::Core::FileHandle file) {
95  VirtualFileHandle *handle = (VirtualFileHandle*) file;
96  if (handle == NULL) {
97  return 0;
98  }
99 
100  handle->_stream->read((char*) buffer, size);
101  return handle->_stream->gcount();
102 }
103 
104 ////////////////////////////////////////////////////////////////////
105 // Function: RocketFileInterface::Seek
106 // Access: Public
107 // Description:
108 ////////////////////////////////////////////////////////////////////
109 bool RocketFileInterface::
110 Seek(Rocket::Core::FileHandle file, long offset, int origin) {
111  VirtualFileHandle *handle = (VirtualFileHandle*) file;
112  if (handle == NULL) {
113  return false;
114  }
115 
116  switch(origin) {
117  case SEEK_SET:
118  handle->_stream->seekg(offset, ios::beg);
119  break;
120  case SEEK_CUR:
121  handle->_stream->seekg(offset, ios::cur);
122  break;
123  case SEEK_END:
124  handle->_stream->seekg(offset, ios::end);
125  };
126 
127  return !handle->_stream->fail();
128 }
129 
130 ////////////////////////////////////////////////////////////////////
131 // Function: RocketFileInterface::Tell
132 // Access: Public
133 // Description:
134 ////////////////////////////////////////////////////////////////////
135 size_t RocketFileInterface::
136 Tell(Rocket::Core::FileHandle file) {
137  VirtualFileHandle *handle = (VirtualFileHandle*) file;
138  if (handle == NULL) {
139  return 0;
140  }
141 
142  return handle->_stream->tellg();
143 }
144 
145 ////////////////////////////////////////////////////////////////////
146 // Function: RocketFileInterface::Length
147 // Access: Public
148 // Description:
149 ////////////////////////////////////////////////////////////////////
150 size_t RocketFileInterface::
151 Length(Rocket::Core::FileHandle file) {
152  VirtualFileHandle *handle = (VirtualFileHandle*) file;
153  if (handle == NULL) {
154  return 0;
155  }
156 
157  return handle->_file->get_file_size(handle->_stream);
158 }
bool resolve_filename(Filename &filename, const DSearchPath &searchpath, const string &default_extension=string()) const
Searches the given search path for the filename.
RocketFileInterface(VirtualFileSystem *vfs=NULL)
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, even though the files may originate from several different sources that may not be related to the actual OS&#39;s file system.
The abstract base class for a file or directory within the VirtualFileSystem.
Definition: virtualFile.h:37
static void close_read_file(istream *stream)
Closes a file opened by a previous call to open_read_file().
The name of a file, such as a texture file or an Egg file.
Definition: filename.h:44
static VirtualFileSystem * get_global_ptr()
Returns the default global VirtualFileSystem.
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 Filename from_os_specific(const string &os_specific, Type type=T_general)
This named constructor returns a Panda-style filename (that is, using forward slashes, and no drive letter) based on the supplied filename string that describes a filename in the local system conventions (for instance, on Windows, it may use backslashes or begin with a drive letter and a colon).
Definition: filename.cxx:332