LibrePCB Developers Documentation
|
This class represents absolute, well-formatted paths to files or directories. More...
#include <filepath.h>
Public Types | |
enum | CleanFileNameOption |
Public Member Functions | |
FilePath () noexcept | |
The default constructor (this will create an invalid object!) More... | |
FilePath (const QString &filepath) noexcept | |
Constructor to create a librepcb::FilePath object from a QString. More... | |
FilePath (const FilePath &other) noexcept | |
The copy constructor. More... | |
bool | setPath (const QString &filepath) noexcept |
Set a new filepath. More... | |
bool | isValid () const noexcept |
Check whether this object contains a valid filepath or not. More... | |
bool | isExistingFile () const noexcept |
Check if the specified filepath is an existing file. More... | |
bool | isExistingDir () const noexcept |
Check if the specified filepath is an existing directory. More... | |
bool | isEmptyDir () const noexcept |
Check if the specified filepath is an existing, empty directory. More... | |
bool | isRoot () const noexcept |
Check if the specified filepath is the root directory. More... | |
bool | isLocatedInDir (const FilePath &dir) const noexcept |
Check if the filepath is located inside another directory. More... | |
QString | toStr () const noexcept |
Get the absolute and well-formatted filepath as a QString. More... | |
QString | toNative () const noexcept |
Get the absolute filepath with native directory separators. More... | |
FilePath | toUnique () const noexcept |
Get a unique version of the filepath (resolve symbolic links if possible) More... | |
QString | toRelative (const FilePath &base) const noexcept |
Convert an absolute filepath to a relative filepath (relative to another filepath) More... | |
QString | toRelativeNative (const FilePath &base) const noexcept |
Same as toRelative(), but with native directory separators. More... | |
QUrl | toQUrl () const noexcept |
Create and return a QUrl object with this filepath. More... | |
QString | getBasename () const noexcept |
Get the basename of the file or directory. More... | |
QString | getCompleteBasename () const noexcept |
Get the complete basename of the file or directory. More... | |
QString | getSuffix () const noexcept |
Get the suffix of the file or directory. More... | |
QString | getCompleteSuffix () const noexcept |
Get the complete suffix of the file or directory. More... | |
QString | getFilename () const noexcept |
Get the whole filename (without the path) of the file or directory. More... | |
FilePath | getParentDir () const noexcept |
Get the filepath of the parent directory of the file or directory. More... | |
FilePath | getPathTo (const QString &filename) const noexcept |
Get the filepath to a file or directory which is relative to this filepath. More... | |
FilePath & | operator= (const FilePath &rhs) noexcept |
The assign operator to copy a FilePath into another FilePath object. More... | |
bool | operator== (const FilePath &rhs) const noexcept |
The "==" operator to compare two FilePath objects. More... | |
bool | operator!= (const FilePath &rhs) const noexcept |
The "!=" operator to compare two FilePath objects. More... | |
bool | operator< (const FilePath &rhs) const noexcept |
The "<" operator to compare two FilePath objects. More... | |
Static Public Member Functions | |
static FilePath | fromRelative (const FilePath &base, const QString &relative) noexcept |
Build an absolute and well-formatted filepath from a relative filepath. More... | |
static FilePath | getTempPath () noexcept |
Get the path to the temporary directory (e.g. "/tmp" on Unix/Linux) More... | |
static FilePath | getApplicationTempPath () noexcept |
Get the path to the temporary application directory (e.g. "/tmp/librepcb") More... | |
static FilePath | getRandomTempPath () noexcept |
Get a random temporary directory path (e.g. "/tmp/librepcb/42") More... | |
static QString | cleanFileName (const QString &userInput, CleanFileNameOptions options) noexcept |
Clean a given string so that it becomes a valid filename. More... | |
Static Private Member Functions | |
static QString | makeWellFormatted (const QString &filepath) noexcept |
Make a filepath well-formatted (except making it absolute!) More... | |
Private Attributes | |
bool | mIsValid |
QFileInfo | mFileInfo |
the absolute and well-formatted filepath in a QFileInfo More... | |
This class represents absolute, well-formatted paths to files or directories.
Why we need well-formatted paths and what do they look like
Using QString for paths to directories or files works not bad, but there are some disadventages. As the receiver of a filepath in form of a QString, you...
To reach this goal, you need to convert each filepath which comes from outside the application (user input, read from file, ...) immediately into such a well-formatted filepath (make absolute, convert separators, remove redundant entries and so on). Then the filepath can be processed by the application. If a filepath must be printed to outside the application (print to a message box, write to a file, ...), the filepath can be converted (back) to the needed format (maybe "\" instead of "/", maybe relative instead of absolute, ...). <b>How the class ::librepcb::FilePath works and how to use it</b> The class ::librepcb::FilePath represents a well-formatted filepath to a file or directory and provides methods to convert paths between different formats. Every ::librepcb::FilePath object represents either a well-formatted filepath or an invalid object (see #isValid()). It's not possible to create ::librepcb::FilePath objects with non-well-formatted filepaths. <b>Example:</b> @code FilePath fp("C:\foo\bar.txt"); // a file qDebug(fp.toStr()); // "C:/foo/bar.txt" qDebug(fp.toNative()); // "C:/foo/bar.txt"; Windows: "C:\foo\bar.txt" fp.setPath("/foo/bar/"); // a directory qDebug(fp.toStr()); // "/foo/bar" (trailing slash removed!) qDebug(fp.toNative()); // "/foo/bar" ("\foo\bar" on Windows) @endcode @note A filepath represented by a FilePath object do not need to exist on the file system. Most methods of this class do not depend on whether a filepath exists or not. Exceptions: #toUnique(), #isExistingFile(), #isExistingDir(). @warning Please consider that the conversion from filepaths with backslashes as directory separator (Windows style) to filepaths with slashes as directory separator (UNIX style, well-formatted paths) will work only on windows! The reason is that backslashes are allowed in UNIX filepaths (and are NOT interpreted as directory separator), so it's not allowed to replace all backslashes in a UNIX filepath with slashes! @warning Be careful with special characters in filepaths! Especially (relative) filepaths in the library or in projects must always work on all operating systems (the whole workspace must be platform independent!). This means that only characters are allowed which can be used in filepaths on all operating systems (for example the backslash "" is allowed in UNIX filenames, but not in Windows filenames). Other filepaths, like filepaths to recently used projects (which are stored in the user's profile), do not need to be platform independent.
enum CleanFileNameOption |
|
noexcept |
The default constructor (this will create an invalid object!)
|
explicitnoexcept |
Constructor to create a librepcb::FilePath object from a QString.
filepath | See setPath() |
The copy constructor.
other | The object to copy |
|
noexcept |
Set a new filepath.
filepath | An absolute (!) filepath to a file or directory (the target do not need to exist). On Windows, both forward ("/") and backward ("\") slashes are allowed as directory separators (also mixed in one filepath). On other operating systems, only forward slashes ("/") are allowed!. Also ".", ".." and redundant directory separators are allowed. |
|
inlinenoexcept |
Check whether this object contains a valid filepath or not.
|
noexcept |
Check if the specified filepath is an existing file.
|
noexcept |
Check if the specified filepath is an existing directory.
|
noexcept |
Check if the specified filepath is an existing, empty directory.
|
noexcept |
Check if the specified filepath is the root directory.
|
noexcept |
Check if the filepath is located inside another directory.
|
noexcept |
Get the absolute and well-formatted filepath as a QString.
|
noexcept |
|
noexcept |
Get a unique version of the filepath (resolve symbolic links if possible)
Because of symbolic links, the user is able to have different paths which all point to the same file/directory. But sometimes you want to determine whether two paths point to the same file/directory ("equal paths") or not ("different paths"). For this purpose you can use toUnique(). This method will resolve symbolic links if possible (this is only possible if the filepath exists!). If the filepath does not exist, this method will return the same as toStr().
|
noexcept |
Convert an absolute filepath to a relative filepath (relative to another filepath)
base | The base of the relative filepath (the part which will be removed from the absolute filepath). This must be a filepath to a directory, paths to a file will produce wrong results! |
|
noexcept |
Same as toRelative(), but with native directory separators.
base | See toRelative(). |
|
inlinenoexcept |
Create and return a QUrl object with this filepath.
|
noexcept |
Get the basename of the file or directory.
|
noexcept |
Get the complete basename of the file or directory.
|
noexcept |
Get the suffix of the file or directory.
|
noexcept |
Get the complete suffix of the file or directory.
|
noexcept |
Get the whole filename (without the path) of the file or directory.
|
noexcept |
Get the filepath of the parent directory of the file or directory.
|
noexcept |
Get the filepath to a file or directory which is relative to this filepath.
filename | A relative filepath to a file or directory, like "file.txt" or "subdir/file.txt" |
librepcb::FilePath::fromRelative(*this, filename);
Build an absolute and well-formatted filepath from a relative filepath.
base | The base of the relative filepath (the part which is missed from the absolute filepath). This must be a filepath to a directory, paths to a file will produce wrong results! |
relative | The relative path (relative to "base") |
|
staticnoexcept |
Get the path to the temporary directory (e.g. "/tmp" on Unix/Linux)
|
staticnoexcept |
Get the path to the temporary application directory (e.g. "/tmp/librepcb")
|
staticnoexcept |
Get a random temporary directory path (e.g. "/tmp/librepcb/42")
|
staticnoexcept |
Clean a given string so that it becomes a valid filename.
Every time a file- or directory name needs to be constructed (e.g. from user input), you must use this function to replace/remove all characters which are not allowed for file/dir paths.
These are the only allowed characters: "-._ 0-9A-Za-z"
In addition, the length of the filename will be limited to 120 characters.
userInput | An arbitrary string (may be directly from a user input field) |
options | Some options to define how the filename should be escaped |
|
noexcept |
|
noexcept |
|
noexcept |
|
staticprivatenoexcept |
Make a filepath well-formatted (except making it absolute!)
filepath | An absolute or relative filepath which may isn't well-formatted |
|
private |
|
private |
the absolute and well-formatted filepath in a QFileInfo