PageRenderTime 39ms CodeModel.GetById 21ms app.highlight 15ms RepoModel.GetById 1ms app.codeStats 0ms

/Rainman2/filestore_adaptors.cpp

http://modstudio2.googlecode.com/
C++ | 213 lines | 161 code | 28 blank | 24 comment | 17 complexity | f9145f6a967dd7cd32712237a96fd876 MD5 | raw file
  1/*
  2Copyright (c) 2008 Peter "Corsix" Cawley
  3
  4Permission is hereby granted, free of charge, to any person
  5obtaining a copy of this software and associated documentation
  6files (the "Software"), to deal in the Software without
  7restriction, including without limitation the rights to use,
  8copy, modify, merge, publish, distribute, sublicense, and/or sell
  9copies of the Software, and to permit persons to whom the
 10Software is furnished to do so, subject to the following
 11conditions:
 12
 13The above copyright notice and this permission notice shall be
 14included in all copies or substantial portions of the Software.
 15
 16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 17EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 18OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 19NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 20HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 21WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 22FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 23OTHER DEALINGS IN THE SOFTWARE.
 24*/
 25#pragma once
 26#include "filestore_adaptors.h"
 27#include "exception.h"
 28
 29class ReadOnlyFileStoreDirectoryAdaptor : public IDirectory
 30{
 31public:
 32  ReadOnlyFileStoreDirectoryAdaptor(IDirectory *pDirectory, ReadOnlyFileStoreAdaptor *pAdaptor)
 33    : m_pDirectory(pDirectory), m_pAdaptor(pAdaptor)
 34  {
 35  }
 36
 37  ~ReadOnlyFileStoreDirectoryAdaptor()
 38  {
 39    delete m_pDirectory;
 40  }
 41
 42  virtual size_t getItemCount() throw()
 43  {
 44    return m_pDirectory->getItemCount();
 45  }
 46
 47  virtual void getItemDetails(size_t iIndex, directory_item_t& oDetails) throw(...)
 48  {
 49    return m_pDirectory->getItemDetails(iIndex, oDetails);
 50  }
 51
 52  virtual const RainString& getPath() throw()
 53  {
 54    return m_pDirectory->getPath();
 55  }
 56
 57  virtual IFileStore* getStore() throw()
 58  {
 59    return m_pAdaptor;
 60  }
 61
 62  virtual IFile* openFile(size_t iIndex, eFileOpenMode eMode) throw(...)
 63  {
 64    if(eMode == FM_Write)
 65      THROW_SIMPLE_(L"Cannot open index %lu for writing - it is read-only", static_cast<unsigned long>(iIndex));
 66    return m_pDirectory->openFile(iIndex, eMode);
 67  }
 68
 69  virtual IFile* openFileNoThrow(size_t iIndex, eFileOpenMode eMode) throw()
 70  {
 71    if(eMode == FM_Write)
 72      return 0;
 73    return m_pDirectory->openFileNoThrow(iIndex, eMode);
 74  }
 75
 76  virtual IDirectory* openDirectory(size_t iIndex) throw(...)
 77  {
 78    IDirectory* pDirectory = m_pDirectory->openDirectory(iIndex);
 79    IDirectory* pWrapped = new (std::nothrow) ReadOnlyFileStoreDirectoryAdaptor(pDirectory, m_pAdaptor);
 80    if(pWrapped == 0)
 81    {
 82      delete pDirectory;
 83      CHECK_ALLOCATION(pWrapped);
 84    }
 85    return pWrapped;
 86  }
 87
 88  virtual IDirectory* openDirectoryNoThrow(size_t iIndex) throw()
 89  {
 90    IDirectory* pDirectory = m_pDirectory->openDirectoryNoThrow(iIndex);
 91    IDirectory* pWrapped = new (std::nothrow) ReadOnlyFileStoreDirectoryAdaptor(pDirectory, m_pAdaptor);
 92    if(pWrapped == 0)
 93    {
 94      delete pDirectory;
 95      return 0;
 96    }
 97    return pWrapped;
 98  }
 99
100protected:
101  IDirectory *m_pDirectory;
102  ReadOnlyFileStoreAdaptor *m_pAdaptor;
103};
104
105ReadOnlyFileStoreAdaptor::ReadOnlyFileStoreAdaptor(IFileStore *pFileStore, bool bTakeOwnership) throw()
106  : m_pFileStore(pFileStore), m_bOwnsFileStore(bTakeOwnership)
107{
108}
109
110ReadOnlyFileStoreAdaptor::~ReadOnlyFileStoreAdaptor() throw()
111{
112  if(m_bOwnsFileStore)
113    delete m_pFileStore;
114}
115
116void ReadOnlyFileStoreAdaptor::getCaps(file_store_caps_t& oCaps) const throw()
117{
118  m_pFileStore->getCaps(oCaps);
119  oCaps.bCanWriteFiles = false;
120  oCaps.bCanDeleteFiles = false;
121  oCaps.bCanCreateDirectories = false;
122  oCaps.bCanDeleteDirectories = false;
123}
124
125IFile* ReadOnlyFileStoreAdaptor::openFile(const RainString& sPath, eFileOpenMode eMode) throw(...)
126{
127  if(eMode == FM_Write)
128    THROW_SIMPLE_(L"Cannot write to file \'%s\' - it is read-only", sPath.getCharacters());
129  else
130    return m_pFileStore->openFile(sPath, eMode);
131}
132
133IFile* ReadOnlyFileStoreAdaptor::openFileNoThrow(const RainString& sPath, eFileOpenMode eMode) throw()
134{
135  if(eMode == FM_Write)
136    return 0;
137  else
138    return m_pFileStore->openFileNoThrow(sPath, eMode);
139}
140
141bool ReadOnlyFileStoreAdaptor::doesFileExist(const RainString& sPath) throw()
142{
143  return m_pFileStore->doesFileExist(sPath);
144}
145
146void ReadOnlyFileStoreAdaptor::deleteFile(const RainString& sPath) throw(...)
147{
148  THROW_SIMPLE_(L"Cannot delete file \'%s\' - it is read-only", sPath.getCharacters());
149}
150
151bool ReadOnlyFileStoreAdaptor::deleteFileNoThrow(const RainString& sPath) throw()
152{
153  return false;
154}
155
156size_t ReadOnlyFileStoreAdaptor::getEntryPointCount() throw()
157{
158  return m_pFileStore->getEntryPointCount();
159}
160
161const RainString& ReadOnlyFileStoreAdaptor::getEntryPointName(size_t iIndex) throw(...)
162{
163  return m_pFileStore->getEntryPointName(iIndex);
164}
165
166IDirectory* ReadOnlyFileStoreAdaptor::openDirectory(const RainString& sPath) throw(...)
167{
168  IDirectory* pDirectory = m_pFileStore->openDirectory(sPath);
169  IDirectory* pWrapped = new (std::nothrow) ReadOnlyFileStoreDirectoryAdaptor(pDirectory, this);
170  if(pWrapped == 0)
171  {
172    delete pDirectory;
173    CHECK_ALLOCATION(pWrapped);
174  }
175  return pWrapped;
176}
177
178IDirectory* ReadOnlyFileStoreAdaptor::openDirectoryNoThrow(const RainString& sPath) throw()
179{
180  IDirectory* pDirectory = m_pFileStore->openDirectoryNoThrow(sPath);
181  IDirectory* pWrapped = new (std::nothrow) ReadOnlyFileStoreDirectoryAdaptor(pDirectory, this);
182  if(pWrapped == 0)
183  {
184    delete pDirectory;
185    return 0;
186  }
187  return pWrapped;
188}
189
190bool ReadOnlyFileStoreAdaptor::doesDirectoryExist(const RainString& sPath) throw()
191{
192  return m_pFileStore->doesDirectoryExist(sPath);
193}
194
195void ReadOnlyFileStoreAdaptor::createDirectory(const RainString& sPath) throw(...)
196{
197  THROW_SIMPLE_(L"Cannot create directory \'%s\' in read-only file store", sPath.getCharacters());
198}
199
200bool ReadOnlyFileStoreAdaptor::createDirectoryNoThrow(const RainString& sPath) throw()
201{
202  return false;
203}
204
205void ReadOnlyFileStoreAdaptor::deleteDirectory(const RainString& sPath) throw(...)
206{
207  THROW_SIMPLE_(L"Cannot delete directory \'%s\' in read-only file store", sPath.getCharacters());
208}
209
210bool ReadOnlyFileStoreAdaptor::deleteDirectoryNoThrow(const RainString& sPath) throw()
211{
212  return false;
213}