| 1 |
/* |
| 2 |
* Descent 3 |
| 3 |
* Copyright (C) 2024 Descent Developers |
| 4 |
* |
| 5 |
* This program is free software: you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation, either version 3 of the License, or |
| 8 |
* (at your option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License |
| 16 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
*/ |
| 18 |
|
| 19 |
#include <algorithm> |
| 20 |
#include <fstream> |
| 21 |
#include <iterator> |
| 22 |
|
| 23 |
#include "HogFormat.h" |
| 24 |
#include "IOOps.h" |
| 25 |
|
| 26 |
namespace D3 { |
| 27 |
|
| 28 |
// TODO: To our descendants from the future: remove it when code will support C++20 |
| 29 |
template <typename TP> std::time_t to_time_t(TP tp) { |
| 30 |
auto sctp = std::chrono::duration_cast<std::chrono::system_clock::duration>(tp - TP::clock::now()) + |
| 31 |
std::chrono::system_clock::now(); |
| 32 |
return std::chrono::system_clock::to_time_t(sctp); |
| 33 |
} |
| 34 |
|
| 35 |
HogFileEntry::HogFileEntry(const std::filesystem::path &input) { |
| 36 |
m_real_path = input; |
| 37 |
m_flags = 0; |
| 38 |
m_len = (uint32_t)file_size(input); |
| •39 |
m_timestamp = 0; |
| 40 |
auto name = input.filename().u8string(); |
| 41 |
std::copy(name.begin(), name.end(), m_name.begin()); |
| 42 |
} |
| 43 |
|
| 44 |
HogFileEntry::HogFileEntry(std::string name, uint32_t flags, uint32_t len, uint32_t timestamp) { |
| 45 |
std::copy(name.begin(), name.end(), m_name.begin()); |
| 46 |
m_flags = flags; |
| 47 |
m_len = len; |
| 48 |
m_timestamp = timestamp; |
| 49 |
} |
| 50 |
|
| 51 |
void HogFileEntry::SetRealPath(const std::filesystem::path &input) { |
| 52 |
m_real_path = input; |
| 53 |
m_flags = 0; |
| 54 |
m_len = (uint32_t)file_size(input); |
| 55 |
m_timestamp = (uint32_t)to_time_t(last_write_time(input)); |
| 56 |
auto name = input.filename().u8string(); |
| 57 |
std::copy(name.begin(), name.end(), m_name.begin()); |
| 58 |
} |
| 59 |
|
| 60 |
std::array<char, 36> char_tolower(std::array<char, 36> s) { |
| 61 |
std::transform(s.begin(), s.end(), s.begin(), [](uint8_t c) { return std::tolower(c); }); |
| 62 |
return s; |
| 63 |
} |
| 64 |
|
| 65 |
std::ostream &operator<<(std::ostream &output, const HogHeader &header) { |
| 66 |
std::copy(header.tag.begin(), header.tag.end(), std::ostream_iterator<char>(output)); |
| 67 |
bin_write(output, header.nfiles); |
| 68 |
bin_write(output, header.file_data_offset); |
| 69 |
std::copy(header.reserved.begin(), header.reserved.end(), std::ostream_iterator<char>(output)); |
| 70 |
|
| 71 |
return output; |
| 72 |
} |
| 73 |
|
| 74 |
std::ostream &operator<<(std::ostream &output, const HogFileEntry &entry) { |
| 75 |
std::copy(entry.m_name.begin(), entry.m_name.end(), std::ostream_iterator<char>(output)); |
| 76 |
bin_write(output, entry.m_flags); |
| 77 |
bin_write(output, entry.m_len); |
| 78 |
bin_write(output, entry.m_timestamp); |
| 79 |
|
| 80 |
return output; |
| 81 |
} |
| 82 |
|
| 83 |
std::ostream &operator<<(std::ostream &output, const HogFormat &format) { |
| 84 |
output << format.m_header; |
| 85 |
for (const HogFileEntry &entry : format.m_file_entries) { |
| 86 |
output << entry; |
| 87 |
} |
| 88 |
|
| 89 |
return output; |
| 90 |
} |
| 91 |
|
| 92 |
void HogFormat::SortEntries() { |
| 93 |
auto customLess = [](const HogFileEntry &a, const HogFileEntry &b) { |
| 94 |
return (char_tolower(a.m_name) < char_tolower(b.m_name)); |
| 95 |
}; |
| 96 |
|
| 97 |
std::sort(m_file_entries.begin(), m_file_entries.end(), customLess); |
| 98 |
} |
| 99 |
|
| 100 |
} // namespace D3 |