Line data Source code
1 : /*
2 : * SPDX-License-Identifier: GPL-3.0-only
3 : * MuseScore-CLA-applies
4 : *
5 : * MuseScore Studio
6 : * Music Composition & Notation
7 : *
8 : * Copyright (C) 2021 MuseScore Limited and others
9 : *
10 : * This program is free software: you can redistribute it and/or modify
11 : * it under the terms of the GNU General Public License version 3 as
12 : * published by the Free Software Foundation.
13 : *
14 : * This program is distributed in the hope that it will be useful,
15 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 : * GNU General Public License for more details.
18 : *
19 : * You should have received a copy of the GNU General Public License
20 : * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 : */
22 : #ifndef MUSE_IO_PATH_H
23 : #define MUSE_IO_PATH_H
24 :
25 : #include <string>
26 :
27 : #include "global/types/string.h"
28 : #include "global/logstream.h"
29 :
30 : #ifndef NO_QT_SUPPORT
31 : #include <QString>
32 : #include <QUrl>
33 : #endif
34 :
35 : namespace muse::io {
36 : struct path_t;
37 : using paths_t = std::vector<path_t>;
38 : struct path_t {
39 5755 : path_t() = default;
40 : path_t(const String& s);
41 : path_t(const std::string& s);
42 : path_t(std::string&& s);
43 : path_t(const char* s);
44 :
45 : bool empty() const;
46 : size_t size() const;
47 : bool hasSuffix(const char* str) const;
48 :
49 : path_t appendingComponent(const path_t& other) const;
50 : path_t appendingSuffix(const path_t& suffix) const;
51 :
52 1507 : inline path_t& operator=(const String& other) { m_path = other.toStdString(); return *this; }
53 22 : inline path_t& operator=(const char* other) { m_path = other; return *this; }
54 :
55 466 : inline bool operator==(const path_t& other) const { return m_path == other.m_path; }
56 31 : inline bool operator!=(const path_t& other) const { return !(m_path == other.m_path); }
57 :
58 93 : inline path_t operator+(const path_t& other) const { path_t p = *this; p += other; return p; }
59 3139 : inline path_t operator+(const String& other) const { path_t p = *this; p += other; return p; }
60 5331 : inline path_t operator+(const char* other) const { path_t p = *this; p += other; return p; }
61 5 : inline path_t operator+(const char other) const { path_t p = *this; p += other; return p; }
62 :
63 93 : inline path_t& operator+=(const path_t& other) { m_path += other.m_path; return *this; }
64 3185 : inline path_t& operator+=(const String& other) { m_path += other.toStdString(); return *this; }
65 5331 : inline path_t& operator+=(const char* other) { m_path += other; return *this; }
66 5 : inline path_t& operator+=(const char other) { m_path += other; return *this; }
67 :
68 118 : inline bool operator<(const path_t& other) const { return m_path < other.m_path; }
69 :
70 : String toString() const;
71 : const std::string& toStdString() const;
72 : const char* c_str() const;
73 :
74 : #ifndef NO_QT_SUPPORT
75 : path_t(const QString& s);
76 : explicit path_t(const QUrl& u);
77 49 : inline path_t& operator=(const QString& other) { m_path = other.toStdString(); return *this; }
78 0 : inline path_t operator+(const QString& other) const { path_t p = *this; p += String::fromQString(other); return p; }
79 0 : inline path_t& operator+=(const QString& other) { m_path += other.toStdString(); return *this; }
80 : QString toQString() const;
81 : QUrl toQUrl() const;
82 : std::wstring toStdWString() const;
83 : #endif
84 :
85 : private:
86 : std::string m_path;
87 : };
88 :
89 : inline path_t operator+(const String& one, const path_t& other) { return path_t(one) + other; }
90 : inline path_t operator+(const char* one, const path_t& other) { return path_t(one) + other; }
91 :
92 : #ifndef NO_QT_SUPPORT
93 : inline path_t operator+(const QString& one, const path_t& other) { return path_t(one) + other; }
94 : #endif
95 :
96 217 : inline muse::logger::Stream& operator<<(muse::logger::Stream& s, const muse::io::path_t& p)
97 : {
98 217 : s << p.toStdString();
99 217 : return s;
100 : }
101 :
102 : std::string suffix(const path_t& path);
103 : path_t filename(const path_t& path, bool includingExtension = true);
104 : path_t basename(const path_t& path);
105 : path_t completeBasename(const path_t& path);
106 : path_t absolutePath(const path_t& path);
107 : path_t absoluteFilePath(const path_t& path);
108 : //path_t dirname(const path_t& path);
109 : path_t dirpath(const path_t& path);
110 : path_t absoluteDirpath(const path_t& path);
111 :
112 : bool isAbsolute(const path_t& path);
113 :
114 : bool isAllowedFileName(const path_t& fn);
115 : path_t escapeFileName(const path_t& fn);
116 :
117 : path_t toNativeSeparators(const path_t& path);
118 :
119 : paths_t pathsFromString(const std::string& str, const std::string& delim = ";");
120 : std::string pathsToString(const paths_t& ps, const std::string& delim = ";");
121 : }
122 :
123 : #endif // MUSE_IO_PATH_H
|