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

clang-format files in rwlib/source/platform

This commit is contained in:
Daniel Evans 2016-09-09 21:13:21 +01:00
parent c4bb714e54
commit 8a19f9b5d0
3 changed files with 187 additions and 196 deletions

View File

@ -6,20 +6,16 @@
/**
* @brief Contains a pointer to a file's contents.
*/
struct FileContentsInfo
{
char* data;
size_t length;
struct FileContentsInfo {
char* data;
size_t length;
FileContentsInfo(char* mem, size_t len)
: data(mem), length(len)
{
FileContentsInfo(char* mem, size_t len) : data(mem), length(len) {
}
}
~FileContentsInfo() {
delete[] data;
}
~FileContentsInfo() {
delete[] data;
}
};
using FileHandle = std::shared_ptr<FileContentsInfo>;

View File

@ -1,127 +1,128 @@
#include <algorithm>
#include <fstream>
#include <boost/range/iterator_range.hpp>
#include <platform/FileIndex.hpp>
#include <fstream>
#include <loaders/LoaderIMG.hpp>
#include <platform/FileIndex.hpp>
using namespace boost::filesystem;
void FileIndex::indexGameDirectory(const fs::path& base_path)
{
gamedatapath_ = base_path;
void FileIndex::indexGameDirectory(const fs::path& base_path) {
gamedatapath_ = base_path;
for(const path& entry : boost::make_iterator_range(recursive_directory_iterator(base_path), {})) {
if(is_regular_file(entry)) {
std::string name = entry.string();
std::transform(name.begin(), name.end(), name.begin(), ::tolower);
for (const path& entry : boost::make_iterator_range(
recursive_directory_iterator(base_path), {})) {
if (is_regular_file(entry)) {
std::string name = entry.string();
std::transform(name.begin(), name.end(), name.begin(), ::tolower);
filesystemfiles_[name] = entry;
}
}
filesystemfiles_[name] = entry;
}
}
}
FileHandle FileIndex::openFilePath(const std::string &file_path)
{
auto datapath = findFilePath(file_path);
std::ifstream dfile(datapath.string(), std::ios_base::binary | std::ios_base::ate);
if ( ! dfile.is_open()) {
throw std::runtime_error("Unable to open file: " + file_path);
}
FileHandle FileIndex::openFilePath(const std::string& file_path) {
auto datapath = findFilePath(file_path);
std::ifstream dfile(datapath.string(),
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);
auto length = dfile.tellg();
dfile.seekg(0);
auto data = new char[length];
dfile.read(data, length);
return std::make_shared<FileContentsInfo> ( 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), {})) {
std::string directory = entry.parent_path().string();
std::string realName = entry.filename().string();
std::string lowerName = realName;
std::transform(lowerName.begin(), lowerName.end(), lowerName.begin(), ::tolower);
void FileIndex::indexTree(const std::string& root) {
for (const path& entry :
boost::make_iterator_range(recursive_directory_iterator(root), {})) {
std::string directory = entry.parent_path().string();
std::string realName = entry.filename().string();
std::string lowerName = realName;
std::transform(lowerName.begin(), lowerName.end(), lowerName.begin(),
::tolower);
if(is_regular_file(entry)) {
files[lowerName] = {lowerName, realName, directory, ""};
}
}
if (is_regular_file(entry)) {
files[lowerName] = {lowerName, realName, directory, ""};
}
}
}
void FileIndex::indexArchive(const std::string& archive)
{
// Split directory from archive name
path archive_path = path(archive);
path directory = archive_path.parent_path();
path archive_basename = archive_path.filename();
path archive_full_path = directory/archive_basename;
LoaderIMG img;
if(!img.load(archive_full_path.string())) {
throw std::runtime_error("Failed to load IMG archive: " + archive_full_path.string());
}
std::string lowerName;
for( size_t i = 0; i < img.getAssetCount(); ++i ) {
auto& asset = img.getAssetInfoByIndex(i);
if( asset.size == 0 ) continue;
lowerName = asset.name;
std::transform(lowerName.begin(), lowerName.end(), lowerName.begin(), ::tolower);
files[lowerName] = {lowerName, asset.name, directory.string(), archive_basename.string()};
}
void FileIndex::indexArchive(const std::string& archive) {
// Split directory from archive name
path archive_path = path(archive);
path directory = archive_path.parent_path();
path archive_basename = archive_path.filename();
path archive_full_path = directory / archive_basename;
LoaderIMG img;
if (!img.load(archive_full_path.string())) {
throw std::runtime_error("Failed to load IMG archive: " +
archive_full_path.string());
}
std::string lowerName;
for (size_t i = 0; i < img.getAssetCount(); ++i) {
auto& asset = img.getAssetInfoByIndex(i);
if (asset.size == 0) continue;
lowerName = asset.name;
std::transform(lowerName.begin(), lowerName.end(), lowerName.begin(),
::tolower);
files[lowerName] = {lowerName, asset.name, directory.string(),
archive_basename.string()};
}
}
FileHandle FileIndex::openFile(const std::string& filename)
{
auto iterator = files.find( filename );
if( iterator == files.end() ) {
return nullptr;
}
IndexData& f = iterator->second;
bool isArchive = !f.archive.empty();
auto fsName = f.directory + "/" + f.originalName;
char* data = nullptr;
size_t length = 0;
if( isArchive ) {
fsName = f.directory + "/" + f.archive;
LoaderIMG img;
if( ! img.load(fsName) ) {
throw std::runtime_error("Failed to load IMG archive: " + fsName);
}
LoaderIMGFile file;
if( img.findAssetInfo(f.originalName, file) ) {
length = file.size * 2048;
data = img.loadToMemory(f.originalName);
}
}
else {
std::ifstream dfile(fsName.c_str(), std::ios_base::binary);
if ( ! dfile.is_open()) {
throw std::runtime_error("Unable to open file: " + fsName);
}
FileHandle FileIndex::openFile(const std::string& filename) {
auto iterator = files.find(filename);
if (iterator == files.end()) {
return nullptr;
}
dfile.seekg(0, std::ios_base::end);
length = dfile.tellg();
dfile.seekg(0);
data = new char[length];
dfile.read(data, length);
}
if( data == nullptr ) {
return nullptr;
}
return FileHandle( new FileContentsInfo{ data, length } );
IndexData& f = iterator->second;
bool isArchive = !f.archive.empty();
auto fsName = f.directory + "/" + f.originalName;
char* data = nullptr;
size_t length = 0;
if (isArchive) {
fsName = f.directory + "/" + f.archive;
LoaderIMG img;
if (!img.load(fsName)) {
throw std::runtime_error("Failed to load IMG archive: " + fsName);
}
LoaderIMGFile file;
if (img.findAssetInfo(f.originalName, file)) {
length = file.size * 2048;
data = img.loadToMemory(f.originalName);
}
} else {
std::ifstream dfile(fsName.c_str(), std::ios_base::binary);
if (!dfile.is_open()) {
throw std::runtime_error("Unable to open file: " + fsName);
}
dfile.seekg(0, std::ios_base::end);
length = dfile.tellg();
dfile.seekg(0);
data = new char[length];
dfile.read(data, length);
}
if (data == nullptr) {
return nullptr;
}
return FileHandle(new FileContentsInfo{data, length});
}

View File

@ -4,103 +4,97 @@
#include <boost/filesystem.hpp>
#include <boost/functional/hash.hpp>
#include <string>
#include <map>
#include <string>
#include <unordered_map>
namespace fs = boost::filesystem;
namespace std
{
template<> struct hash<fs::path>
{
size_t operator()(const fs::path& p) const
{
return fs::hash_value(p);
}
};
namespace std {
template <>
struct hash<fs::path> {
size_t operator()(const fs::path& p) const {
return fs::hash_value(p);
}
};
}
class FileIndex
{
class FileIndex {
private:
/**
* Mapping type (lower case name) => (on disk name)
*/
using FileSystemMap = std::unordered_map<fs::path, fs::path>;
/**
* Mapping type (lower case name) => (on disk name)
*/
using FileSystemMap = std::unordered_map<fs::path, fs::path>;
fs::path gamedatapath_;
FileSystemMap filesystemfiles_;
fs::path gamedatapath_;
FileSystemMap filesystemfiles_;
public:
/**
* @brief indexDirectory finds the true case for each file in the tree
* @param base_path
*
* This is used to build the mapping of lower-case file paths to the
* true case on the file system for platforms where this is an issue.
*
*/
void indexGameDirectory(const fs::path& base_path);
/**
* @brief indexDirectory finds the true case for each file in the tree
* @param base_path
*
* This is used to build the mapping of lower-case file paths to the
* true case on the file system for platforms where this is an issue.
*
*/
void indexGameDirectory(const fs::path& base_path);
/**
* @brief findFilePath finds disk path for a game data file
* @param path
* @return The file path as it exists on disk
*/
fs::path findFilePath(std::string path) {
auto backslash = std::string::npos;
while ((backslash = path.find("\\")) != std::string::npos) {
path.replace(backslash, 1, "/");
}
auto realpath = gamedatapath_ / path;
std::string name = realpath.string();
std::transform(name.begin(), name.end(), name.begin(), ::tolower);
/**
* @brief findFilePath finds disk path for a game data file
* @param path
* @return The file path as it exists on disk
*/
fs::path findFilePath(std::string path)
{
auto backslash = std::string::npos;
while ((backslash = path.find("\\")) != std::string::npos) {
path.replace(backslash, 1, "/");
}
auto realpath = gamedatapath_ / path;
std::string name = realpath.string();
std::transform(name.begin(), name.end(), name.begin(), ::tolower);
return filesystemfiles_[name];
}
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);
/**
* @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
std::string filename;
/// Original filename
std::string originalName;
/// Containing directory
std::string directory;
/// The archive filename (if applicable)
std::string archive;
};
struct IndexData
{
/// Lowercase identifying filename
std::string filename;
/// Original filename
std::string originalName;
/// Containing directory
std::string directory;
/// The archive filename (if applicable)
std::string archive;
};
/**
* Adds the files contained within the given directory tree to the
* file index.
*/
void indexTree(const std::string& root);
/**
* Adds the files contained within the given Archive file to the
* file index.
*/
void indexArchive(const std::string& archive);
/**
* Adds the files contained within the given directory tree to the
* file index.
*/
void indexTree(const std::string& root);
/**
* Returns a FileHandle for the file if it can be found in the
* file index, otherwise an empty FileHandle is returned.
*/
FileHandle openFile(const std::string& filename);
/**
* Adds the files contained within the given Archive file to the
* file index.
*/
void indexArchive(const std::string& archive);
/**
* Returns a FileHandle for the file if it can be found in the
* file index, otherwise an empty FileHandle is returned.
*/
FileHandle openFile(const std::string& filename);
private:
std::map<std::string, IndexData> files;
std::map<std::string, IndexData> files;
};
#endif