00001 /* 00002 Copyright (C) 2005 The Pentagram team 00003 00004 This program is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU General Public License 00006 as published by the Free Software Foundation; either version 2 00007 of the License, or (at your option) any later version. 00008 00009 This program is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 GNU General Public License for more details. 00013 00014 You should have received a copy of the GNU General Public License 00015 along with this program; if not, write to the Free Software 00016 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00017 */ 00018 00019 #include "pent_include.h" 00020 00021 #include "Archive.h" 00022 #include "IDataSource.h" 00023 #include "ArchiveFile.h" 00024 00025 #include "ZipFile.h" 00026 #include "FlexFile.h" 00027 #include "U8SaveFile.h" 00028 #include "DirFile.h" 00029 00030 namespace Pentagram { 00031 00032 DEFINE_RUNTIME_CLASSTYPE_CODE_BASE_CLASS(Archive); 00033 00034 Archive::Archive() 00035 { 00036 count = 0; 00037 } 00038 00039 Archive::~Archive() 00040 { 00041 for (unsigned int i = 0; i < sources.size(); ++i) 00042 delete sources[i]; 00043 sources.clear(); 00044 } 00045 00046 00047 Archive::Archive(ArchiveFile* af) 00048 { 00049 count = 0; 00050 addSource(af); 00051 } 00052 00053 Archive::Archive(IDataSource* ids) 00054 { 00055 count = 0; 00056 addSource(ids); 00057 } 00058 00059 Archive::Archive(const std::string& path) 00060 { 00061 count = 0; 00062 addSource(path); 00063 } 00064 00065 bool Archive::addSource(ArchiveFile* af) 00066 { 00067 sources.push_back(af); 00068 00069 uint32 indexcount = af->getIndexCount(); 00070 if (indexcount > count) count = indexcount; 00071 00072 return true; 00073 } 00074 00075 bool Archive::addSource(IDataSource* ids) 00076 { 00077 ArchiveFile* s = 0; 00078 00079 if (FlexFile::isFlexFile(ids)) { 00080 s = new FlexFile(ids); 00081 } else if (U8SaveFile::isU8SaveFile(ids)) { 00082 s = new U8SaveFile(ids); 00083 } else if (ZipFile::isZipFile(ids)) { 00084 s = new ZipFile(ids); 00085 } 00086 00087 if (!s) return false; 00088 if (!s->isValid()) { 00089 delete s; 00090 return false; 00091 } 00092 00093 return addSource(s); 00094 } 00095 00096 bool Archive::addSource(const std::string& path) 00097 { 00098 ArchiveFile* s = new DirFile(path); 00099 if (!s->isValid()) { 00100 delete s; 00101 return false; 00102 } 00103 00104 return addSource(s); 00105 } 00106 00107 void Archive::cache() 00108 { 00109 for (unsigned int i = 0; i < count; ++i) 00110 cache(i); 00111 } 00112 00113 void Archive::uncache() 00114 { 00115 for (unsigned int i = 0; i < count; ++i) 00116 uncache(i); 00117 } 00118 00119 uint8* Archive::getRawObject(uint32 index, uint32* sizep) 00120 { 00121 ArchiveFile* f = findArchiveFile(index); 00122 if (!f) return 0; 00123 00124 return f->getObject(index, sizep); 00125 } 00126 00127 uint32 Archive::getRawSize(uint32 index) 00128 { 00129 ArchiveFile* f = findArchiveFile(index); 00130 if (!f) return 0; 00131 00132 return f->getSize(index); 00133 } 00134 00135 ArchiveFile* Archive::findArchiveFile(uint32 index) 00136 { 00137 unsigned int n = sources.size(); 00138 for (unsigned int i = 1; i <= n; ++i) { 00139 if (sources[n-i]->exists(index)) 00140 return sources[n-i]; 00141 } 00142 00143 return 0; 00144 } 00145 00146 }