PPath Class Reference

Detailed Description

The PPath class is an helper class to get handle paths.

Definition at line 27 of file PPath.h.

#include <PPath.h>

Member Function Documentation

◆ AddPathSeparatorIfMissing()

static PString PPath::AddPathSeparatorIfMissing ( const PString s)
static

Builds a string by appending the path separator to the specified string, if missing.

Path separator depends on the platform (either '/' or '\').

◆ ChangeRootDir()

static PResult PPath::ChangeRootDir ( PString path,
const PString oldRoot,
const PString newRoot,
bool  forceWinToLinux = true 
)
static

Changes root directory of path from oldRoot to newRoot - after cleaning all three paths with PPath::Clean()

If oldRoot is empty, we just prefix newRoot to path. If newRoot is empty, we make path relative by removing oldRoot from its beginning

If oldRoot is non-empty and path does NOT start with oldRoot, a message is logged at warning level, path is left unchanged (except for cleaning) and PResult::ErrorInvalidState is returned.

WARNING:: this function uses PPath::Clean(); see warning for that function.

Otherwise return PResult::C_OK

◆ Clean()

static PString PPath::Clean ( const PString dirPath,
bool  forceWinToLinux = true 
)
static

Returns path with directory separators normalized (converted to "/") and redundant separators removed, and "."s and ".."s resolved (as far as possible).

Symbolic links are kept. This function does not return the canonical path, but rather the simplest version of the input. For example, "./local" becomes "local", "local/../bin" becomes "bin" and "/local/usr/../bin" becomes "/local/bin".

WARNING:
On Windows (except in the DOS cmd shell), both "\" and "/" are considered directory separators, so we always normalize to "/" ON WINDOWS.
On Linux, however, "\" is a legal filename character, so you can perfectly well have a filename containing "\". Therefore, on Linux, converting "\" to "/" will be a bug IF the path originates from Linux. BUT, if the path actually originates from Windows (e.g. in a .INI file or .XML file) and you are using it on Linux then you DO want the conversion. We suppose this is the most common case. Therefore the option forceWinToLinux is true by default.
HOWEVER, if you are working under Linux with paths that you KNOW are from Linux, you should set the option to false to avoid bugs with files that (absurdly, but perfectly legally) have "\" in their name.

◆ CurrentPath()

static PString PPath::CurrentPath ( )
static

Returns the absolute path of the system's current directory.

◆ GetAbsolutePath()

static PString PPath::GetAbsolutePath ( const PString path)
static

Returns the absolute path (a path that starts with "/" or with a drive specification) from the specified path.

◆ GetApplicationDirectory()

static PString PPath::GetApplicationDirectory ( )
static

Returns the path of the directory that the caller executable resides.

◆ GetApplicationName()

static PString PPath::GetApplicationName ( )
static

Tries and gets the full-path of the running application.

◆ GetBaseName()

static PString PPath::GetBaseName ( const PString path)
static

Returns the base name of the file without the path (and without extension).

The base name consists of all characters in the file up to (but not including) the first '.' character.

Example:

PPath::GetBaseName("/tmp/archive.tar.gz"); // will return "archive" (path and extension have been removed).

◆ GetBaseNameWithoutEndDigits()

static PString PPath::GetBaseNameWithoutEndDigits ( const PString path)
static

Returns the base name of the file without the path and without end digits (and without extension).

The base name consists of all characters in the file up to (but not including) the first '.' character.

Examples:

PPath::GetBaseNameWithoutEndDigits("/tmp/archive.tar.gz"); // will return "archive"
PPath::GetBaseNameWithoutEndDigits("/tmp/archive3.tar.gz") // will return "archive"
PPath::GetBaseNameWithoutEndDigits("/tmp/archive001.tar.gz") // will return "archive"

◆ GetDirectory()

static PString PPath::GetDirectory ( const PString path)
static

Returns the directory of a given path.

Example1 (Linux):

PPath::GetDirectory("/tmp/test/archive.tar.gz"); // will return "/tmp/test"

Example2 (Windows):

PPath::GetDirectory("C:\\buf\\bin\\app.exe"); // will return "C:/buf/bin"

◆ GetExtension()

static PString PPath::GetExtension ( const PString path)
static

Returns the extension of the file.

This consists of all the characters in the file after (but not including) the first '.'

Example:

PPath::GetExtension("/tmp/archive.tar.gz"); // will return "tar.gz"

◆ IsDirectory()

static bool PPath::IsDirectory ( const PString path)
static

Returns true if the specified path is a directory, false otherwise.

◆ IsFile()

static bool PPath::IsFile ( const PString path)
static

Returns true if the specified path is a file, false otherwise.

◆ IsPathExist()

static bool PPath::IsPathExist ( const PString path)
static

Returns true if the specified path exists, false otherwise.

Path can be relative or absolute.

◆ Join() [1/5]

static PString PPath::Join ( const PString path1,
const PString path2 
)
static

Joins two paths using the native directory separator.

If one of the two path is empty, then returns the other.

◆ Join() [2/5]

static PString PPath::Join ( const PString path1,
const PString path2,
const PString path3 
)
static

Joins three paths using the native directory separator.

Skips empty paths.

◆ Join() [3/5]

static PString PPath::Join ( const PString path1,
const PString path2,
const PString path3,
const PString path4 
)
static

Joins four paths using the native directory separator.

Skips empty paths.

◆ Join() [4/5]

static PString PPath::Join ( const PString path1,
const PString path2,
const PString path3,
const PString path4,
const PString path5 
)
static

Joins five paths using the native directory separator.

Skips empty paths.

◆ Join() [5/5]

static PString PPath::Join ( const PString path1,
const PString path2,
const PString path3,
const PString path4,
const PString path5,
const PString path6 
)
static

Joins six paths using the native directory separator.

Skips empty paths.

◆ ListEntries()

static PStringList PPath::ListEntries ( const PString path,
bool  withAbsolutePath,
bool  filesOnly 
)
static

Returns the list of entries in the specified path.

◆ MakePath()

static bool PPath::MakePath ( const PString dirPath)
static

Creates all parent directories necessary to create the specified directory.

Returns true if success, false otherwise.

◆ RemoveNonEmptyDirectory()

static bool PPath::RemoveNonEmptyDirectory ( const PString dirPath)
static

Removes the specified directory.

Recursively delete the contents of the directory first. Returns true if success, false otherwise.

◆ Separator()

static const PString& PPath::Separator ( )
static

Returns path separator according to the host platform, typically '\' for Windows and '/' for Linux.

◆ SetCurrentWorkingDirectory()

static bool PPath::SetCurrentWorkingDirectory ( const PString path)
static

Sets the application's current working directory to path.

Returns true if the directory was successfully changed; otherwise returns false.

◆ TempPath()

static PString PPath::TempPath ( )
static

Returns the absolute path of the system's temporary directory.