Panda3D

rocketFileInterface.cxx

00001 // Filename: rocketFileInterface.cxx
00002 // Created by:  rdb (03Nov11)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
00012 //
00013 ////////////////////////////////////////////////////////////////////
00014 
00015 #include "rocketFileInterface.h"
00016 #include "virtualFileSystem.h"
00017 
00018 ////////////////////////////////////////////////////////////////////
00019 //     Function: RocketFileInterface::Constructor
00020 //       Access: Public
00021 //  Description: Constructs a RocketFileInterface for the given
00022 //               VFS, or the default if NULL is given.
00023 ////////////////////////////////////////////////////////////////////
00024 RocketFileInterface::
00025 RocketFileInterface(VirtualFileSystem *vfs) : _vfs(vfs) {
00026   if (_vfs == NULL) {
00027     _vfs = VirtualFileSystem::get_global_ptr();
00028   }
00029 }
00030 
00031 ////////////////////////////////////////////////////////////////////
00032 //     Function: RocketFileInterface::Open
00033 //       Access: Public
00034 //  Description: 
00035 ////////////////////////////////////////////////////////////////////
00036 Rocket::Core::FileHandle RocketFileInterface::
00037 Open(const Rocket::Core::String& path) {
00038   rocket_cat.debug() << "Opening " << path.CString() << "\n";
00039 
00040   Filename fn = Filename::from_os_specific(path.CString());
00041   void *ptr = (void*) _vfs->open_read_file(fn, true);
00042 
00043   if (ptr == NULL) {
00044     rocket_cat.error() << "Failed to open " << fn << "\n";
00045   }
00046 
00047   // A FileHandle is actually just a void pointer
00048   return (Rocket::Core::FileHandle) ptr;
00049 }
00050 
00051 ////////////////////////////////////////////////////////////////////
00052 //     Function: RocketFileInterface::Close
00053 //       Access: Public
00054 //  Description: 
00055 ////////////////////////////////////////////////////////////////////
00056 void RocketFileInterface::
00057 Close(Rocket::Core::FileHandle file) {
00058   if ((istream*) file != (istream*) NULL) {
00059     _vfs->close_read_file((istream*) file);
00060   }
00061 }
00062 
00063 ////////////////////////////////////////////////////////////////////
00064 //     Function: RocketFileInterface::Read
00065 //       Access: Public
00066 //  Description: 
00067 ////////////////////////////////////////////////////////////////////
00068 size_t RocketFileInterface::
00069 Read(void* buffer, size_t size, Rocket::Core::FileHandle file) {
00070   istream* const stream = (istream*) file;
00071   if (stream == (istream*) NULL) {
00072     return 0;
00073   }
00074 
00075   stream->read((char*) buffer, size);
00076   return stream->gcount();
00077 }
00078 
00079 ////////////////////////////////////////////////////////////////////
00080 //     Function: RocketFileInterface::Seek
00081 //       Access: Public
00082 //  Description: 
00083 ////////////////////////////////////////////////////////////////////
00084 bool RocketFileInterface::
00085 Seek(Rocket::Core::FileHandle file, long offset, int origin) {
00086   istream* stream = (istream*) file;
00087   if (stream == (istream*) NULL) {
00088     return false;
00089   }
00090 
00091   switch(origin) {
00092   case SEEK_SET:
00093     stream->seekg(offset, ios::beg);
00094     break;
00095   case SEEK_CUR:
00096     stream->seekg(offset, ios::cur);
00097     break;
00098   case SEEK_END:
00099     stream->seekg(offset, ios::end);
00100   };
00101 
00102   return !stream->fail();
00103 }
00104 
00105 ////////////////////////////////////////////////////////////////////
00106 //     Function: RocketFileInterface::Tell
00107 //       Access: Public   
00108 //  Description: 
00109 ////////////////////////////////////////////////////////////////////
00110 size_t RocketFileInterface::
00111 Tell(Rocket::Core::FileHandle file) {
00112   if ((istream*) file == (istream*) NULL) {
00113     return -1;
00114   }
00115   return ((istream*) file)->tellg();
00116 }
 All Classes Functions Variables Enumerations