Skip to content

Commit

Permalink
Fixed|Win32: NativePath cleanup
Browse files Browse the repository at this point in the history
de::NativePath will now automatically resolve any '..' and '.' references
in native paths. de::CommandLine now uses NativePath when converting arguments
to absolute paths, as it is assumed that CommandLine::makeAbsolutePath() is
only called when expecting a native path.
  • Loading branch information
skyjake committed Nov 8, 2012
1 parent 0bd65f2 commit a8e59be
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
2 changes: 1 addition & 1 deletion doomsday/libdeng2/include/de/core/commandline.h
Expand Up @@ -146,7 +146,7 @@ namespace de
const char* const* argv() const;

/**
* Converts the argument at position @a pos into an absolute path.
* Converts the argument at position @a pos into an absolute native path.
* Relative paths are converted relative to the directory that was
* current at the time the CommandLine was created.
*
Expand Down
11 changes: 10 additions & 1 deletion doomsday/libdeng2/src/core/commandline.cpp
Expand Up @@ -19,6 +19,7 @@

#include "de/CommandLine"
#include "de/String"
#include "de/NativePath"
#include "de/Log"

#include <QFile>
Expand Down Expand Up @@ -274,19 +275,22 @@ void CommandLine::makeAbsolutePath(duint pos)

if(!isOption(pos) && !arg.startsWith("}"))
{
bool converted = false;
QDir dir(arg); // note: strips trailing slash

/// @todo The path expansion logic here should match the native shell's behavior.
#ifdef UNIX
if(dir.path().startsWith("~/"))
{
dir.setPath(QDir::home().filePath(dir.path().mid(2)));
converted = true;
}
else
#endif
if(!QDir::isAbsolutePath(arg))
{
dir.setPath(d->initialDir.filePath(dir.path()));
converted = true;
}

// Update the argument string.
Expand All @@ -299,11 +303,16 @@ void CommandLine::makeAbsolutePath(duint pos)
d->arguments[pos] += '/';
}

d->arguments[pos] = NativePath(d->arguments[pos]);

// Replace the pointer string.
free(d->pointers[pos]);
d->pointers[pos] = duplicateStringAsUtf8(d->arguments[pos]);

LOG_DEBUG("Argument %i converted to absolute path: \"%s\"") << pos << d->pointers[pos];
if(converted)
{
LOG_DEBUG("Argument %i converted to absolute path: \"%s\"") << pos << d->pointers[pos];
}
}
}

Expand Down
15 changes: 11 additions & 4 deletions doomsday/libdeng2/src/filesys/nativepath.cpp
Expand Up @@ -36,20 +36,27 @@

namespace de {

static QString toNative(const QString& s)
{
// This will resolve parent references (".."), multiple separators
// (hello//world), and self-references (".").
return QDir::toNativeSeparators(QDir::cleanPath(s));
}

NativePath::NativePath()
{}

NativePath::NativePath(const QString &str) : String(QDir::toNativeSeparators(str))
NativePath::NativePath(const QString &str) : String(toNative(str))
{}

NativePath::NativePath(const char *nullTerminatedCStr)
: String(QDir::toNativeSeparators(nullTerminatedCStr))
: String(toNative(nullTerminatedCStr))
{
}

NativePath& NativePath::operator = (const QString& str)
{
*static_cast<String*>(this) = QDir::toNativeSeparators(str);
*static_cast<String*>(this) = toNative(str);
return *this;
}

Expand All @@ -60,7 +67,7 @@ NativePath& NativePath::operator = (const char *nullTerminatedCStr)

NativePath NativePath::concatenatePath(const NativePath& nativePath) const
{
return String::concatenatePath(nativePath, DIR_SEPARATOR);
return String::concatenatePath(nativePath, QChar(DIR_SEPARATOR));
}

NativePath NativePath::concatenatePath(const QString& nativePath) const
Expand Down

0 comments on commit a8e59be

Please sign in to comment.