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 "DirFile.h" 00022 #include "IDataSource.h" 00023 #include "FileSystem.h" 00024 00025 DEFINE_RUNTIME_CLASSTYPE_CODE(DirFile,NamedArchiveFile); 00026 00027 DirFile::DirFile(const std::string& path_) 00028 { 00029 path = path_; 00030 if (path.size() == 0) { 00031 valid = false; 00032 return; 00033 } 00034 00035 if (path[path.size()-1] != '/') path += "/"; 00036 00037 valid = readMetadata(); 00038 } 00039 00040 00041 DirFile::~DirFile() 00042 { 00043 00044 } 00045 00046 bool DirFile::readMetadata() 00047 { 00048 FileSystem* filesys = FileSystem::get_instance(); 00049 FileSystem::FileList files; 00050 00051 /*int ret =*/ filesys->ListFiles(path + "*", files); 00052 00053 // TODO: check if directory actually exists 00054 00055 count = files.size(); 00056 00057 FileSystem::FileList::iterator iter; 00058 00059 for (iter = files.begin(); iter != files.end(); ++iter) { 00060 std::string name = *iter; 00061 std::string::size_type pos = name.rfind("/"); 00062 if (pos != std::string::npos) { 00063 name.erase(0, pos+1); 00064 pout << "DirFile: " << name << std::endl; 00065 storeIndexedName(name); 00066 } 00067 } 00068 00069 return true; 00070 } 00071 00072 bool DirFile::exists(const std::string& name) 00073 { 00074 FileSystem* filesys = FileSystem::get_instance(); 00075 IDataSource* ids = filesys->ReadFile(path + name); 00076 if (!ids) return false; 00077 00078 delete ids; 00079 return true; 00080 } 00081 00082 uint32 DirFile::getSize(const std::string& name) 00083 { 00084 FileSystem* filesys = FileSystem::get_instance(); 00085 IDataSource* ids = filesys->ReadFile(path + name); 00086 if (!ids) return 0; 00087 00088 uint32 size = ids->getSize(); 00089 delete ids; 00090 return size; 00091 } 00092 00093 uint8* DirFile::getObject(const std::string& name, uint32* sizep) 00094 { 00095 FileSystem* filesys = FileSystem::get_instance(); 00096 IDataSource* ids = filesys->ReadFile(path + name); 00097 if (!ids) return 0; 00098 00099 uint32 size = ids->getSize(); 00100 if (size == 0) return 0; 00101 00102 uint8* buf = new uint8[size]; 00103 ids->read(buf, size); 00104 delete ids; 00105 00106 if (sizep) *sizep = size; 00107 00108 return buf; 00109 }