summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAki <please@ignore.pl>2022-08-11 23:00:34 +0200
committerAki <please@ignore.pl>2022-08-11 23:00:34 +0200
commita74cf471b8637a107b77a94717eba7c538b2964f (patch)
tree7e7a7dc73a6a51045515c1b73b5c916cfbbe1995
parentff972b194ff36252e9974327f66f05ea394fd704 (diff)
downloadstarshatter-a74cf471b8637a107b77a94717eba7c538b2964f.zip
starshatter-a74cf471b8637a107b77a94717eba7c538b2964f.tar.gz
starshatter-a74cf471b8637a107b77a94717eba7c538b2964f.tar.bz2
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).
-rw-r--r--ArchiveEx/Archive.cpp19
1 files 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 <cstdio>
#include <cstring>
#include <memory>
-#include <vector>
#include <zlib.h>
@@ -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<Bytef> compressed(header.directory.compressed_size);
- length = std::fread(compressed.data(), 1, header.directory.compressed_size, file.get());
+ auto compressed = std::make_unique<Bytef[]>(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<Bytef*>(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<Bytef> compressed(entry.compressed_size);
+ auto compressed = std::make_unique<Bytef[]>(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<std::uint8_t[]>(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<char> path(std::strlen(filepath) + 1);
- ConvertPathSeparator(filepath, path.data());
+ auto path = std::make_unique<char[]>(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;