Skip to content

Commit

Permalink
Remove superfluous template parameter from read and write functions
Browse files Browse the repository at this point in the history
  • Loading branch information
kimci86 committed Feb 27, 2024
1 parent 76afc47 commit a82d1c6
Showing 1 changed file with 8 additions and 12 deletions.
20 changes: 8 additions & 12 deletions src/Zip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@
namespace
{

template <typename T, std::size_t N = sizeof(T)>
template <typename T>
auto read(std::istream& is) -> T
{
static_assert(N <= sizeof(T), "read requires output type to have at least N bytes");

// We make no assumption about platform endianness.
auto x = T{};
for (auto index = std::size_t{}; index < N; index++)
for (auto index = std::size_t{}; index < sizeof(T); index++)
x |= static_cast<T>(is.get()) << (8 * index);

return x;
Expand All @@ -31,13 +29,11 @@ auto read(std::istream& is, std::size_t length) -> std::string
return string;
}

template <typename T, std::size_t N = sizeof(T)>
template <typename T>
auto write(std::ostream& os, const T& x) -> std::ostream&
{
static_assert(N <= sizeof(T), "write requires input type to have at least N bytes");

// We make no assumption about platform endianness.
for (auto index = std::size_t{}; index < N; index++)
for (auto index = std::size_t{}; index < sizeof(T); index++)
os.put(lsb(x >> (8 * index)));

return os;
Expand Down Expand Up @@ -80,7 +76,7 @@ auto findCentralDirectoryOffset(std::istream& is) -> std::uint64_t
{
const auto disk = read<std::uint16_t>(is);
is.seekg(10, std::ios::cur);
centralDirectoryOffset = read<std::uint64_t, 4>(is);
centralDirectoryOffset = read<std::uint32_t>(is);

if (!is)
throw Zip::Error{"could not read end of central directory record"};
Expand Down Expand Up @@ -144,13 +140,13 @@ auto Zip::Iterator::operator++() -> Zip::Iterator&
const auto lastModTime = read<std::uint16_t>(*m_is);
m_is->seekg(2, std::ios::cur);
m_entry->crc32 = read<std::uint32_t>(*m_is);
m_entry->packedSize = read<std::uint64_t, 4>(*m_is);
m_entry->uncompressedSize = read<std::uint64_t, 4>(*m_is);
m_entry->packedSize = read<std::uint32_t>(*m_is);
m_entry->uncompressedSize = read<std::uint32_t>(*m_is);
const auto filenameLength = read<std::uint16_t>(*m_is);
const auto extraFieldLength = read<std::uint16_t>(*m_is);
const auto fileCommentLength = read<std::uint16_t>(*m_is);
m_is->seekg(8, std::ios::cur);
m_entry->offset = read<std::uint64_t, 4>(*m_is);
m_entry->offset = read<std::uint32_t>(*m_is);
m_entry->name = read(*m_is, filenameLength);

m_entry->encryption = flags & 1
Expand Down

0 comments on commit a82d1c6

Please sign in to comment.