Skip to content

Commit

Permalink
LumpInfo|Cleanup: Implemented C++ constructors and copy-assignment
Browse files Browse the repository at this point in the history
  • Loading branch information
danij-deng committed Oct 7, 2012
1 parent 1c905fc commit d05a775
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 109 deletions.
6 changes: 6 additions & 0 deletions doomsday/engine/portable/include/abstractfile.h
Expand Up @@ -116,6 +116,12 @@ class AbstractFile
return info().size;
}

// Convenient lookup method for when only the is-compressed property is needed from info().
/// @return Size of the uncompressed resource.
inline bool isCompressed() const {
return info().isCompressed();
}

/// @return @c true if the resource is marked "startup".
bool hasStartup() const;

Expand Down
4 changes: 2 additions & 2 deletions doomsday/engine/portable/include/lumpindex.h
Expand Up @@ -52,7 +52,7 @@ namespace de {
class LumpIndex
{
public:
typedef QList<LumpInfo const*> LumpInfos;
typedef QList<LumpInfo const*> Lumps;

public:
/**
Expand Down Expand Up @@ -80,7 +80,7 @@ class LumpIndex
/**
* Provides access to the list of lumps for efficient traversals.
*/
LumpInfos const& lumps();
Lumps const& lumps();

/**
* Clear the index back to its default (i.e., empty state).
Expand Down
41 changes: 39 additions & 2 deletions doomsday/engine/portable/include/lumpinfo.h
Expand Up @@ -28,6 +28,8 @@
#include <de/types.h>

#ifdef __cplusplus
#include <algorithm>

extern "C" {
#endif

Expand All @@ -37,18 +39,53 @@ struct abstractfile_s;
* LumpInfo record. POD.
* @ingroup fs
*/
typedef struct {
typedef struct lumpinfo_s {
uint lastModified; /// Unix timestamp.
int lumpIdx; /// Relative index of this lump in the owning package else zero.
size_t baseOffset; /// Offset from the start of the owning package.
size_t size; /// Size of the uncompressed file.
size_t compressedSize; /// Size of the original file compressed.

/// @todo Move this property up to file level.
struct abstractfile_s* container; /// Owning package else @c NULL.

#ifdef __cplusplus
lumpinfo_s(uint _lastModified = 0, int _lumpIdx = 0, size_t _baseOffset = 0,
size_t _size = 0, size_t _compressedSize = 0, struct abstractfile_s* _container = 0)
: lastModified(_lastModified), lumpIdx(_lumpIdx), baseOffset(_baseOffset),
size(_size), compressedSize(_compressedSize), container(_container)
{}

lumpinfo_s(lumpinfo_s const& other)
: lastModified(other.lastModified), lumpIdx(other.lumpIdx), baseOffset(other.baseOffset),
size(other.size), compressedSize(other.compressedSize), container(other.container)
{}

~lumpinfo_s() {}

lumpinfo_s& operator = (lumpinfo_s other)
{
swap(*this, other);
return *this;
}

friend void swap(lumpinfo_s& first, lumpinfo_s& second) // nothrow
{
using std::swap;
swap(first.lastModified, second.lastModified);
swap(first.lumpIdx, second.lumpIdx);
swap(first.baseOffset, second.baseOffset);
swap(first.size, second.size);
swap(first.compressedSize, second.compressedSize);
swap(first.container, second.container);
}

inline bool isCompressed() const { return size != compressedSize; }
#endif
} LumpInfo;

void F_InitLumpInfo(LumpInfo* info);
void F_CopyLumpInfo(LumpInfo* dst, const LumpInfo* src);
void F_DestroyLumpInfo(LumpInfo* info);

#ifdef __cplusplus
} // extern "C"
Expand Down
3 changes: 1 addition & 2 deletions doomsday/engine/portable/src/abstractfile.cpp
Expand Up @@ -45,14 +45,13 @@ AbstractFile::AbstractFile(filetype_t _type, char const* _path, DFile& file, Lum
flags.startup = false;
flags.custom = true;
Str_Init(&path_); Str_Set(&path_, _path);
F_CopyLumpInfo(&info_, &_info);
info_ = _info;
}

AbstractFile::~AbstractFile()
{
FS::releaseFile(this);
Str_Free(&path_);
F_DestroyLumpInfo(&info_);
if(file) delete file;
}

Expand Down
48 changes: 12 additions & 36 deletions doomsday/engine/portable/src/fs_main.cpp
Expand Up @@ -441,17 +441,7 @@ void F_InitLumpInfo(LumpInfo* info)
void F_CopyLumpInfo(LumpInfo* dst, LumpInfo const* src)
{
DENG_ASSERT(dst && src);
dst->lumpIdx = src->lumpIdx;
dst->baseOffset = src->baseOffset;
dst->size = src->size;
dst->compressedSize = src->compressedSize;
dst->lastModified = src->lastModified;
dst->container = src->container;
}

void F_DestroyLumpInfo(LumpInfo* /*info*/)
{
// Nothing to do.
*dst = *src;
}

void F_Init(void)
Expand Down Expand Up @@ -830,8 +820,7 @@ lumpnum_t FS::openAuxiliary(char const* path, size_t baseOffset)
auxiliaryWadLumpIndexInUse = true;

// Prepare the temporary info descriptor.
LumpInfo info; F_InitLumpInfo(&info);
info.lastModified = lastModified(Str_Text(foundPath));
LumpInfo info = LumpInfo(lastModified(Str_Text(foundPath)));

WadFile* wad = new WadFile(*hndl, Str_Text(foundPath), info);
hndl = DFileBuilder::fromFile(*wad);
Expand All @@ -843,8 +832,6 @@ lumpnum_t FS::openAuxiliary(char const* path, size_t baseOffset)

Str_Delete(foundPath);

// We're done with the descriptor.
F_DestroyLumpInfo(&info);
return AUXILIARY_BASE;
}

Expand Down Expand Up @@ -1257,7 +1244,7 @@ int FS::allResourcePaths(char const* rawSearchPattern, int flags,
// Check the Zip directory.
{
int result = 0;
DENG2_FOR_EACH(i, zipLumpIndex->lumps(), LumpIndex::LumpInfos::const_iterator)
DENG2_FOR_EACH(i, zipLumpIndex->lumps(), LumpIndex::Lumps::const_iterator)
{
LumpInfo const* lumpInfo = *i;
AbstractFile* container = reinterpret_cast<AbstractFile*>(lumpInfo->container);
Expand Down Expand Up @@ -1475,9 +1462,8 @@ static DFile* openAsLumpFile(AbstractFile* container, int lumpIdx,
/// but should instead be deferred until the content of the lump is read.
DFile* hndl = DFileBuilder::fromFileLump(*container, lumpIdx, false/*dontBuffer*/);

// Prepare the temporary info descriptor.
LumpInfo info; F_InitLumpInfo(&info);
F_CopyLumpInfo(&info, &container->lumpInfo(lumpIdx));
// Prepare a the temporary info descriptor.
LumpInfo info = container->lumpInfo(lumpIdx);

// Try to open the referenced file as a specialised file type.
DFile* file = tryOpenFile3(hndl, Str_Text(&absPath), &info);
Expand All @@ -1490,11 +1476,7 @@ static DFile* openAsLumpFile(AbstractFile* container, int lumpIdx,
}
DENG_ASSERT(file);

// We're done with the descriptor.
F_DestroyLumpInfo(&info);

Str_Free(&absPath);

return file;
}

Expand Down Expand Up @@ -1623,8 +1605,7 @@ static DFile* tryOpenFile2(char const* path, char const* mode, size_t baseOffset
DFile* hndl = DFileBuilder::fromNativeFile(*nativeFile, baseOffset);

// Prepare the temporary info descriptor.
LumpInfo info; F_InitLumpInfo(&info);
info.lastModified = FS::lastModified(Str_Text(foundPath));
LumpInfo info = LumpInfo(FS::lastModified(Str_Text(foundPath)));

// Search path is used here rather than found path as the latter may have
// been mapped to another location. We want the file to be attributed with
Expand All @@ -1639,12 +1620,8 @@ static DFile* tryOpenFile2(char const* path, char const* mode, size_t baseOffset
}
DENG_ASSERT(dfile);

// We're done with the descriptor.
F_DestroyLumpInfo(&info);

Str_Delete(foundPath);
Str_Free(&searchPath);

return dfile;
}

Expand Down Expand Up @@ -2208,24 +2185,23 @@ D_CMD(DumpLump)
/**
* Print a content listing of lumps in this directory to stdout (for debug).
*/
static void printLumpIndex(LumpIndex& ld)
static void printLumpIndex(LumpIndex& index)
{
const int numRecords = ld.size();
const int numRecords = index.size();
const int numIndexDigits = MAX_OF(3, M_NumDigits(numRecords));

Con_Printf("LumpIndex %p (%i records):\n", &ld, numRecords);
Con_Printf("LumpIndex %p (%i records):\n", &index, numRecords);

int idx = 0;
DENG2_FOR_EACH(i, ld.lumps(), LumpIndex::LumpInfos::const_iterator)
DENG2_FOR_EACH(i, index.lumps(), LumpIndex::Lumps::const_iterator)
{
LumpInfo const* lumpInfo = *i;
AbstractFile* container = reinterpret_cast<AbstractFile*>(lumpInfo->container);
AutoStr* lumpPath = container->composeLumpPath(lumpInfo->lumpIdx);
Con_Printf("%0*i - \"%s:%s\" (size: %lu bytes%s)\n", numIndexDigits, idx++,
F_PrettyPath(Str_Text(container->path())),
F_PrettyPath(Str_Text(lumpPath)),
F_PrettyPath(Str_Text(container->composeLumpPath(lumpInfo->lumpIdx))),
(unsigned long) lumpInfo->size,
(lumpInfo->compressedSize != lumpInfo->size? " compressed" : ""));
(lumpInfo->isCompressed()? " compressed" : ""));
}
Con_Printf("---End of lumps---\n");
}
Expand Down
6 changes: 3 additions & 3 deletions doomsday/engine/portable/src/lumpindex.cpp
Expand Up @@ -62,7 +62,7 @@ struct LumpIndex::Instance

LumpIndex* self;
int flags; /// @see lumpIndexFlags
LumpIndex::LumpInfos lumpInfos;
LumpIndex::Lumps lumpInfos;
HashMap* hashMap;

Instance(LumpIndex* d, int _flags)
Expand Down Expand Up @@ -277,7 +277,7 @@ LumpInfo const& LumpIndex::lumpInfo(lumpnum_t lumpNum)
return *d->lumpInfos[lumpNum];
}

LumpIndex::LumpInfos const& LumpIndex::lumps()
LumpIndex::Lumps const& LumpIndex::lumps()
{
// We may need to prune path-duplicate lumps.
d->pruneDuplicates();
Expand Down Expand Up @@ -364,7 +364,7 @@ bool LumpIndex::catalogues(AbstractFile& file)
// We may need to prune path-duplicate lumps.
d->pruneDuplicates();

DENG2_FOR_EACH(i, d->lumpInfos, LumpInfos::iterator)
DENG2_FOR_EACH(i, d->lumpInfos, Lumps::iterator)
{
LumpInfo const* lumpInfo = *i;
if(reinterpret_cast<AbstractFile*>(lumpInfo->container) == &file) return true;
Expand Down
68 changes: 36 additions & 32 deletions doomsday/engine/portable/src/wadfile.cpp
Expand Up @@ -38,6 +38,7 @@
#include <de/memoryzone.h>

using namespace de;
using de::AbstractFile;
using de::DFile;
using de::PathDirectoryNode;

Expand All @@ -58,23 +59,36 @@ typedef struct {

struct WadLumpRecord
{
uint crc;
LumpInfo info_;
public:
explicit WadLumpRecord(LumpInfo const& _info) : crc_(0), info_(_info)
{}

explicit WadLumpRecord(LumpInfo const& _info) : crc(0)
{
F_CopyLumpInfo(&info_, &_info);
LumpInfo const& info() const {
return info_;
}

~WadLumpRecord()
{
F_DestroyLumpInfo(&info_);
}
uint crc() const { return crc_; }

LumpInfo const& info() const
/// @attention Calls back into the owning container instance in order to obtain the name.
WadLumpRecord& updateCRC()
{
return info_;
crc_ = uint(info_.size);

AbstractFile* container = reinterpret_cast<AbstractFile*>(info_.container);
DENG_ASSERT(container);
PathDirectoryNode const& node = container->lumpDirectoryNode(info_.lumpIdx);
ddstring_t const* name = node.pathFragment();
int const nameLen = Str_Length(name);
for(int k = 0; k < nameLen; ++k)
{
crc_ += Str_At(name, k);
}
return *this;
}

private:
uint crc_;
LumpInfo info_;
};

struct WadFile::Instance
Expand Down Expand Up @@ -231,34 +245,24 @@ struct WadFile::Instance
wadlumprecord_t const* arcRecord = arcRecords;
for(int i = 0; i < arcRecordsCount; ++i, arcRecord++)
{
LumpInfo info; F_InitLumpInfo(&info);
info.baseOffset = littleEndianByteOrder.toNative(arcRecord->filePos);
info.size = littleEndianByteOrder.toNative(arcRecord->size);
info.compressedSize = info.size;
info.container = reinterpret_cast<abstractfile_s*>(self);
// The modification date is inherited from the file (note recursion).
info.lastModified = self->lastModified();
info.lumpIdx = i;

// Determine the name for this lump in the VFS.
normalizeName(*arcRecord, &absPath);
F_PrependBasePath(&absPath, &absPath); // Make it absolute.

WadLumpRecord* record = new WadLumpRecord(info);
WadLumpRecord* record =
new WadLumpRecord(LumpInfo(self->lastModified(), // Inherited from the file (note recursion).
i,
littleEndianByteOrder.toNative(arcRecord->filePos),
littleEndianByteOrder.toNative(arcRecord->size),
littleEndianByteOrder.toNative(arcRecord->size),
reinterpret_cast<abstractfile_s*>(self)));
PathDirectoryNode* node = lumpDirectory->insert(Str_Text(&absPath));
node->setUserData(record);

F_DestroyLumpInfo(&info);

// Calcuate a simple CRC checksum for the lump.
/// @note If we intend to use the CRC for anything meaningful this algorithm
/// should be replaced and execution deferred until the CRC is needed.
record->crc = uint(record->info().size);
int nameLen = nameLength(*arcRecord);
for(int k = 0; k < nameLen; ++k)
{
record->crc += arcRecord->name[k];
}
record->updateCRC();
}

Str_Free(&absPath);
Expand Down Expand Up @@ -415,7 +419,7 @@ uint8_t const* WadFile::cacheLump(int lumpIdx)
<< F_PrettyPath(Str_Text(path()))
<< F_PrettyPath(Str_Text(composeLumpPath(lumpIdx, '/')))
<< (unsigned long) info.size
<< (info.compressedSize != info.size? ", compressed" : "");
<< (info.isCompressed()? ", compressed" : "");

// Time to create the cache?
if(!d->lumpCache)
Expand Down Expand Up @@ -478,7 +482,7 @@ size_t WadFile::readLump(int lumpIdx, uint8_t* buffer, size_t startOffset,
<< F_PrettyPath(Str_Text(path()))
<< F_PrettyPath(Str_Text(composeLumpPath(lumpIdx, '/')))
<< (unsigned long) lrec->info().size
<< (lrec->info().compressedSize != lrec->info().size? ", compressed" : "")
<< (lrec->info().isCompressed()? ", compressed" : "")
<< (unsigned long) startOffset
<< (unsigned long)length;

Expand Down Expand Up @@ -512,7 +516,7 @@ uint WadFile::calculateCRC()
for(int i = 0; i < numLumps; ++i)
{
WadLumpRecord const* lrec = d->lumpRecord(i);
crc += lrec->crc;
crc += lrec->crc();
}
return crc;
}
Expand Down

0 comments on commit d05a775

Please sign in to comment.