From ed2a6b3ce80bf35b3171e1ae263afd1f53aff781 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Mon, 2 Sep 2019 08:23:38 -0400 Subject: [PATCH] Card: Make querying functions const member functions where applicable These don't modify member state, so they can be made const . --- include/kabufuda/Card.hpp | 8 ++++---- lib/kabufuda/Card.cpp | 15 +++++++++------ 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/include/kabufuda/Card.hpp b/include/kabufuda/Card.hpp index 6b913db..5a56a51 100644 --- a/include/kabufuda/Card.hpp +++ b/include/kabufuda/Card.hpp @@ -204,7 +204,7 @@ public: * * @return Gets the name of the given file. */ - const char* getFilename(const FileHandle& fh); + const char* getFilename(const FileHandle& fh) const; /** * @brief deleteFile @@ -269,7 +269,7 @@ public: * * @return The offset or -1 if an invalid handle is passed. */ - int32_t tell(const FileHandle& fh); + int32_t tell(const FileHandle& fh) const; /** * @brief setPublic @@ -425,7 +425,7 @@ public: * @param checksum The checksum of the system header. * @param inverse The inverse checksum of the system header. */ - void getChecksum(uint16_t& checksum, uint16_t& inverse); + void getChecksum(uint16_t& checksum, uint16_t& inverse) const; /** * @brief Retrieves the available storage and directory space. @@ -433,7 +433,7 @@ public: * @param bytesNotUsed Number of free bytes out. * @param filesNotUsed Number of free files out. */ - void getFreeBlocks(int32_t& bytesNotUsed, int32_t& filesNotUsed); + void getFreeBlocks(int32_t& bytesNotUsed, int32_t& filesNotUsed) const; /** * @brief Formats the memory card and assigns a new serial. diff --git a/lib/kabufuda/Card.cpp b/lib/kabufuda/Card.cpp index f517df8..63f1cf8 100644 --- a/lib/kabufuda/Card.cpp +++ b/lib/kabufuda/Card.cpp @@ -246,10 +246,13 @@ FileHandle Card::nextFile(const FileHandle& cur) { return FileHandle(m_dirs[m_currentDir].indexForFile(next)); } -const char* Card::getFilename(const FileHandle& fh) { - File* f = _fileFromHandle(fh); - if (!f) +const char* Card::getFilename(const FileHandle& fh) const { + const File* const f = _fileFromHandle(fh); + + if (f == nullptr) { return nullptr; + } + return f->m_filename; } @@ -454,7 +457,7 @@ void Card::seek(FileHandle& fh, int32_t pos, SeekOrigin whence) { } } -int32_t Card::tell(const FileHandle& fh) { return fh.offset; } +int32_t Card::tell(const FileHandle& fh) const { return fh.offset; } void Card::setPublic(const FileHandle& fh, bool pub) { File* file = _fileFromHandle(fh); @@ -755,12 +758,12 @@ void Card::getSerial(uint64_t& serial) { m_ch._swapEndian(); } -void Card::getChecksum(uint16_t& checksum, uint16_t& inverse) { +void Card::getChecksum(uint16_t& checksum, uint16_t& inverse) const { checksum = m_ch.m_checksum; inverse = m_ch.m_checksumInv; } -void Card::getFreeBlocks(int32_t& bytesNotUsed, int32_t& filesNotUsed) { +void Card::getFreeBlocks(int32_t& bytesNotUsed, int32_t& filesNotUsed) const { bytesNotUsed = m_bats[m_currentBat].numFreeBlocks() * 0x2000; filesNotUsed = m_dirs[m_currentDir].numFreeFiles(); }