From a74cf471b8637a107b77a94717eba7c538b2964f Mon Sep 17 00:00:00 2001 From: Aki Date: Thu, 11 Aug 2022 23:00:34 +0200 Subject: Code now consistently uses unique_ptr for arrays Now, I realize this is a rather interesting choice. I want them to be consistent. In case of extraction the array gets released as part of the legacy interface. Because of this std::vector is out of question. All other cases could use it, but they all have the same start and purpose. Only that one differs and I don't like making it unique (pun intended). --- ArchiveEx/Archive.cpp | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/ArchiveEx/Archive.cpp b/ArchiveEx/Archive.cpp index 68375f2..513d161 100644 --- a/ArchiveEx/Archive.cpp +++ b/ArchiveEx/Archive.cpp @@ -4,7 +4,6 @@ #include #include #include -#include #include @@ -43,8 +42,8 @@ Archive::Archive(const char* p) : int err = std::fseek(file.get(), sizeof(Header) + header.directory.offset, SEEK_SET); if (-1 == err) throw "could not find directory in archive"; - std::vector compressed(header.directory.compressed_size); - length = std::fread(compressed.data(), 1, header.directory.compressed_size, file.get()); + auto compressed = std::make_unique(header.directory.compressed_size); + length = std::fread(compressed.get(), 1, header.directory.compressed_size, file.get()); if (header.directory.compressed_size != length) throw "could not read compressed directory"; const std::size_t total_entries = header.total_entries + DIRECTORY_MARGIN; @@ -54,7 +53,7 @@ Archive::Archive(const char* p) : err = uncompress( reinterpret_cast(entries.data()), &uncompressed_size, - compressed.data(), + compressed.get(), header.directory.compressed_size); if (Z_OK != err) throw "could not uncompress directory"; @@ -71,16 +70,16 @@ Archive::Expand(const int index, std::uint8_t*& buffer, const bool null_terminat if (!file) return -1; const auto& entry = entries[index]; - std::vector compressed(entry.compressed_size); + auto compressed = std::make_unique(entry.compressed_size); int err = std::fseek(file.get(), sizeof(Header) + entry.offset, SEEK_SET); if (-1 == err) return -1; - const std::size_t length = std::fread(compressed.data(), 1, entry.compressed_size, file.get()); + const std::size_t length = std::fread(compressed.get(), 1, entry.compressed_size, file.get()); if (entry.compressed_size != length) return -1; uLongf output_length = entry.original_size; auto uncompressed = std::make_unique(output_length + null_terminated); - err = uncompress(uncompressed.get(), &output_length, compressed.data(), entry.compressed_size); + err = uncompress(uncompressed.get(), &output_length, compressed.get(), entry.compressed_size); if (Z_OK != err) return -1; buffer = uncompressed.release(); @@ -103,10 +102,10 @@ Archive::Expand(const char* filepath, std::uint8_t*& buffer, const bool null_ter int Archive::Find(const char* filepath) const { - std::vector path(std::strlen(filepath) + 1); - ConvertPathSeparator(filepath, path.data()); + auto path = std::make_unique(std::strlen(filepath) + 1); + ConvertPathSeparator(filepath, path.get()); for (std::size_t i = 0; i < entries.size(); ++i) { - if (0 == std::strcmp(entries[i].name, path.data())) + if (0 == std::strcmp(entries[i].name, path.get())) return i; } return -1; -- cgit v1.1