Skip to content

Commit

Permalink
Refactor: Added more convenient way to create AutoStrs from text
Browse files Browse the repository at this point in the history
The function AutoStr_FromTextStd() addresses a recurring pattern of
"Str_Set(AutoStr_NewStd(), …)".
  • Loading branch information
skyjake committed Oct 16, 2012
1 parent f9edf2b commit eca92cf
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 12 deletions.
2 changes: 1 addition & 1 deletion doomsday/engine/portable/src/audiodriver.c
Expand Up @@ -601,7 +601,7 @@ AutoStr* AudioDriver_InterfaceName(void* anyAudioInterface)
audiointerface_music_generic_t* gen = anyAudioInterface;
if(gen->Get(MUSIP_ID, buf))
{
return Str_Set(AutoStr_NewStd(), buf);
return AutoStr_FromTextStd(buf);
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion doomsday/engine/portable/src/fileid.cpp
Expand Up @@ -79,7 +79,7 @@ FileId FileId::fromPath(char const* path)
FileId::Md5Hash FileId::hash(char const* path)
{
// First normalize the name.
AutoStr* absPath = Str_Set(AutoStr_NewStd(), path);
AutoStr* absPath = AutoStr_FromTextStd(path);
F_MakeAbsolute(absPath, absPath);
F_FixSlashes(absPath, absPath);
#if defined(WIN32) || defined(MACOSX)
Expand Down
14 changes: 7 additions & 7 deletions doomsday/engine/portable/src/fs_main.cpp
Expand Up @@ -282,7 +282,7 @@ static FS1::FileList::iterator findListFileByPath(FS1::FileList& list, char cons
if(!path_ || !path_[0]) return list.end();

// Transform the path into one we can process.
AutoStr* path = Str_Set(AutoStr_NewStd(), path_);
AutoStr* path = AutoStr_FromTextStd(path_);
F_FixSlashes(path, path);

// Perform the search.
Expand Down Expand Up @@ -1297,7 +1297,7 @@ void FS1::mapPathToLump(char const* symbolicPath, char const* lumpName)
if(!symbolicPath || !symbolicPath[0] || !lumpName || !lumpName[0]) return;

// Convert the symbolic path into a real path.
AutoStr* path = Str_Set(AutoStr_NewStd(), symbolicPath);
AutoStr* path = AutoStr_FromTextStd(symbolicPath);
F_ResolveSymbolicPath(path, path);

// Since the path might be relative, let's explicitly make the path absolute.
Expand Down Expand Up @@ -1461,12 +1461,12 @@ static bool applyPathMapping(ddstring_t* path, PathMapping const& pm)
{
if(!path) return false;
QByteArray destUtf8 = pm.first.toUtf8();
AutoStr* dest = Str_Set(AutoStr_NewStd(), destUtf8.constData());
AutoStr* dest = AutoStr_FromTextStd(destUtf8.constData());
if(qstrnicmp(Str_Text(path), Str_Text(dest), Str_Length(dest))) return false;

// Replace the beginning with the source path.
QByteArray sourceUtf8 = pm.second.toUtf8();
AutoStr* temp = Str_Set(AutoStr_NewStd(), sourceUtf8.constData());
AutoStr* temp = AutoStr_FromTextStd(sourceUtf8.constData());
Str_PartAppend(temp, Str_Text(path), pm.first.length(), Str_Length(path) - pm.first.length());
Str_Copy(path, temp);
return true;
Expand All @@ -1476,14 +1476,14 @@ void FS1::mapPath(char const* source, char const* destination)
{
if(!source || !source[0] || !destination || !destination[0]) return;

AutoStr* dest = Str_Set(AutoStr_NewStd(), destination);
AutoStr* dest = AutoStr_FromTextStd(destination);
Str_Strip(dest);
F_FixSlashes(dest, dest);
F_ExpandBasePath(dest, dest);
F_PrependWorkPath(dest, dest);
F_AppendMissingSlash(dest);

AutoStr* src = Str_Set(AutoStr_NewStd(), source);
AutoStr* src = AutoStr_FromTextStd(source);
Str_Strip(src);
F_FixSlashes(src, src);
F_ExpandBasePath(src, src);
Expand Down Expand Up @@ -1542,7 +1542,7 @@ void FS1::initPathMap()

void FS1::printDirectory(ddstring_t const* path)
{
AutoStr* searchPattern = Str_Set(AutoStr_NewStd(), Str_Text(path));
AutoStr* searchPattern = AutoStr_FromTextStd(Str_Text(path));

Str_Strip(searchPattern);
F_FixSlashes(searchPattern, searchPattern);
Expand Down
2 changes: 1 addition & 1 deletion doomsday/engine/portable/src/image.cpp
Expand Up @@ -195,7 +195,7 @@ boolean Image_Save(const image_t* img, const char* filePath)
DENG_ASSERT(img);

// Compose the full path.
AutoStr* fullPath = Str_Set(AutoStr_NewStd(), filePath);
AutoStr* fullPath = AutoStr_FromTextStd(filePath);
if(Str_IsEmpty(fullPath))
{
static int n = 0;
Expand Down
2 changes: 1 addition & 1 deletion doomsday/engine/portable/src/resource/materials.cpp
Expand Up @@ -1511,7 +1511,7 @@ static void printMaterialOverview(material_t* mat, boolean printNamespace)
{
int numUidDigits = MAX_OF(3/*uid*/, M_NumDigits(Materials_Size()));
Uri* uri = Materials_ComposeUri(Materials_Id(mat));
AutoStr* path = (printNamespace? Uri_ToString(uri) : Str_PercentDecode(Str_Set(AutoStr_NewStd(), Str_Text(Uri_Path(uri)))));
AutoStr* path = (printNamespace? Uri_ToString(uri) : Str_PercentDecode(AutoStr_FromTextStd(Str_Text(Uri_Path(uri)))));

Con_Printf("%-*s %*u %s\n", printNamespace? 22 : 14, F_PrettyPath(Str_Text(path)),
numUidDigits, Materials_Id(mat),
Expand Down
2 changes: 1 addition & 1 deletion doomsday/engine/portable/src/resource/textures.cpp
Expand Up @@ -1220,7 +1220,7 @@ static void printTextureOverview(TextureDirectoryNode* node, boolean printNamesp
textureid_t texId = findBindIdForDirectoryNode(node);
int numUidDigits = MAX_OF(3/*uid*/, M_NumDigits(Textures_Size()));
Uri* uri = record->texture? Textures_ComposeUri(texId) : Uri_New();
AutoStr* path = printNamespace? Uri_ToString(uri) : Str_PercentDecode(Str_Set(AutoStr_NewStd(), Str_Text(Uri_Path(uri))));
AutoStr* path = printNamespace? Uri_ToString(uri) : Str_PercentDecode(AutoStr_FromTextStd(Str_Text(Uri_Path(uri))));
AutoStr* resourcePath = Uri_ToString(Textures_ResourcePath(texId));

Con_FPrintf(!record->texture? CPF_LIGHT : CPF_WHITE,
Expand Down
10 changes: 10 additions & 0 deletions doomsday/libdeng/include/de/str.h
Expand Up @@ -415,6 +415,16 @@ DENG_PUBLIC AutoStr* AutoStr_FromStr(Str* str);
*/
DENG_PUBLIC AutoStr* AutoStr_FromText(const char* text);

/**
* Constructs an AutoStr instance (standard malloc) and initializes its
* contents with @a text.
*
* @param text Text for the new string.
*
* @return AutoStr instance.
*/
DENG_PUBLIC AutoStr* AutoStr_FromTextStd(const char* text);

/**
* Converts an AutoStr to a normal ddstring. You must call Str_Delete()
* on the returned string manually to destroy it.
Expand Down
5 changes: 5 additions & 0 deletions doomsday/libdeng/src/str.c
Expand Up @@ -837,6 +837,11 @@ AutoStr* AutoStr_FromText(const char* text)
return Str_Set(AutoStr_New(), text);
}

AutoStr* AutoStr_FromTextStd(const char *text)
{
return Str_Set(AutoStr_NewStd(), text);
}

ddstring_t* Str_FromAutoStr(AutoStr* as)
{
DENG_ASSERT(as);
Expand Down

0 comments on commit eca92cf

Please sign in to comment.