Stream.cpp 4.0 KB

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