CMemoryFile.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // Copyright (C) 2002-2012 Nikolaus Gebhardt
  2. // This file is part of the "Irrlicht Engine".
  3. // For conditions of distribution and use, see copyright notice in irrlicht.h
  4. #include "CMemoryFile.h"
  5. #include "irrString.h"
  6. namespace irr
  7. {
  8. namespace io
  9. {
  10. CMemoryReadFile::CMemoryReadFile(const void *memory, long len, const io::path &fileName, bool d) :
  11. Buffer(memory), Len(len), Pos(0), Filename(fileName), deleteMemoryWhenDropped(d)
  12. {}
  13. CMemoryReadFile::~CMemoryReadFile()
  14. {
  15. if (deleteMemoryWhenDropped)
  16. delete[] (c8 *)Buffer;
  17. }
  18. //! returns how much was read
  19. size_t CMemoryReadFile::read(void *buffer, size_t sizeToRead)
  20. {
  21. long amount = static_cast<long>(sizeToRead);
  22. if (Pos + amount > Len)
  23. amount -= Pos + amount - Len;
  24. if (amount <= 0)
  25. return 0;
  26. c8 *p = (c8 *)Buffer;
  27. memcpy(buffer, p + Pos, amount);
  28. Pos += amount;
  29. return static_cast<size_t>(amount);
  30. }
  31. //! changes position in file, returns true if successful
  32. //! if relativeMovement==true, the pos is changed relative to current pos,
  33. //! otherwise from begin of file
  34. bool CMemoryReadFile::seek(long finalPos, bool relativeMovement)
  35. {
  36. if (relativeMovement) {
  37. if (Pos + finalPos < 0 || Pos + finalPos > Len)
  38. return false;
  39. Pos += finalPos;
  40. } else {
  41. if (finalPos < 0 || finalPos > Len)
  42. return false;
  43. Pos = finalPos;
  44. }
  45. return true;
  46. }
  47. //! returns size of file
  48. long CMemoryReadFile::getSize() const
  49. {
  50. return Len;
  51. }
  52. //! returns where in the file we are.
  53. long CMemoryReadFile::getPos() const
  54. {
  55. return Pos;
  56. }
  57. //! returns name of file
  58. const io::path &CMemoryReadFile::getFileName() const
  59. {
  60. return Filename;
  61. }
  62. CMemoryWriteFile::CMemoryWriteFile(void *memory, long len, const io::path &fileName, bool d) :
  63. Buffer(memory), Len(len), Pos(0), Filename(fileName), deleteMemoryWhenDropped(d)
  64. {}
  65. CMemoryWriteFile::~CMemoryWriteFile()
  66. {
  67. if (deleteMemoryWhenDropped)
  68. delete[] (c8 *)Buffer;
  69. }
  70. //! returns how much was written
  71. size_t CMemoryWriteFile::write(const void *buffer, size_t sizeToWrite)
  72. {
  73. long amount = (long)sizeToWrite;
  74. if (Pos + amount > Len)
  75. amount -= Pos + amount - Len;
  76. if (amount <= 0)
  77. return 0;
  78. c8 *p = (c8 *)Buffer;
  79. memcpy(p + Pos, buffer, amount);
  80. Pos += amount;
  81. return (size_t)amount;
  82. }
  83. //! changes position in file, returns true if successful
  84. //! if relativeMovement==true, the pos is changed relative to current pos,
  85. //! otherwise from begin of file
  86. bool CMemoryWriteFile::seek(long finalPos, bool relativeMovement)
  87. {
  88. if (relativeMovement) {
  89. if (Pos + finalPos < 0 || Pos + finalPos > Len)
  90. return false;
  91. Pos += finalPos;
  92. } else {
  93. if (finalPos < 0 || finalPos > Len)
  94. return false;
  95. Pos = finalPos;
  96. }
  97. return true;
  98. }
  99. //! returns where in the file we are.
  100. long CMemoryWriteFile::getPos() const
  101. {
  102. return Pos;
  103. }
  104. //! returns name of file
  105. const io::path &CMemoryWriteFile::getFileName() const
  106. {
  107. return Filename;
  108. }
  109. bool CMemoryWriteFile::flush()
  110. {
  111. return true; // no buffering, so nothing to do
  112. }
  113. IReadFile *createMemoryReadFile(const void *memory, long size, const io::path &fileName, bool deleteMemoryWhenDropped)
  114. {
  115. CMemoryReadFile *file = new CMemoryReadFile(memory, size, fileName, deleteMemoryWhenDropped);
  116. return file;
  117. }
  118. IWriteFile *createMemoryWriteFile(void *memory, long size, const io::path &fileName, bool deleteMemoryWhenDropped)
  119. {
  120. CMemoryWriteFile *file = new CMemoryWriteFile(memory, size, fileName, deleteMemoryWhenDropped);
  121. return file;
  122. }
  123. } // end namespace io
  124. } // end namespace irr