1
0
mirror of https://github.com/rwengine/openrw.git synced 2024-09-15 06:52:34 +02:00

Add new openFilePath method to FileIndex

This commit is contained in:
dan 2016-08-31 20:36:29 +01:00 committed by Daniel Evans
parent 03309d76d9
commit 23e25ed751
3 changed files with 30 additions and 1 deletions

View File

@ -11,11 +11,17 @@ struct FileContentsInfo
char* data;
size_t length;
FileContentsInfo(char* mem, size_t len)
: data(mem), length(len)
{
}
~FileContentsInfo() {
delete[] data;
}
};
typedef std::shared_ptr<FileContentsInfo> FileHandle;
using FileHandle = std::shared_ptr<FileContentsInfo>;
#endif

View File

@ -20,6 +20,22 @@ void FileIndex::indexGameDirectory(const fs::path& base_path)
}
}
FileHandle FileIndex::openFilePath(const std::string &file_path)
{
auto datapath = findFilePath(file_path);
std::ifstream dfile(datapath.c_str(), std::ios_base::binary | std::ios_base::ate);
if ( ! dfile.is_open()) {
throw std::runtime_error("Unable to open file: " + file_path);
}
auto length = dfile.tellg();
dfile.seekg(0);
auto data = new char[length];
dfile.read(data, length);
return std::make_shared<FileContentsInfo> ( data, length );
}
void FileIndex::indexTree(const std::string& root)
{
for(const path& entry : boost::make_iterator_range(recursive_directory_iterator(root), {})) {

View File

@ -62,6 +62,13 @@ public:
return filesystemfiles_[name];
}
/**
* @brief openFilePath opens a file on the disk
* @param file_path
* @return A handle for the file on the disk
*/
FileHandle openFilePath(const std::string& file_path);
struct IndexData
{
/// Lowercase identifying filename