PDA

View Full Version : Interpreting escape characters entered by user, best method?



deano
13th September 2009, 12:29
Hi,
I have the situation where I need a user to be able to set a (some) characters to either separate fields or end a line in an input interpreter. Once I have a set of RegExp matches (QStringList), I then write them out. I have added options for the user to set how they want the fields separated and then the lines ended, the code to do it looks something like;



QByteArray result;
if(-1 != regExpMatcher.indexIn(regExpBuffer.constData()))
{
regExpBuffer.clear();
QStringList captures = regExpMatcher.capturedTexts();
int numberOfCaps = captures.size();
if(numberOfCaps > 1)
{
for(int i=1; i < numberOfCaps; i++)
{
result.append(captures.at(i));
if(i != (numberOfCaps-1))
result.append(seperator.toAscii());
}
}
else
result.append(captures.at(0));
result.append(lineEnd.toAscii());
}

My problem is, if a user wants to enter a TAB as the seperator '\t' or some variant of a newline '\r\n'. How do I easily parse their input? What I would LOVE is something similar to the way the RegExps are done, some one character escapes for the common ones \0x00 (or \0xxx) for others. Of course I could write a parser, but surely there is an easy 'QT way' for this?

Any ideas?

deano
14th September 2009, 02:24
Ok, so this may not be the 'best' method but it was easy enough and does the job as far as I am concerned.
Looking at the QByteArray there is a method 'fromPercentEncoding', I used it like



seperator = QByteArray::fromPercentEncoding(ui->yourInput->text().toAscii(), '\\');
lineEnd = QByteArray::fromPercentEncoding(ui->yourOtherInput->text().toAscii(), '\\');


Then to add it to output....



QByteArray result;
if(-1 != regExpMatcher.indexIn(regExpBuffer.constData()))
{
regExpBuffer.clear();
QStringList captures = regExpMatcher.capturedTexts();
int numberOfCaps = captures.size();
if(numberOfCaps > 1)
{
captures.removeAt(0);
result += captures.join(seperator.constData()).toAscii();
}
else
result.append(captures.at(0));
result.append(lineEnd);
}


I would still be interested if anyone has other ideas, but this seemed simple enough to me.