DataStream.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /**
  2. * Copyright (c) 2006-2022 LOVE Development Team
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented; you must not
  13. * claim that you wrote the original software. If you use this software
  14. * in a product, an acknowledgment in the product documentation would be
  15. * appreciated but is not required.
  16. * 2. Altered source versions must be plainly marked as such, and must not be
  17. * misrepresented as being the original software.
  18. * 3. This notice may not be removed or altered from any source distribution.
  19. **/
  20. #include "DataStream.h"
  21. #include "common/Exception.h"
  22. #include "common/int.h"
  23. #include "common/Data.h"
  24. #include <algorithm>
  25. namespace love
  26. {
  27. namespace data
  28. {
  29. love::Type DataStream::type("DataStream", &Stream::type);
  30. DataStream::DataStream(Data *data)
  31. : data(data)
  32. , offset(0)
  33. , size(data->getSize())
  34. , memory((const uint8 *) data->getData())
  35. , writableMemory((uint8 *) data->getData()) // TODO: disallow writing sometimes?
  36. {
  37. }
  38. DataStream::DataStream(const DataStream &other)
  39. : data(other.data)
  40. , offset(0)
  41. , size(other.size)
  42. , memory(other.memory)
  43. , writableMemory(other.writableMemory)
  44. {
  45. }
  46. DataStream::~DataStream()
  47. {
  48. }
  49. DataStream *DataStream::clone()
  50. {
  51. return new DataStream(*this);
  52. }
  53. bool DataStream::isReadable() const
  54. {
  55. return true;
  56. }
  57. bool DataStream::isWritable() const
  58. {
  59. return writableMemory != nullptr;
  60. }
  61. bool DataStream::isSeekable() const
  62. {
  63. return true;
  64. }
  65. int64 DataStream::read(void* data, int64 size)
  66. {
  67. if (size <= 0)
  68. return 0;
  69. if (offset >= getSize())
  70. return 0;
  71. int64 readsize = std::min<int64>(size, getSize() - offset);
  72. memcpy(data, memory + offset, readsize);
  73. offset += readsize;
  74. return readsize;
  75. }
  76. bool DataStream::write(const void* data, int64 size)
  77. {
  78. if (size <= 0 || writableMemory == nullptr)
  79. return false;
  80. if (offset >= getSize())
  81. return false;
  82. int64 writesize = std::min<int64>(size, getSize() - offset);
  83. memcpy(writableMemory + offset, data, writesize);
  84. offset += writesize;
  85. return true;
  86. }
  87. bool DataStream::flush()
  88. {
  89. return true;
  90. }
  91. int64 DataStream::getSize()
  92. {
  93. return size;
  94. }
  95. bool DataStream::seek(int64 pos, SeekOrigin origin)
  96. {
  97. if (origin == SEEKORIGIN_CURRENT)
  98. pos += offset;
  99. else if (origin == SEEKORIGIN_END)
  100. pos += size;
  101. if (pos < 0 || pos > size)
  102. return false;
  103. offset = pos;
  104. return true;
  105. }
  106. int64 DataStream::tell()
  107. {
  108. return offset;
  109. }
  110. } // data
  111. } // love