Stream.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * This source file is part of libRocket, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://www.librocket.com
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #include "precompiled.h"
  28. #include "../../Include/Rocket/Core/Stream.h"
  29. #include <algorithm>
  30. #include <stdio.h>
  31. namespace Rocket {
  32. namespace Core {
  33. const size_t READ_BLOCK_SIZE = 1024;
  34. Stream::Stream() : ReferenceCountable(1)
  35. {
  36. stream_mode = 0;
  37. }
  38. Stream::~Stream()
  39. {
  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, Core::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.Length();
  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.CString(), string.Length());
  103. }
  104. // Push onto the front of the stream
  105. size_t Stream::PushFront(const void* ROCKET_UNUSED_PARAMETER(buffer), size_t ROCKET_UNUSED_PARAMETER(bytes))
  106. {
  107. ROCKET_UNUSED(buffer);
  108. ROCKET_UNUSED(bytes);
  109. ROCKET_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 ROCKET_UNUSED_PARAMETER(bytes))
  123. {
  124. ROCKET_UNUSED(bytes);
  125. ROCKET_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. // Deletes the stream.
  140. void Stream::OnReferenceDeactivate()
  141. {
  142. Close();
  143. delete this;
  144. }
  145. }
  146. }