Avoid redundant conversion to const char* and use make_shared

pull/3226/head
elsid 3 years ago
parent 7ebf9a3c45
commit 5eb8c4aebe
No known key found for this signature in database
GPG Key ID: B845CB9FEE18AB40

@ -131,7 +131,7 @@ int main(int argc, char **argv)
if(isNIF(name)) if(isNIF(name))
{ {
//std::cout << "Decoding: " << name << std::endl; //std::cout << "Decoding: " << name << std::endl;
Nif::NIFFile temp_nif(Files::openConstrainedFileStream(name.c_str()),name); Nif::NIFFile temp_nif(Files::openConstrainedFileStream(name), name);
} }
else if(isBSA(name)) else if(isBSA(name))
{ {

@ -39,7 +39,7 @@ namespace
std::fill_n(std::back_inserter(content), GetParam().mSize, 'a'); std::fill_n(std::back_inserter(content), GetParam().mSize, 'a');
std::fstream(fileName, std::ios_base::out | std::ios_base::binary) std::fstream(fileName, std::ios_base::out | std::ios_base::binary)
.write(content.data(), static_cast<std::streamsize>(content.size())); .write(content.data(), static_cast<std::streamsize>(content.size()));
const auto stream = Files::openConstrainedFileStream(fileName.data(), 0, content.size()); const auto stream = Files::openConstrainedFileStream(fileName, 0, content.size());
EXPECT_EQ(getHash(fileName, *stream), GetParam().mHash); EXPECT_EQ(getHash(fileName, *stream), GetParam().mHash);
} }

@ -125,7 +125,7 @@ public:
*/ */
Files::IStreamPtr getFile(const FileStruct *file) Files::IStreamPtr getFile(const FileStruct *file)
{ {
return Files::openConstrainedFileStream (mFilename.c_str (), file->offset, file->fileSize); return Files::openConstrainedFileStream(mFilename, file->offset, file->fileSize);
} }
void addFile(const std::string& filename, std::istream& file); void addFile(const std::string& filename, std::istream& file);

@ -355,7 +355,7 @@ Files::IStreamPtr CompressedBSAFile::getFile(const FileRecord& fileRecord)
size_t size = fileRecord.getSizeWithoutCompressionFlag(); size_t size = fileRecord.getSizeWithoutCompressionFlag();
size_t uncompressedSize = size; size_t uncompressedSize = size;
bool compressed = fileRecord.isCompressed(mCompressedByDefault); bool compressed = fileRecord.isCompressed(mCompressedByDefault);
Files::IStreamPtr streamPtr = Files::openConstrainedFileStream(mFilename.c_str(), fileRecord.offset, size); Files::IStreamPtr streamPtr = Files::openConstrainedFileStream(mFilename, fileRecord.offset, size);
std::istream* fileStream = streamPtr.get(); std::istream* fileStream = streamPtr.get();
if (mEmbeddedFileNames) if (mEmbeddedFileNames)
{ {
@ -458,7 +458,7 @@ void CompressedBSAFile::convertCompressedSizesToUncompressed()
continue; continue;
} }
Files::IStreamPtr dataBegin = Files::openConstrainedFileStream(mFilename.c_str(), fileRecord.offset, fileRecord.getSizeWithoutCompressionFlag()); Files::IStreamPtr dataBegin = Files::openConstrainedFileStream(mFilename, fileRecord.offset, fileRecord.getSizeWithoutCompressionFlag());
if (mEmbeddedFileNames) if (mEmbeddedFileNames)
{ {

@ -16,7 +16,7 @@ namespace ESM
{ {
Reader* Reader::getReader(const std::string &filename) Reader* Reader::getReader(const std::string &filename)
{ {
Files::IStreamPtr esmStream(Files::openConstrainedFileStream (filename.c_str ())); Files::IStreamPtr esmStream(Files::openConstrainedFileStream(filename));
std::uint32_t modVer = 0; // get the first 4 bytes of the record header only std::uint32_t modVer = 0; // get the first 4 bytes of the record header only
esmStream->read((char*)&modVer, sizeof(modVer)); esmStream->read((char*)&modVer, sizeof(modVer));

@ -93,7 +93,7 @@ void ESMReader::openRaw(Files::IStreamPtr _esm, const std::string& name)
void ESMReader::openRaw(const std::string& filename) void ESMReader::openRaw(const std::string& filename)
{ {
openRaw(Files::openConstrainedFileStream(filename.c_str()), filename); openRaw(Files::openConstrainedFileStream(filename), filename);
} }
void ESMReader::open(Files::IStreamPtr _esm, const std::string &name) void ESMReader::open(Files::IStreamPtr _esm, const std::string &name)
@ -110,7 +110,7 @@ void ESMReader::open(Files::IStreamPtr _esm, const std::string &name)
void ESMReader::open(const std::string &file) void ESMReader::open(const std::string &file)
{ {
open (Files::openConstrainedFileStream (file.c_str ()), file); open (Files::openConstrainedFileStream(file), file);
} }
std::string ESMReader::getHNOString(NAME name) std::string ESMReader::getHNOString(NAME name)

@ -210,7 +210,7 @@ void Reader::buildLStringIndex(const std::string& stringFile, LocalizedStringTyp
sp.type = stringType; sp.type = stringType;
// TODO: possibly check if the resource exists? // TODO: possibly check if the resource exists?
Files::IStreamPtr filestream = Files::IStreamPtr(Files::openConstrainedFileStream(stringFile.c_str())); Files::IStreamPtr filestream = Files::IStreamPtr(Files::openConstrainedFileStream(stringFile));
filestream->seekg(0, std::ios::end); filestream->seekg(0, std::ios::end);
std::size_t fileSize = filestream->tellg(); std::size_t fileSize = filestream->tellg();

@ -128,12 +128,14 @@ namespace ESM4 {
~Reader(); ~Reader();
// FIXME: should be private but ESMTool uses it // FIXME: should be private but ESMTool uses it
void openRaw(const std::string& filename) { void openRaw(const std::string& filename)
openRaw(Files::openConstrainedFileStream(filename.c_str()), filename); {
openRaw(Files::openConstrainedFileStream(filename), filename);
} }
void open(const std::string& filename) { void open(const std::string& filename)
open(Files::openConstrainedFileStream (filename.c_str ()), filename); {
open(Files::openConstrainedFileStream(filename), filename);
} }
void close() final; void close() final;

@ -27,7 +27,7 @@ namespace Files
ConstrainedFileStreamBuf(const std::string &fname, size_t start, size_t length) ConstrainedFileStreamBuf(const std::string &fname, size_t start, size_t length)
{ {
mFile.open (fname.c_str ()); mFile.open (fname.c_str ());
mSize = length != 0xFFFFFFFF ? length : mFile.size () - start; mSize = length != std::numeric_limits<std::size_t>::max() ? length : mFile.size () - start;
if (start != 0) if (start != 0)
mFile.seek(start); mFile.seek(start);
@ -109,10 +109,8 @@ namespace Files
{ {
} }
IStreamPtr openConstrainedFileStream(const char *filename, IStreamPtr openConstrainedFileStream(const std::string& filename, std::size_t start, std::size_t length)
size_t start, size_t length)
{ {
auto buf = std::unique_ptr<std::streambuf>(new ConstrainedFileStreamBuf(filename, start, length)); return std::make_shared<ConstrainedFileStream>(std::make_unique<ConstrainedFileStreamBuf>(filename, start, length));
return IStreamPtr(new ConstrainedFileStream(std::move(buf)));
} }
} }

@ -3,6 +3,8 @@
#include <istream> #include <istream>
#include <memory> #include <memory>
#include <limits>
#include <string>
namespace Files namespace Files
{ {
@ -20,7 +22,8 @@ private:
typedef std::shared_ptr<std::istream> IStreamPtr; typedef std::shared_ptr<std::istream> IStreamPtr;
IStreamPtr openConstrainedFileStream(const char *filename, size_t start=0, size_t length=0xFFFFFFFF); IStreamPtr openConstrainedFileStream(const std::string& filename, std::size_t start = 0,
std::size_t length = std::numeric_limits<std::size_t>::max());
} }

@ -78,7 +78,7 @@ namespace VFS
Files::IStreamPtr FileSystemArchiveFile::open() Files::IStreamPtr FileSystemArchiveFile::open()
{ {
return Files::openConstrainedFileStream(mPath.c_str()); return Files::openConstrainedFileStream(mPath);
} }
} }

Loading…
Cancel
Save