26 |
#include <stdio.h> |
#include <stdio.h> |
27 |
#include <qregexp.h> |
#include <qregexp.h> |
28 |
|
|
29 |
|
namespace qsamplerUtilities { |
30 |
|
|
31 |
|
static int _hexToNumber(char hex_digit) { |
32 |
|
switch (hex_digit) { |
33 |
|
case '0': return 0; |
34 |
|
case '1': return 1; |
35 |
|
case '2': return 2; |
36 |
|
case '3': return 3; |
37 |
|
case '4': return 4; |
38 |
|
case '5': return 5; |
39 |
|
case '6': return 6; |
40 |
|
case '7': return 7; |
41 |
|
case '8': return 8; |
42 |
|
case '9': return 9; |
43 |
|
|
44 |
|
case 'a': return 10; |
45 |
|
case 'b': return 11; |
46 |
|
case 'c': return 12; |
47 |
|
case 'd': return 13; |
48 |
|
case 'e': return 14; |
49 |
|
case 'f': return 15; |
50 |
|
|
51 |
|
case 'A': return 10; |
52 |
|
case 'B': return 11; |
53 |
|
case 'C': return 12; |
54 |
|
case 'D': return 13; |
55 |
|
case 'E': return 14; |
56 |
|
case 'F': return 15; |
57 |
|
|
58 |
|
default: return 0; |
59 |
|
} |
60 |
|
} |
61 |
|
|
62 |
|
static int _hexsToNumber(char hex_digit0, char hex_digit1) { |
63 |
|
return _hexToNumber(hex_digit1)*16 + _hexToNumber(hex_digit0); |
64 |
|
} |
65 |
|
|
66 |
|
// returns true if the connected LSCP server supports escape sequences |
67 |
|
static bool _remoteSupportsEscapeSequences() { |
68 |
|
const lscpVersion_t version = getRemoteLscpVersion(); |
69 |
|
// LSCP v1.2 or younger required |
70 |
|
return (version.major > 1 || (version.major == 1 && version.minor >= 2)); |
71 |
|
} |
72 |
|
|
73 |
// converts the given file path into a path as expected by LSCP 1.2 |
// converts the given file path into a path as expected by LSCP 1.2 |
74 |
QString lscpEscapePath ( const QString& sPath ) |
QString lscpEscapePath ( const QString& sPath ) |
75 |
{ |
{ |
76 |
QString path(sPath); |
if (!_remoteSupportsEscapeSequences()) return sPath; |
77 |
|
|
78 |
// check if remote side supports LSCP escape sequences |
QString path(sPath); |
|
const lscpVersion_t version = getRemoteLscpVersion(); |
|
|
if (version.major < 1 || version.minor < 2) |
|
|
return path; // LSCP v1.2 or younger required |
|
79 |
|
|
80 |
// replace POSIX path escape sequences (%HH) by LSCP escape sequences (\xHH) |
// replace POSIX path escape sequences (%HH) by LSCP escape sequences (\xHH) |
81 |
// TODO: missing code for other systems like Windows |
// TODO: missing code for other systems like Windows |
82 |
{ |
{ |
83 |
QRegExp regexp("%[0-9a-fA-F][0-9a-fA-F]"); |
QRegExp regexp("%[0-9a-fA-F][0-9a-fA-F]"); |
84 |
for (int i = path.find(regexp); i >= 0; i = path.find(regexp, i + 3)) |
for (int i = path.find(regexp); i >= 0; i = path.find(regexp, i + 4)) |
85 |
path.replace(i, 1, "\\x"); |
path.replace(i, 1, "\\x"); |
86 |
} |
} |
87 |
|
// replace POSIX path escape sequence (%%) by its raw character |
88 |
|
for (int i = path.find("%%"); i >= 0; i = path.find("%%", ++i)) |
89 |
|
path.remove(i, 1); |
90 |
|
|
91 |
// replace all non-basic characters by LSCP escape sequences |
// replace all non-basic characters by LSCP escape sequences |
92 |
{ |
{ |
110 |
) { |
) { |
111 |
// convert the non-basic character into a LSCP escape sequence |
// convert the non-basic character into a LSCP escape sequence |
112 |
char buf[5]; |
char buf[5]; |
113 |
::snprintf(buf, sizeof(buf), "\\x%2x", static_cast<unsigned char>(c)); |
::snprintf(buf, sizeof(buf), "\\x%02x", static_cast<unsigned char>(c)); |
114 |
path.replace(i, 1, buf); |
path.replace(i, 1, buf); |
115 |
i += 3; |
i += 3; |
116 |
} |
} |
120 |
return path; |
return path; |
121 |
} |
} |
122 |
|
|
123 |
|
// converts a path returned by a LSCP command (and may contain escape |
124 |
|
// sequences) into the appropriate POSIX path |
125 |
|
QString lscpEscapedPathToPosix(QString path) { |
126 |
|
if (!_remoteSupportsEscapeSequences()) return path; |
127 |
|
|
128 |
|
// first escape all percent ('%') characters for POSIX |
129 |
|
for (int i = path.find('%'); i >= 0; i = path.find('%', i+2)) |
130 |
|
path.replace(i, 1, "%%"); |
131 |
|
|
132 |
|
// resolve LSCP hex escape sequences (\xHH) |
133 |
|
QRegExp regexp(QRegExp::escape("\\x") + "[0-9a-fA-F][0-9a-fA-F]"); |
134 |
|
for (int i = path.find(regexp); i >= 0; i = path.find(regexp, i + 4)) { |
135 |
|
const QString sHex = path.mid(i+2, 2).lower(); |
136 |
|
// the slash has to be escaped for POSIX as well |
137 |
|
if (sHex == "2f") { |
138 |
|
path.replace(i, 4, "%2f"); |
139 |
|
continue; |
140 |
|
} |
141 |
|
// all other characters we simply decode |
142 |
|
char cAscii = _hexsToNumber(sHex.at(1).latin1(), sHex.at(0).latin1()); |
143 |
|
path.replace(i, 4, cAscii); |
144 |
|
} |
145 |
|
|
146 |
|
return path; |
147 |
|
} |
148 |
|
|
149 |
|
// converts a text returned by a LSCP command and may contain escape |
150 |
|
// sequences) into raw text, that is with all escape sequences decoded |
151 |
|
QString lscpEscapedTextToRaw(QString txt) { |
152 |
|
if (!_remoteSupportsEscapeSequences()) return txt; |
153 |
|
|
154 |
|
// resolve LSCP hex escape sequences (\xHH) |
155 |
|
QRegExp regexp(QRegExp::escape("\\x") + "[0-9a-fA-F][0-9a-fA-F]"); |
156 |
|
for (int i = txt.find(regexp); i >= 0; i = txt.find(regexp, i + 4)) { |
157 |
|
const QString sHex = txt.mid(i+2, 2).lower(); |
158 |
|
// decode into raw ASCII character |
159 |
|
char cAscii = _hexsToNumber(sHex.at(1).latin1(), sHex.at(0).latin1()); |
160 |
|
txt.replace(i, 4, cAscii); |
161 |
|
} |
162 |
|
|
163 |
|
return txt; |
164 |
|
} |
165 |
|
|
166 |
lscpVersion_t getRemoteLscpVersion (void) |
lscpVersion_t getRemoteLscpVersion (void) |
167 |
{ |
{ |
169 |
|
|
170 |
qsamplerMainForm *pMainForm = qsamplerMainForm::getInstance(); |
qsamplerMainForm *pMainForm = qsamplerMainForm::getInstance(); |
171 |
if (pMainForm == NULL) |
if (pMainForm == NULL) |
172 |
return result; |
return result; |
173 |
if (pMainForm->client() == NULL) |
if (pMainForm->client() == NULL) |
174 |
return result; |
return result; |
175 |
|
|
176 |
lscp_server_info_t* pServerInfo = |
lscp_server_info_t* pServerInfo = |
177 |
::lscp_get_server_info(pMainForm->client()); |
::lscp_get_server_info(pMainForm->client()); |
178 |
if (pServerInfo && pServerInfo->protocol_version) |
if (pServerInfo && pServerInfo->protocol_version) |
179 |
::sscanf(pServerInfo->protocol_version, "%d.%d", |
::sscanf(pServerInfo->protocol_version, "%d.%d", |
180 |
&result.major, &result.minor); |
&result.major, &result.minor); |
181 |
|
|
182 |
return result; |
return result; |
183 |
} |
} |
184 |
|
|
185 |
|
} // namespace qsamplerUtilities |