Skip to content

Commit

Permalink
Refactor|libdeng2: NativePath is derived from Path
Browse files Browse the repository at this point in the history
NativePath is now a special Path that takes care of native FS related
path processing (e.g., expanding ~) and enforces native directory
separators.
  • Loading branch information
skyjake committed Nov 24, 2012
1 parent d7eedf7 commit ab5fbff
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 38 deletions.
24 changes: 12 additions & 12 deletions doomsday/libdeng2/include/de/filesys/nativepath.h
Expand Up @@ -23,25 +23,25 @@
#ifndef LIBDENG2_NATIVEPATH_H
#define LIBDENG2_NATIVEPATH_H

#include "../String"
#include "../Path"

namespace de {

/**
* Specialized String that is used for manipulating paths of the native file
* system. Always uses the directory separator characters appropriate for the
* native file system: any directory separators present in the strings are
* automatically converted to the native ones.
* Manipulates paths of the native file system. Always uses the directory
* separator characters appropriate for the native file system: any directory
* separators present in the strings are automatically converted to the native
* ones.
*
* Note that some of the methods of String are overridden, for instance
* String::fileNamePath(), so that they operate using native separator
* characters.
* The public interface of NativePath closely mirrors that of String, e.g.,
* String::fileNamePath(), so that equivalent operations are provided except
* with native separator characters.
*/
class DENG2_PUBLIC NativePath : public String
class DENG2_PUBLIC NativePath : public Path
{
public:
/// An unknown user name was encounterd in the string. @ingroup errors
DENG2_SUB_ERROR(Error, UnknownUserError);
DENG2_ERROR(UnknownUserError);

public:
/**
Expand All @@ -58,7 +58,7 @@ class DENG2_PUBLIC NativePath : public String
NativePath(const QString& str);

NativePath(const char* nullTerminatedCStr);
NativePath(const char* cStr, size_type length);
NativePath(const char* cStr, dsize length);

/**
* Assignment.
Expand Down Expand Up @@ -127,7 +127,7 @@ class DENG2_PUBLIC NativePath : public String
* from absolute to relative, the result should only be used for paths
* appearing in messages intended for the user.
*/
NativePath pretty() const;
String pretty() const;

/**
* Converts all separator characters in the path to @a sep and returns the
Expand Down
8 changes: 6 additions & 2 deletions doomsday/libdeng2/src/core/app.cpp
Expand Up @@ -97,7 +97,9 @@ NativePath App::nativeBinaryPath()
path = DENG_LIBRARY_DIR;
# endif
// Also check the system config files.
_unixInfo.path("libdir", path);
String configured;
_unixInfo.path("libdir", configured);
path = configured;
#endif
return path;
}
Expand Down Expand Up @@ -153,7 +155,9 @@ NativePath App::nativeBasePath()
path = DENG_BASE_DIR;
# endif
// Also check the system config files.
_unixInfo.path("basedir", path);
String configured;
_unixInfo.path("basedir", configured);
path = configured;
#endif
return path;
}
Expand Down
14 changes: 14 additions & 0 deletions doomsday/libdeng2/src/core/log.cpp
Expand Up @@ -31,6 +31,14 @@ namespace de {

const char* MAIN_SECTION = "";

#ifdef DENG2_DEBUG
/// If the section is longer than this, it will be alone on one line while
/// the rest of the entry continues after a break.
static const int LINE_BREAKING_SECTION_LENGTH = 30;
#else
static const int LINE_BREAKING_SECTION_LENGTH = 55;
#endif

namespace internal {

/**
Expand Down Expand Up @@ -140,6 +148,12 @@ String LogEntry::asText(const Flags& formattingFlags) const
{
output << TEXT_STYLE_SECTION << _section << ": ";
}

// If the section is very long, it's clearer to break the line here.
if(_section.length() > LINE_BREAKING_SECTION_LENGTH)
{
output << "\n";
}
}

if(flags.testFlag(Styled))
Expand Down
2 changes: 1 addition & 1 deletion doomsday/libdeng2/src/filesys/directoryfeed.cpp
Expand Up @@ -212,7 +212,7 @@ void DirectoryFeed::changeWorkingDir(const NativePath& nativePath)
void DirectoryFeed::createDir(const NativePath& nativePath)
{
NativePath parentPath = nativePath.fileNamePath();
if(!parentPath.empty() && !exists(parentPath))
if(!parentPath.isEmpty() && !exists(parentPath))
{
createDir(parentPath);
}
Expand Down
47 changes: 24 additions & 23 deletions doomsday/libdeng2/src/filesys/nativepath.cpp
Expand Up @@ -25,9 +25,9 @@
#include <QDir>

#ifdef WIN32
# define DIR_SEPARATOR '\\'
# define DIR_SEPARATOR QChar('\\')
#else
# define DIR_SEPARATOR '/'
# define DIR_SEPARATOR QChar('/')
# ifdef UNIX
# include <sys/types.h>
# include <pwd.h>
Expand All @@ -43,24 +43,24 @@ static QString toNative(const QString& s)
return QDir::toNativeSeparators(QDir::cleanPath(s));
}

NativePath::NativePath()
NativePath::NativePath() : Path()
{}

NativePath::NativePath(const QString &str) : String(toNative(str))
NativePath::NativePath(const QString &str) : Path(toNative(str), DIR_SEPARATOR)
{}

NativePath::NativePath(const char *nullTerminatedCStr)
: String(toNative(nullTerminatedCStr))
: Path(toNative(nullTerminatedCStr), DIR_SEPARATOR)
{
}

NativePath::NativePath(const char* cStr, size_type length)
: String(toNative(QString::fromUtf8(cStr, length)))
NativePath::NativePath(const char* cStr, dsize length)
: Path(toNative(QString::fromUtf8(cStr, length)), DIR_SEPARATOR)
{}

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

Expand All @@ -72,7 +72,7 @@ NativePath& NativePath::operator = (const char *nullTerminatedCStr)
NativePath NativePath::concatenatePath(const NativePath& nativePath) const
{
if(nativePath.isAbsolute()) return nativePath;
return String::concatenatePath(nativePath, QChar(DIR_SEPARATOR));
return toString().concatenatePath(nativePath, QChar(DIR_SEPARATOR));
}

NativePath NativePath::concatenatePath(const QString& nativePath) const
Expand All @@ -97,7 +97,7 @@ NativePath NativePath::operator / (const char *nullTerminatedCStr) const

NativePath NativePath::fileNamePath() const
{
return String::fileNamePath(DIR_SEPARATOR);
return String(*this).fileNamePath(DIR_SEPARATOR);
}

bool NativePath::isAbsolute() const
Expand All @@ -110,18 +110,19 @@ NativePath NativePath::expand(bool* didExpand) const
if(first() == '>' || first() == '}')
{
if(didExpand) *didExpand = true;
return App::app().nativeBasePath() / mid(1);
return App::app().nativeBasePath() / toString().mid(1);
}
#ifdef UNIX
else if(first() == '~')
{
if(didExpand) *didExpand = true;

int firstSlash = indexOf('/');
const String path = toString();
int firstSlash = path.indexOf('/');
if(firstSlash > 1)
{
// Parse the user's home directory (from passwd).
QByteArray userName = mid(1, firstSlash - 1).toLatin1();
QByteArray userName = path.mid(1, firstSlash - 1).toLatin1();
struct passwd* pw = getpwnam(userName);
if(!pw)
{
Expand All @@ -130,11 +131,13 @@ NativePath NativePath::expand(bool* didExpand) const
String("Unknown user '%1'").arg(QLatin1String(userName)));
}

return NativePath(pw->pw_dir) / mid(firstSlash + 1);
return NativePath(pw->pw_dir) / path.mid(firstSlash + 1);
}
else
{
// Replace with the HOME path.
return NativePath(QDir::homePath()) / path.mid(2);
}

// Replace with the HOME path.
return NativePath(QDir::homePath()) / mid(2);
}
#endif

Expand All @@ -143,11 +146,11 @@ NativePath NativePath::expand(bool* didExpand) const
return *this;
}

NativePath NativePath::pretty() const
String NativePath::pretty() const
{
if(empty()) return *this;
if(isEmpty()) return *this;

NativePath result = *this;
String result = *this;

// Hide relative directives like '}'
if(result.length() > 1 && (result.first() == '}' || result.first() == '>'))
Expand All @@ -170,9 +173,7 @@ NativePath NativePath::pretty() const

String NativePath::withSeparators(QChar sep) const
{
String s = *this;
s.replace(QChar(DIR_SEPARATOR), sep);
return s;
return Path::withSeparators(sep);
}

NativePath NativePath::workPath()
Expand Down

0 comments on commit ab5fbff

Please sign in to comment.