Skip to content

Commit

Permalink
Refactor|FileSys: Rewired mechanism for composing file paths
Browse files Browse the repository at this point in the history
de::File1 no longer defines a virtual composeLumpPath(lumpIdx)
method at file container level. This functionality has been merged
with the path() method at contained file level and is now named
composePath().

Todo: The absolute, fully-composed path stored in the path_ member
of de::File1 is now largely redundant (only used by files with no
container).
  • Loading branch information
danij-deng committed Oct 22, 2012
1 parent fb7ab2d commit e6d2de9
Show file tree
Hide file tree
Showing 18 changed files with 155 additions and 153 deletions.
23 changes: 8 additions & 15 deletions doomsday/engine/portable/include/file.h
Expand Up @@ -84,8 +84,14 @@ class File1
/// @return Name of this file.
virtual ddstring_t const* name() const;

/// @return Absolute (i.e., resolved but possibly virtual/mapped) path to this file.
ddstring_t const* path() const;
/**
* Compose the absolute VFS path to this file.
*
* @param delimiter Delimit directory using this character.
*
* @return String containing the absolute path.
*/
virtual AutoStr* composePath(char delimiter = '/') const;

/// @return @c true iff this file is contained by another.
bool isContained() const;
Expand Down Expand Up @@ -176,19 +182,6 @@ class File1
*/
virtual int lumpCount() const { return 1; }

/**
* Compose the absolute VFS path to a lump contained by this file.
*
* @note Always returns a valid string object. If @a lumpIdx is not valid a
* zero-length string is returned.
*
* @param lumpIdx Logical index for the lump.
* @param delimiter Delimit directory separators using this character.
*
* @return String containing the absolute path.
*/
virtual AutoStr* composeLumpPath(int lumpIdx, char delimiter = '/');

/**
* Retrieve a lump contained by this file.
*
Expand Down
4 changes: 2 additions & 2 deletions doomsday/engine/portable/include/fs_main.h
Expand Up @@ -420,7 +420,7 @@ boolean F_IsValidLumpNum(lumpnum_t absoluteLumpNum);

lumpnum_t F_LumpNumForName(char const* name);

ddstring_t const* F_LumpFilePath(lumpnum_t absoluteLumpNum);
AutoStr* F_ComposeLumpFilePath(lumpnum_t absoluteLumpNum);

boolean F_LumpIsCustom(lumpnum_t absoluteLumpNum);

Expand All @@ -437,7 +437,7 @@ void F_Close(struct filehandle_s* file);

void F_Delete(struct filehandle_s* file);

Str const* F_Path(struct file1_s const* file);
AutoStr* F_ComposePath(struct file1_s const* file);

void F_SetCustom(struct file1_s* file, boolean yes);

Expand Down
33 changes: 20 additions & 13 deletions doomsday/engine/portable/include/lumpfileadaptor.h
Expand Up @@ -67,6 +67,26 @@ class LumpFileAdaptor : public File1
throw de::Error("LumpFileAdaptor::name", "Unknown de::File1 type");
}

/**
* Compose the absolute VFS path for this file.
*
* @param delimiter Delimit directory using this character.
*
* @return String containing the absolute path.
*/
AutoStr* composePath(char delimiter = '/')
{
if(Wad* wad = dynamic_cast<Wad*>(&container()))
{
return wad->lump(info().lumpIdx).composePath(delimiter);
}
if(Zip* zip = dynamic_cast<Zip*>(&container()))
{
return zip->lump(info().lumpIdx).composePath(delimiter);
}
throw de::Error("LumpFileAdaptor::composePath", "Unknown de::File1 type");
}

/**
* Retrieve the directory node for this file.
*
Expand All @@ -85,19 +105,6 @@ class LumpFileAdaptor : public File1
throw de::Error("LumpFileAdaptor::directoryNode", "Unknown de::File1 type");
}

/**
* Compose the absolute VFS path to a lump contained by this file.
*
* @note Always returns a valid string object. If @a lumpIdx is not valid a
* zero-length string is returned.
*
* @param lumpIdx Logical index for the lump.
* @param delimiter Delimit directory separators using this character.
*
* @return String containing the absolute path.
*/
AutoStr* composeLumpPath(int lumpIdx, char delimiter = '/');

/**
* Read the data associated with lump @a lumpIdx into @a buffer.
*
Expand Down
2 changes: 1 addition & 1 deletion doomsday/engine/portable/src/dam_main.c
Expand Up @@ -256,7 +256,7 @@ boolean DAM_AttemptMapLoad(const Uri* uri)
if(0 > markerLump) return false;

// Compose the cache directory path and ensure it exists.
cachedMapDir = DAM_ComposeCacheDir(Str_Text(F_LumpFilePath(markerLump)));
cachedMapDir = DAM_ComposeCacheDir(Str_Text(F_ComposeLumpFilePath(markerLump)));
F_MakePath(Str_Text(cachedMapDir));

// Compose the full path to the cached map data file.
Expand Down
2 changes: 1 addition & 1 deletion doomsday/engine/portable/src/dd_wad.cpp
Expand Up @@ -87,7 +87,7 @@ char const* W_LumpSourceFile(lumpnum_t absoluteLumpNum)
{
lumpnum_t lumpNum = absoluteLumpNum;
de::File1 const& lump = App_FileSystem()->nameIndexForLump(lumpNum).lump(lumpNum);
return Str_Text(lump.container().path());
return Str_Text(lump.container().composePath());
}
catch(LumpIndex::NotFoundError const&)
{
Expand Down
2 changes: 1 addition & 1 deletion doomsday/engine/portable/src/def_main.cpp
Expand Up @@ -778,7 +778,7 @@ void Def_ReadLumpDefs(void)

if(!DED_ReadLump(&defs, lump.info().lumpIdx))
{
Con_Error("DD_ReadLumpDefs: Parse error when reading \"%s:DD_DEFNS\".\n", Str_Text(lump.container().path()));
Con_Error("DD_ReadLumpDefs: Parse error when reading \"%s:DD_DEFNS\".\n", Str_Text(lump.container().composePath()));
}
}

Expand Down
2 changes: 1 addition & 1 deletion doomsday/engine/portable/src/def_read.c
Expand Up @@ -2741,7 +2741,7 @@ int DED_ReadLump(ded_t* ded, lumpnum_t absoluteLumpNum)
if(F_LumpLength(absoluteLumpNum) != 0)
{
uint8_t const* lumpPtr = F_CacheLump(file, lumpIdx);
DED_ReadData(ded, (char const*)lumpPtr, Str_Text(F_Path(file)));
DED_ReadData(ded, (char const*)lumpPtr, Str_Text(F_ComposePath(file)));
F_UnlockLump(file, lumpIdx);
}
return true;
Expand Down
2 changes: 1 addition & 1 deletion doomsday/engine/portable/src/edit_map.c
Expand Up @@ -1724,7 +1724,7 @@ boolean MPE_End(void)
{
// Yes, write the cached map data file.
lumpnum_t markerLumpNum = F_LumpNumForName(Str_Text(Uri_Path(gamemap->uri)));
AutoStr* cachedMapDir = DAM_ComposeCacheDir(Str_Text(F_LumpFilePath(markerLumpNum)));
AutoStr* cachedMapDir = DAM_ComposeCacheDir(Str_Text(F_ComposeLumpFilePath(markerLumpNum)));
Str cachedMapPath;

Str_InitStd(&cachedMapPath);
Expand Down
14 changes: 6 additions & 8 deletions doomsday/engine/portable/src/file.cpp
Expand Up @@ -61,7 +61,7 @@ bool File1::isContained() const

File1& File1::container() const
{
if(!container_) throw NotContainedError("File1::container", QString("%s is not contained").arg(Str_Text(path())));
if(!container_) throw NotContainedError("File1::container", QString("%s is not contained").arg(Str_Text(composePath())));
return *container_;
}

Expand All @@ -70,9 +70,12 @@ de::FileHandle& File1::handle()
return *handle_;
}

ddstring_t const* File1::path() const
AutoStr* File1::composePath(char delimiter) const
{
return &path_;
AutoStr* path = Str_Copy(AutoStr_NewStd(), &path_);

This comment has been minimized.

Copy link
@skyjake

skyjake Oct 22, 2012

Owner

I hope this doesn't cause a massive number of AutoStrs to be allocated... Note that it is ok to return a Str* as AutoStr* if ownership is not relinquished to the caller.

This comment has been minimized.

Copy link
@danij-deng

danij-deng Oct 22, 2012

Author Collaborator

Ownership of the composed string is indeed given to the caller. However, this is temporary as soon all files will have an owning directory/feed (which will supply the path). Also, the only times when a fully composed path is required is while locating a file in the local native file system (and when compiling the main file index, though this too is temporary as there should be no need to compose absolute paths for this).

if(delimiter != '/')
throw de::Error("File1::composePath", "Non '/' delimiter not yet implemented");
return path;
}

uint File1::loadOrderIndex() const
Expand Down Expand Up @@ -113,11 +116,6 @@ ddstring_t const* File1::name() const
return name_;
}

AutoStr* File1::composeLumpPath(int /*lumpIdx*/, char /*delimiter*/)
{
return AutoStr_NewStd();
}

size_t File1::readLump(int /*lumpIdx*/, uint8_t* /*buffer*/, bool /*tryCache*/)
{
/// @todo writeme
Expand Down
51 changes: 25 additions & 26 deletions doomsday/engine/portable/src/filehandle.cpp
Expand Up @@ -125,47 +125,46 @@ FileHandle* FileHandleBuilder::fromFileLump(File1& container, int lumpIdx, bool
{
if(!container.isValidIndex(lumpIdx)) return 0;

de::FileHandle* file = new de::FileHandle();
de::FileHandle* hndl = new de::FileHandle();
// Init and load in the lump data.
file->d->file = &container.lump(lumpIdx);
file->d->flags.open = true;
File1& file = container.lump(lumpIdx);
hndl->d->file = &file;
hndl->d->flags.open = true;
if(!dontBuffer)
{
FileInfo const& lumpInfo = container.lump(lumpIdx).info();
file->d->size = lumpInfo.size;
file->d->pos = file->d->data = (uint8_t*) M_Malloc(file->d->size);
if(!file->d->data)
hndl->d->size = file.size();
hndl->d->pos = hndl->d->data = (uint8_t*) M_Malloc(hndl->d->size);
if(!hndl->d->data)
Con_Error("FileHandleBuilder::fromFileLump: Failed on allocation of %lu bytes for data buffer.",
(unsigned long) file->d->size);
(unsigned long) hndl->d->size);
#if _DEBUG
VERBOSE2(
AutoStr* path = container.composeLumpPath(lumpIdx);
Con_Printf("FileHandle [%p] buffering \"%s:%s\"...\n", (void*)file,
F_PrettyPath(Str_Text(container.path())),
F_PrettyPath(Str_Text(path)));
Con_Printf("FileHandle [%p] buffering \"%s:%s\"...\n", (void*)hndl,
F_PrettyPath(Str_Text(container.composePath())),
F_PrettyPath(Str_Text(file.composePath())));
)
#endif
container.readLump(lumpIdx, (uint8_t*)file->d->data, 0, lumpInfo.size);
container.readLump(lumpIdx, (uint8_t*)hndl->d->data, 0, file.size());
}
return file;
return hndl;
}

FileHandle* FileHandleBuilder::fromFile(File1& af)
FileHandle* FileHandleBuilder::fromFile(File1& file)
{
de::FileHandle* file = new de::FileHandle();
file->d->file = &af;
file->d->flags.open = true;
file->d->flags.reference = true;
return file;
de::FileHandle* hndl = new de::FileHandle();
hndl->d->file = &file;
hndl->d->flags.open = true;
hndl->d->flags.reference = true;
return hndl;
}

FileHandle* FileHandleBuilder::fromNativeFile(FILE& hndl, size_t baseOffset)
FileHandle* FileHandleBuilder::fromNativeFile(FILE& file, size_t baseOffset)
{
de::FileHandle* file = new de::FileHandle();
file->d->flags.open = true;
file->d->hndl = &hndl;
file->d->baseOffset = baseOffset;
return file;
de::FileHandle* hndl = new de::FileHandle();
hndl->d->flags.open = true;
hndl->d->hndl = &file;
hndl->d->baseOffset = baseOffset;
return hndl;
}

FileHandle* FileHandleBuilder::dup(de::FileHandle const& hndl)
Expand Down

0 comments on commit e6d2de9

Please sign in to comment.