Alientech 0.01
An extendable game engine
C:/Alientech/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::utils;
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 string ACHDirectory::getPath()
00020 {
00021         return path;
00022 }
00023 
00024 vector<ACHFile*> ACHDirectory::getListOfFiles()
00025 {
00026         vector<ACHFile*> ret;
00027         for (unsigned i=0;i<files.size();i++)
00028                 ret.push_back(files[i]);
00029         return ret;
00030 }
00031 
00032 vector<ACHDirectory*> ACHDirectory::getListOfSubdirs()
00033 {
00034         vector<ACHDirectory*> ret;
00035         for (unsigned i=0;i<subdirectories.size();i++)
00036                 ret.push_back(subdirectories[i]);
00037         return ret;
00038 }
00039 
00040 void ACHDirectory::print(string pre)
00041 {
00042         for (unsigned i=0;i<files.size();i++)
00043         {
00044                 cout << pre << files[i]->getName() << " [size:" << files[i]->getSize() << "]" << endl;
00045         }
00046         for (unsigned i=0;i<subdirectories.size();i++)
00047         {
00048                 cout << pre << subdirectories[i]->getName() << endl;
00049                 subdirectories[i]->print(pre+"\t");
00050         }
00051 }
00052 
00053 void ACHDirectory::setName(string NewName)
00054 {
00055         name = NewName;
00056 }
00057 
00058 void ACHDirectory::setParent(ACHDirectory* NewParent)
00059 {
00060         parent = NewParent;
00061 }
00062 
00063 void ACHDirectory::updatePath()
00064 {
00065         path = "\\";
00066         ACHDirectory* next = getParent();
00067         if (next != NULL)
00068         {
00069                 while (next->getName().compare("root") != 0)
00070                 {
00071                         path = '\\'+next->getName()+path;
00072                         next = next->getParent();
00073                 }
00074         }
00075         for (unsigned i=0;i<files.size();i++)
00076                 files[i]->setPath(buildFilePath(files[i]));
00077         for (unsigned i=0;i<subdirectories.size();i++)
00078                 subdirectories[i]->updatePath();
00079 }
00080 
00081 void ACHDirectory::addSubdir(ACHDirectory* subdirectory)
00082 {
00083         subdirectory->parent = this;
00084         subdirectories.push_back(subdirectory);
00085 }
00086 
00087 void ACHDirectory::addFile(ACHFile* file)
00088 {
00089         file->setParent(this);
00090         files.push_back(file);
00091 }
00092 
00093 bool ACHDirectory::containsSubdir(string DirectoryName)
00094 {
00095         for (unsigned i=0;i<subdirectories.size();i++)
00096                 if (DirectoryName.compare(subdirectories[i]->getName()) == 0)
00097                         return true;
00098         return false;
00099 }
00100 
00101 bool ACHDirectory::containsFile(string FileName)
00102 {
00103         for (unsigned i=0;i<files.size();i++)
00104                 if (FileName.compare(files[i]->getName()) == 0)
00105                         return true;
00106         return false;
00107 }
00108 
00109 ACHDirectory* ACHDirectory::readDirectory(ifstream* file)
00110 {
00111         ACHDirectory* ret = new ACHDirectory(NULL);
00112         ret->setName(ACHreadName(file));
00113 
00114         // Read subdirectories
00115         int numberOfSubdirectories = ACHreadInt(file);
00116         streampos nextSubdir;
00117         streampos subdirOffset;
00118         ACHDirectory* subdir;
00119         for (int i=0;i<numberOfSubdirectories;i++)
00120         {
00121                 nextSubdir = (file->tellg())+streampos(4); // save location of next subdirectory
00122                 subdirOffset = ACHreadInt(file); // grab where the next directory is located
00123                 file->seekg(subdirOffset); // go there
00124                 subdir = readDirectory(file); // read subdirectory
00125 
00126                 subdir->setParent(ret);
00127                 ret->addSubdir(subdir);
00128 
00129                 file->seekg(nextSubdir); // go to the next subdirectory
00130         }
00131 
00132         // Read files
00133         int numberOfFiles = ACHreadInt(file);
00134         streampos fileOffset;
00135         string fileName;
00136         unsigned fileSize;
00137         streampos nextFile;
00138         ACHFile* achFile;
00139         for (int i=0;i<numberOfFiles;i++)
00140         {
00141                 fileOffset = ACHreadInt(file); // grab location of file
00142                 nextFile = file->tellg(); // store pointer to next file listing
00143                 file->seekg(fileOffset); // go to file to retrieve name and size
00144                 fileName = ACHreadName(file);
00145                 fileSize = ACHreadUnsigned(file);
00146 
00147                 achFile = new ACHFile(ret,fileName,fileOffset,fileSize);
00148                 ret->addFile(achFile);
00149 
00150                 file->seekg(nextFile); // go back to next file listing
00151         }
00152         return ret;
00153 }
00154 
00155 string ACHDirectory::buildFilePath(ACHFile* File)
00156 {
00157         string ret = "\\";
00158         ACHDirectory* next = File->getParent();
00159         while (next->getName().compare("root") != 0)
00160         {
00161                 ret = '\\'+next->getName()+ret;
00162                 next = next->getParent();
00163         }
00164         return ret;
00165 }
00166 
00167 void ACHDirectory::destroy()
00168 {
00169         for (unsigned i=0;i<files.size();i++)
00170                 delete files[i];
00171         for (unsigned i=0;i<subdirectories.size();i++)
00172                 subdirectories[i]->destroy();
00173         delete this;
00174 }
00175 
00176 ACHFile* ACHDirectory::getFile(string path)
00177 {
00178         if (ACHisAFile(path))
00179         {
00180                 // search own files
00181                 for (unsigned i=0;i<files.size();i++)
00182                 {
00183                         if (files[i]->getName().compare(path) == 0)
00184                                 return files[i];
00185                 }
00186                 return NULL;
00187         } else
00188         {
00189                 // search for subdirectory
00190                 string subdir = ACHgrabFirstSubdir(path);
00191                 for (unsigned i=0;i<subdirectories.size();i++)
00192                 {
00193                         if (subdirectories[i]->getName().compare(subdir) == 0)
00194                                 return subdirectories[i]->getFile(ACHstripFirstSubdir(path));
00195                 }
00196                 return NULL;
00197         }
00198 }