1 |
// qsamplerUtilities.cpp |
2 |
// |
3 |
/**************************************************************************** |
4 |
Copyright (C) 2004-2007, rncbc aka Rui Nuno Capela. All rights reserved. |
5 |
|
6 |
This program is free software; you can redistribute it and/or |
7 |
modify it under the terms of the GNU General Public License |
8 |
as published by the Free Software Foundation; either version 2 |
9 |
of the License, or (at your option) any later version. |
10 |
|
11 |
This program is distributed in the hope that it will be useful, |
12 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
GNU General Public License for more details. |
15 |
|
16 |
You should have received a copy of the GNU General Public License along |
17 |
with this program; if not, write to the Free Software Foundation, Inc., |
18 |
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
19 |
|
20 |
*****************************************************************************/ |
21 |
|
22 |
#include "qsamplerUtilities.h" |
23 |
|
24 |
#include "qsamplerMainForm.h" |
25 |
|
26 |
#include <stdio.h> |
27 |
#include <qregexp.h> |
28 |
|
29 |
// converts the given file path into a path as expected by LSCP 1.2 |
30 |
QString lscpEscapePath ( const QString& sPath ) |
31 |
{ |
32 |
QString path(sPath); |
33 |
|
34 |
// check if remote side supports LSCP escape sequences |
35 |
const lscpVersion_t version = getRemoteLscpVersion(); |
36 |
if (version.major < 1 || version.minor < 2) |
37 |
return path; // LSCP v1.2 or younger required |
38 |
|
39 |
// replace POSIX path escape sequences (%HH) by LSCP escape sequences (\xHH) |
40 |
// TODO: missing code for other systems like Windows |
41 |
{ |
42 |
QRegExp regexp("%[0-9a-fA-F][0-9a-fA-F]"); |
43 |
for (int i = path.find(regexp); i >= 0; i = path.find(regexp, i + 3)) |
44 |
path.replace(i, 1, "\\x"); |
45 |
} |
46 |
|
47 |
// replace all non-basic characters by LSCP escape sequences |
48 |
{ |
49 |
const char pathSeparator = '/'; |
50 |
QRegExp regexp(QRegExp::escape("\\x") + "[0-9a-fA-F][0-9a-fA-F]"); |
51 |
for (int i = 0; i < int(path.length()); i++) { |
52 |
// first skip all previously added LSCP escape sequences |
53 |
if (path.find(regexp, i) == i) { |
54 |
i += 3; |
55 |
continue; |
56 |
} |
57 |
// now match all non-alphanumerics |
58 |
// (we could exclude much more characters here, but that way |
59 |
// we're sure it just works^TM) |
60 |
const char c = path.at(i).latin1(); |
61 |
if ( |
62 |
!(c >= '0' && c <= '9') && |
63 |
!(c >= 'a' && c <= 'z') && |
64 |
!(c >= 'A' && c <= 'Z') && |
65 |
!(c == pathSeparator) |
66 |
) { |
67 |
// convert the non-basic character into a LSCP escape sequence |
68 |
char buf[5]; |
69 |
::snprintf(buf, sizeof(buf), "\\x%2x", static_cast<unsigned char>(c)); |
70 |
path.replace(i, 1, buf); |
71 |
i += 3; |
72 |
} |
73 |
} |
74 |
} |
75 |
|
76 |
return path; |
77 |
} |
78 |
|
79 |
|
80 |
lscpVersion_t getRemoteLscpVersion (void) |
81 |
{ |
82 |
lscpVersion_t result = { 0, 0 }; |
83 |
|
84 |
qsamplerMainForm *pMainForm = qsamplerMainForm::getInstance(); |
85 |
if (pMainForm == NULL) |
86 |
return result; |
87 |
if (pMainForm->client() == NULL) |
88 |
return result; |
89 |
|
90 |
lscp_server_info_t* pServerInfo = |
91 |
::lscp_get_server_info(pMainForm->client()); |
92 |
if (pServerInfo && pServerInfo->protocol_version) |
93 |
::sscanf(pServerInfo->protocol_version, "%d.%d", |
94 |
&result.major, &result.minor); |
95 |
|
96 |
return result; |
97 |
} |