20 |
|
|
21 |
#include "Path.h" |
#include "Path.h" |
22 |
|
|
23 |
|
// for function hexsToNumber() |
24 |
|
#include "global.h" |
25 |
|
|
26 |
namespace LinuxSampler { |
namespace LinuxSampler { |
27 |
|
|
28 |
void Path::appendNode(std::string Name) { |
void Path::appendNode(std::string Name) { |
71 |
return result; |
return result; |
72 |
} |
} |
73 |
|
|
74 |
|
std::string Path::toLscp() { |
75 |
|
std::string result; |
76 |
|
for (int iElement = 0; iElement < elements.size(); iElement++) { |
77 |
|
// replace "special characters" by LSCP escape sequences |
78 |
|
std::string e = elements[iElement]; |
79 |
|
for (int i = 0; i < e.length(); i++) { |
80 |
|
const char c = e.c_str()[i]; |
81 |
|
if ( |
82 |
|
!(c >= '0' && c <= '9') && |
83 |
|
!(c >= 'a' && c <= 'z') && |
84 |
|
!(c >= 'A' && c <= 'Z') && |
85 |
|
!(c == '!') && !(c == '#') && !(c == '$') && !(c == '%') && |
86 |
|
!(c == '&') && !(c == '(') && !(c == ')') && !(c == '*') && |
87 |
|
!(c == '+') && !(c == ',') && !(c == '-') && !(c == '.') && |
88 |
|
!(c == ':') && !(c == ';') && !(c == '<') && !(c == '=') && |
89 |
|
!(c == '>') && !(c == '?') && !(c == '@') && !(c == '[') && |
90 |
|
!(c == ']') && !(c == '^') && !(c == '_') && !(c == '`') && |
91 |
|
!(c == '{') && !(c == '|') && !(c == '}') && !(c == '~') |
92 |
|
) { |
93 |
|
// convert the "special" character into a "\xHH" LSCP escape sequence |
94 |
|
char buf[5]; |
95 |
|
snprintf(buf, sizeof(buf), "\\x%02x", static_cast<unsigned char>(c)); |
96 |
|
e.replace(i, 1, buf); |
97 |
|
i += 3; |
98 |
|
} |
99 |
|
} |
100 |
|
// append encoded node to full encoded path |
101 |
|
result += "/" + e; |
102 |
|
} |
103 |
|
if (!result.size()) result = "/"; |
104 |
|
return result; |
105 |
|
} |
106 |
|
|
107 |
Path Path::operator+(const Path& p) { |
Path Path::operator+(const Path& p) { |
108 |
Path result = *this; |
Path result = *this; |
109 |
for (int i = 0; i < p.elements.size(); i++) |
for (int i = 0; i < p.elements.size(); i++) |
115 |
return *this + *p; |
return *this + *p; |
116 |
} |
} |
117 |
|
|
118 |
|
Path Path::fromPosix(std::string path) { |
119 |
|
Path result; |
120 |
|
// first split the nodes |
121 |
|
{ |
122 |
|
int nodeEnd; |
123 |
|
for ( |
124 |
|
int nodeBegin = path.find_first_not_of('/'); |
125 |
|
nodeBegin != std::string::npos; |
126 |
|
nodeBegin = path.find_first_not_of('/', nodeEnd) |
127 |
|
) { |
128 |
|
nodeEnd = path.find_first_of('/', nodeBegin); |
129 |
|
result.appendNode( |
130 |
|
(nodeEnd != std::string::npos) ? |
131 |
|
path.substr(nodeBegin, nodeEnd - nodeBegin) : |
132 |
|
path.substr(nodeBegin) |
133 |
|
); |
134 |
|
} |
135 |
|
} |
136 |
|
// resolve POSIX escape sequences in all nodes |
137 |
|
for (int iNode = 0; iNode < result.elements.size(); iNode++) { |
138 |
|
std::string& s = result.elements[iNode]; |
139 |
|
for (int pos = s.find('%'); pos < s.length(); pos = s.find('%', ++pos)) { |
140 |
|
if (pos+1 >= s.length()) { // unexpected character |
141 |
|
//TODO: we might want to throw an exception here, for now we simply replace the character by '?' |
142 |
|
s.replace(pos, 1, "?"); |
143 |
|
continue; |
144 |
|
} |
145 |
|
if (s.c_str()[pos+1] == '%') { |
146 |
|
s.replace(pos, 2, "%"); |
147 |
|
continue; |
148 |
|
} |
149 |
|
if (pos+2 >= s.length()) { |
150 |
|
//TODO: we might want to throw an exception here, for now we simply replace the character by '?' |
151 |
|
s.replace(pos, 2, "?"); |
152 |
|
continue; |
153 |
|
} |
154 |
|
// expecting a "%HH" sequence here, convert it into the respective character |
155 |
|
const std::string sHex = s.substr(pos+1, 2); |
156 |
|
char cAscii = hexsToNumber(sHex.c_str()[1], sHex.c_str()[0]); |
157 |
|
char pcAscii[] = { cAscii, 0 }; |
158 |
|
s.replace(pos, 3, pcAscii); |
159 |
|
} |
160 |
|
} |
161 |
|
return result; |
162 |
|
} |
163 |
|
|
164 |
} // namespace LinuxSampler |
} // namespace LinuxSampler |