//-------------------------------------------------------------- //-------------------------------------------------------------- u32 FileSystem::GetDirectoryChecksumCRC32(StorageLocation in_storageLocation, const std::string& in_directoryPath) const { std::vector<std::string> filenames = GetFilePaths(in_storageLocation, in_directoryPath, true); //get a hash for each of the files. std::vector<u32> hashes; for (const std::string& filename : filenames) { u32 fileHash = GetFileChecksumCRC32(in_storageLocation, in_directoryPath + filename); u32 pathHash = HashCRC32::GenerateHashCode(filename.c_str(), static_cast<u32>(filename.length())); hashes.push_back(fileHash); hashes.push_back(pathHash); } //sort the list so that ordering of std::sort(hashes.begin(), hashes.end()); //build this into a hashable string std::string hashableDirectoryContents; for (const u32& hash : hashes) { hashableDirectoryContents += ToString(hash); } //return the hash of this as the output u32 output = 0; if (hashableDirectoryContents.length() > 0) { CS_ASSERT(hashableDirectoryContents.length() < static_cast<std::vector<std::string>::size_type>(std::numeric_limits<u32>::max()), "Hashable directory contents too large. It cannot exceed " + CSCore::ToString(std::numeric_limits<u32>::max()) + " characters."); output = HashCRC32::GenerateHashCode(hashableDirectoryContents.c_str(), static_cast<u32>(hashableDirectoryContents.length())); } return output; }
//-------------------------------------------------------------- //-------------------------------------------------------------- u32 FileSystem::GetDirectorySize(StorageLocation in_storageLocation, const std::string& in_directory) const { std::vector<std::string> filenames = GetFilePaths(in_storageLocation, in_directory, true); u32 totalSize = 0; for (const std::string& filename : filenames) { totalSize += GetFileSize(in_storageLocation, in_directory + "/" + filename); } return totalSize; }
//-------------------------------------------------------------- //-------------------------------------------------------------- std::vector<std::string> FileSystem::GetFilePathsWithFileName(StorageLocation in_storageLocation, const std::string& in_directoryPath, bool in_recursive, const std::string& in_fileName) const { std::vector<std::string> filePaths = GetFilePaths(in_storageLocation, in_directoryPath, in_recursive); auto it = std::remove_if(filePaths.begin(), filePaths.end(), [&in_fileName] (const std::string& in_path) { return (Core::StringUtils::EndsWith(in_path, in_fileName, true) == false); }); filePaths.resize(it - filePaths.begin()); return filePaths; }
// return the filenames of all files that have the specified extension // in the specified directory and all subdirectories // The input _exts can be like ".jpg,.png,...." void GetFilePaths(const string folderPath, const char* _exts, vector<string>& filePaths) { char* pch; char str[4096]; strcpy(str, _exts); pch = strtok(str, "|"); vector<string> exts; while (pch != 0) { exts.push_back(string(pch)); pch = strtok(NULL, "|"); } GetFilePaths(folderPath, exts, filePaths); }