Stream.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 "../../Include/RmlUi/Core/Stream.h"
  29. #include <stdio.h>
  30. #include <string.h>
  31. namespace Rml {
  32. const size_t READ_BLOCK_SIZE = 1024;
  33. Stream::Stream()
  34. {
  35. stream_mode = 0;
  36. }
  37. Stream::~Stream()
  38. {
  39. Stream::Close();
  40. }
  41. void Stream::Close()
  42. {
  43. stream_mode = 0;
  44. }
  45. int Stream::GetStreamMode() const
  46. {
  47. return stream_mode;
  48. }
  49. const URL& Stream::GetSourceURL() const
  50. {
  51. return url;
  52. }
  53. bool Stream::IsEOS() const
  54. {
  55. return Tell() >= Length();
  56. }
  57. size_t Stream::Peek(void* buffer, size_t bytes) const
  58. {
  59. size_t pos = Tell();
  60. size_t read = Read(buffer, bytes);
  61. Seek((long)pos, SEEK_SET);
  62. return read;
  63. }
  64. size_t Stream::Read(Stream* stream, size_t bytes) const
  65. {
  66. byte buffer[READ_BLOCK_SIZE];
  67. size_t total_bytes_read = 0;
  68. while (total_bytes_read < bytes)
  69. {
  70. size_t bytes_read = this->Read(buffer, Math::Min(READ_BLOCK_SIZE, bytes - total_bytes_read));
  71. if (bytes_read < 1)
  72. return total_bytes_read;
  73. stream->Write(buffer, bytes_read);
  74. total_bytes_read += bytes_read;
  75. }
  76. return total_bytes_read;
  77. }
  78. size_t Stream::Read(String& string, size_t bytes) const
  79. {
  80. size_t string_size = string.size();
  81. string.resize(string_size + bytes + 1);
  82. size_t read = Read(&string[string_size], bytes);
  83. string[string_size + read] = '\0';
  84. string.resize(string_size + read);
  85. return read;
  86. }
  87. size_t Stream::Write(const Stream* stream, size_t bytes)
  88. {
  89. return stream->Read(this, bytes);
  90. }
  91. size_t Stream::Write(const char* string)
  92. {
  93. return Write(string, strlen(string));
  94. }
  95. size_t Stream::Write(const String& string)
  96. {
  97. return Write(string.c_str(), string.size());
  98. }
  99. size_t Stream::PushFront(const void* /*buffer*/, size_t /*bytes*/)
  100. {
  101. RMLUI_ERRORMSG("No generic way to PushFront to a stream.");
  102. return false;
  103. }
  104. size_t Stream::PushBack(const void* buffer, size_t bytes)
  105. {
  106. size_t pos = Tell();
  107. Seek(0, SEEK_END);
  108. size_t wrote = Write(buffer, bytes);
  109. Seek((long)pos, SEEK_SET);
  110. return wrote;
  111. }
  112. size_t Stream::PopFront(size_t /*bytes*/)
  113. {
  114. RMLUI_ERRORMSG("No generic way to PopFront from a stream.");
  115. return 0;
  116. }
  117. size_t Stream::PopBack(size_t bytes)
  118. {
  119. return Truncate(Length() - bytes);
  120. }
  121. void Stream::SetStreamDetails(const URL& _url, int _stream_mode)
  122. {
  123. url = _url;
  124. stream_mode = _stream_mode;
  125. }
  126. } // namespace Rml