| 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 | #pragma once |
| 19 | |
| 20 | #include <cstdint> |
| 21 | #include <istream> |
| 22 | #include <ostream> |
| 23 | |
| 24 | #include "byteswap.h" |
| 25 | |
| 26 | namespace D3 { |
| 27 | |
| 28 | template <class T, typename = std::enable_if_t<std::is_arithmetic_v<T>>> |
| 29 | static inline std::ostream &bin_write(std::ostream &output, T value, bool is_little_endian = true, |
| 30 | size_t n = sizeof(T)) { |
| 31 | value = is_little_endian ? convert_le(value) : convert_be(value); |
| 32 | output.write(reinterpret_cast<const char *>(&value), n); |
| 33 | |
| 34 | return output; |
| 35 | } |
| 36 | |
| 37 | template <class T, typename = std::enable_if_t<std::is_arithmetic_v<T>>> |
| 38 | static inline std::istream &bin_read(std::istream &input, T &value, bool is_little_endian = true, size_t n = sizeof(T)) { |
| 39 | input.read(reinterpret_cast<char *>(&value), n); |
| 40 | value = is_little_endian ? convert_le(value) : convert_be(value); |
| 41 | |
| 42 | return input; |
| 43 | } |
| 44 | |
| 45 | } // namespace D3 |