CInterfaceIOWrapper.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2020, assimp team
  6. All rights reserved.
  7. Redistribution and use of this software in source and binary forms,
  8. with or without modification, are permitted provided that the following
  9. conditions are met:
  10. * Redistributions of source code must retain the above
  11. copyright notice, this list of conditions and the
  12. following disclaimer.
  13. * Redistributions in binary form must reproduce the above
  14. copyright notice, this list of conditions and the
  15. following disclaimer in the documentation and/or other
  16. materials provided with the distribution.
  17. * Neither the name of the assimp team, nor the names of its
  18. contributors may be used to endorse or promote products
  19. derived from this software without specific prior
  20. written permission of the assimp team.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. ---------------------------------------------------------------------------
  33. */
  34. /** @file aiFileIO -> IOSystem wrapper*/
  35. #include "CInterfaceIOWrapper.h"
  36. namespace Assimp {
  37. CIOStreamWrapper::~CIOStreamWrapper(void) {
  38. /* Various places depend on this destructor to close the file */
  39. if (mFile) {
  40. mIO->mFileSystem->CloseProc(mIO->mFileSystem, mFile);
  41. mFile = nullptr;
  42. }
  43. }
  44. // ...................................................................
  45. size_t CIOStreamWrapper::Read(void *pvBuffer,
  46. size_t pSize,
  47. size_t pCount) {
  48. // need to typecast here as C has no void*
  49. return mFile->ReadProc(mFile, (char *)pvBuffer, pSize, pCount);
  50. }
  51. // ...................................................................
  52. size_t CIOStreamWrapper::Write(const void *pvBuffer,
  53. size_t pSize,
  54. size_t pCount) {
  55. // need to typecast here as C has no void*
  56. return mFile->WriteProc(mFile, (const char *)pvBuffer, pSize, pCount);
  57. }
  58. // ...................................................................
  59. aiReturn CIOStreamWrapper::Seek(size_t pOffset,
  60. aiOrigin pOrigin) {
  61. return mFile->SeekProc(mFile, pOffset, pOrigin);
  62. }
  63. // ...................................................................
  64. size_t CIOStreamWrapper::Tell(void) const {
  65. return mFile->TellProc(mFile);
  66. }
  67. // ...................................................................
  68. size_t CIOStreamWrapper::FileSize() const {
  69. return mFile->FileSizeProc(mFile);
  70. }
  71. // ...................................................................
  72. void CIOStreamWrapper::Flush() {
  73. return mFile->FlushProc(mFile);
  74. }
  75. // ------------------------------------------------------------------------------------------------
  76. // Custom IOStream implementation for the C-API
  77. bool CIOSystemWrapper::Exists(const char *pFile) const {
  78. aiFile *p = mFileSystem->OpenProc(mFileSystem, pFile, "rb");
  79. if (p) {
  80. mFileSystem->CloseProc(mFileSystem, p);
  81. return true;
  82. }
  83. return false;
  84. }
  85. // ...................................................................
  86. char CIOSystemWrapper::getOsSeparator() const {
  87. #ifndef _WIN32
  88. return '/';
  89. #else
  90. return '\\';
  91. #endif
  92. }
  93. // ...................................................................
  94. IOStream *CIOSystemWrapper::Open(const char *pFile, const char *pMode) {
  95. aiFile *p = mFileSystem->OpenProc(mFileSystem, pFile, pMode);
  96. if (!p) {
  97. return nullptr;
  98. }
  99. return new CIOStreamWrapper(p, this);
  100. }
  101. // ...................................................................
  102. void CIOSystemWrapper::Close(IOStream *pFile) {
  103. if (!pFile) {
  104. return;
  105. }
  106. delete pFile;
  107. }
  108. } // namespace Assimp