rocketFileInterface.cxx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. #include "rocketFileInterface.h"
  15. #include "virtualFileSystem.h"
  16. ////////////////////////////////////////////////////////////////////
  17. // Function: RocketFileInterface::Constructor
  18. // Access: Public
  19. // Description: Constructs a RocketFileInterface for the given
  20. // VFS, or the default if NULL is given.
  21. ////////////////////////////////////////////////////////////////////
  22. RocketFileInterface::
  23. RocketFileInterface(VirtualFileSystem *vfs) : _vfs(vfs) {
  24. if (_vfs == NULL) {
  25. _vfs = VirtualFileSystem::get_global_ptr();
  26. }
  27. }
  28. ////////////////////////////////////////////////////////////////////
  29. // Function: RocketFileInterface::Open
  30. // Access: Public
  31. // Description:
  32. ////////////////////////////////////////////////////////////////////
  33. Rocket::Core::FileHandle RocketFileInterface::
  34. Open(const Rocket::Core::String& path) {
  35. rocket_cat.debug() << "Opening " << path.CString() << "\n";
  36. Filename fn = Filename::from_os_specific(path.CString());
  37. void *ptr = (void*) _vfs->open_read_file(fn, true);
  38. if (ptr == NULL) {
  39. rocket_cat.error() << "Failed to open " << fn << "\n";
  40. }
  41. // A FileHandle is actually just a void pointer
  42. return (Rocket::Core::FileHandle) ptr;
  43. }
  44. ////////////////////////////////////////////////////////////////////
  45. // Function: RocketFileInterface::Close
  46. // Access: Public
  47. // Description:
  48. ////////////////////////////////////////////////////////////////////
  49. void RocketFileInterface::
  50. Close(Rocket::Core::FileHandle file) {
  51. if ((istream*) file != (istream*) NULL) {
  52. _vfs->close_read_file((istream*) file);
  53. }
  54. }
  55. ////////////////////////////////////////////////////////////////////
  56. // Function: RocketFileInterface::Read
  57. // Access: Public
  58. // Description:
  59. ////////////////////////////////////////////////////////////////////
  60. size_t RocketFileInterface::
  61. Read(void* buffer, size_t size, Rocket::Core::FileHandle file) {
  62. istream* const stream = (istream*) file;
  63. if (stream == (istream*) NULL) {
  64. return 0;
  65. }
  66. stream->read((char*) buffer, size);
  67. return stream->gcount();
  68. }
  69. ////////////////////////////////////////////////////////////////////
  70. // Function: RocketFileInterface::Seek
  71. // Access: Public
  72. // Description:
  73. ////////////////////////////////////////////////////////////////////
  74. bool RocketFileInterface::
  75. Seek(Rocket::Core::FileHandle file, long offset, int origin) {
  76. istream* stream = (istream*) file;
  77. if (stream == (istream*) NULL) {
  78. return false;
  79. }
  80. switch(origin) {
  81. case SEEK_SET:
  82. stream->seekg(offset, ios::beg);
  83. break;
  84. case SEEK_CUR:
  85. stream->seekg(offset, ios::cur);
  86. break;
  87. case SEEK_END:
  88. stream->seekg(offset, ios::end);
  89. };
  90. return !stream->fail();
  91. }
  92. ////////////////////////////////////////////////////////////////////
  93. // Function: RocketFileInterface::Tell
  94. // Access: Public
  95. // Description:
  96. ////////////////////////////////////////////////////////////////////
  97. size_t RocketFileInterface::
  98. Tell(Rocket::Core::FileHandle file) {
  99. if ((istream*) file == (istream*) NULL) {
  100. return -1;
  101. }
  102. return ((istream*) file)->tellg();
  103. }