Stream.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #include "../../Include/RmlUi/Core/Stream.h"
  2. #include <stdio.h>
  3. #include <string.h>
  4. namespace Rml {
  5. const size_t READ_BLOCK_SIZE = 1024;
  6. Stream::Stream()
  7. {
  8. stream_mode = 0;
  9. }
  10. Stream::~Stream()
  11. {
  12. Stream::Close();
  13. }
  14. void Stream::Close()
  15. {
  16. stream_mode = 0;
  17. }
  18. int Stream::GetStreamMode() const
  19. {
  20. return stream_mode;
  21. }
  22. const URL& Stream::GetSourceURL() const
  23. {
  24. return url;
  25. }
  26. bool Stream::IsEOS() const
  27. {
  28. return Tell() >= Length();
  29. }
  30. size_t Stream::Peek(void* buffer, size_t bytes) const
  31. {
  32. size_t pos = Tell();
  33. size_t read = Read(buffer, bytes);
  34. Seek((long)pos, SEEK_SET);
  35. return read;
  36. }
  37. size_t Stream::Read(Stream* stream, size_t bytes) const
  38. {
  39. byte buffer[READ_BLOCK_SIZE];
  40. size_t total_bytes_read = 0;
  41. while (total_bytes_read < bytes)
  42. {
  43. size_t bytes_read = this->Read(buffer, Math::Min(READ_BLOCK_SIZE, bytes - total_bytes_read));
  44. if (bytes_read < 1)
  45. return total_bytes_read;
  46. stream->Write(buffer, bytes_read);
  47. total_bytes_read += bytes_read;
  48. }
  49. return total_bytes_read;
  50. }
  51. size_t Stream::Read(String& string, size_t bytes) const
  52. {
  53. size_t string_size = string.size();
  54. string.resize(string_size + bytes + 1);
  55. size_t read = Read(&string[string_size], bytes);
  56. string[string_size + read] = '\0';
  57. string.resize(string_size + read);
  58. return read;
  59. }
  60. size_t Stream::Write(const Stream* stream, size_t bytes)
  61. {
  62. return stream->Read(this, bytes);
  63. }
  64. size_t Stream::Write(const char* string)
  65. {
  66. return Write(string, strlen(string));
  67. }
  68. size_t Stream::Write(const String& string)
  69. {
  70. return Write(string.c_str(), string.size());
  71. }
  72. size_t Stream::PushFront(const void* /*buffer*/, size_t /*bytes*/)
  73. {
  74. RMLUI_ERRORMSG("No generic way to PushFront to a stream.");
  75. return false;
  76. }
  77. size_t Stream::PushBack(const void* buffer, size_t bytes)
  78. {
  79. size_t pos = Tell();
  80. Seek(0, SEEK_END);
  81. size_t wrote = Write(buffer, bytes);
  82. Seek((long)pos, SEEK_SET);
  83. return wrote;
  84. }
  85. size_t Stream::PopFront(size_t /*bytes*/)
  86. {
  87. RMLUI_ERRORMSG("No generic way to PopFront from a stream.");
  88. return 0;
  89. }
  90. size_t Stream::PopBack(size_t bytes)
  91. {
  92. return Truncate(Length() - bytes);
  93. }
  94. void Stream::SetStreamDetails(const URL& _url, int _stream_mode)
  95. {
  96. url = _url;
  97. stream_mode = _stream_mode;
  98. }
  99. } // namespace Rml