/[svn]/qsampler/trunk/src/qsamplerUtilities.cpp
ViewVC logotype

Contents of /qsampler/trunk/src/qsamplerUtilities.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1386 - (show annotations) (download)
Fri Oct 5 17:41:49 2007 UTC (16 years, 6 months ago) by schoenebeck
File size: 3301 byte(s)
* Added support for LSCP escape sequences to allow loading and
  mapping instrument files with special characters in their
  filename (fixes #47).

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(QString path) {
31 // check if remote side supports LSCP escape sequences
32 const lscpVersion_t version = getRemoteLscpVersion();
33 if (version.major < 1 || version.minor < 2)
34 return path; // LSCP v1.2 or younger required
35
36 // replace POSIX path escape sequences (%HH) by LSCP escape sequences (\xHH)
37 // TODO: missing code for other systems like Windows
38 {
39 QRegExp regexp("%[0-9a-fA-F][0-9a-fA-F]");
40 for (int i = path.find(regexp); i >= 0; i = path.find(regexp, i + 3))
41 path.replace(i, 1, "\\x");
42 }
43 // replace all non-basic characters by LSCP escape sequences
44 {
45 const char pathSeparator = '/';
46 QRegExp regexp(QRegExp::escape("\\x") + "[0-9a-fA-F][0-9a-fA-F]");
47 for (int i = 0; i < int(path.length()); i++) {
48 // first skip all previously added LSCP escape sequences
49 if (path.find(regexp, i) == i) {
50 i += 3;
51 continue;
52 }
53 // now match all non-alphanumerics
54 // (we could exclude much more characters here, but that way
55 // we're sure it just works^TM)
56 const char c = path.at(i).latin1();
57 if (
58 !(c >= '0' && c <= '9') &&
59 !(c >= 'a' && c <= 'z') &&
60 !(c >= 'A' && c <= 'Z') &&
61 !(c == pathSeparator)
62 ) {
63 // convert the non-basic character into a LSCP escape sequence
64 char buf[5];
65 snprintf(buf, sizeof(buf), "\\x%2x", static_cast<unsigned char>(c));
66 path.replace(i, 1, buf);
67 i += 3;
68 }
69 }
70 }
71 return path;
72 }
73
74 lscpVersion_t getRemoteLscpVersion() {
75 lscpVersion_t result = { 0, 0 };
76
77 qsamplerMainForm *pMainForm = qsamplerMainForm::getInstance();
78 if (!pMainForm || !pMainForm->client()) return result;
79
80 lscp_server_info_t* pServerInfo =
81 ::lscp_get_server_info(pMainForm->client());
82 if (!pServerInfo) return result;
83
84 sscanf(pServerInfo->protocol_version, "%d.%d", &result.major, &result.minor);
85 return result;
86 }

  ViewVC Help
Powered by ViewVC