StreamFile.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #include "StreamFile.h"
  2. #include "../../Include/RmlUi/Core/Core.h"
  3. #include "../../Include/RmlUi/Core/FileInterface.h"
  4. #include "../../Include/RmlUi/Core/StringUtilities.h"
  5. namespace Rml {
  6. StreamFile::StreamFile()
  7. {
  8. file_handle = 0;
  9. length = 0;
  10. }
  11. StreamFile::~StreamFile()
  12. {
  13. if (file_handle)
  14. StreamFile::Close();
  15. }
  16. bool StreamFile::Open(const String& path)
  17. {
  18. String url_safe_path = StringUtilities::Replace(path, ':', '|');
  19. SetStreamDetails(URL(url_safe_path), Stream::MODE_READ);
  20. if (file_handle)
  21. Close();
  22. // Fix the path if a leading colon has been replaced with a pipe.
  23. String fixed_path = StringUtilities::Replace(path, '|', ':');
  24. file_handle = GetFileInterface()->Open(fixed_path);
  25. if (!file_handle)
  26. {
  27. Log::Message(Log::LT_WARNING, "Unable to open file %s.", fixed_path.c_str());
  28. return false;
  29. }
  30. GetLength();
  31. return true;
  32. }
  33. void StreamFile::Close()
  34. {
  35. if (file_handle)
  36. {
  37. GetFileInterface()->Close(file_handle);
  38. file_handle = 0;
  39. }
  40. length = 0;
  41. Stream::Close();
  42. }
  43. size_t StreamFile::Length() const
  44. {
  45. return length;
  46. }
  47. size_t StreamFile::Tell() const
  48. {
  49. return GetFileInterface()->Tell(file_handle);
  50. }
  51. bool StreamFile::Seek(long offset, int origin) const
  52. {
  53. return GetFileInterface()->Seek(file_handle, offset, origin);
  54. }
  55. size_t StreamFile::Read(void* buffer, size_t bytes) const
  56. {
  57. return GetFileInterface()->Read(buffer, bytes, file_handle);
  58. }
  59. size_t StreamFile::Write(const void* /*buffer*/, size_t /*bytes*/)
  60. {
  61. RMLUI_ERROR;
  62. return 0;
  63. }
  64. size_t StreamFile::Truncate(size_t /*bytes*/)
  65. {
  66. RMLUI_ERROR;
  67. return 0;
  68. }
  69. bool StreamFile::IsReadReady()
  70. {
  71. return Tell() < Length();
  72. }
  73. bool StreamFile::IsWriteReady()
  74. {
  75. return false;
  76. }
  77. void StreamFile::GetLength()
  78. {
  79. length = GetFileInterface()->Length(file_handle);
  80. }
  81. } // namespace Rml