Skip to content

Commit

Permalink
Completed refactorings to replace DFILE with abstractfile_t:
Browse files Browse the repository at this point in the history
All file handles (including virtual ones, for lumps within containers)
used throughout the engine and the public API are now derived from this
core generic base.

Also, ccmd "listfiles" now sorts output alphanumerically.

Todo: Cleanup! As one might expect, these changes touch nearly every
corner of the codebase. There are now numerous places where redundancy
is rife and several of the previously well-defined object interfaces have
been subverted to reach this point. It is however completely functional,
therefore I am commiting this now, as-is, so it can be used to assist in
tracking down potential regressions that may be inadvertently introduced
in the cleanup process.
  • Loading branch information
danij-deng committed Sep 2, 2011
1 parent 8b20c89 commit 3661f85
Show file tree
Hide file tree
Showing 31 changed files with 398 additions and 312 deletions.
17 changes: 13 additions & 4 deletions doomsday/engine/portable/include/abstractfile.h
Expand Up @@ -25,12 +25,13 @@
#ifndef LIBDENG_FILESYS_ABSTRACTFILE_H
#define LIBDENG_FILESYS_ABSTRACTFILE_H

#include "sys_file.h"
#include <stdio.h>

struct lumpdirectory_s;

// File types.
typedef enum {
FT_UNKNOWNFILE,
FT_ZIPFILE,
FT_WADFILE,
FT_LUMPFILE
Expand All @@ -42,14 +43,22 @@ typedef enum {
*/
typedef struct abstractfile_s {
filetype_t _type;
DFILE* _handle;
struct abstractfile_flags_s {
unsigned char open:1;
unsigned char eof:1;
} flags;
size_t size;
FILE* hndl;
uint8_t* data;
uint8_t* pos;
unsigned int lastModified;
ddstring_t _absolutePath;
/// Load order depth index.
uint _order;
} abstractfile_t;

/// Initialize this file.
void AbstractFile_Init(abstractfile_t* file, filetype_t type, DFILE* handle, const char* absolutePath);
void AbstractFile_Init(abstractfile_t* file, filetype_t type, FILE* handle, const char* absolutePath);

/// @return Type of this file.
filetype_t AbstractFile_Type(const abstractfile_t* file);
Expand All @@ -58,7 +67,7 @@ filetype_t AbstractFile_Type(const abstractfile_t* file);
* Accessors:
*/
/// @return File handle acquired for this resource else @c NULL
DFILE* AbstractFile_Handle(abstractfile_t* file);
FILE* AbstractFile_Handle(abstractfile_t* file);

/// @return Resolved (possibly virtual/mapped) path to this file.
const ddstring_t* AbstractFile_AbsolutePath(abstractfile_t* file);
Expand Down
17 changes: 12 additions & 5 deletions doomsday/engine/portable/include/fs_main.h
Expand Up @@ -45,6 +45,8 @@
#ifndef LIBDENG_FILESYS_MAIN_H
#define LIBDENG_FILESYS_MAIN_H

#include <stdio.h>

#include "zipfile.h"
#include "wadfile.h"
#include "lumpfile.h"
Expand Down Expand Up @@ -100,7 +102,7 @@ boolean F_ReleaseFileId(const char* path);
/**
* Frees the memory allocated to the handle.
*/
void F_Release(DFILE* file);
void F_Release(abstractfile_t* file);

/// @return Number of files in the currently active primary LumpDirectory.
int F_LumpCount(void);
Expand Down Expand Up @@ -142,8 +144,8 @@ lumpnum_t F_CheckLumpNumForName(const char* name, boolean silent);
* Release with F_CloseAuxiliary.
* @return Base index for lumps in this archive.
*/
lumpnum_t F_OpenAuxiliary3(const char* fileName, DFILE* prevOpened, boolean silent);
lumpnum_t F_OpenAuxiliary2(const char* fileName, DFILE* prevOpened);
lumpnum_t F_OpenAuxiliary3(const char* fileName, FILE* prevOpened, boolean silent);
lumpnum_t F_OpenAuxiliary2(const char* fileName, FILE* prevOpened);
lumpnum_t F_OpenAuxiliary(const char* fileName);

void F_CloseAuxiliary(void);
Expand Down Expand Up @@ -175,14 +177,19 @@ boolean F_AddFiles(const char* const* filenames, size_t num, boolean allowDuplic
boolean F_RemoveFile(const char* fileName);
boolean F_RemoveFiles(const char* const* filenames, size_t num);

/**
* @return @c true if the file can be opened for reading.
*/
int F_Access(const char* path);

/**
* Opens the given file (will be translated) for reading.
* "t" = text mode (with real files, lumps are always binary)
* "b" = binary
* "f" = must be a real file in the local file system.
* "x" = just test for access (don't buffer anything)
*/
DFILE* F_Open(const char* path, const char* mode);
abstractfile_t* F_Open(const char* path, const char* mode);

/**
* Try to locate the specified lump for reading.
Expand All @@ -192,7 +199,7 @@ DFILE* F_Open(const char* path, const char* mode);
*
* @return Handle to the opened file if found.
*/
DFILE* F_OpenLump(lumpnum_t lumpNum, boolean dontBuffer);
abstractfile_t* F_OpenLump(lumpnum_t lumpNum, boolean dontBuffer);

/**
* @return The time when the file was last modified, as seconds since
Expand Down
2 changes: 1 addition & 1 deletion doomsday/engine/portable/include/gl_pcx.h
Expand Up @@ -39,7 +39,7 @@
* The caller must free the allocated buffer with Z_Free.
* Width, height and pixelSize can't be NULL.
*/
uint8_t* PCX_Load(DFILE* file, int* width, int* height, int* pixelSize);
uint8_t* PCX_Load(abstractfile_t* file, int* width, int* height, int* pixelSize);

/**
* @return Textual message detailing the last error encountered else @c 0.
Expand Down
4 changes: 2 additions & 2 deletions doomsday/engine/portable/include/gl_png.h
Expand Up @@ -31,15 +31,15 @@
#ifndef LIBDENG_GRAPHICS_PNG_H
#define LIBDENG_GRAPHICS_PNG_H

struct DFILE;
struct abstractfile_t;

/**
* Reads the given PNG image and returns a pointer to a planar RGB or
* RGBA buffer. Width and height are set, and pixelSize is either 3 (RGB)
* or 4 (RGBA). The caller must free the allocated buffer with Z_Free.
* Width, height and pixelSize can't be NULL. Handles 1-4 channels.
*/
uint8_t* PNG_Load(DFILE* file, int* width, int* height, int* pixelSize);
uint8_t* PNG_Load(abstractfile_t* file, int* width, int* height, int* pixelSize);

/**
* @return Textual message detailing the last error encountered else @c 0.
Expand Down
11 changes: 5 additions & 6 deletions doomsday/engine/portable/include/gl_texmanager.h
Expand Up @@ -201,18 +201,17 @@ byte GL_LoadExtTexture(struct image_s* image, const char* name, gfxmode_t mode);
byte GL_LoadExtTextureEX(struct image_s* image, const char* searchPath,
const char* optionalSuffix, boolean silent);

byte GL_LoadFlatLump(struct image_s* image, DFILE* file, const char* lumpName);
byte GL_LoadFlatLump(struct image_s* image, abstractfile_t* file);

byte GL_LoadPatchLumpAsPatch(struct image_s* image, DFILE* file, lumpnum_t lumpNum, int tclass,
byte GL_LoadPatchLumpAsPatch(struct image_s* image, abstractfile_t* file, int tclass,
int tmap, int border, patchtex_t* patchTex);
byte GL_LoadPatchLumpAsSprite(struct image_s* image, DFILE* file, lumpnum_t lumpNum, int tclass,
byte GL_LoadPatchLumpAsSprite(struct image_s* image, abstractfile_t* file, int tclass,
int tmap, int border, spritetex_t* spriteTex);

byte GL_LoadDetailTextureLump(struct image_s* image, DFILE* file, const char* lumpName);
byte GL_LoadDetailTextureLump(struct image_s* image, abstractfile_t* file);

byte GL_LoadPatchComposite(struct image_s* image, const struct texture_s* tex);
byte GL_LoadPatchCompositeAsSky(struct image_s* image, const struct texture_s* tex,
boolean zeroMask);
byte GL_LoadPatchCompositeAsSky(struct image_s* image, const struct texture_s* tex, boolean zeroMask);

/**
* Set mode to 2 to include an alpha channel. Set to 3 to make the
Expand Down
4 changes: 2 additions & 2 deletions doomsday/engine/portable/include/gl_tga.h
Expand Up @@ -29,7 +29,7 @@
#ifndef LIBDENG_GRAPHICS_TGA_H
#define LIBDENG_GRAPHICS_TGA_H

struct DFILE;
struct abstractfile_t;

/**
* Saves the buffer (which is formatted rgb565) to a Targa 24 image file.
Expand Down Expand Up @@ -87,7 +87,7 @@ int TGA_Save16_rgb888(FILE* file, int w, int h, const uint8_t* buf);
*
* @return Non-zero iff the image is loaded successfully.
*/
uint8_t* TGA_Load(DFILE* file, int* width, int* height, int* pixelSize);
uint8_t* TGA_Load(abstractfile_t* file, int* width, int* height, int* pixelSize);

/**
* @return Textual message detailing the last error encountered else @c 0.
Expand Down
2 changes: 1 addition & 1 deletion doomsday/engine/portable/include/lumpfile.h
Expand Up @@ -42,7 +42,7 @@ typedef struct lumpfile_s {
void** _cacheData;
} lumpfile_t;

lumpfile_t* LumpFile_New(DFILE* handle, const char* absolutePath, lumpname_t name, size_t lumpSize);
lumpfile_t* LumpFile_New(const char* absolutePath, const lumpname_t name, size_t lumpSize);
void LumpFile_Delete(lumpfile_t* lump);

/// Close this file if open and release any acquired file identifiers.
Expand Down
2 changes: 1 addition & 1 deletion doomsday/engine/portable/include/lumpinfo.h
Expand Up @@ -33,7 +33,7 @@
*/
typedef struct {
lumpname_t name; /// Ends in '\0'. Used with WAD lumps.
ddstring_t path; /// Full variable-length path. Used with ZipFile/LumpFile
ddstring_t path; /// Full variable-length path.
size_t baseOffset; /// Offset from start of owning package.
size_t size; /// Size of the uncompressed file.
size_t compressedSize; /// Size of the original file compressed.
Expand Down
2 changes: 1 addition & 1 deletion doomsday/engine/portable/include/m_misc.h
Expand Up @@ -73,7 +73,7 @@ void M_Strip(char* str, size_t len);
char* M_SkipLine(char* str);
void M_WriteCommented(FILE* file, const char* text);
void M_WriteTextEsc(FILE* file, const char* text);
void M_ReadLine(char* buffer, size_t len, DFILE* file);
void M_ReadLine(char* buffer, size_t len, abstractfile_t* file);

boolean M_IsComment(const char* text);

Expand Down
46 changes: 19 additions & 27 deletions doomsday/engine/portable/include/sys_file.h
Expand Up @@ -36,82 +36,74 @@

#include <stdio.h>

#include "dd_types.h"
#include "abstractfile.h"

struct abstractfile_s;

struct dfile_s; // The file instance (opaque).
typedef struct dfile_s DFILE;

DFILE* F_NewFile(void);
abstractfile_t* F_NewFile(const char* absolutePath);
void F_Delete(abstractfile_t* file);

/**
* @return @c true if the file can be opened for reading.
* Close the current file if open, removing all references to it in the open file list.
*/
int F_Access(const char* path);

void F_Close(DFILE* file);
void F_Close(abstractfile_t* file);

/// @return The length of the file, in bytes.
size_t F_Length(DFILE* file);
size_t F_Length(abstractfile_t* file);

/// @return "Last modified" timestamp of the file.
unsigned int F_LastModified(DFILE* file);
unsigned int F_LastModified(abstractfile_t* file);

/**
* @return Number of bytes read (at most @a count bytes will be read).
*/
size_t F_Read(DFILE* file, uint8_t* buffer, size_t count);
size_t F_Read(abstractfile_t* file, uint8_t* buffer, size_t count);

/**
* Read a character from the stream, advancing the read position in the process.
*/
unsigned char F_GetC(DFILE* file);
unsigned char F_GetC(abstractfile_t* file);

/**
* @return @c true iff the stream has reached the end of the file.
*/
boolean F_AtEnd(DFILE* file);
boolean F_AtEnd(abstractfile_t* file);

/**
* @return Current position in the stream as an offset from the beginning of the file.
*/
size_t F_Tell(DFILE* file);
size_t F_Tell(abstractfile_t* file);

/**
* @return The current position in the file, before the move, as an
* offset from the beginning of the file.
*/
size_t F_Seek(DFILE* file, size_t offset, int whence);
size_t F_Seek(abstractfile_t* file, size_t offset, int whence);

/**
* Rewind the stream to the start of the file.
*/
void F_Rewind(DFILE* file);
void F_Rewind(abstractfile_t* file);

/**
* Open a new stream on the specified lump for reading.
*
* @param fsObject File system record for the file containing the lump to be read.
* @param file File system handle used to reference the lump once read.
* @param container File system record for the file containing the lump to be read.
* @param lump Index of the lump to open.
* @param dontBuffer Just test for access (don't buffer anything).
*
* @return Same as @a file for convenience.
*/
DFILE* F_OpenStreamLump(DFILE* file, struct abstractfile_s* fsObject, int lumpIdx, boolean dontBuffer);
abstractfile_t* F_OpenStreamLump(abstractfile_t* file, abstractfile_t* container, int lumpIdx, boolean dontBuffer);

/**
* Open a new stream on the specified lump for reading.
*
* @param fsObject File system record for the file containing the lump to be read.
* @param lump Index of the lump to open.
* @param file File system handle used to reference the file once read.
* @param hndl Handle to the file containing the data to be read.
*
* @return Same as @a file for convenience.
*/
DFILE* F_OpenStreamFile(DFILE* file, FILE* hndl, const char* path);

/// \note deprecated
FILE* F_Handle(DFILE* file);
abstractfile_t* F_OpenStreamFile(abstractfile_t* file, FILE* hndl, const char* path);

/**
* This is a case-insensitive test.
Expand Down
2 changes: 1 addition & 1 deletion doomsday/engine/portable/include/wadfile.h
Expand Up @@ -47,7 +47,7 @@ typedef struct wadfile_s {
void** _lumpCache;
} wadfile_t;

wadfile_t* WadFile_New(DFILE* handle, const char* absolutePath);
wadfile_t* WadFile_New(FILE* handle, const char* absolutePath);
void WadFile_Delete(wadfile_t* wad);

/// Close this file if open and release any acquired file identifiers.
Expand Down
2 changes: 1 addition & 1 deletion doomsday/engine/portable/include/zipfile.h
Expand Up @@ -47,7 +47,7 @@ typedef struct {
void** _lumpCache;
} zipfile_t;

zipfile_t* ZipFile_New(DFILE* handle, const char* absolutePath);
zipfile_t* ZipFile_New(FILE* handle, const char* absolutePath);
void ZipFile_Delete(zipfile_t* zip);

/// Close this file if open and release any acquired file identifiers.
Expand Down
29 changes: 19 additions & 10 deletions doomsday/engine/portable/src/abstractfile.c
Expand Up @@ -27,19 +27,28 @@
#include "sys_reslocator.h"
#include "abstractfile.h"

void AbstractFile_Init(abstractfile_t* file, filetype_t type,
DFILE* handle, const char* absolutePath)
void AbstractFile_Init(abstractfile_t* file, filetype_t type, FILE* handle,
const char* absolutePath)
{
// Used with to favor newer files when duplicates are pruned.
// Used to favor newer files when duplicates are pruned.
static uint fileCounter = 0;
assert(NULL != file && NULL != handle && NULL != absolutePath);
assert(NULL != file && NULL != absolutePath);
file->_order = fileCounter++;
file->_type = type;
file->_handle = handle;
Str_Init(&file->_absolutePath);
Str_Set(&file->_absolutePath, absolutePath);
Str_Strip(&file->_absolutePath);
F_FixSlashes(&file->_absolutePath, &file->_absolutePath);
if(absolutePath)
{
Str_Set(&file->_absolutePath, absolutePath);
Str_Strip(&file->_absolutePath);
F_FixSlashes(&file->_absolutePath, &file->_absolutePath);
}
file->hndl = handle;
file->flags.eof = 0;
file->flags.open = 0;
file->size = 0;
file->data = NULL;
file->pos = 0;
file->lastModified = 0;
}

filetype_t AbstractFile_Type(const abstractfile_t* file)
Expand All @@ -48,10 +57,10 @@ filetype_t AbstractFile_Type(const abstractfile_t* file)
return file->_type;
}

DFILE* AbstractFile_Handle(abstractfile_t* file)
FILE* AbstractFile_Handle(abstractfile_t* file)
{
assert(NULL != file);
return file->_handle;
return file->hndl;
}

const ddstring_t* AbstractFile_AbsolutePath(abstractfile_t* file)
Expand Down

0 comments on commit 3661f85

Please sign in to comment.