OgreZip.h
Go to the documentation of this file.
00001 /*
00002 -----------------------------------------------------------------------------
00003 This source file is part of OGRE
00004 (Object-oriented Graphics Rendering Engine)
00005 For the latest info, see http://www.ogre3d.org/
00006 
00007 Copyright (c) 2000-2011 Torus Knot Software Ltd
00008 
00009 Permission is hereby granted, free of charge, to any person obtaining a copy
00010 of this software and associated documentation files (the "Software"), to deal
00011 in the Software without restriction, including without limitation the rights
00012 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00013 copies of the Software, and to permit persons to whom the Software is
00014 furnished to do so, subject to the following conditions:
00015 
00016 The above copyright notice and this permission notice shall be included in
00017 all copies or substantial portions of the Software.
00018 
00019 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00020 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00021 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00022 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00023 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00024 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00025 THE SOFTWARE.
00026 -----------------------------------------------------------------------------
00027 */
00028 #ifndef __Zip_H__
00029 #define __Zip_H__
00030 
00031 #include "OgrePrerequisites.h"
00032 
00033 #include "OgreArchive.h"
00034 #include "OgreArchiveFactory.h"
00035 
00036 // Forward declaration for zziplib to avoid header file dependency.
00037 typedef struct zzip_dir     ZZIP_DIR;
00038 typedef struct zzip_file    ZZIP_FILE;
00039 
00040 namespace Ogre {
00041 
00054     class _OgreExport ZipArchive : public Archive 
00055     {
00056     protected:
00058         ZZIP_DIR* mZzipDir;
00060         void checkZzipError(int zzipError, const String& operation) const;
00062         FileInfoList mFileList;
00063 
00064         OGRE_AUTO_MUTEX
00065     public:
00066         ZipArchive(const String& name, const String& archType );
00067         ~ZipArchive();
00069         bool isCaseSensitive(void) const { return false; }
00070 
00072         void load();
00074         void unload();
00075 
00077         DataStreamPtr open(const String& filename, bool readOnly = true) const;
00078 
00080         DataStreamPtr create(const String& filename) const;
00081 
00083         void remove(const String& filename) const;
00084 
00086         StringVectorPtr list(bool recursive = true, bool dirs = false);
00087 
00089         FileInfoListPtr listFileInfo(bool recursive = true, bool dirs = false);
00090 
00092         StringVectorPtr find(const String& pattern, bool recursive = true,
00093             bool dirs = false);
00094 
00096         FileInfoListPtr findFileInfo(const String& pattern, bool recursive = true,
00097             bool dirs = false);
00098 
00100         bool exists(const String& filename);
00101 
00103         time_t getModifiedTime(const String& filename);
00104     };
00105 
00107     class _OgrePrivate ZipArchiveFactory : public ArchiveFactory
00108     {
00109     public:
00110         virtual ~ZipArchiveFactory() {}
00112         const String& getType(void) const;
00114         Archive *createInstance( const String& name ) 
00115         {
00116             return OGRE_NEW ZipArchive(name, "Zip");
00117         }
00119         void destroyInstance( Archive* arch) { OGRE_DELETE arch; }
00120     };
00121 
00124     template <size_t cacheSize>
00125     class StaticCache
00126     {
00127     protected:
00129         char mBuffer[cacheSize];
00130 
00132         size_t mValidBytes;
00134         size_t mPos;
00135 
00136     public:
00138         StaticCache()
00139         {
00140             mValidBytes = 0;
00141             mPos = 0;
00142         }
00143 
00146         size_t cacheData(const void* buf, size_t count)
00147         {
00148             assert(avail() == 0 && "It is assumed that you cache data only after you have read everything.");
00149 
00150             if (count < cacheSize)
00151             {
00152                 // number of bytes written is less than total size of cache
00153                 if (count + mValidBytes <= cacheSize)
00154                 {
00155                     // just append
00156                     memcpy(mBuffer + mValidBytes, buf, count);
00157                     mValidBytes += count;
00158                 }
00159                 else
00160                 {
00161                     size_t begOff = count - (cacheSize - mValidBytes);
00162                     // override old cache content in the beginning
00163                     memmove(mBuffer, mBuffer + begOff, mValidBytes - begOff);
00164                     // append new data
00165                     memcpy(mBuffer + cacheSize - count, buf, count);
00166                     mValidBytes = cacheSize;
00167                 }
00168                 mPos = mValidBytes;
00169                 return count;
00170             }
00171             else
00172             {
00173                 // discard all
00174                 memcpy(mBuffer, (const char*)buf + count - cacheSize, cacheSize);
00175                 mValidBytes = mPos = cacheSize;
00176                 return cacheSize;
00177             }
00178         }
00180         size_t read(void* buf, size_t count)
00181         {
00182             size_t rb = avail();
00183             rb = (rb < count) ? rb : count;
00184             memcpy(buf, mBuffer + mPos, rb);
00185             mPos += rb;
00186             return rb;
00187         }
00188 
00190         bool rewind(size_t count)
00191         {
00192             if (mPos < count)
00193             {
00194                 clear();
00195                 return false;
00196             }
00197             else
00198             {
00199                 mPos -= count;
00200                 return true;
00201             }
00202         }
00204         bool ff(size_t count)
00205         {
00206             if (avail() < count)
00207             {
00208                 clear();
00209                 return false;
00210             }
00211             else
00212             {
00213                 mPos += count;
00214                 return true;
00215             }
00216         }
00217 
00219         size_t avail() const
00220         {
00221             return mValidBytes - mPos;
00222         }
00223 
00225         void clear()
00226         {
00227             mValidBytes = 0;
00228             mPos = 0;
00229         }
00230     };
00231 
00235     /*template <size_t cacheSize>
00236     class NoStaticCache
00237     {
00238     public:
00239         NoStaticCache() { }
00240 
00241         size_t cacheData(const void* buf, size_t count) { return 0; }
00242         size_t read(void* buf, size_t count) { return 0; }
00243 
00244         bool rewind(size_t count) { return false; }
00245         bool ff(size_t count) { return false; }
00246 
00247         size_t avail() const { return 0; }
00248 
00249         void clear() { }
00250     };*/
00251 
00253     class _OgrePrivate ZipDataStream : public DataStream
00254     {
00255     protected:
00256         ZZIP_FILE* mZzipFile;
00258         StaticCache<2 * OGRE_STREAM_TEMP_SIZE> mCache;
00259     public:
00261         ZipDataStream(ZZIP_FILE* zzipFile, size_t uncompressedSize);
00263         ZipDataStream(const String& name, ZZIP_FILE* zzipFile, size_t uncompressedSize);
00264         ~ZipDataStream();
00266         size_t read(void* buf, size_t count);
00268         size_t write(void* buf, size_t count);
00270         void skip(long count);
00272         void seek( size_t pos );
00274         size_t tell(void) const;
00276         bool eof(void) const;
00278         void close(void);
00279 
00280 
00281     };
00282 
00286 }
00287 
00288 #endif

Copyright © 2008 Torus Knot Software Ltd
Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Last modified Sat Jan 14 2012 18:40:44