/Rainman2/filestore_adaptors.cpp

http://modstudio2.googlecode.com/ · C++ · 213 lines · 161 code · 28 blank · 24 comment · 17 complexity · f9145f6a967dd7cd32712237a96fd876 MD5 · raw file

  1. /*
  2. Copyright (c) 2008 Peter "Corsix" Cawley
  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. #pragma once
  23. #include "filestore_adaptors.h"
  24. #include "exception.h"
  25. class ReadOnlyFileStoreDirectoryAdaptor : public IDirectory
  26. {
  27. public:
  28. ReadOnlyFileStoreDirectoryAdaptor(IDirectory *pDirectory, ReadOnlyFileStoreAdaptor *pAdaptor)
  29. : m_pDirectory(pDirectory), m_pAdaptor(pAdaptor)
  30. {
  31. }
  32. ~ReadOnlyFileStoreDirectoryAdaptor()
  33. {
  34. delete m_pDirectory;
  35. }
  36. virtual size_t getItemCount() throw()
  37. {
  38. return m_pDirectory->getItemCount();
  39. }
  40. virtual void getItemDetails(size_t iIndex, directory_item_t& oDetails) throw(...)
  41. {
  42. return m_pDirectory->getItemDetails(iIndex, oDetails);
  43. }
  44. virtual const RainString& getPath() throw()
  45. {
  46. return m_pDirectory->getPath();
  47. }
  48. virtual IFileStore* getStore() throw()
  49. {
  50. return m_pAdaptor;
  51. }
  52. virtual IFile* openFile(size_t iIndex, eFileOpenMode eMode) throw(...)
  53. {
  54. if(eMode == FM_Write)
  55. THROW_SIMPLE_(L"Cannot open index %lu for writing - it is read-only", static_cast<unsigned long>(iIndex));
  56. return m_pDirectory->openFile(iIndex, eMode);
  57. }
  58. virtual IFile* openFileNoThrow(size_t iIndex, eFileOpenMode eMode) throw()
  59. {
  60. if(eMode == FM_Write)
  61. return 0;
  62. return m_pDirectory->openFileNoThrow(iIndex, eMode);
  63. }
  64. virtual IDirectory* openDirectory(size_t iIndex) throw(...)
  65. {
  66. IDirectory* pDirectory = m_pDirectory->openDirectory(iIndex);
  67. IDirectory* pWrapped = new (std::nothrow) ReadOnlyFileStoreDirectoryAdaptor(pDirectory, m_pAdaptor);
  68. if(pWrapped == 0)
  69. {
  70. delete pDirectory;
  71. CHECK_ALLOCATION(pWrapped);
  72. }
  73. return pWrapped;
  74. }
  75. virtual IDirectory* openDirectoryNoThrow(size_t iIndex) throw()
  76. {
  77. IDirectory* pDirectory = m_pDirectory->openDirectoryNoThrow(iIndex);
  78. IDirectory* pWrapped = new (std::nothrow) ReadOnlyFileStoreDirectoryAdaptor(pDirectory, m_pAdaptor);
  79. if(pWrapped == 0)
  80. {
  81. delete pDirectory;
  82. return 0;
  83. }
  84. return pWrapped;
  85. }
  86. protected:
  87. IDirectory *m_pDirectory;
  88. ReadOnlyFileStoreAdaptor *m_pAdaptor;
  89. };
  90. ReadOnlyFileStoreAdaptor::ReadOnlyFileStoreAdaptor(IFileStore *pFileStore, bool bTakeOwnership) throw()
  91. : m_pFileStore(pFileStore), m_bOwnsFileStore(bTakeOwnership)
  92. {
  93. }
  94. ReadOnlyFileStoreAdaptor::~ReadOnlyFileStoreAdaptor() throw()
  95. {
  96. if(m_bOwnsFileStore)
  97. delete m_pFileStore;
  98. }
  99. void ReadOnlyFileStoreAdaptor::getCaps(file_store_caps_t& oCaps) const throw()
  100. {
  101. m_pFileStore->getCaps(oCaps);
  102. oCaps.bCanWriteFiles = false;
  103. oCaps.bCanDeleteFiles = false;
  104. oCaps.bCanCreateDirectories = false;
  105. oCaps.bCanDeleteDirectories = false;
  106. }
  107. IFile* ReadOnlyFileStoreAdaptor::openFile(const RainString& sPath, eFileOpenMode eMode) throw(...)
  108. {
  109. if(eMode == FM_Write)
  110. THROW_SIMPLE_(L"Cannot write to file \'%s\' - it is read-only", sPath.getCharacters());
  111. else
  112. return m_pFileStore->openFile(sPath, eMode);
  113. }
  114. IFile* ReadOnlyFileStoreAdaptor::openFileNoThrow(const RainString& sPath, eFileOpenMode eMode) throw()
  115. {
  116. if(eMode == FM_Write)
  117. return 0;
  118. else
  119. return m_pFileStore->openFileNoThrow(sPath, eMode);
  120. }
  121. bool ReadOnlyFileStoreAdaptor::doesFileExist(const RainString& sPath) throw()
  122. {
  123. return m_pFileStore->doesFileExist(sPath);
  124. }
  125. void ReadOnlyFileStoreAdaptor::deleteFile(const RainString& sPath) throw(...)
  126. {
  127. THROW_SIMPLE_(L"Cannot delete file \'%s\' - it is read-only", sPath.getCharacters());
  128. }
  129. bool ReadOnlyFileStoreAdaptor::deleteFileNoThrow(const RainString& sPath) throw()
  130. {
  131. return false;
  132. }
  133. size_t ReadOnlyFileStoreAdaptor::getEntryPointCount() throw()
  134. {
  135. return m_pFileStore->getEntryPointCount();
  136. }
  137. const RainString& ReadOnlyFileStoreAdaptor::getEntryPointName(size_t iIndex) throw(...)
  138. {
  139. return m_pFileStore->getEntryPointName(iIndex);
  140. }
  141. IDirectory* ReadOnlyFileStoreAdaptor::openDirectory(const RainString& sPath) throw(...)
  142. {
  143. IDirectory* pDirectory = m_pFileStore->openDirectory(sPath);
  144. IDirectory* pWrapped = new (std::nothrow) ReadOnlyFileStoreDirectoryAdaptor(pDirectory, this);
  145. if(pWrapped == 0)
  146. {
  147. delete pDirectory;
  148. CHECK_ALLOCATION(pWrapped);
  149. }
  150. return pWrapped;
  151. }
  152. IDirectory* ReadOnlyFileStoreAdaptor::openDirectoryNoThrow(const RainString& sPath) throw()
  153. {
  154. IDirectory* pDirectory = m_pFileStore->openDirectoryNoThrow(sPath);
  155. IDirectory* pWrapped = new (std::nothrow) ReadOnlyFileStoreDirectoryAdaptor(pDirectory, this);
  156. if(pWrapped == 0)
  157. {
  158. delete pDirectory;
  159. return 0;
  160. }
  161. return pWrapped;
  162. }
  163. bool ReadOnlyFileStoreAdaptor::doesDirectoryExist(const RainString& sPath) throw()
  164. {
  165. return m_pFileStore->doesDirectoryExist(sPath);
  166. }
  167. void ReadOnlyFileStoreAdaptor::createDirectory(const RainString& sPath) throw(...)
  168. {
  169. THROW_SIMPLE_(L"Cannot create directory \'%s\' in read-only file store", sPath.getCharacters());
  170. }
  171. bool ReadOnlyFileStoreAdaptor::createDirectoryNoThrow(const RainString& sPath) throw()
  172. {
  173. return false;
  174. }
  175. void ReadOnlyFileStoreAdaptor::deleteDirectory(const RainString& sPath) throw(...)
  176. {
  177. THROW_SIMPLE_(L"Cannot delete directory \'%s\' in read-only file store", sPath.getCharacters());
  178. }
  179. bool ReadOnlyFileStoreAdaptor::deleteDirectoryNoThrow(const RainString& sPath) throw()
  180. {
  181. return false;
  182. }