MemoryFile.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  3. Permission is hereby granted, free of charge, to any person
  4. obtaining a copy of this software and associated documentation
  5. files (the "Software"), to deal in the Software without
  6. restriction, including without limitation the rights to use,
  7. copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the
  9. Software is furnished to do so, subject to the following
  10. conditions:
  11. The above copyright notice and this permission notice shall be
  12. included in all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  15. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  17. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  18. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. OTHER DEALINGS IN THE SOFTWARE.
  21. */
  22. #include <stdlib.h>
  23. #include "MemoryFile.h"
  24. #include "MathUtils.h"
  25. #include "Log.h"
  26. #include "Types.h"
  27. #include "Allocator.h"
  28. #include "OS.h"
  29. namespace crown
  30. {
  31. //-----------------------------------------------------------------------------
  32. MemoryBuffer::MemoryBuffer()
  33. {
  34. }
  35. //-----------------------------------------------------------------------------
  36. MemoryBuffer::~MemoryBuffer()
  37. {
  38. }
  39. //-----------------------------------------------------------------------------
  40. DynamicMemoryBuffer::DynamicMemoryBuffer(Allocator& allocator, size_t initial_capacity) :
  41. m_allocator(allocator),
  42. m_buffer(NULL)
  43. {
  44. m_buffer = (uint8_t*)m_allocator.allocate(initial_capacity);
  45. }
  46. //-----------------------------------------------------------------------------
  47. DynamicMemoryBuffer::~DynamicMemoryBuffer()
  48. {
  49. if (m_buffer)
  50. {
  51. m_allocator.deallocate(m_buffer);
  52. }
  53. }
  54. //-----------------------------------------------------------------------------
  55. void DynamicMemoryBuffer::check_space(size_t offset, size_t size)
  56. {
  57. if (offset + size > m_capacity)
  58. {
  59. m_capacity = (size_t) ((offset + size) * 1.2f);
  60. // FIXME FIXME FIXME
  61. m_buffer = (uint8_t*) realloc(m_buffer, m_capacity);
  62. }
  63. }
  64. //-----------------------------------------------------------------------------
  65. void DynamicMemoryBuffer::write(uint8_t* src, size_t offset, size_t size)
  66. {
  67. check_space(offset, size);
  68. for (size_t i = 0; i < size; i++)
  69. {
  70. m_buffer[offset + i] = src[i];
  71. }
  72. //If the writing goes beyond the end of buffer
  73. if (offset + size > this->m_size)
  74. {
  75. this->m_size = offset + size;
  76. }
  77. }
  78. //-----------------------------------------------------------------------------
  79. void DynamicMemoryBuffer::release()
  80. {
  81. // FIXME
  82. }
  83. //-----------------------------------------------------------------------------
  84. void DynamicMemoryBuffer::allocate(size_t capacity)
  85. {
  86. // FIXME
  87. }
  88. //-----------------------------------------------------------------------------
  89. MemoryFile::MemoryFile(MemoryBuffer* buffer, FileOpenMode mode) :
  90. File(mode),
  91. m_memory(buffer),
  92. m_memory_offset(0)
  93. {
  94. }
  95. //-----------------------------------------------------------------------------
  96. MemoryFile::~MemoryFile()
  97. {
  98. }
  99. //-----------------------------------------------------------------------------
  100. void MemoryFile::seek(size_t position)
  101. {
  102. check_valid();
  103. m_memory_offset = position;
  104. // Allow seek to m_memory->size() position, that means end of file,
  105. // reading not allowed but you can write if it's dynamic
  106. CE_ASSERT(m_memory_offset <= m_memory->size(), "Trying to seek beyond end of file");
  107. }
  108. //-----------------------------------------------------------------------------
  109. void MemoryFile::seek_to_end()
  110. {
  111. check_valid();
  112. m_memory_offset = m_memory->size() - 1;
  113. }
  114. //-----------------------------------------------------------------------------
  115. void MemoryFile::skip(size_t bytes)
  116. {
  117. check_valid();
  118. m_memory_offset += bytes;
  119. //Allow seek to m_memory->getSize() position, that means end of file, reading not allowed but you can write if it's dynamic
  120. CE_ASSERT(m_memory_offset <= m_memory->size(), "Trying to skip beyond end of file");
  121. }
  122. //-----------------------------------------------------------------------------
  123. void MemoryFile::read(void* buffer, size_t size)
  124. {
  125. check_valid();
  126. uint8_t* src = m_memory->data();
  127. uint8_t* dest = (uint8_t*) buffer;
  128. if (m_memory_offset + size > m_memory->size())
  129. {
  130. Log::e("Trying to read beyond the end of file.");
  131. }
  132. for (size_t i = 0; i < size; i++)
  133. {
  134. dest[i] = src[m_memory_offset+i];
  135. }
  136. m_memory_offset += size;
  137. }
  138. //-----------------------------------------------------------------------------
  139. bool MemoryFile::copy_to(File& file, size_t size)
  140. {
  141. check_valid();
  142. file.write(&(m_memory->data()[m_memory_offset]), math::min(m_memory->size()-m_memory_offset, size));
  143. return true;
  144. }
  145. //-----------------------------------------------------------------------------
  146. void MemoryFile::write(const void* buffer, size_t size)
  147. {
  148. check_valid();
  149. m_memory->write((uint8_t*)buffer, m_memory_offset, size);
  150. m_memory_offset += size;
  151. }
  152. //-----------------------------------------------------------------------------
  153. void MemoryFile::flush()
  154. {
  155. return;
  156. }
  157. //-----------------------------------------------------------------------------
  158. void MemoryFile::dump()
  159. {
  160. uint8_t* buff = m_memory->data();
  161. for (size_t i = 0; i < m_memory->size(); i++)
  162. {
  163. os::printf("%3i ", buff[i]);
  164. }
  165. }
  166. } // namespace crown