Skip to content

Commit

Permalink
Refactor|libdoomsday|DED: Use de::String for source file paths
Browse files Browse the repository at this point in the history
Avoids potentially truncating such paths to FILENAME_T_MAXLEN through
use of ye olde directory_t
  • Loading branch information
danij-deng committed Jun 19, 2014
1 parent 8515929 commit 01cc501
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 77 deletions.
2 changes: 1 addition & 1 deletion doomsday/client/src/def_main.cpp
Expand Up @@ -549,7 +549,7 @@ static void readDefinitionFile(String path)
if(path.isEmpty()) return;

LOG_RES_VERBOSE("Reading \"%s\"") << NativePath(path).pretty();
Def_ReadProcessDED(&defs, path.toUtf8());
Def_ReadProcessDED(&defs, path);
}

/**
Expand Down
16 changes: 9 additions & 7 deletions doomsday/libdoomsday/include/doomsday/defs/dedfile.h
@@ -1,7 +1,8 @@
/** @file defs/dedfile.h Definition files. @ingroup defs
/** @file dedfile.h Definition files.
* @ingroup defs
*
* @authors Copyright © 2003-2013 Jaakko Keränen <jaakko.keranen@iki.fi>
* @authors Copyright © 2006-2013 Daniel Swanson <danij@dengine.net>
* @authors Copyright © 2003-2014 Jaakko Keränen <jaakko.keranen@iki.fi>
* @authors Copyright © 2006-2014 Daniel Swanson <danij@dengine.net>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
Expand All @@ -23,13 +24,14 @@

#include "../libdoomsday.h"
#include "ded.h"
#include <de/String>

LIBDOOMSDAY_PUBLIC void Def_ReadProcessDED(ded_t *defs, char const* path);
LIBDOOMSDAY_PUBLIC void Def_ReadProcessDED(ded_t *defs, de::String path);

/**
* Reads definitions from the given lump.
*/
LIBDOOMSDAY_PUBLIC int DED_ReadLump(ded_t* ded, lumpnum_t lumpNum);
LIBDOOMSDAY_PUBLIC int DED_ReadLump(ded_t *ded, lumpnum_t lumpNum);

/**
* Reads definitions from the given buffer.
Expand All @@ -38,12 +40,12 @@ LIBDOOMSDAY_PUBLIC int DED_ReadLump(ded_t* ded, lumpnum_t lumpNum);
* @param buffer The data to be read, must be null-terminated.
* @param _sourceFile Just FYI.
*/
int DED_ReadData(ded_t* ded, const char* buffer, const char* _sourceFile);
int DED_ReadData(ded_t *ded, char const *buffer, de::String _sourceFile);

/**
* @return @c true, if the file was successfully loaded.
*/
int DED_Read(ded_t* ded, const char* path);
int DED_Read(ded_t *ded, de::String path);

void DED_SetError(char const *str);

Expand Down
5 changes: 3 additions & 2 deletions doomsday/libdoomsday/include/doomsday/defs/dedparser.h
@@ -1,4 +1,5 @@
/** @file defs/dedparser.h DED v1 parser. @ingroup defs
/** @file dedparser.h DED v1 parser.
* @ingroup defs
*
* @authors Copyright © 2003-2014 Jaakko Keränen <jaakko.keranen@iki.fi>
* @authors Copyright © 2006-2014 Daniel Swanson <danij@dengine.net>
Expand Down Expand Up @@ -36,7 +37,7 @@ class LIBDOOMSDAY_PUBLIC DEDParser
public:
DEDParser(ded_t *ded);

int parse(char const *buffer, char const *sourceFile);
int parse(char const *buffer, de::String sourceFile);

private:
DENG2_PRIVATE(d)
Expand Down
40 changes: 13 additions & 27 deletions doomsday/libdoomsday/src/defs/dedfile.cpp
@@ -1,7 +1,7 @@
/** @file defs/dedfile.cpp
/** @file dedfile.cpp
*
* @authors Copyright © 2003-2013 Jaakko Keränen <jaakko.keranen@iki.fi>
* @authors Copyright © 2005-2013 Daniel Swanson <danij@dengine.net>
* @authors Copyright © 2003-2014 Jaakko Keränen <jaakko.keranen@iki.fi>
* @authors Copyright © 2005-2014 Daniel Swanson <danij@dengine.net>
* @authors Copyright © 2006 Jamie Jones <jamie_jones_au@yahoo.com.au>
*
* @par License
Expand Down Expand Up @@ -33,11 +33,11 @@ void DED_SetError(char const *str)
strncpy(dedReadError, str, sizeof(dedReadError));
}

void Def_ReadProcessDED(ded_t *defs, char const* path)
void Def_ReadProcessDED(ded_t *defs, String path)
{
LOG_AS("Def_ReadProcessDED");

if(!path || !path[0]) return;
if(path.isEmpty()) return;

de::Uri const uri(path, RC_NULL);
if(!App_FileSystem().accessFile(uri))
Expand Down Expand Up @@ -69,7 +69,7 @@ int DED_ReadLump(ded_t *ded, lumpnum_t lumpNum)
{
uint8_t const *data = lump.cache();
String sourcePath = lump.container().composePath();
DED_ReadData(ded, (char const *)data, sourcePath.toUtf8().constData());
DED_ReadData(ded, (char const *)data, sourcePath);
lump.unlock();
}
return true;
Expand All @@ -80,53 +80,39 @@ int DED_ReadLump(ded_t *ded, lumpnum_t lumpNum)
return false;
}

int DED_Read(ded_t *ded, char const *path)
int DED_Read(ded_t *ded, String path)
{
// Compose the (possibly-translated) path.
ddstring_t transPath; Str_InitStd(&transPath);
Str_Set(&transPath, path);
F_FixSlashes(&transPath, &transPath);
F_ExpandBasePath(&transPath, &transPath);

// Attempt to open a definition file on this path.
try
{
// Relative paths are relative to the native working directory.
String path = (NativePath::workPath() / NativePath(Str_Text(&transPath)).expand()).withSeparators('/');
QScopedPointer<FileHandle> hndl(&App_FileSystem().openFile(path, "rb"));
String fullPath = (NativePath::workPath() / NativePath(path).expand()).withSeparators('/');
QScopedPointer<FileHandle> hndl(&App_FileSystem().openFile(fullPath, "rb"));

// We will buffer a local copy of the file. How large a buffer do we need?
hndl->seek(0, SeekEnd);
size_t bufferedDefSize = hndl->tell();
hndl->rewind();
char *bufferedDef = (char *) calloc(1, bufferedDefSize + 1);
if(!bufferedDef)
{
DED_SetError("Out of memory while trying to buffer file for reading.");
Str_Free(&transPath);
return false;
}
char *bufferedDef = (char *) M_Calloc(bufferedDefSize + 1);

// Copy the file into the local buffer and parse definitions.
hndl->read((uint8_t *)bufferedDef, bufferedDefSize);
App_FileSystem().releaseFile(hndl->file());

int result = DED_ReadData(ded, bufferedDef, Str_Text(&transPath));
int result = DED_ReadData(ded, bufferedDef, path);

// Done. Release temporary storage and return the result.
free(bufferedDef);
Str_Free(&transPath);
M_Free(bufferedDef);
return result;
}
catch(FS1::NotFoundError const &)
{} // Ignore.

DED_SetError("File could not be opened for reading.");
Str_Free(&transPath);
return false;
}

int DED_ReadData(ded_t* ded, const char* buffer, const char* _sourceFile)
int DED_ReadData(ded_t *ded, char const *buffer, String _sourceFile)
{
return DEDParser(ded).parse(buffer, _sourceFile);
}
70 changes: 30 additions & 40 deletions doomsday/libdoomsday/src/defs/dedparser.cpp
Expand Up @@ -141,30 +141,29 @@ DENG2_PIMPL(DEDParser)

struct dedsource_s
{
const char* buffer;
const char* pos;
dd_bool atEnd;
int lineNumber;
const char* fileName;
int version; // v6 does not require semicolons.
char const *buffer;
char const *pos;
dd_bool atEnd;
int lineNumber;
String fileName;
int version; // v6 does not require semicolons.
};

typedef dedsource_s dedsource_t;

dedsource_t sourceStack[MAX_RECUR_DEPTH];
dedsource_t* source; // Points to the current source.
dedsource_t *source; // Points to the current source.

char token[MAX_TOKEN_LEN+1];
char unreadToken[MAX_TOKEN_LEN+1];

Instance(Public *i) : Base(i), ded(0), source(0)
{
zap(sourceStack);
zap(token);
zap(unreadToken);
}

void DED_InitReader(const char* buffer, const char* fileName)
void DED_InitReader(char const *buffer, String fileName)
{
if(source && source - sourceStack >= MAX_RECUR_DEPTH)
{
Expand All @@ -183,21 +182,21 @@ DENG2_PIMPL(DEDParser)
}

source->pos = source->buffer = buffer;
source->atEnd = false;

source->atEnd = false;
source->lineNumber = 1;
source->fileName = fileName;
source->version = DED_VERSION;
source->fileName = fileName;
source->version = DED_VERSION;
}

void DED_CloseReader(void)
void DED_CloseReader()
{
if(source == sourceStack)
{
source = NULL;
source = 0;
}
else
{
memset(source, 0, sizeof(*source));
source--;
}
}
Expand Down Expand Up @@ -752,7 +751,7 @@ DENG2_PIMPL(DEDParser)
return value == expected;
}

int readData(const char* buffer, const char* _sourceFile)
int readData(char const *buffer, String sourceFile)
{
char dummy[128], label[128], tmp[256];
int dummyInt, idx, retVal = true;
Expand All @@ -772,24 +771,17 @@ DENG2_PIMPL(DEDParser)
int depth;
char *rootStr = 0, *ptr;
int bCopyNext = 0;
::Str sourceFile, sourceFileDir;

Str_Init(&sourceFile); Str_Set(&sourceFile, _sourceFile);
F_FixSlashes(&sourceFile, &sourceFile);
F_ExpandBasePath(&sourceFile, &sourceFile);
// Get the next entry from the source stack.
DED_InitReader(buffer, sourceFile);

// For including other files -- we must know where we are.
Str_Init(&sourceFileDir);
String sourceFileDir = sourceFile.fileNamePath();
if(sourceFileDir.isEmpty())
{
/// @todo Potentially truncates @a src to FILENAME_T_MAXLEN
directory_t *dir = Dir_FromText(Str_Text(&sourceFile));
Str_Set(&sourceFileDir, Dir_Path(dir));
Dir_Delete(dir);
sourceFileDir = NativePath::workPath();
}

// Get the next entry from the source stack.
DED_InitReader(buffer, Str_Text(&sourceFile));

while(ReadToken())
{
if(ISTOKEN("Copy") || ISTOKEN("*"))
Expand Down Expand Up @@ -829,7 +821,7 @@ DENG2_PIMPL(DEDParser)
READSTR(tmp);
CHECKSC;

DED_Include(tmp, Str_Text(&sourceFileDir));
DED_Include(tmp, sourceFileDir);
strcpy(label, "");
}

Expand All @@ -849,7 +841,7 @@ DENG2_PIMPL(DEDParser)
READSTR(tmp);
CHECKSC;

DED_Include(tmp, Str_Text(&sourceFileDir));
DED_Include(tmp, sourceFileDir);
strcpy(label, "");
}
else
Expand Down Expand Up @@ -2597,25 +2589,23 @@ DENG2_PIMPL(DEDParser)
// Free the source stack entry we were using.
DED_CloseReader();

Str_Free(&sourceFile);
Str_Free(&sourceFileDir);

return retVal;
}

void DED_Include(const char* fileName, const char* parentDirectory)
void DED_Include(char const *fileName, String parentDirectory)
{
ddstring_t tmp;

Str_Init(&tmp); Str_Set(&tmp, fileName);
Str_InitStd(&tmp); Str_Set(&tmp, fileName);
F_FixSlashes(&tmp, &tmp);
F_ExpandBasePath(&tmp, &tmp);
if(!F_IsAbsolute(&tmp))
{
Str_Prepend(&tmp, parentDirectory);
Str_PrependChar(&tmp, '/');
Str_Prepend(&tmp, parentDirectory.toUtf8().constData());
}

Def_ReadProcessDED(ded, Str_Text(&tmp));
Def_ReadProcessDED(ded, String(Str_Text(&tmp)));
Str_Free(&tmp);

// Reset state for continuing.
Expand All @@ -2628,14 +2618,14 @@ DENG2_PIMPL(DEDParser)
if(more)
{
sprintf(dedReadError, "Error in %s:\n Line %i: %s (%s)",
source? source->fileName : "?",
source? source->fileName.toUtf8().constData() : "?",
source? source->lineNumber : 0,
str, more);
}
else
{
sprintf(dedReadError, "Error in %s:\n Line %i: %s",
source? source->fileName : "?",
source? source->fileName.toUtf8().constData() : "?",
source? source->lineNumber : 0,
str);
}
Expand All @@ -2647,7 +2637,7 @@ DEDParser::DEDParser(ded_t *ded) : d(new Instance(this))
d->ded = ded;
}

int DEDParser::parse(const char *buffer, const char *sourceFile)
int DEDParser::parse(char const *buffer, String sourceFile)
{
return d->readData(buffer, sourceFile);
}

0 comments on commit 01cc501

Please sign in to comment.