Stream.cpp 4.1 KB

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