1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-10-14 10:26:41 +00:00

Use std::string_view for BSA file name

This commit is contained in:
elsid 2025-09-19 12:21:31 +02:00
parent c388fda5e4
commit c92e321a08
No known key found for this signature in database
GPG key ID: B845CB9FEE18AB40
7 changed files with 17 additions and 22 deletions

View file

@ -4,6 +4,7 @@
#include <cassert>
#include <cstring>
#include <filesystem>
#include <format>
#include <fstream>
#include <zlib.h>
@ -146,13 +147,13 @@ namespace Bsa
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)
{
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
// 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.
std::string path = str;
std::string path(str);
std::replace(path.begin(), path.end(), '\\', '/');
#endif

View file

@ -41,7 +41,7 @@ namespace Bsa
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);

View file

@ -3,6 +3,7 @@
#include <algorithm>
#include <cassert>
#include <filesystem>
#include <format>
#include <fstream>
#include <zlib.h>
@ -137,13 +138,13 @@ namespace Bsa
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)
{
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
// 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.
std::string path = str;
std::string path(str);
std::replace(path.begin(), path.end(), '\\', '/');
#endif

View file

@ -29,7 +29,7 @@ namespace Bsa
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);

View file

@ -68,18 +68,11 @@ namespace Bsa
uint32_t mFileSize = 0;
uint32_t mOffset = 0;
Hash mHash{};
// Zero-terminated file name
const char* name() const
{
if (mNameSize == 0)
return "";
return mNamesBuffer->data() + mNameOffset;
}
uint32_t mNameOffset = 0;
uint32_t mNameSize = 0;
std::vector<char>* mNamesBuffer = nullptr;
std::string_view name() const { return std::string_view(mNamesBuffer->data() + mNameOffset, mNameSize); }
};
typedef std::vector<FileStruct> FileList;
@ -153,7 +146,6 @@ namespace Bsa
// checks version of BSA from file header
static BsaVersion detectVersion(const std::filesystem::path& filePath);
};
}
#endif

View file

@ -27,6 +27,7 @@
#include <algorithm>
#include <cassert>
#include <filesystem>
#include <format>
#include <fstream>
#include <lz4frame.h>
@ -193,13 +194,13 @@ namespace Bsa
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)
{
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
// 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.
std::string path = str;
std::string path(str);
std::replace(path.begin(), path.end(), '\\', '/');
#endif

View file

@ -108,7 +108,7 @@ namespace Bsa
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.
static std::uint64_t generateHash(const std::filesystem::path& stem, std::string extension);