MemoryFile.cpp 5.4 KB

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