Directory: Make indexForFile() a const member function

This doesn't modify instance state, so it can be turned into a const
member function.
This commit is contained in:
Lioncash 2019-09-02 08:17:36 -04:00
parent 91a0a41cee
commit 66ce5ed823
3 changed files with 22 additions and 13 deletions

View File

@ -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

View File

@ -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));
}

View File

@ -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;
}