OGRE  1.7
Object-Oriented Graphics Rendering Engine
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
OgreZip.h
Go to the documentation of this file.
1 /*
2 -----------------------------------------------------------------------------
3 This source file is part of OGRE
4 (Object-oriented Graphics Rendering Engine)
5 For the latest info, see http://www.ogre3d.org/
6 
7 Copyright (c) 2000-2011 Torus Knot Software Ltd
8 
9 Permission is hereby granted, free of charge, to any person obtaining a copy
10 of this software and associated documentation files (the "Software"), to deal
11 in the Software without restriction, including without limitation the rights
12 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 copies of the Software, and to permit persons to whom the Software is
14 furnished to do so, subject to the following conditions:
15 
16 The above copyright notice and this permission notice shall be included in
17 all copies or substantial portions of the Software.
18 
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 THE SOFTWARE.
26 -----------------------------------------------------------------------------
27 */
28 #ifndef __Zip_H__
29 #define __Zip_H__
30 
31 #include "OgrePrerequisites.h"
32 
33 #include "OgreArchive.h"
34 #include "OgreArchiveFactory.h"
35 
36 // Forward declaration for zziplib to avoid header file dependency.
37 typedef struct zzip_dir ZZIP_DIR;
38 typedef struct zzip_file ZZIP_FILE;
39 
40 namespace Ogre {
41 
54  class _OgreExport ZipArchive : public Archive
55  {
56  protected:
60  void checkZzipError(int zzipError, const String& operation) const;
63 
65  public:
66  ZipArchive(const String& name, const String& archType );
67  ~ZipArchive();
69  bool isCaseSensitive(void) const { return false; }
70 
72  void load();
74  void unload();
75 
77  DataStreamPtr open(const String& filename, bool readOnly = true) const;
78 
80  DataStreamPtr create(const String& filename) const;
81 
83  void remove(const String& filename) const;
84 
86  StringVectorPtr list(bool recursive = true, bool dirs = false);
87 
89  FileInfoListPtr listFileInfo(bool recursive = true, bool dirs = false);
90 
92  StringVectorPtr find(const String& pattern, bool recursive = true,
93  bool dirs = false);
94 
96  FileInfoListPtr findFileInfo(const String& pattern, bool recursive = true,
97  bool dirs = false);
98 
100  bool exists(const String& filename);
101 
103  time_t getModifiedTime(const String& filename);
104  };
105 
108  {
109  public:
110  virtual ~ZipArchiveFactory() {}
112  const String& getType(void) const;
114  Archive *createInstance( const String& name )
115  {
116  return OGRE_NEW ZipArchive(name, "Zip");
117  }
119  void destroyInstance( Archive* arch) { OGRE_DELETE arch; }
120  };
121 
124  template <size_t cacheSize>
126  {
127  protected:
129  char mBuffer[cacheSize];
130 
132  size_t mValidBytes;
134  size_t mPos;
135 
136  public:
139  {
140  mValidBytes = 0;
141  mPos = 0;
142  }
143 
146  size_t cacheData(const void* buf, size_t count)
147  {
148  assert(avail() == 0 && "It is assumed that you cache data only after you have read everything.");
149 
150  if (count < cacheSize)
151  {
152  // number of bytes written is less than total size of cache
153  if (count + mValidBytes <= cacheSize)
154  {
155  // just append
156  memcpy(mBuffer + mValidBytes, buf, count);
157  mValidBytes += count;
158  }
159  else
160  {
161  size_t begOff = count - (cacheSize - mValidBytes);
162  // override old cache content in the beginning
163  memmove(mBuffer, mBuffer + begOff, mValidBytes - begOff);
164  // append new data
165  memcpy(mBuffer + cacheSize - count, buf, count);
166  mValidBytes = cacheSize;
167  }
168  mPos = mValidBytes;
169  return count;
170  }
171  else
172  {
173  // discard all
174  memcpy(mBuffer, (const char*)buf + count - cacheSize, cacheSize);
175  mValidBytes = mPos = cacheSize;
176  return cacheSize;
177  }
178  }
180  size_t read(void* buf, size_t count)
181  {
182  size_t rb = avail();
183  rb = (rb < count) ? rb : count;
184  memcpy(buf, mBuffer + mPos, rb);
185  mPos += rb;
186  return rb;
187  }
188 
190  bool rewind(size_t count)
191  {
192  if (mPos < count)
193  {
194  clear();
195  return false;
196  }
197  else
198  {
199  mPos -= count;
200  return true;
201  }
202  }
204  bool ff(size_t count)
205  {
206  if (avail() < count)
207  {
208  clear();
209  return false;
210  }
211  else
212  {
213  mPos += count;
214  return true;
215  }
216  }
217 
219  size_t avail() const
220  {
221  return mValidBytes - mPos;
222  }
223 
225  void clear()
226  {
227  mValidBytes = 0;
228  mPos = 0;
229  }
230  };
231 
235  /*template <size_t cacheSize>
236  class NoStaticCache
237  {
238  public:
239  NoStaticCache() { }
240 
241  size_t cacheData(const void* buf, size_t count) { return 0; }
242  size_t read(void* buf, size_t count) { return 0; }
243 
244  bool rewind(size_t count) { return false; }
245  bool ff(size_t count) { return false; }
246 
247  size_t avail() const { return 0; }
248 
249  void clear() { }
250  };*/
251 
254  {
255  protected:
259  public:
261  ZipDataStream(ZZIP_FILE* zzipFile, size_t uncompressedSize);
263  ZipDataStream(const String& name, ZZIP_FILE* zzipFile, size_t uncompressedSize);
264  ~ZipDataStream();
266  size_t read(void* buf, size_t count);
268  size_t write(void* buf, size_t count);
270  void skip(long count);
272  void seek( size_t pos );
274  size_t tell(void) const;
276  bool eof(void) const;
278  void close(void);
279 
280 
281  };
282 
286 }
287 
288 #endif
#define OGRE_AUTO_MUTEX
ZZIP_FILE * mZzipFile
Definition: OgreZip.h:256
ZZIP_DIR * mZzipDir
Handle to root zip file.
Definition: OgreZip.h:58
char mBuffer[cacheSize]
Static buffer.
Definition: OgreZip.h:129
Archive * createInstance(const String &name)
Creates a new object.
Definition: OgreZip.h:114
#define _OgreExport
Definition: OgrePlatform.h:203
size_t cacheData(const void *buf, size_t count)
Cache data pointed by 'buf'.
Definition: OgreZip.h:146
struct zzip_dir ZZIP_DIR
Definition: OgreZip.h:37
Template version of cache based on static array.
Definition: OgreZip.h:125
bool rewind(size_t count)
Step back in cached stream by 'count' bytes.
Definition: OgreZip.h:190
size_t avail() const
Returns number of bytes available for reading in cache after rewinding.
Definition: OgreZip.h:219
Specialisation of the Archive class to allow reading of files from a zip format source archive...
Definition: OgreZip.h:54
#define _OgrePrivate
Definition: OgrePlatform.h:204
void clear()
Clear the cache.
Definition: OgreZip.h:225
#define OGRE_DELETE
Specialisation of ArchiveFactory for Zip files.
Definition: OgreZip.h:107
Abstract factory class, archive codec plugins can register concrete subclasses of this...
FileInfoList mFileList
File list (since zziplib seems to only allow scanning of dir tree once)
Definition: OgreZip.h:62
struct zzip_file ZZIP_FILE
Definition: OgreZip.h:38
vector< FileInfo >::type FileInfoList
Definition: OgreArchive.h:68
#define OGRE_NEW
bool ff(size_t count)
Step forward in cached stream by 'count' bytes.
Definition: OgreZip.h:204
size_t read(void *buf, size_t count)
Read data from cache to 'buf' (maximum 'count' bytes).
Definition: OgreZip.h:180
Archive-handling class.
Definition: OgreArchive.h:87
General purpose class used for encapsulating the reading and writing of data.
_StringBase String
Dummy version of cache to test no caching.
Definition: OgreZip.h:253
void destroyInstance(Archive *arch)
Destroys an object which was created by this factory.
Definition: OgreZip.h:119
StaticCache< 2 *OGRE_STREAM_TEMP_SIZE > mCache
We need caching because sometimes serializers step back in data stream and zziplib behaves slow...
Definition: OgreZip.h:258
virtual ~ZipArchiveFactory()
Definition: OgreZip.h:110
bool isCaseSensitive(void) const
Returns whether this archive is case sensitive in the way it matches files.
Definition: OgreZip.h:69
StaticCache()
Constructor.
Definition: OgreZip.h:138
size_t mValidBytes
Number of bytes valid in cache (written from the beginning of static buffer)
Definition: OgreZip.h:132
size_t mPos
Current read position.
Definition: OgreZip.h:134