mirror of
https://github.com/1dot13/source.git
synced 2026-08-12 14:10:23 +02:00
Original Source for 1.13 Mod High Resolution version from 12/06/05
git-svn-id: https://ja2svn.mooo.com/source/ja2/trunk/GameSource/ja2_v1.13/Build@21 3b4a5df2-a311-0410-b5c6-a8a6f20db521
This commit is contained in:
@@ -0,0 +1,97 @@
|
||||
//
|
||||
// Snap: Implementation of the TFileCat class
|
||||
//
|
||||
#include "FileCat.h"
|
||||
#include "readdir.h"
|
||||
|
||||
|
||||
// Remove a slash or backslash (if any) from the end of a string
|
||||
void ChompSlash(std::string& s)
|
||||
{
|
||||
if ( s.empty() ) return;
|
||||
|
||||
if ( *s.rbegin() == '\\' || *s.rbegin() == '/' ) {
|
||||
s.erase( s.length() - 1 );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Build a new file catalogue by recursively traversing the root directory
|
||||
void TFileCat::NewCat(std::string root)
|
||||
{
|
||||
fRootDir = root;
|
||||
ChompSlash(fRootDir);
|
||||
|
||||
fFileCat.clear();
|
||||
|
||||
TraverseDir(fRootDir);
|
||||
}
|
||||
|
||||
|
||||
// Look for a given file in the catalogue
|
||||
// Unless pathIncludesRoot == true, will prepend the root directory to path
|
||||
bool TFileCat::FindFile(std::string path, bool pathIncludesRoot) const
|
||||
{
|
||||
if (pathIncludesRoot) return fFileCat.find(path) != fFileCat.end();
|
||||
else return fFileCat.find(fRootDir + '\\' + path) != fFileCat.end();
|
||||
}
|
||||
|
||||
|
||||
// Delete a given file from the catalogue
|
||||
// Unless pathIncludesRoot == true, will prepend the root directory to path
|
||||
size_t TFileCat::RemoveFile(std::string path, bool pathIncludesRoot)
|
||||
{
|
||||
if (pathIncludesRoot) return fFileCat.erase(path);
|
||||
else return fFileCat.erase(fRootDir + '\\' + path);
|
||||
}
|
||||
|
||||
|
||||
// Delete all files from a given directory in the catalogue
|
||||
// Unless pathIncludesRoot == true, will prepend the root directory to path
|
||||
size_t TFileCat::RemoveDir(std::string dir, bool pathIncludesRoot)
|
||||
{
|
||||
if ( !pathIncludesRoot ) dir = fRootDir + '\\' + dir;
|
||||
ChompSlash(dir);
|
||||
std::string dirlower = dir + '\\';
|
||||
std::string dirupper = dir + char('\\'+1);
|
||||
|
||||
TCatalogue::iterator upper = fFileCat.upper_bound(dirupper);
|
||||
TCatalogue::iterator lower;
|
||||
|
||||
int deleted = 0;
|
||||
|
||||
while ( ( lower = fFileCat.lower_bound(dirlower) ) != upper) {
|
||||
fFileCat.erase(lower);
|
||||
deleted++;
|
||||
}
|
||||
|
||||
return deleted;
|
||||
}
|
||||
|
||||
|
||||
// Recursively traverse a directory, adding regular files to the catalogue
|
||||
void TFileCat::TraverseDir(std::string dir, int depth)
|
||||
{
|
||||
using std::string;
|
||||
|
||||
if (!dir.empty()) dir += '\\';
|
||||
|
||||
TReadDir readDir((dir + "*").c_str());
|
||||
|
||||
char const* fileName;
|
||||
unsigned attrib;
|
||||
|
||||
while ( readDir.NextFile(fileName, attrib) ) {
|
||||
if (string(".") == fileName || string("..") == fileName) continue;
|
||||
|
||||
string fullPath = dir + fileName;
|
||||
|
||||
if (attrib & FILE_ATTRIBUTE_DIRECTORY) {
|
||||
if (depth < 0) TraverseDir(fullPath);
|
||||
else if (depth > 0) TraverseDir(fullPath, depth-1);
|
||||
}
|
||||
else {
|
||||
fFileCat.insert(fullPath);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user