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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1394 by capela, Mon Oct 8 09:37:39 2007 UTC revision 1464 by capela, Thu Nov 1 17:14:21 2007 UTC
# Line 2  Line 2 
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
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
# Line 26  Line 27 
27  #include <stdio.h>  #include <stdio.h>
28  #include <qregexp.h>  #include <qregexp.h>
29    
30    using namespace QSampler;
31    
32    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  // 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
77  QString lscpEscapePath ( const QString& sPath )  QString lscpEscapePath ( const QString& sPath )
78  {  {
79          QString path(sPath);      if (!_remoteSupportsEscapeSequences()) return sPath;
80    
81      // 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  
82    
83      // replace POSIX path escape sequences (%HH) by LSCP escape sequences (\xHH)      // replace POSIX path escape sequences (%HH) by LSCP escape sequences (\xHH)
84      // TODO: missing code for other systems like Windows      // TODO: missing code for other systems like Windows
85      {      {
86          QRegExp regexp("%[0-9a-fA-F][0-9a-fA-F]");          QRegExp regexp("%[0-9a-fA-F][0-9a-fA-F]");
87          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))
88              path.replace(i, 1, "\\x");              path.replace(i, 1, "\\x");
89      }      }
90        // replace POSIX path escape sequence (%%) by its raw character
91        for (int i = path.find("%%"); i >= 0; i = path.find("%%", ++i))
92            path.remove(i, 1);
93    
94      // replace all non-basic characters by LSCP escape sequences      // replace all non-basic characters by LSCP escape sequences
95      {      {
# Line 66  QString lscpEscapePath ( const QString& Line 113  QString lscpEscapePath ( const QString&
113              ) {              ) {
114                  // convert the non-basic character into a LSCP escape sequence                  // convert the non-basic character into a LSCP escape sequence
115                  char buf[5];                  char buf[5];
116                  ::snprintf(buf, sizeof(buf), "\\x%2x", static_cast<unsigned char>(c));                  ::snprintf(buf, sizeof(buf), "\\x%02x", static_cast<unsigned char>(c));
117                  path.replace(i, 1, buf);                  path.replace(i, 1, buf);
118                  i += 3;                  i += 3;
119              }              }
# Line 76  QString lscpEscapePath ( const QString& Line 123  QString lscpEscapePath ( const QString&
123      return path;      return path;
124  }  }
125    
126    // converts a path returned by a LSCP command (and may contain escape
127    // sequences) into the appropriate POSIX path
128    QString lscpEscapedPathToPosix(QString path) {
129        if (!_remoteSupportsEscapeSequences()) return path;
130    
131        // first escape all percent ('%') characters for POSIX
132        for (int i = path.find('%'); i >= 0; i = path.find('%', i+2))
133            path.replace(i, 1, "%%");
134    
135        // resolve LSCP hex escape sequences (\xHH)
136        QRegExp regexp(QRegExp::escape("\\x") + "[0-9a-fA-F][0-9a-fA-F]");
137        for (int i = path.find(regexp); i >= 0; i = path.find(regexp, i + 4)) {
138            const QString sHex = path.mid(i+2, 2).lower();
139            // the slash has to be escaped for POSIX as well
140            if (sHex == "2f") {
141                path.replace(i, 4, "%2f");
142                continue;
143            }
144            // all other characters we simply decode
145            char cAscii = _hexsToNumber(sHex.at(1).latin1(), sHex.at(0).latin1());
146            path.replace(i, 4, cAscii);
147        }
148    
149        return path;
150    }
151    
152    // converts a text returned by a LSCP command and may contain escape
153    // sequences) into raw text, that is with all escape sequences decoded
154    QString lscpEscapedTextToRaw(QString txt) {
155        if (!_remoteSupportsEscapeSequences()) return txt;
156    
157        // resolve LSCP hex escape sequences (\xHH)
158        QRegExp regexp(QRegExp::escape("\\x") + "[0-9a-fA-F][0-9a-fA-F]");
159        for (int i = txt.find(regexp); i >= 0; i = txt.find(regexp, i + 4)) {
160            const QString sHex = txt.mid(i+2, 2).lower();
161            // decode into raw ASCII character
162            char cAscii = _hexsToNumber(sHex.at(1).latin1(), sHex.at(0).latin1());
163            txt.replace(i, 4, cAscii);
164        }
165    
166        return txt;
167    }
168    
169  lscpVersion_t getRemoteLscpVersion (void)  lscpVersion_t getRemoteLscpVersion (void)
170  {  {
171      lscpVersion_t result = { 0, 0 };      lscpVersion_t result = { 0, 0 };
172    
173      qsamplerMainForm *pMainForm = qsamplerMainForm::getInstance();      MainForm* pMainForm = MainForm::getInstance();
174      if (pMainForm == NULL)      if (pMainForm == NULL)
175                  return result;          return result;
176          if (pMainForm->client() == NULL)      if (pMainForm->client() == NULL)
177                  return result;          return result;
178    
179      lscp_server_info_t* pServerInfo =      lscp_server_info_t* pServerInfo =
180          ::lscp_get_server_info(pMainForm->client());          ::lscp_get_server_info(pMainForm->client());
181      if (pServerInfo && pServerInfo->protocol_version)      if (pServerInfo && pServerInfo->protocol_version)
182                  ::sscanf(pServerInfo->protocol_version, "%d.%d",          ::sscanf(pServerInfo->protocol_version, "%d.%d",
183                          &result.major, &result.minor);              &result.major, &result.minor);
184    
185      return result;      return result;
186  }  }
187    
188    } // namespace qsamplerUtilities
189    
190    
191    // end of qsamplerUtilities.cpp

Legend:
Removed from v.1394  
changed lines
  Added in v.1464

  ViewVC Help
Powered by ViewVC