LibrePCB Developers Documentation
filepath.h
Go to the documentation of this file.
1 /*
2  * LibrePCB - Professional EDA for everyone!
3  * Copyright (C) 2013 LibrePCB Developers, see AUTHORS.md for contributors.
4  * https://librepcb.org/
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef LIBREPCB_CORE_FILEPATH_H
21 #define LIBREPCB_CORE_FILEPATH_H
22 
23 /*******************************************************************************
24  * Includes
25  ******************************************************************************/
26 #include <QtCore>
27 
28 /*******************************************************************************
29  * Namespace / Forward Declarations
30  ******************************************************************************/
31 namespace librepcb {
32 
33 /*******************************************************************************
34  * Class FilePath
35  ******************************************************************************/
36 
127 class FilePath final {
128 public: // Types
130  // spaces
132  ReplaceSpaces = 1 << 1,
133  // case
134  KeepCase = 0,
135  ToLowerCase = 1 << 2,
136  ToUpperCase = 1 << 3,
137  // default
139  };
140  Q_DECLARE_FLAGS(CleanFileNameOptions, CleanFileNameOption)
141 
142 public: // Methods
143  // Constructors / Destructor
144 
148  FilePath() noexcept;
149 
155  explicit FilePath(const QString& filepath) noexcept;
156 
162  FilePath(const FilePath& other) noexcept;
163 
164  // Setters
165 
177  bool setPath(const QString& filepath) noexcept;
178 
179  // Getters
180 
186  bool isValid() const noexcept { return mIsValid; }
187 
193  bool isExistingFile() const noexcept;
194 
200  bool isExistingDir() const noexcept;
201 
208  bool isEmptyDir() const noexcept;
209 
215  bool isRoot() const noexcept;
216 
223  bool isLocatedInDir(const FilePath& dir) const noexcept;
224 
231  QString toStr() const noexcept;
232 
239  QString toNative() const noexcept;
240 
256  FilePath toUnique() const noexcept;
257 
273  QString toRelative(const FilePath& base) const noexcept;
274 
284  QString toRelativeNative(const FilePath& base) const noexcept;
285 
291  QUrl toQUrl() const noexcept { return QUrl::fromLocalFile(toStr()); }
292 
298  QString getBasename() const noexcept;
299 
305  QString getCompleteBasename() const noexcept;
306 
312  QString getSuffix() const noexcept;
313 
319  QString getCompleteSuffix() const noexcept;
320 
326  QString getFilename() const noexcept;
327 
334  FilePath getParentDir() const noexcept;
335 
351  FilePath getPathTo(const QString& filename) const noexcept;
352 
353  // Static Methods
354 
370  static FilePath fromRelative(const FilePath& base,
371  const QString& relative) noexcept;
372 
378  static FilePath getTempPath() noexcept;
379 
386  static FilePath getApplicationTempPath() noexcept;
387 
393  static FilePath getRandomTempPath() noexcept;
394 
418  static QString cleanFileName(const QString& userInput,
419  CleanFileNameOptions options) noexcept;
420 
421  // Operator Overloadings
422 
426  FilePath& operator=(const FilePath& rhs) noexcept;
427 
435  bool operator==(const FilePath& rhs) const noexcept;
436 
444  bool operator!=(const FilePath& rhs) const noexcept;
445 
457  bool operator<(const FilePath& rhs) const noexcept;
458 
459 private:
460  // Private Methods
461 
471  static QString makeWellFormatted(const QString& filepath) noexcept;
472 
473  // Attributes
474 
475  bool mIsValid;
476  QFileInfo
478 };
479 
480 // Non-Member Functions
481 QDataStream& operator<<(QDataStream& stream, const FilePath& filepath);
482 QDebug& operator<<(QDebug& stream, const FilePath& filepath);
483 inline uint qHash(const FilePath& key, uint seed) noexcept {
484  return qHash(key.toStr(), seed);
485 }
486 
487 /*******************************************************************************
488  * End of File
489  ******************************************************************************/
490 
491 } // namespace librepcb
492 
493 Q_DECLARE_METATYPE(librepcb::FilePath)
494 Q_DECLARE_OPERATORS_FOR_FLAGS(librepcb::FilePath::CleanFileNameOptions)
495 
496 #endif
Definition: filepath.h:136
bool isExistingFile() const noexcept
Check if the specified filepath is an existing file.
Definition: filepath.cpp:70
FilePath() noexcept
The default constructor (this will create an invalid object!)
Definition: filepath.cpp:41
QDataStream & operator<<(QDataStream &stream, const AttributeKey &obj)
Definition: attributekey.h:108
bool isLocatedInDir(const FilePath &dir) const noexcept
Check if the filepath is located inside another directory.
Definition: filepath.cpp:96
static QString cleanFileName(const QString &userInput, CleanFileNameOptions options) noexcept
Clean a given string so that it becomes a valid filename.
Definition: filepath.cpp:234
QString getCompleteBasename() const noexcept
Get the complete basename of the file or directory.
Definition: filepath.cpp:142
QString getFilename() const noexcept
Get the whole filename (without the path) of the file or directory.
Definition: filepath.cpp:163
QString toRelativeNative(const FilePath &base) const noexcept
Same as toRelative(), but with native directory separators.
Definition: filepath.cpp:131
Definition: occmodel.cpp:76
QString getBasename() const noexcept
Get the basename of the file or directory.
Definition: filepath.cpp:135
bool isRoot() const noexcept
Check if the specified filepath is the root directory.
Definition: filepath.cpp:90
Definition: filepath.h:134
bool operator!=(const FilePath &rhs) const noexcept
The "!=" operator to compare two FilePath objects.
Definition: filepath.cpp:196
static FilePath fromRelative(const FilePath &base, const QString &relative) noexcept
Build an absolute and well-formatted filepath from a relative filepath.
Definition: filepath.cpp:208
bool mIsValid
Definition: filepath.h:475
static FilePath getRandomTempPath() noexcept
Get a random temporary directory path (e.g. "/tmp/librepcb/42")
Definition: filepath.cpp:228
CleanFileNameOption
Definition: filepath.h:129
QString toRelative(const FilePath &base) const noexcept
Convert an absolute filepath to a relative filepath (relative to another filepath) ...
Definition: filepath.cpp:124
FilePath toUnique() const noexcept
Get a unique version of the filepath (resolve symbolic links if possible)
Definition: filepath.cpp:114
QFileInfo mFileInfo
the absolute and well-formatted filepath in a QFileInfo
Definition: filepath.h:477
bool isExistingDir() const noexcept
Check if the specified filepath is an existing directory.
Definition: filepath.cpp:76
bool operator<(const FilePath &rhs) const noexcept
The "<" operator to compare two FilePath objects.
Definition: filepath.cpp:200
FilePath getParentDir() const noexcept
Get the filepath of the parent directory of the file or directory.
Definition: filepath.cpp:170
FilePath & operator=(const FilePath &rhs) noexcept
The assign operator to copy a FilePath into another FilePath object.
Definition: filepath.cpp:184
bool isValid() const noexcept
Check whether this object contains a valid filepath or not.
Definition: filepath.h:186
This class represents absolute, well-formatted paths to files or directories.
Definition: filepath.h:127
QString toNative() const noexcept
Get the absolute filepath with native directory separators.
Definition: filepath.cpp:108
bool isEmptyDir() const noexcept
Check if the specified filepath is an existing, empty directory.
Definition: filepath.cpp:82
Definition: filepath.h:131
FilePath getPathTo(const QString &filename) const noexcept
Get the filepath to a file or directory which is relative to this filepath.
Definition: filepath.cpp:176
bool operator==(const FilePath &rhs) const noexcept
The "==" operator to compare two FilePath objects.
Definition: filepath.cpp:190
static FilePath getApplicationTempPath() noexcept
Get the path to the temporary application directory (e.g. "/tmp/librepcb")
Definition: filepath.cpp:224
bool setPath(const QString &filepath) noexcept
Set a new filepath.
Definition: filepath.cpp:60
QString getCompleteSuffix() const noexcept
Get the complete suffix of the file or directory.
Definition: filepath.cpp:156
static FilePath getTempPath() noexcept
Get the path to the temporary directory (e.g. "/tmp" on Unix/Linux)
Definition: filepath.cpp:215
Definition: filepath.h:138
QString getSuffix() const noexcept
Get the suffix of the file or directory.
Definition: filepath.cpp:149
static QString makeWellFormatted(const QString &filepath) noexcept
Make a filepath well-formatted (except making it absolute!)
Definition: filepath.cpp:253
Definition: filepath.h:132
QString toStr() const noexcept
Get the absolute and well-formatted filepath as a QString.
Definition: filepath.cpp:102
uint qHash(const AttributeKey &key, uint seed=0) noexcept
Definition: attributekey.h:118
Definition: filepath.h:135
QUrl toQUrl() const noexcept
Create and return a QUrl object with this filepath.
Definition: filepath.h:291