diff --git a/include/kabufuda/Directory.hpp b/include/kabufuda/Directory.hpp index de5fdf4..1047d75 100644 --- a/include/kabufuda/Directory.hpp +++ b/include/kabufuda/Directory.hpp @@ -36,6 +36,6 @@ public: File* getFirstNonFreeFile(uint32_t start, const char* game, const char* maker); File* getFile(const char* game, const char* maker, const char* filename); File* getFile(uint32_t idx); - int32_t indexForFile(File* f); + int32_t indexForFile(const File* f) const; }; } // namespace kabufuda diff --git a/lib/kabufuda/Card.cpp b/lib/kabufuda/Card.cpp index 7dcb661..f517df8 100644 --- a/lib/kabufuda/Card.cpp +++ b/lib/kabufuda/Card.cpp @@ -118,19 +118,23 @@ Card::Card(const char* game, const char* maker) { Card::~Card() { close(); } ECardResult Card::openFile(const char* filename, FileHandle& handleOut) { - ECardResult openRes = _pumpOpen(); - if (openRes != ECardResult::READY) + const ECardResult openRes = _pumpOpen(); + if (openRes != ECardResult::READY) { return openRes; + } handleOut = {}; - File* f = m_dirs[m_currentDir].getFile(m_game, m_maker, filename); - if (!f || f->m_game[0] == 0xFF) + const File* const f = m_dirs[m_currentDir].getFile(m_game, m_maker, filename); + if (!f || f->m_game[0] == 0xFF) { return ECardResult::NOFILE; - int32_t idx = m_dirs[m_currentDir].indexForFile(f); + } + + const int32_t idx = m_dirs[m_currentDir].indexForFile(f); if (idx != -1) { handleOut = FileHandle(idx); return ECardResult::READY; } + return ECardResult::FATAL_ERROR; } @@ -219,11 +223,13 @@ ECardResult Card::closeFile(FileHandle& fh) { } FileHandle Card::firstFile() { - File* f = m_dirs[m_currentDir].getFirstNonFreeFile(0, m_game, m_maker); - if (f) - return FileHandle(m_dirs[m_currentDir].indexForFile(f)); + const File* const f = m_dirs[m_currentDir].getFirstNonFreeFile(0, m_game, m_maker); - return {}; + if (f == nullptr) { + return {}; + } + + return FileHandle(m_dirs[m_currentDir].indexForFile(f)); } FileHandle Card::nextFile(const FileHandle& cur) { @@ -231,9 +237,12 @@ FileHandle Card::nextFile(const FileHandle& cur) { NullFileAccess(); return {}; } - File* next = m_dirs[m_currentDir].getFirstNonFreeFile(cur.idx + 1, m_game, m_maker); - if (!next) + + const File* const next = m_dirs[m_currentDir].getFirstNonFreeFile(cur.idx + 1, m_game, m_maker); + if (next == nullptr) { return {}; + } + return FileHandle(m_dirs[m_currentDir].indexForFile(next)); } diff --git a/lib/kabufuda/Directory.cpp b/lib/kabufuda/Directory.cpp index fb4e464..83d416a 100644 --- a/lib/kabufuda/Directory.cpp +++ b/lib/kabufuda/Directory.cpp @@ -125,7 +125,7 @@ File* Directory::getFile(uint32_t idx) { return &data.m_files[idx]; } -int32_t Directory::indexForFile(File* f) { +int32_t Directory::indexForFile(const File* f) const { if (f == nullptr) { return -1; }