2
0

Stream.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 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. Close();
  40. }
  41. void Stream::Close()
  42. {
  43. stream_mode = 0;
  44. }
  45. // Returns the mode the stream was opened in.
  46. int Stream::GetStreamMode() const
  47. {
  48. return stream_mode;
  49. }
  50. // Returns the source url (if available)
  51. const URL& Stream::GetSourceURL() const
  52. {
  53. return url;
  54. }
  55. bool Stream::IsEOS() const
  56. {
  57. return Tell() >= Length();
  58. }
  59. size_t Stream::Peek(void* buffer, size_t bytes) const
  60. {
  61. size_t pos = Tell();
  62. size_t read = Read( buffer, bytes );
  63. Seek( (long)pos, SEEK_SET );
  64. return read;
  65. }
  66. // Read from one stream into another
  67. size_t Stream::Read(Stream* stream, size_t bytes) const
  68. {
  69. byte buffer[ READ_BLOCK_SIZE ];
  70. size_t total_bytes_read = 0;
  71. while (total_bytes_read < bytes)
  72. {
  73. size_t bytes_read = this->Read(buffer, Math::Min(READ_BLOCK_SIZE, bytes - total_bytes_read));
  74. if (bytes_read < 1)
  75. return total_bytes_read;
  76. stream->Write(buffer, bytes_read);
  77. total_bytes_read += bytes_read;
  78. }
  79. return total_bytes_read;
  80. }
  81. // Read from one stream into another
  82. size_t Stream::Read(String& string, size_t bytes) const
  83. {
  84. size_t string_size = string.size();
  85. string.resize(string_size + bytes + 1);
  86. size_t read = Read(&string[string_size], bytes);
  87. string[string_size + read] = '\0';
  88. string.resize(string_size + read);
  89. return read;
  90. }
  91. /// Write to this stream from another stream
  92. size_t Stream::Write(const Stream* stream, size_t bytes)
  93. {
  94. return stream->Read(this, bytes);
  95. }
  96. size_t Stream::Write(const char* string)
  97. {
  98. return Write(string, strlen(string));
  99. }
  100. size_t Stream::Write(const String& string)
  101. {
  102. return Write(string.c_str(), string.size());
  103. }
  104. // Push onto the front of the stream
  105. size_t Stream::PushFront(const void* RMLUI_UNUSED_PARAMETER(buffer), size_t RMLUI_UNUSED_PARAMETER(bytes))
  106. {
  107. RMLUI_UNUSED(buffer);
  108. RMLUI_UNUSED(bytes);
  109. RMLUI_ERRORMSG("No generic way to PushFront to a stream.");
  110. return false;
  111. }
  112. // Push onto the back of the stream
  113. size_t Stream::PushBack(const void* buffer, size_t bytes)
  114. {
  115. size_t pos = Tell();
  116. Seek(0, SEEK_END);
  117. size_t wrote = Write(buffer, bytes);
  118. Seek((long)pos, SEEK_SET);
  119. return wrote;
  120. }
  121. // Push onto the front of the stream
  122. size_t Stream::PopFront(size_t RMLUI_UNUSED_PARAMETER(bytes))
  123. {
  124. RMLUI_UNUSED(bytes);
  125. RMLUI_ERRORMSG("No generic way to PopFront from a stream.");
  126. return 0;
  127. }
  128. // Push onto the back of the stream
  129. size_t Stream::PopBack(size_t bytes)
  130. {
  131. return Truncate(Length() - bytes);
  132. }
  133. // Sets the mode on the stream; should be called by a stream when it is opened.
  134. void Stream::SetStreamDetails(const URL& _url, int _stream_mode)
  135. {
  136. url = _url;
  137. stream_mode = _stream_mode;
  138. }
  139. } // namespace Rml