Visualization Library v1.0.3A lightweight C++ OpenGL middleware for 2D/3D graphics |
[Download] [Tutorials] [All Classes] [Grouped Classes] |
00001 /**************************************************************************************/ 00002 /* */ 00003 /* Visualization Library */ 00004 /* http://visualizationlibrary.org */ 00005 /* */ 00006 /* Copyright (c) 2005-2010, Michele Bosi */ 00007 /* All rights reserved. */ 00008 /* */ 00009 /* Redistribution and use in source and binary forms, with or without modification, */ 00010 /* are permitted provided that the following conditions are met: */ 00011 /* */ 00012 /* - Redistributions of source code must retain the above copyright notice, this */ 00013 /* list of conditions and the following disclaimer. */ 00014 /* */ 00015 /* - Redistributions in binary form must reproduce the above copyright notice, this */ 00016 /* list of conditions and the following disclaimer in the documentation and/or */ 00017 /* other materials provided with the distribution. */ 00018 /* */ 00019 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND */ 00020 /* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED */ 00021 /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */ 00022 /* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR */ 00023 /* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */ 00024 /* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; */ 00025 /* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON */ 00026 /* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */ 00027 /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */ 00028 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ 00029 /* */ 00030 /**************************************************************************************/ 00031 00032 #ifndef ZippedFile_INCLUDE_ONCE 00033 #define ZippedFile_INCLUDE_ONCE 00034 00035 #include <vlCore/VirtualFile.hpp> 00036 struct z_stream_s; 00037 00038 namespace vl 00039 { 00040 //--------------------------------------------------------------------------- 00041 // ZippedFileInfo 00042 //--------------------------------------------------------------------------- 00046 class ZippedFileInfo: public Object 00047 { 00048 friend class ZippedFile; 00049 friend class ZippedDirectory; 00050 00051 VL_INSTRUMENT_CLASS(vl::ZippedFileInfo, Object) 00052 00053 public: 00054 ZippedFileInfo() 00055 { 00056 mVersionNeeded = 0; 00057 mGeneralPurposeFlag = 0; 00058 mCompressionMethod = 0; 00059 mCRC32 = 0; 00060 mCompressedSize = 0; 00061 mUncompressedSize = 0; 00062 mFileNameLength = 0; 00063 mExtraFieldLength = 0; 00064 mSecond = 0; 00065 mMinute = 0; 00066 mHour = 0; 00067 mDay = 0; 00068 mMonth = 0; 00069 mYear = 0; 00070 mZippedFileOffset = 0; 00071 } 00072 00073 public: 00074 unsigned short versionNeeded() const { return mVersionNeeded; } 00075 unsigned short generalPurposeFlag() const { return mGeneralPurposeFlag; } 00076 unsigned short compressionMethod() const { return mCompressionMethod; } 00077 unsigned int crc32() const { return mCRC32; } 00078 unsigned int compressedSize() const { return mCompressedSize; } 00079 unsigned int uncompressedSize() const { return mUncompressedSize; } 00080 unsigned short fileNameLength() const { return mFileNameLength; } 00081 unsigned short extraFieldLength() const { return mExtraFieldLength; } 00082 int second() const { return mSecond; } 00083 int minute() const { return mMinute; } 00084 int hour() const { return mHour; } 00085 int day() const { return mDay; } 00086 int month() const { return mMonth; } 00087 int year() const { return mYear; } 00088 const String& path() const { return mFileName; } 00089 // offset of the compressed data in the source zip file 00090 unsigned int zippedFileOffset() const { return mZippedFileOffset; } 00091 // source stream used to seek and read the compressed zip data 00092 const VirtualFile* sourceZipFile() const { return mSourceZipFile.get(); } 00093 VirtualFile* sourceZipFile() { return mSourceZipFile.get(); } 00094 void setSourceZipFile(VirtualFile* file) { mSourceZipFile = file; } 00095 00096 public: 00097 unsigned short mVersionNeeded; 00098 unsigned short mGeneralPurposeFlag; 00099 unsigned short mCompressionMethod; 00100 unsigned int mCRC32; 00101 unsigned int mCompressedSize; 00102 unsigned int mUncompressedSize; 00103 unsigned short mFileNameLength; 00104 unsigned short mExtraFieldLength; 00105 int mSecond; 00106 int mMinute; 00107 int mHour; 00108 int mDay; 00109 int mMonth; 00110 int mYear; 00111 String mFileName; 00112 // offset of the compressed data in the zip file 00113 unsigned int mZippedFileOffset; 00114 // source stream used to seek and read the compressed zip data 00115 ref<VirtualFile> mSourceZipFile; 00116 }; 00117 //--------------------------------------------------------------------------- 00118 // ZippedFile 00119 //--------------------------------------------------------------------------- 00133 class VLCORE_EXPORT ZippedFile: public VirtualFile 00134 { 00135 VL_INSTRUMENT_CLASS(vl::ZippedFile, VirtualFile) 00136 00137 // Lower this if you need to limit the amount of data allocated to the stack, for example to 16K. 00138 static const int CHUNK_SIZE = 128*1024; 00139 00140 public: 00141 00142 ZippedFile(); 00143 ~ZippedFile(); 00144 00145 const ZippedFileInfo* zippedFileInfo() const; 00146 00147 ZippedFileInfo* zippedFileInfo(); 00148 00149 void setZippedFileInfo(ZippedFileInfo* info); 00150 00153 virtual bool exists() const; 00154 00155 virtual bool open(EOpenMode mode); 00156 00157 virtual bool isOpen() const; 00158 00159 virtual void close(); 00160 00161 virtual long long size() const; 00162 00163 bool extract(char* destination, bool check_sum = true); 00164 00165 ZippedFile& operator=(const ZippedFile& other) 00166 { 00167 close(); 00168 super::operator=(other); 00169 mZippedFileInfo = new ZippedFileInfo(*other.mZippedFileInfo); 00170 if (mZippedFileInfo->sourceZipFile()) 00171 { 00172 ref<VirtualFile> src_zip_copy = mZippedFileInfo->sourceZipFile()->clone(); 00173 mZippedFileInfo->setSourceZipFile(src_zip_copy.get()); 00174 } 00175 return *this; 00176 } 00177 00178 virtual ref<VirtualFile> clone() const; 00179 00180 void resetStream(); 00181 00182 protected: 00183 virtual long long read_Implementation(void* buffer, long long bytes_to_read); 00184 00185 virtual long long write_Implementation(const void* /*buffer*/, long long /*byte_count*/) { return 0; } // not supported yet 00186 00187 virtual bool fillUncompressedBuffer(); 00188 00189 virtual long long position_Implementation() const { return mReadBytes; } 00190 00191 virtual bool seekSet_Implementation(long long); 00192 00193 protected: 00194 ref<ZippedFileInfo> mZippedFileInfo; 00195 long long mReadBytes; 00196 00197 z_stream_s* mZStream; 00198 unsigned char mZipBufferIn[CHUNK_SIZE]; 00199 unsigned char mZipBufferOut[CHUNK_SIZE]; 00200 std::vector<char> mUncompressedBuffer; 00201 int mUncompressedBufferPtr; 00202 }; 00203 //--------------------------------------------------------------------------- 00204 // utilty functions 00205 bool compress(const void* data, size_t size, std::vector<unsigned char>& out, int level); 00206 bool decompress(const void* cdata, size_t csize, void* data_out); 00207 //--------------------------------------------------------------------------- 00208 } 00209 00210 #endif