Skip to content

Commit

Permalink
Refactor: Reimplemented LumpFile in C++
Browse files Browse the repository at this point in the history
  • Loading branch information
danij-deng committed Sep 28, 2012
1 parent 86f59c9 commit b926b07
Show file tree
Hide file tree
Showing 2 changed files with 161 additions and 67 deletions.
110 changes: 79 additions & 31 deletions doomsday/engine/portable/include/lumpfile.h
@@ -1,25 +1,28 @@
/**\file lumpfile.h
*\section License
* License: GPL
* Online License Link: http://www.gnu.org/licenses/gpl.html
/**
* @file lumpfile.h
* Specialization of AbstractFile for working with the lumps of containers
* objects such as WadFile and ZipFile.
*
* @ingroup fs
*
*\author Copyright © 2003-2012 Jaakko Keränen <jaakko.keranen@iki.fi>
*\author Copyright © 2005-2012 Daniel Swanson <danij@dengine.net>
* @see abstractfile.h, AbstractFile
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
* @author Copyright &copy; 2003-2012 Jaakko Keränen <jaakko.keranen@iki.fi>
* @author Copyright &copy; 2006-2012 Daniel Swanson <danij@dengine.net>
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301 USA
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details. You should have received a copy of the GNU
* General Public License along with this program; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA</small>
*/

#ifndef LIBDENG_FILESYS_LUMPFILE_H
Expand All @@ -28,28 +31,73 @@
#include "lumpinfo.h"
#include "abstractfile.h"

struct lumpdirectory_s;
#ifdef __cplusplus
namespace de {

/**
* LumpFile. Runtime representation of a lump-file for use with LumpDirectory
*
* @ingroup FS
*/
typedef struct lumpfile_s {
// Base file.
abstractfile_t _base;
} LumpFile;
class LumpFile
{
public:
/// Base file instance.
/// @todo Inherit from this instead.
abstractfile_t base;

LumpFile* LumpFile_New(DFile* file, const char* path, const LumpInfo* info);
void LumpFile_Delete(LumpFile* lump);
public:
LumpFile(DFile& file, char const* path, LumpInfo const& info);
~LumpFile();

/// @return Number of lumps (always @c =1).
int lumpCount();

const LumpInfo* LumpFile_LumpInfo(LumpFile* lump, int lumpIdx);
/**
* Lookup a lump info descriptor for this lump.
*
* @param lumpIdx Ignored. Required argument.
*
* @return Found lump info.
*/
LumpInfo const* lumpInfo(int lumpIdx);

private:
struct Instance;
Instance* d;
};

} // namespace de

extern "C" {
#endif // __cplusplus

/**
* Accessors:
* C wrapper API:
*/

/// @return Number of lumps contained within this file.
struct lumpfile_s; // The lumpfile instance (opaque)
typedef struct lumpfile_s LumpFile;

/**
* Constructs a new LumpFile instance which must be destroyed with LumpFile_Delete()
* once it is no longer needed.
*
* @param file Virtual file handle to the underlying file resource.
* @param path Virtual file system path to associate with the resultant LumpFile.
* @param info File info descriptor for the resultant LumpFile. A copy is made.
*/
LumpFile* LumpFile_New(DFile* file, char const* path, LumpInfo const* info);

/**
* Destroy LumpFile instance @a lump.
*/
void LumpFile_Delete(LumpFile* lump);

LumpInfo const* LumpFile_LumpInfo(LumpFile* lump, int lumpIdx);

int LumpFile_LumpCount(LumpFile* lump);

#endif /// LIBDENG_FILESYS_LUMPFILE_H
#ifdef __cplusplus
} // extern "C"
#endif

#endif /* LIBDENG_FILESYS_LUMPFILE_H */
118 changes: 82 additions & 36 deletions doomsday/engine/portable/src/lumpfile.cpp
@@ -1,60 +1,106 @@
/**\file lumpfile.c
*\section License
* License: GPL
* Online License Link: http://www.gnu.org/licenses/gpl.html
/**
* @file lumpfile.cpp
* Lump (file) accessor abstraction for containers. @ingroup fs
*
*\author Copyright © 2003-2012 Jaakko Keränen <jaakko.keranen@iki.fi>
*\author Copyright © 2005-2012 Daniel Swanson <danij@dengine.net>
* @author Copyright &copy; 2003-2012 Jaakko Keränen <jaakko.keranen@iki.fi>
* @author Copyright &copy; 2005-2012 Daniel Swanson <danij@dengine.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301 USA
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details. You should have received a copy of the GNU
* General Public License along with this program; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA</small>
*/

#include "de_base.h"
#include "de_console.h"
#include "de_filesys.h"

#include "lumpdirectory.h"
#include "lumpfile.h"

LumpFile* LumpFile_New(DFile* file, const char* path, const LumpInfo* info)
#include <de/Error>
#include <de/Log>
#include <de/memory.h>

de::LumpFile::LumpFile(DFile& file, char const* path, LumpInfo const& info)
{
AbstractFile_Init(reinterpret_cast<abstractfile_t*>(this), FT_LUMPFILE, path, &file, &info);
}

de::LumpFile::~LumpFile()
{
LumpFile* lump = (LumpFile*)malloc(sizeof *lump);
if(!lump) Con_Error("LumpFile::Construct: Failed on allocation of %lu bytes for new LumpFile.",
(unsigned long) sizeof *lump);
F_ReleaseFile(reinterpret_cast<abstractfile_t*>(this));
AbstractFile_Destroy(reinterpret_cast<abstractfile_t*>(this));
}

AbstractFile_Init((abstractfile_t*)lump, FT_LUMPFILE, path, file, info);
return lump;
LumpInfo const* de::LumpFile::lumpInfo(int /*lumpIdx*/)
{
// Lump files are special cases for this *is* the lump.
return AbstractFile_Info(reinterpret_cast<abstractfile_t*>(this));
}

int de::LumpFile::lumpCount()
{
return 1; // Always.
}

/**
* C Wrapper API:
*/

#define TOINTERNAL(inst) \
(inst) != 0? reinterpret_cast<de::LumpFile*>(inst) : NULL

#define TOINTERNAL_CONST(inst) \
(inst) != 0? reinterpret_cast<de::LumpFile const*>(inst) : NULL

#define SELF(inst) \
DENG2_ASSERT(inst); \
de::LumpFile* self = TOINTERNAL(inst)

#define SELF_CONST(inst) \
DENG2_ASSERT(inst); \
de::LumpFile const* self = TOINTERNAL_CONST(inst)

LumpFile* LumpFile_New(DFile* file, char const* path, LumpInfo const* info)
{
if(!info) LegacyCore_FatalError("LumpFile_New: Received invalid LumpInfo (=NULL).");
try
{
return reinterpret_cast<LumpFile*>(new de::LumpFile(*file, path, *info));
}
catch(de::Error& er)
{
QString msg = QString("LumpFile_New: Failed to instantiate new LumpFile. ") + er.asText();
LegacyCore_FatalError(msg.toUtf8().constData());
exit(1); // Unreachable.
}
}

void LumpFile_Delete(LumpFile* lump)
{
assert(lump);
F_ReleaseFile((abstractfile_t*)lump);
AbstractFile_Destroy((abstractfile_t*)lump);
free(lump);
if(lump)
{
SELF(lump);
delete self;
}
}

const LumpInfo* LumpFile_LumpInfo(LumpFile* lump, int lumpIdx)
LumpInfo const* LumpFile_LumpInfo(LumpFile* lump, int lumpIdx)
{
assert(lump);
/// Lump files are special cases for this *is* the lump.
return AbstractFile_Info((abstractfile_t*)lump);
SELF(lump);
return self->lumpInfo(lumpIdx);
}

int LumpFile_LumpCount(LumpFile* lump)
{
return 1; // Always.
SELF(lump);
return self->lumpCount();
}

0 comments on commit b926b07

Please sign in to comment.