--- qsampler/trunk/src/qsamplerUtilities.cpp 2007/11/11 23:07:06 1476 +++ qsampler/trunk/src/qsamplerUtilities.cpp 2008/02/04 23:24:19 1667 @@ -2,7 +2,7 @@ // /**************************************************************************** Copyright (C) 2004-2007, rncbc aka Rui Nuno Capela. All rights reserved. - Copyright (C) 2007, Christian Schoenebeck + 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 @@ -22,10 +22,11 @@ #include "qsamplerUtilities.h" +#include "qsamplerOptions.h" #include "qsamplerMainForm.h" -#include -#include +#include + using namespace QSampler; @@ -84,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 @@ -97,14 +98,14 @@ 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') && @@ -132,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) { @@ -159,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); }