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