Skip to content

Commit

Permalink
WadFile: Improved lump index out-of-range exception messages
Browse files Browse the repository at this point in the history
  • Loading branch information
danij-deng committed Sep 26, 2012
1 parent 7eef2fe commit c2d180d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
2 changes: 1 addition & 1 deletion doomsday/engine/portable/include/wadfile.h
Expand Up @@ -64,7 +64,7 @@ class WadFile
/// @return @c true= @a lumpIdx is a valid logical index for a lump in this file.
bool isValidIndex(int lumpIdx);

/// @return Logical index of the last lump in this file's directory or @c 0 if empty.
/// @return Logical index of the last lump in this file's directory or @c -1 if empty.
int lastIndex();

/// @return Number of lumps contained by this file or @c 0 if empty.
Expand Down
15 changes: 11 additions & 4 deletions doomsday/engine/portable/src/wadfile.cpp
Expand Up @@ -314,8 +314,7 @@ bool de::WadFile::isValidIndex(int lumpIdx)

int de::WadFile::lastIndex()
{
int numLumps = lumpCount();
return numLumps? numLumps - 1 : 0;
return lumpCount() - 1;
}

int de::WadFile::lumpCount()
Expand All @@ -335,19 +334,27 @@ de::PathDirectoryNode* de::WadFile::lumpDirectoryNode(int lumpIdx)
return (*d->lumpNodeLut)[lumpIdx];
}

static QString invalidIndexMessage(int invalidIdx, int lastValidIdx)
{
QString msg = QString("Invalid lump index %1 ").arg(invalidIdx);
if(lastValidIdx < 0) msg += "(file is empty)";
else msg += QString("(valid range: [0..%2])").arg(lastValidIdx);
return msg;
}

LumpInfo const* de::WadFile::lumpInfo(int lumpIdx)
{
LOG_AS("WadFile");
LumpRecord* lrec = d->lumpRecord(lumpIdx);
if(!lrec) throw de::Error("WadFile::lumpInfo", QString("Invalid lump index %1 (valid range: [0..%2])").arg(lumpIdx).arg(lastIndex()));
if(!lrec) throw de::Error("WadFile::lumpInfo", invalidIndexMessage(lumpIdx, lastIndex()));
return &lrec->info;
}

size_t de::WadFile::lumpSize(int lumpIdx)
{
LOG_AS("WadFile");
LumpRecord* lrec = d->lumpRecord(lumpIdx);
if(!lrec) throw de::Error("WadFile::lumpSize", QString("Invalid lump index %1 (valid range: [0..%2])").arg(lumpIdx).arg(lastIndex()));
if(!lrec) throw de::Error("WadFile::lumpSize", invalidIndexMessage(lumpIdx, lastIndex()));
return lrec->info.size;
}

Expand Down

0 comments on commit c2d180d

Please sign in to comment.