2 |
// |
// |
3 |
/**************************************************************************** |
/**************************************************************************** |
4 |
Copyright (C) 2004-2007, rncbc aka Rui Nuno Capela. All rights reserved. |
Copyright (C) 2004-2007, rncbc aka Rui Nuno Capela. All rights reserved. |
5 |
Copyright (C) 2007, Christian Schoenebeck |
Copyright (C) 2007, 2008 Christian Schoenebeck |
6 |
|
|
7 |
This program is free software; you can redistribute it and/or |
This program is free software; you can redistribute it and/or |
8 |
modify it under the terms of the GNU General Public License |
modify it under the terms of the GNU General Public License |
22 |
|
|
23 |
#include "qsamplerUtilities.h" |
#include "qsamplerUtilities.h" |
24 |
|
|
25 |
|
#include "qsamplerOptions.h" |
26 |
#include "qsamplerMainForm.h" |
#include "qsamplerMainForm.h" |
27 |
|
|
28 |
#include <stdio.h> |
#include <QRegExp> |
29 |
#include <qregexp.h> |
|
30 |
|
|
31 |
using namespace QSampler; |
using namespace QSampler; |
32 |
|
|
85 |
// TODO: missing code for other systems like Windows |
// TODO: missing code for other systems like Windows |
86 |
{ |
{ |
87 |
QRegExp regexp("%[0-9a-fA-F][0-9a-fA-F]"); |
QRegExp regexp("%[0-9a-fA-F][0-9a-fA-F]"); |
88 |
for (int i = path.find(regexp); i >= 0; i = path.find(regexp, i + 4)) |
for (int i = path.indexOf(regexp); i >= 0; i = path.indexOf(regexp, i + 4)) |
89 |
path.replace(i, 1, "\\x"); |
path.replace(i, 1, "\\x"); |
90 |
} |
} |
91 |
// replace POSIX path escape sequence (%%) by its raw character |
// replace POSIX path escape sequence (%%) by its raw character |
92 |
for (int i = path.find("%%"); i >= 0; i = path.find("%%", ++i)) |
for (int i = path.indexOf("%%"); i >= 0; i = path.indexOf("%%", ++i)) |
93 |
path.remove(i, 1); |
path.remove(i, 1); |
94 |
|
|
95 |
// replace all non-basic characters by LSCP escape sequences |
// replace all non-basic characters by LSCP escape sequences |
98 |
QRegExp regexp(QRegExp::escape("\\x") + "[0-9a-fA-F][0-9a-fA-F]"); |
QRegExp regexp(QRegExp::escape("\\x") + "[0-9a-fA-F][0-9a-fA-F]"); |
99 |
for (int i = 0; i < int(path.length()); i++) { |
for (int i = 0; i < int(path.length()); i++) { |
100 |
// first skip all previously added LSCP escape sequences |
// first skip all previously added LSCP escape sequences |
101 |
if (path.find(regexp, i) == i) { |
if (path.indexOf(regexp, i) == i) { |
102 |
i += 3; |
i += 3; |
103 |
continue; |
continue; |
104 |
} |
} |
105 |
// now match all non-alphanumerics |
// now match all non-alphanumerics |
106 |
// (we could exclude much more characters here, but that way |
// (we could exclude much more characters here, but that way |
107 |
// we're sure it just works^TM) |
// we're sure it just works^TM) |
108 |
const char c = path.at(i).latin1(); |
const char c = path.at(i).toLatin1(); |
109 |
if ( |
if ( |
110 |
!(c >= '0' && c <= '9') && |
!(c >= '0' && c <= '9') && |
111 |
!(c >= 'a' && c <= 'z') && |
!(c >= 'a' && c <= 'z') && |
133 |
if (!_remoteSupportsEscapeSequences()) return path; |
if (!_remoteSupportsEscapeSequences()) return path; |
134 |
|
|
135 |
// first escape all percent ('%') characters for POSIX |
// first escape all percent ('%') characters for POSIX |
136 |
for (int i = path.find('%'); i >= 0; i = path.find('%', i+2)) |
for (int i = path.indexOf('%'); i >= 0; i = path.indexOf('%', i+2)) |
137 |
path.replace(i, 1, "%%"); |
path.replace(i, 1, "%%"); |
138 |
|
|
139 |
// resolve LSCP hex escape sequences (\xHH) |
// resolve LSCP hex escape sequences (\xHH) |
140 |
QRegExp regexp(QRegExp::escape("\\x") + "[0-9a-fA-F][0-9a-fA-F]"); |
QRegExp regexp(QRegExp::escape("\\x") + "[0-9a-fA-F][0-9a-fA-F]"); |
141 |
for (int i = path.find(regexp); i >= 0; i = path.find(regexp, i + 4)) { |
for (int i = path.indexOf(regexp); i >= 0; i = path.indexOf(regexp, i + 4)) { |
142 |
const QString sHex = path.mid(i+2, 2).lower(); |
const QString sHex = path.mid(i+2, 2).toLower(); |
143 |
// the slash has to be escaped for POSIX as well |
// the slash has to be escaped for POSIX as well |
144 |
if (sHex == "2f") { |
if (sHex == "2f") { |
145 |
path.replace(i, 4, "%2f"); |
path.replace(i, 4, "%2f"); |
146 |
continue; |
continue; |
147 |
} |
} |
148 |
// all other characters we simply decode |
// all other characters we simply decode |
149 |
char cAscii = _hexsToNumber(sHex.at(1).latin1(), sHex.at(0).latin1()); |
char cAscii = _hexsToNumber(sHex.at(1).toLatin1(), sHex.at(0).toLatin1()); |
150 |
path.replace(i, 4, cAscii); |
path.replace(i, 4, cAscii); |
151 |
} |
} |
152 |
|
|
153 |
return path; |
return path; |
154 |
} |
} |
155 |
|
|
156 |
|
// converts the given text as expected by LSCP 1.2 |
157 |
|
// (that is by encoding special characters with LSCP escape sequences) |
158 |
|
QString lscpEscapeText(const QString& txt) { |
159 |
|
if (!_remoteSupportsEscapeSequences()) return txt; |
160 |
|
|
161 |
|
QString text(txt); |
162 |
|
|
163 |
|
// replace all non-basic characters by LSCP escape sequences |
164 |
|
for (int i = 0; i < int(text.length()); ++i) { |
165 |
|
// match all non-alphanumerics |
166 |
|
// (we could exclude much more characters here, but that way |
167 |
|
// we're sure it just works^TM) |
168 |
|
const char c = text.at(i).toLatin1(); |
169 |
|
if ( |
170 |
|
!(c >= '0' && c <= '9') && |
171 |
|
!(c >= 'a' && c <= 'z') && |
172 |
|
!(c >= 'A' && c <= 'Z') |
173 |
|
) { |
174 |
|
// convert the non-basic character into a LSCP escape sequence |
175 |
|
char buf[5]; |
176 |
|
::snprintf(buf, sizeof(buf), "\\x%02x", static_cast<unsigned char>(c)); |
177 |
|
text.replace(i, 1, buf); |
178 |
|
i += 3; |
179 |
|
} |
180 |
|
} |
181 |
|
|
182 |
|
return text; |
183 |
|
} |
184 |
|
|
185 |
// converts a text returned by a LSCP command and may contain escape |
// converts a text returned by a LSCP command and may contain escape |
186 |
// sequences) into raw text, that is with all escape sequences decoded |
// sequences) into raw text, that is with all escape sequences decoded |
187 |
QString lscpEscapedTextToRaw(QString txt) { |
QString lscpEscapedTextToRaw(QString txt) { |
189 |
|
|
190 |
// resolve LSCP hex escape sequences (\xHH) |
// resolve LSCP hex escape sequences (\xHH) |
191 |
QRegExp regexp(QRegExp::escape("\\x") + "[0-9a-fA-F][0-9a-fA-F]"); |
QRegExp regexp(QRegExp::escape("\\x") + "[0-9a-fA-F][0-9a-fA-F]"); |
192 |
for (int i = txt.find(regexp); i >= 0; i = txt.find(regexp, i + 4)) { |
for (int i = txt.indexOf(regexp); i >= 0; i = txt.indexOf(regexp, i + 4)) { |
193 |
const QString sHex = txt.mid(i+2, 2).lower(); |
const QString sHex = txt.mid(i+2, 2).toLower(); |
194 |
// decode into raw ASCII character |
// decode into raw ASCII character |
195 |
char cAscii = _hexsToNumber(sHex.at(1).latin1(), sHex.at(0).latin1()); |
char cAscii = _hexsToNumber(sHex.at(1).toLatin1(), sHex.at(0).toLatin1()); |
196 |
txt.replace(i, 4, cAscii); |
txt.replace(i, 4, cAscii); |
197 |
} |
} |
198 |
|
|