diff --git a/doomsday/client/src/def_main.cpp b/doomsday/client/src/def_main.cpp index 57e4b8f505..0fbdc478e9 100644 --- a/doomsday/client/src/def_main.cpp +++ b/doomsday/client/src/def_main.cpp @@ -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); } /** diff --git a/doomsday/libdoomsday/include/doomsday/defs/dedfile.h b/doomsday/libdoomsday/include/doomsday/defs/dedfile.h index 5032bfebd6..5911b65a8a 100644 --- a/doomsday/libdoomsday/include/doomsday/defs/dedfile.h +++ b/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 - * @authors Copyright © 2006-2013 Daniel Swanson + * @authors Copyright © 2003-2014 Jaakko Keränen + * @authors Copyright © 2006-2014 Daniel Swanson * * @par License * GPL: http://www.gnu.org/licenses/gpl.html @@ -23,13 +24,14 @@ #include "../libdoomsday.h" #include "ded.h" +#include -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. @@ -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); diff --git a/doomsday/libdoomsday/include/doomsday/defs/dedparser.h b/doomsday/libdoomsday/include/doomsday/defs/dedparser.h index b161f83dbd..a619ab3e8d 100644 --- a/doomsday/libdoomsday/include/doomsday/defs/dedparser.h +++ b/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 * @authors Copyright © 2006-2014 Daniel Swanson @@ -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) diff --git a/doomsday/libdoomsday/src/defs/dedfile.cpp b/doomsday/libdoomsday/src/defs/dedfile.cpp index 302c4e3776..9bdecc079c 100644 --- a/doomsday/libdoomsday/src/defs/dedfile.cpp +++ b/doomsday/libdoomsday/src/defs/dedfile.cpp @@ -1,7 +1,7 @@ -/** @file defs/dedfile.cpp +/** @file dedfile.cpp * - * @authors Copyright © 2003-2013 Jaakko Keränen - * @authors Copyright © 2005-2013 Daniel Swanson + * @authors Copyright © 2003-2014 Jaakko Keränen + * @authors Copyright © 2005-2014 Daniel Swanson * @authors Copyright © 2006 Jamie Jones * * @par License @@ -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)) @@ -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; @@ -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 hndl(&App_FileSystem().openFile(path, "rb")); + String fullPath = (NativePath::workPath() / NativePath(path).expand()).withSeparators('/'); + QScopedPointer 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); } diff --git a/doomsday/libdoomsday/src/defs/dedparser.cpp b/doomsday/libdoomsday/src/defs/dedparser.cpp index 03d8db883d..389bbdba99 100644 --- a/doomsday/libdoomsday/src/defs/dedparser.cpp +++ b/doomsday/libdoomsday/src/defs/dedparser.cpp @@ -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) { @@ -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--; } } @@ -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; @@ -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("*")) @@ -829,7 +821,7 @@ DENG2_PIMPL(DEDParser) READSTR(tmp); CHECKSC; - DED_Include(tmp, Str_Text(&sourceFileDir)); + DED_Include(tmp, sourceFileDir); strcpy(label, ""); } @@ -849,7 +841,7 @@ DENG2_PIMPL(DEDParser) READSTR(tmp); CHECKSC; - DED_Include(tmp, Str_Text(&sourceFileDir)); + DED_Include(tmp, sourceFileDir); strcpy(label, ""); } else @@ -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. @@ -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); } @@ -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); }