mirror of
https://github.com/OpenMW/openmw.git
synced 2025-10-14 17:26:33 +00:00
Use std::string_view for BSA file name
This commit is contained in:
parent
c388fda5e4
commit
c92e321a08
7 changed files with 17 additions and 22 deletions
|
@ -4,6 +4,7 @@
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
|
#include <format>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
|
||||||
#include <zlib.h>
|
#include <zlib.h>
|
||||||
|
@ -146,13 +147,13 @@ namespace Bsa
|
||||||
mIsLoaded = true;
|
mIsLoaded = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::optional<BA2DX10File::FileRecord> BA2DX10File::getFileRecord(const std::string& str) const
|
std::optional<BA2DX10File::FileRecord> BA2DX10File::getFileRecord(std::string_view str) const
|
||||||
{
|
{
|
||||||
for (const auto c : str)
|
for (const auto c : str)
|
||||||
{
|
{
|
||||||
if (((static_cast<unsigned>(c) >> 7U) & 1U) != 0U)
|
if (((static_cast<unsigned>(c) >> 7U) & 1U) != 0U)
|
||||||
{
|
{
|
||||||
fail("File record " + str + " contains unicode characters, refusing to load.");
|
fail(std::format("File record {} contains unicode characters, refusing to load.", str));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -162,7 +163,7 @@ namespace Bsa
|
||||||
// Force-convert the path into something UNIX can handle first
|
// Force-convert the path into something UNIX can handle first
|
||||||
// to make sure std::filesystem::path doesn't think the entire path is the filename on Linux
|
// to make sure std::filesystem::path doesn't think the entire path is the filename on Linux
|
||||||
// and subsequently purge it to determine the file folder.
|
// and subsequently purge it to determine the file folder.
|
||||||
std::string path = str;
|
std::string path(str);
|
||||||
std::replace(path.begin(), path.end(), '\\', '/');
|
std::replace(path.begin(), path.end(), '\\', '/');
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
|
@ -41,7 +41,7 @@ namespace Bsa
|
||||||
|
|
||||||
std::list<std::vector<char>> mFileNames;
|
std::list<std::vector<char>> mFileNames;
|
||||||
|
|
||||||
std::optional<FileRecord> getFileRecord(const std::string& str) const;
|
std::optional<FileRecord> getFileRecord(std::string_view str) const;
|
||||||
|
|
||||||
Files::IStreamPtr getFile(const FileRecord& fileRecord);
|
Files::IStreamPtr getFile(const FileRecord& fileRecord);
|
||||||
|
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
|
#include <format>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
|
||||||
#include <zlib.h>
|
#include <zlib.h>
|
||||||
|
@ -137,13 +138,13 @@ namespace Bsa
|
||||||
mIsLoaded = true;
|
mIsLoaded = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
BA2GNRLFile::FileRecord BA2GNRLFile::getFileRecord(const std::string& str) const
|
BA2GNRLFile::FileRecord BA2GNRLFile::getFileRecord(std::string_view str) const
|
||||||
{
|
{
|
||||||
for (const auto c : str)
|
for (const auto c : str)
|
||||||
{
|
{
|
||||||
if (((static_cast<unsigned>(c) >> 7U) & 1U) != 0U)
|
if (((static_cast<unsigned>(c) >> 7U) & 1U) != 0U)
|
||||||
{
|
{
|
||||||
fail("File record " + str + " contains unicode characters, refusing to load.");
|
fail(std::format("File record {} contains unicode characters, refusing to load.", str));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -153,7 +154,7 @@ namespace Bsa
|
||||||
// Force-convert the path into something UNIX can handle first
|
// Force-convert the path into something UNIX can handle first
|
||||||
// to make sure std::filesystem::path doesn't think the entire path is the filename on Linux
|
// to make sure std::filesystem::path doesn't think the entire path is the filename on Linux
|
||||||
// and subsequently purge it to determine the file folder.
|
// and subsequently purge it to determine the file folder.
|
||||||
std::string path = str;
|
std::string path(str);
|
||||||
std::replace(path.begin(), path.end(), '\\', '/');
|
std::replace(path.begin(), path.end(), '\\', '/');
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
|
@ -29,7 +29,7 @@ namespace Bsa
|
||||||
|
|
||||||
std::list<std::vector<char>> mFileNames;
|
std::list<std::vector<char>> mFileNames;
|
||||||
|
|
||||||
FileRecord getFileRecord(const std::string& str) const;
|
FileRecord getFileRecord(std::string_view str) const;
|
||||||
|
|
||||||
Files::IStreamPtr getFile(const FileRecord& fileRecord);
|
Files::IStreamPtr getFile(const FileRecord& fileRecord);
|
||||||
|
|
||||||
|
|
|
@ -68,18 +68,11 @@ namespace Bsa
|
||||||
uint32_t mFileSize = 0;
|
uint32_t mFileSize = 0;
|
||||||
uint32_t mOffset = 0;
|
uint32_t mOffset = 0;
|
||||||
Hash mHash{};
|
Hash mHash{};
|
||||||
|
|
||||||
// Zero-terminated file name
|
|
||||||
const char* name() const
|
|
||||||
{
|
|
||||||
if (mNameSize == 0)
|
|
||||||
return "";
|
|
||||||
return mNamesBuffer->data() + mNameOffset;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint32_t mNameOffset = 0;
|
uint32_t mNameOffset = 0;
|
||||||
uint32_t mNameSize = 0;
|
uint32_t mNameSize = 0;
|
||||||
std::vector<char>* mNamesBuffer = nullptr;
|
std::vector<char>* mNamesBuffer = nullptr;
|
||||||
|
|
||||||
|
std::string_view name() const { return std::string_view(mNamesBuffer->data() + mNameOffset, mNameSize); }
|
||||||
};
|
};
|
||||||
typedef std::vector<FileStruct> FileList;
|
typedef std::vector<FileStruct> FileList;
|
||||||
|
|
||||||
|
@ -153,7 +146,6 @@ namespace Bsa
|
||||||
// checks version of BSA from file header
|
// checks version of BSA from file header
|
||||||
static BsaVersion detectVersion(const std::filesystem::path& filePath);
|
static BsaVersion detectVersion(const std::filesystem::path& filePath);
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -27,6 +27,7 @@
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
|
#include <format>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
|
||||||
#include <lz4frame.h>
|
#include <lz4frame.h>
|
||||||
|
@ -193,13 +194,13 @@ namespace Bsa
|
||||||
mIsLoaded = true;
|
mIsLoaded = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
CompressedBSAFile::FileRecord CompressedBSAFile::getFileRecord(const std::string& str) const
|
CompressedBSAFile::FileRecord CompressedBSAFile::getFileRecord(std::string_view str) const
|
||||||
{
|
{
|
||||||
for (const auto c : str)
|
for (const auto c : str)
|
||||||
{
|
{
|
||||||
if (((static_cast<unsigned>(c) >> 7U) & 1U) != 0U)
|
if (((static_cast<unsigned>(c) >> 7U) & 1U) != 0U)
|
||||||
{
|
{
|
||||||
fail("File record " + str + " contains unicode characters, refusing to load.");
|
fail(std::format("File record {} contains unicode characters, refusing to load.", str));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -209,7 +210,7 @@ namespace Bsa
|
||||||
// Force-convert the path into something UNIX can handle first
|
// Force-convert the path into something UNIX can handle first
|
||||||
// to make sure std::filesystem::path doesn't think the entire path is the filename on Linux
|
// to make sure std::filesystem::path doesn't think the entire path is the filename on Linux
|
||||||
// and subsequently purge it to determine the file folder.
|
// and subsequently purge it to determine the file folder.
|
||||||
std::string path = str;
|
std::string path(str);
|
||||||
std::replace(path.begin(), path.end(), '\\', '/');
|
std::replace(path.begin(), path.end(), '\\', '/');
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
|
@ -108,7 +108,7 @@ namespace Bsa
|
||||||
|
|
||||||
std::map<std::uint64_t, FolderRecord> mFolders;
|
std::map<std::uint64_t, FolderRecord> mFolders;
|
||||||
|
|
||||||
FileRecord getFileRecord(const std::string& str) const;
|
FileRecord getFileRecord(std::string_view str) const;
|
||||||
|
|
||||||
/// \brief Normalizes given filename or folder and generates format-compatible hash.
|
/// \brief Normalizes given filename or folder and generates format-compatible hash.
|
||||||
static std::uint64_t generateHash(const std::filesystem::path& stem, std::string extension);
|
static std::uint64_t generateHash(const std::filesystem::path& stem, std::string extension);
|
||||||
|
|
Loading…
Reference in a new issue