Alientech 0.01
An extendable game engine
C:/Alientech/TestWTF/CACHDirectory.cpp
Go to the documentation of this file.
00001 #include <iostream>
00002 #include "CACHDirectory.h"
00003 using namespace std;
00004 
00005 ACHDirectory::ACHDirectory(ACHDirectory* Parent, string Name) : parent(Parent), name(Name)
00006 {}
00007 
00008 string ACHDirectory::getName()
00009 {
00010         return name;
00011 }
00012 
00013 ACHDirectory* ACHDirectory::getParent()
00014 {
00015         return parent;
00016 }
00017 
00018 vector<string> ACHDirectory::getListOfFiles()
00019 {
00020         vector<string> ret;
00021         for (unsigned i=0;i<files.size();i++)
00022                 ret.push_back(files[i]->getName());
00023         return ret;
00024 }
00025 
00026 vector<string> ACHDirectory::getListOfSubdirs()
00027 {
00028         vector<string> ret;
00029         for (unsigned i=0;i<subdirectories.size();i++)
00030                 ret.push_back(subdirectories[i]->getName());
00031         return ret;
00032 }
00033 
00034 void ACHDirectory::print(string pre)
00035 {
00036         for (unsigned i=0;i<files.size();i++)
00037         {
00038                 cout << pre << files[i]->getName() << "[size:" << files[i]->getSize() << ", ";
00039                 printf("offset: %x]\n",(int)files[i]->getOffset());
00040         }
00041         for (unsigned i=0;i<subdirectories.size();i++)
00042         {
00043                 cout << pre << subdirectories[i]->getName() << endl;
00044                 subdirectories[i]->print(pre+"\t");
00045         }
00046 }
00047 
00048 void ACHDirectory::setName(string NewName)
00049 {
00050         name = NewName;
00051 }
00052 
00053 void ACHDirectory::setParent(ACHDirectory* NewParent)
00054 {
00055         parent = NewParent;
00056 }
00057 
00058 void ACHDirectory::addSubdir(ACHDirectory* subdirectory)
00059 {
00060         subdirectory->parent = this;
00061         subdirectories.push_back(subdirectory);
00062 }
00063 
00064 void ACHDirectory::addFile(ACHFile* file)
00065 {
00066         file->setParent(this);
00067         files.push_back(file);
00068 }
00069 
00070 bool ACHDirectory::containsSubdir(string DirectoryName)
00071 {
00072         for (unsigned i=0;i<subdirectories.size();i++)
00073                 if (DirectoryName.compare(subdirectories[i]->getName()) == 0)
00074                         return true;
00075         return false;
00076 }
00077 
00078 bool ACHDirectory::containsFile(string FileName)
00079 {
00080         for (unsigned i=0;i<files.size();i++)
00081                 if (FileName.compare(files[i]->getName()) == 0)
00082                         return true;
00083         return false;
00084 }
00085 
00086 ACHDirectory* ACHDirectory::readDirectory(ifstream* file)
00087 {
00088         ACHDirectory* ret = new ACHDirectory(NULL);
00089         ret->setName(readName(file));
00090 
00091         // Read subdirectories
00092         int numberOfSubdirectories = readInt(file);
00093         streampos nextSubdir;
00094         streampos subdirOffset;
00095         ACHDirectory* subdir;
00096         for (int i=0;i<numberOfSubdirectories;i++)
00097         {
00098                 nextSubdir = (file->tellg())+streampos(4); // save location of next subdirectory
00099                 subdirOffset = readInt(file); // grab where the next directory is located
00100                 file->seekg(subdirOffset); // go there
00101                 subdir = readDirectory(file); // read subdirectory
00102 
00103                 subdir->setParent(ret);
00104                 ret->addSubdir(subdir);
00105 
00106                 file->seekg(nextSubdir); // go to the next subdirectory
00107         }
00108 
00109         // Read files
00110         int numberOfFiles = readInt(file);
00111         streampos fileOffset;
00112         string fileName;
00113         unsigned fileSize;
00114         streampos nextFile;
00115         ACHFile* achFile;
00116         for (int i=0;i<numberOfFiles;i++)
00117         {
00118                 fileOffset = readInt(file); // grab location of file
00119                 nextFile = file->tellg(); // store pointer to next file listing
00120                 file->seekg(fileOffset); // go to file to retrieve name and size
00121                 fileName = readName(file);
00122                 fileSize = readUnsigned(file);
00123 
00124                 achFile = new ACHFile(ret,fileName,fileOffset,fileSize);
00125                 ret->addFile(achFile);
00126 
00127                 file->seekg(nextFile); // go back to next file listing
00128         }
00129         return ret;
00130 }
00131 
00132 void ACHDirectory::destroy()
00133 {
00134         for (unsigned i=0;i<files.size();i++)
00135                 delete files[i];
00136         for (unsigned i=0;i<subdirectories.size();i++)
00137                 subdirectories[i]->destroy();
00138         delete this;
00139 }