--- qsampler/trunk/src/qsamplerUtilities.cpp 2007/10/12 00:03:27 1402 +++ qsampler/trunk/src/qsamplerUtilities.cpp 2008/02/04 23:24:19 1667 @@ -2,6 +2,7 @@ // /**************************************************************************** Copyright (C) 2004-2007, rncbc aka Rui Nuno Capela. All rights reserved. + Copyright (C) 2007, 2008 Christian Schoenebeck This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License @@ -21,10 +22,13 @@ #include "qsamplerUtilities.h" +#include "qsamplerOptions.h" #include "qsamplerMainForm.h" -#include -#include +#include + + +using namespace QSampler; namespace qsamplerUtilities { @@ -81,11 +85,11 @@ // TODO: missing code for other systems like Windows { QRegExp regexp("%[0-9a-fA-F][0-9a-fA-F]"); - 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)) path.replace(i, 1, "\\x"); } // replace POSIX path escape sequence (%%) by its raw character - for (int i = path.find("%%"); i >= 0; i = path.find("%%", ++i)) + for (int i = path.indexOf("%%"); i >= 0; i = path.indexOf("%%", ++i)) path.remove(i, 1); // replace all non-basic characters by LSCP escape sequences @@ -94,18 +98,21 @@ QRegExp regexp(QRegExp::escape("\\x") + "[0-9a-fA-F][0-9a-fA-F]"); for (int i = 0; i < int(path.length()); i++) { // first skip all previously added LSCP escape sequences - if (path.find(regexp, i) == i) { + if (path.indexOf(regexp, i) == i) { i += 3; continue; } // now match all non-alphanumerics // (we could exclude much more characters here, but that way // we're sure it just works^TM) - const char c = path.at(i).latin1(); + const char c = path.at(i).toLatin1(); if ( !(c >= '0' && c <= '9') && !(c >= 'a' && c <= 'z') && !(c >= 'A' && c <= 'Z') && + #if defined(WIN32) + !(c == ':') && + #endif !(c == pathSeparator) ) { // convert the non-basic character into a LSCP escape sequence @@ -126,26 +133,55 @@ if (!_remoteSupportsEscapeSequences()) return path; // first escape all percent ('%') characters for POSIX - for (int i = path.find('%'); i >= 0; i = path.find('%', i+2)) + for (int i = path.indexOf('%'); i >= 0; i = path.indexOf('%', i+2)) path.replace(i, 1, "%%"); // resolve LSCP hex escape sequences (\xHH) QRegExp regexp(QRegExp::escape("\\x") + "[0-9a-fA-F][0-9a-fA-F]"); - for (int i = path.find(regexp); i >= 0; i = path.find(regexp, i + 4)) { - const QString sHex = path.mid(i+2, 2).lower(); + for (int i = path.indexOf(regexp); i >= 0; i = path.indexOf(regexp, i + 4)) { + const QString sHex = path.mid(i+2, 2).toLower(); // the slash has to be escaped for POSIX as well if (sHex == "2f") { path.replace(i, 4, "%2f"); continue; } // all other characters we simply decode - char cAscii = _hexsToNumber(sHex.at(1).latin1(), sHex.at(0).latin1()); + char cAscii = _hexsToNumber(sHex.at(1).toLatin1(), sHex.at(0).toLatin1()); path.replace(i, 4, cAscii); } return path; } +// converts the given text as expected by LSCP 1.2 +// (that is by encoding special characters with LSCP escape sequences) +QString lscpEscapeText(const QString& txt) { + if (!_remoteSupportsEscapeSequences()) return txt; + + QString text(txt); + + // replace all non-basic characters by LSCP escape sequences + for (int i = 0; i < int(text.length()); ++i) { + // match all non-alphanumerics + // (we could exclude much more characters here, but that way + // we're sure it just works^TM) + const char c = text.at(i).toLatin1(); + if ( + !(c >= '0' && c <= '9') && + !(c >= 'a' && c <= 'z') && + !(c >= 'A' && c <= 'Z') + ) { + // convert the non-basic character into a LSCP escape sequence + char buf[5]; + ::snprintf(buf, sizeof(buf), "\\x%02x", static_cast(c)); + text.replace(i, 1, buf); + i += 3; + } + } + + return text; +} + // converts a text returned by a LSCP command and may contain escape // sequences) into raw text, that is with all escape sequences decoded QString lscpEscapedTextToRaw(QString txt) { @@ -153,10 +189,10 @@ // resolve LSCP hex escape sequences (\xHH) QRegExp regexp(QRegExp::escape("\\x") + "[0-9a-fA-F][0-9a-fA-F]"); - for (int i = txt.find(regexp); i >= 0; i = txt.find(regexp, i + 4)) { - const QString sHex = txt.mid(i+2, 2).lower(); + for (int i = txt.indexOf(regexp); i >= 0; i = txt.indexOf(regexp, i + 4)) { + const QString sHex = txt.mid(i+2, 2).toLower(); // decode into raw ASCII character - char cAscii = _hexsToNumber(sHex.at(1).latin1(), sHex.at(0).latin1()); + char cAscii = _hexsToNumber(sHex.at(1).toLatin1(), sHex.at(0).toLatin1()); txt.replace(i, 4, cAscii); } @@ -167,7 +203,7 @@ { lscpVersion_t result = { 0, 0 }; - qsamplerMainForm *pMainForm = qsamplerMainForm::getInstance(); + MainForm* pMainForm = MainForm::getInstance(); if (pMainForm == NULL) return result; if (pMainForm->client() == NULL) @@ -183,3 +219,6 @@ } } // namespace qsamplerUtilities + + +// end of qsamplerUtilities.cpp