General: Use std::array for constructors where applicable

Same behavior, but ensures input absolutely satisfies the total amount
of data that needs to be copied.
This commit is contained in:
Lioncash 2019-09-02 08:09:07 -04:00
parent 91a0a41cee
commit 69d24d1027
4 changed files with 12 additions and 7 deletions

View File

@ -15,9 +15,12 @@ class Directory {
uint16_t m_checksum;
uint16_t m_checksumInv;
};
using RawData = std::array<uint8_t, BlockSize>;
union {
Data data;
std::array<uint8_t, BlockSize> raw;
RawData raw;
};
#pragma pack(pop)
@ -27,7 +30,7 @@ class Directory {
public:
Directory();
Directory(uint8_t data[BlockSize]);
explicit Directory(const RawData& rawData);
~Directory() = default;
bool hasFreeFile() const;

View File

@ -10,6 +10,8 @@ class File {
friend class Directory;
friend class Card;
#pragma pack(push, 4)
using RawData = std::array<uint8_t, 0x40>;
union {
struct {
std::array<uint8_t, 4> m_game;
@ -28,7 +30,7 @@ class File {
uint16_t m_reserved2;
uint32_t m_commentAddr;
};
std::array<uint8_t, 0x40> raw;
RawData raw;
};
#pragma pack(pop)
@ -36,8 +38,8 @@ class File {
public:
File();
File(char data[0x40]);
File(const char* filename);
explicit File(const RawData& rawData);
explicit File(const char* filename);
~File() = default;
};
} // namespace kabufuda

View File

@ -35,7 +35,7 @@ Directory::Directory() {
updateChecksum();
}
Directory::Directory(uint8_t data[]) { std::memcpy(raw.data(), data, BlockSize); }
Directory::Directory(const RawData& rawData) : raw{rawData} {}
bool Directory::hasFreeFile() const {
return std::any_of(data.m_files.cbegin(), data.m_files.cend(),

View File

@ -8,7 +8,7 @@
namespace kabufuda {
File::File() { raw.fill(0xFF); }
File::File(char data[]) { std::memcpy(raw.data(), data, raw.size()); }
File::File(const RawData& rawData) : raw{rawData} {}
File::File(const char* filename) {
raw.fill(0);