1 |
/*************************************************************************** |
2 |
* * |
3 |
* Copyright (C) 2007-2014 Christian Schoenebeck * |
4 |
* * |
5 |
* This library is free software; you can redistribute it and/or modify * |
6 |
* it under the terms of the GNU General Public License as published by * |
7 |
* the Free Software Foundation; either version 2 of the License, or * |
8 |
* (at your option) any later version. * |
9 |
* * |
10 |
* This library is distributed in the hope that it will be useful, * |
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
13 |
* GNU General Public License for more details. * |
14 |
* * |
15 |
* You should have received a copy of the GNU General Public License * |
16 |
* along with this library; if not, write to the Free Software * |
17 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, * |
18 |
* MA 02111-1307 USA * |
19 |
***************************************************************************/ |
20 |
|
21 |
#ifndef LS_PATH_H |
22 |
#define LS_PATH_H |
23 |
|
24 |
#include <vector> |
25 |
#include <string> |
26 |
|
27 |
namespace LinuxSampler { |
28 |
|
29 |
/** @brief Portable Path Abstraction |
30 |
* |
31 |
* Correct path and file names are dependent to the underlying OS and FS. |
32 |
* Especially the set of forbidden characters (and their encodings / escape |
33 |
* sequences) in path and file names differ on the various systems. This |
34 |
* class is meant as a portable wrapper to circumvent these problems by |
35 |
* storing the file names in its raw, human readable (intended) form and |
36 |
* provides OS specific methods like @c toPosix() for converting the path into |
37 |
* the correct encoding, as expected by the respective system. |
38 |
* |
39 |
* This class is currently only used internally by the LSCP parser. A lot |
40 |
* generalization work would have to be done to use this class as a overall |
41 |
* replacement for all @c char* / @c std::string file name arguments in the |
42 |
* sampler or even its C++ API. Probably we'll use something like |
43 |
* @c boost::filesystem::path instead of this class in future. |
44 |
*/ |
45 |
class Path { |
46 |
public: |
47 |
/** |
48 |
* Default constructor. |
49 |
*/ |
50 |
Path(); |
51 |
|
52 |
/** |
53 |
* Creates a path object according to the local system's path conventions. |
54 |
* @see fromPosix(), fromWindows(), fromDbPath() |
55 |
*/ |
56 |
Path(std::string path); |
57 |
|
58 |
/** |
59 |
* Concatenate exactly one path node or filename to the end of this Path |
60 |
* object. This can be used to build up a full qualified path, directory |
61 |
* by directory. |
62 |
* |
63 |
* @param Name - name of the path node's name or filename in it's raw, |
64 |
* human readable/expected form |
65 |
*/ |
66 |
void appendNode(std::string Name); |
67 |
|
68 |
/** |
69 |
* Sets the hardware drive of the path, for systems that use the concept |
70 |
* of drives in absolute pathes (i.e. Windows). |
71 |
*/ |
72 |
void setDrive(const char& Drive); |
73 |
|
74 |
/** |
75 |
* Convert this Path into the correct encoding as expected by POSIX |
76 |
* compliant system calls. |
77 |
*/ |
78 |
std::string toPosix() const; |
79 |
|
80 |
/** |
81 |
* Convert this Path into the correct encoding as expected |
82 |
* by the instruments database implementation. |
83 |
*/ |
84 |
std::string toDbPath() const; |
85 |
|
86 |
/** |
87 |
* Convert this Path into the correct encoding as expected and needed |
88 |
* for LSCP responses. |
89 |
*/ |
90 |
std::string toLscp() const; |
91 |
|
92 |
/** |
93 |
* Convert this Path into the correct encoding as expected by Windows |
94 |
* operating systems. |
95 |
*/ |
96 |
std::string toWindows() const; |
97 |
|
98 |
/** |
99 |
* Concatenate two paths. |
100 |
*/ |
101 |
Path operator+(const Path& p); |
102 |
|
103 |
/** |
104 |
* Concatenate two paths. |
105 |
*/ |
106 |
Path operator+(const Path* p); |
107 |
|
108 |
/** |
109 |
* Create a Path object from a POSIX path / filename string. |
110 |
*/ |
111 |
static Path fromPosix(std::string path); |
112 |
|
113 |
/** |
114 |
* Create a Path object from a DB path. |
115 |
*/ |
116 |
static Path fromDbPath(std::string path); |
117 |
|
118 |
/** |
119 |
* Create a Path object from a Windows path / filename string. |
120 |
*/ |
121 |
static Path fromWindows(std::string path); |
122 |
|
123 |
/** |
124 |
* Returns the name of the file or directory represented by |
125 |
* this path in abstract/raw form. This is the last name in |
126 |
* the path's name sequence. |
127 |
*/ |
128 |
std::string getName(); |
129 |
|
130 |
/** |
131 |
* Returns the name of the file or directory |
132 |
* represented by the specified path in abstract/raw form. |
133 |
* This is the last name in the path's name sequence. |
134 |
*/ |
135 |
static std::string getName(std::string path); |
136 |
|
137 |
/** |
138 |
* Returns the path with the last name |
139 |
* of the path's name sequence stripped off. |
140 |
*/ |
141 |
std::string stripLastName(); |
142 |
|
143 |
/** |
144 |
* Returns the path with the last name |
145 |
* of the path's name sequence stripped off. |
146 |
*/ |
147 |
static std::string stripLastName(std::string path); |
148 |
|
149 |
/** |
150 |
* Returns the last name in the path's name sequence |
151 |
* of this path with the file extension stripped off. |
152 |
*/ |
153 |
std::string getBaseName(); |
154 |
|
155 |
/** |
156 |
* Returns the last name in the path's name sequence |
157 |
* of the specified path with the file extension stripped off. |
158 |
*/ |
159 |
static std::string getBaseName(std::string path); |
160 |
|
161 |
/** |
162 |
* Returns true if the path is reflecting an absolute path, false if it is |
163 |
* rather a relative path. |
164 |
*/ |
165 |
bool isAbsolute() const; |
166 |
|
167 |
private: |
168 |
std::vector<std::string> elements; ///< stores the path names raw = unencoded, each element is one node of the path |
169 |
char drive; |
170 |
bool absolute; |
171 |
}; |
172 |
|
173 |
} // namespace LinuxSampler |
174 |
|
175 |
#endif // LS_PATH_H |