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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1499 - (hide annotations) (download)
Tue Nov 20 16:48:04 2007 UTC (16 years, 4 months ago) by capela
File size: 6513 byte(s)
* Qt4 migration: one first step forward to kiss Qt3Support goodbye.

1 schoenebeck 1386 // qsamplerUtilities.cpp
2     //
3     /****************************************************************************
4     Copyright (C) 2004-2007, rncbc aka Rui Nuno Capela. All rights reserved.
5 capela 1464 Copyright (C) 2007, Christian Schoenebeck
6 schoenebeck 1386
7     This program is free software; you can redistribute it and/or
8     modify it under the terms of the GNU General Public License
9     as published by the Free Software Foundation; either version 2
10     of the License, or (at your option) any later version.
11    
12     This program is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15     GNU General Public License for more details.
16    
17     You should have received a copy of the GNU General Public License along
18     with this program; if not, write to the Free Software Foundation, Inc.,
19     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20    
21     *****************************************************************************/
22    
23     #include "qsamplerUtilities.h"
24    
25     #include "qsamplerMainForm.h"
26    
27 capela 1499 #include <QRegExp>
28 schoenebeck 1386
29 capela 1499
30 schoenebeck 1461 using namespace QSampler;
31    
32 schoenebeck 1402 namespace qsamplerUtilities {
33    
34     static int _hexToNumber(char hex_digit) {
35     switch (hex_digit) {
36     case '0': return 0;
37     case '1': return 1;
38     case '2': return 2;
39     case '3': return 3;
40     case '4': return 4;
41     case '5': return 5;
42     case '6': return 6;
43     case '7': return 7;
44     case '8': return 8;
45     case '9': return 9;
46    
47     case 'a': return 10;
48     case 'b': return 11;
49     case 'c': return 12;
50     case 'd': return 13;
51     case 'e': return 14;
52     case 'f': return 15;
53    
54     case 'A': return 10;
55     case 'B': return 11;
56     case 'C': return 12;
57     case 'D': return 13;
58     case 'E': return 14;
59     case 'F': return 15;
60    
61     default: return 0;
62     }
63     }
64    
65     static int _hexsToNumber(char hex_digit0, char hex_digit1) {
66     return _hexToNumber(hex_digit1)*16 + _hexToNumber(hex_digit0);
67     }
68    
69     // returns true if the connected LSCP server supports escape sequences
70     static bool _remoteSupportsEscapeSequences() {
71     const lscpVersion_t version = getRemoteLscpVersion();
72     // LSCP v1.2 or younger required
73     return (version.major > 1 || (version.major == 1 && version.minor >= 2));
74     }
75    
76 schoenebeck 1386 // converts the given file path into a path as expected by LSCP 1.2
77 capela 1394 QString lscpEscapePath ( const QString& sPath )
78     {
79 schoenebeck 1402 if (!_remoteSupportsEscapeSequences()) return sPath;
80 capela 1394
81 schoenebeck 1402 QString path(sPath);
82 schoenebeck 1386
83     // replace POSIX path escape sequences (%HH) by LSCP escape sequences (\xHH)
84     // TODO: missing code for other systems like Windows
85     {
86     QRegExp regexp("%[0-9a-fA-F][0-9a-fA-F]");
87 capela 1499 for (int i = path.indexOf(regexp); i >= 0; i = path.indexOf(regexp, i + 4))
88 schoenebeck 1386 path.replace(i, 1, "\\x");
89     }
90 schoenebeck 1402 // replace POSIX path escape sequence (%%) by its raw character
91 capela 1499 for (int i = path.indexOf("%%"); i >= 0; i = path.indexOf("%%", ++i))
92 schoenebeck 1402 path.remove(i, 1);
93 capela 1394
94 schoenebeck 1386 // replace all non-basic characters by LSCP escape sequences
95     {
96     const char pathSeparator = '/';
97     QRegExp regexp(QRegExp::escape("\\x") + "[0-9a-fA-F][0-9a-fA-F]");
98     for (int i = 0; i < int(path.length()); i++) {
99     // first skip all previously added LSCP escape sequences
100 capela 1499 if (path.indexOf(regexp, i) == i) {
101 schoenebeck 1386 i += 3;
102     continue;
103     }
104     // now match all non-alphanumerics
105     // (we could exclude much more characters here, but that way
106     // we're sure it just works^TM)
107 capela 1499 const char c = path.at(i).toLatin1();
108 schoenebeck 1386 if (
109     !(c >= '0' && c <= '9') &&
110     !(c >= 'a' && c <= 'z') &&
111     !(c >= 'A' && c <= 'Z') &&
112 senoner 1476 #if defined(WIN32)
113     !(c == ':') &&
114     #endif
115 schoenebeck 1386 !(c == pathSeparator)
116     ) {
117     // convert the non-basic character into a LSCP escape sequence
118     char buf[5];
119 schoenebeck 1402 ::snprintf(buf, sizeof(buf), "\\x%02x", static_cast<unsigned char>(c));
120 schoenebeck 1386 path.replace(i, 1, buf);
121     i += 3;
122     }
123     }
124     }
125 capela 1394
126 schoenebeck 1386 return path;
127     }
128    
129 schoenebeck 1402 // converts a path returned by a LSCP command (and may contain escape
130     // sequences) into the appropriate POSIX path
131     QString lscpEscapedPathToPosix(QString path) {
132     if (!_remoteSupportsEscapeSequences()) return path;
133 capela 1394
134 schoenebeck 1402 // first escape all percent ('%') characters for POSIX
135 capela 1499 for (int i = path.indexOf('%'); i >= 0; i = path.indexOf('%', i+2))
136 schoenebeck 1402 path.replace(i, 1, "%%");
137    
138     // resolve LSCP hex escape sequences (\xHH)
139     QRegExp regexp(QRegExp::escape("\\x") + "[0-9a-fA-F][0-9a-fA-F]");
140 capela 1499 for (int i = path.indexOf(regexp); i >= 0; i = path.indexOf(regexp, i + 4)) {
141     const QString sHex = path.mid(i+2, 2).toLower();
142 schoenebeck 1402 // the slash has to be escaped for POSIX as well
143     if (sHex == "2f") {
144     path.replace(i, 4, "%2f");
145     continue;
146     }
147     // all other characters we simply decode
148 capela 1499 char cAscii = _hexsToNumber(sHex.at(1).toLatin1(), sHex.at(0).toLatin1());
149 schoenebeck 1402 path.replace(i, 4, cAscii);
150     }
151    
152     return path;
153     }
154    
155     // converts a text returned by a LSCP command and may contain escape
156     // sequences) into raw text, that is with all escape sequences decoded
157     QString lscpEscapedTextToRaw(QString txt) {
158     if (!_remoteSupportsEscapeSequences()) return txt;
159    
160     // resolve LSCP hex escape sequences (\xHH)
161     QRegExp regexp(QRegExp::escape("\\x") + "[0-9a-fA-F][0-9a-fA-F]");
162 capela 1499 for (int i = txt.indexOf(regexp); i >= 0; i = txt.indexOf(regexp, i + 4)) {
163     const QString sHex = txt.mid(i+2, 2).toLower();
164 schoenebeck 1402 // decode into raw ASCII character
165 capela 1499 char cAscii = _hexsToNumber(sHex.at(1).toLatin1(), sHex.at(0).toLatin1());
166 schoenebeck 1402 txt.replace(i, 4, cAscii);
167     }
168    
169     return txt;
170     }
171    
172 capela 1394 lscpVersion_t getRemoteLscpVersion (void)
173     {
174 schoenebeck 1386 lscpVersion_t result = { 0, 0 };
175    
176 schoenebeck 1461 MainForm* pMainForm = MainForm::getInstance();
177 capela 1394 if (pMainForm == NULL)
178 schoenebeck 1402 return result;
179     if (pMainForm->client() == NULL)
180     return result;
181 schoenebeck 1386
182     lscp_server_info_t* pServerInfo =
183     ::lscp_get_server_info(pMainForm->client());
184 capela 1394 if (pServerInfo && pServerInfo->protocol_version)
185 schoenebeck 1402 ::sscanf(pServerInfo->protocol_version, "%d.%d",
186     &result.major, &result.minor);
187 schoenebeck 1386
188     return result;
189     }
190 schoenebeck 1402
191     } // namespace qsamplerUtilities
192 capela 1464
193    
194     // end of qsamplerUtilities.cpp

  ViewVC Help
Powered by ViewVC