StreamFile.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019-2023 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include "StreamFile.h"
  29. #include "../../Include/RmlUi/Core/Core.h"
  30. #include "../../Include/RmlUi/Core/FileInterface.h"
  31. #include "../../Include/RmlUi/Core/StringUtilities.h"
  32. namespace Rml {
  33. StreamFile::StreamFile()
  34. {
  35. file_handle = 0;
  36. length = 0;
  37. }
  38. StreamFile::~StreamFile()
  39. {
  40. if (file_handle)
  41. StreamFile::Close();
  42. }
  43. bool StreamFile::Open(const String& path)
  44. {
  45. String url_safe_path = StringUtilities::Replace(path, ':', '|');
  46. SetStreamDetails(URL(url_safe_path), Stream::MODE_READ);
  47. if (file_handle)
  48. Close();
  49. // Fix the path if a leading colon has been replaced with a pipe.
  50. String fixed_path = StringUtilities::Replace(path, '|', ':');
  51. file_handle = GetFileInterface()->Open(fixed_path);
  52. if (!file_handle)
  53. {
  54. Log::Message(Log::LT_WARNING, "Unable to open file %s.", fixed_path.c_str());
  55. return false;
  56. }
  57. GetLength();
  58. return true;
  59. }
  60. void StreamFile::Close()
  61. {
  62. if (file_handle)
  63. {
  64. GetFileInterface()->Close(file_handle);
  65. file_handle = 0;
  66. }
  67. length = 0;
  68. Stream::Close();
  69. }
  70. size_t StreamFile::Length() const
  71. {
  72. return length;
  73. }
  74. size_t StreamFile::Tell() const
  75. {
  76. return GetFileInterface()->Tell(file_handle);
  77. }
  78. bool StreamFile::Seek(long offset, int origin) const
  79. {
  80. return GetFileInterface()->Seek(file_handle, offset, origin);
  81. }
  82. size_t StreamFile::Read(void* buffer, size_t bytes) const
  83. {
  84. return GetFileInterface()->Read(buffer, bytes, file_handle);
  85. }
  86. size_t StreamFile::Write(const void* /*buffer*/, size_t /*bytes*/)
  87. {
  88. RMLUI_ERROR;
  89. return 0;
  90. }
  91. size_t StreamFile::Truncate(size_t /*bytes*/)
  92. {
  93. RMLUI_ERROR;
  94. return 0;
  95. }
  96. bool StreamFile::IsReadReady()
  97. {
  98. return Tell() < Length();
  99. }
  100. bool StreamFile::IsWriteReady()
  101. {
  102. return false;
  103. }
  104. void StreamFile::GetLength()
  105. {
  106. length = GetFileInterface()->Length(file_handle);
  107. }
  108. } // namespace Rml