Results 1 to 2 of 2

Thread: Interpreting escape characters entered by user, best method?

  1. #1
    Join Date
    Sep 2009
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Interpreting escape characters entered by user, best method?

    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;

    Qt Code:
    1. QByteArray result;
    2. if(-1 != regExpMatcher.indexIn(regExpBuffer.constData()))
    3. {
    4. regExpBuffer.clear();
    5. QStringList captures = regExpMatcher.capturedTexts();
    6. int numberOfCaps = captures.size();
    7. if(numberOfCaps > 1)
    8. {
    9. for(int i=1; i < numberOfCaps; i++)
    10. {
    11. result.append(captures.at(i));
    12. if(i != (numberOfCaps-1))
    13. result.append(seperator.toAscii());
    14. }
    15. }
    16. else
    17. result.append(captures.at(0));
    18. result.append(lineEnd.toAscii());
    19. }
    To copy to clipboard, switch view to plain text mode 
    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?

  2. #2
    Join Date
    Sep 2009
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Interpreting escape characters entered by user, best method?

    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

    Qt Code:
    1. seperator = QByteArray::fromPercentEncoding(ui->yourInput->text().toAscii(), '\\');
    2. lineEnd = QByteArray::fromPercentEncoding(ui->yourOtherInput->text().toAscii(), '\\');
    To copy to clipboard, switch view to plain text mode 

    Then to add it to output....

    Qt Code:
    1. QByteArray result;
    2. if(-1 != regExpMatcher.indexIn(regExpBuffer.constData()))
    3. {
    4. regExpBuffer.clear();
    5. QStringList captures = regExpMatcher.capturedTexts();
    6. int numberOfCaps = captures.size();
    7. if(numberOfCaps > 1)
    8. {
    9. captures.removeAt(0);
    10. result += captures.join(seperator.constData()).toAscii();
    11. }
    12. else
    13. result.append(captures.at(0));
    14. result.append(lineEnd);
    15. }
    To copy to clipboard, switch view to plain text mode 

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

Similar Threads

  1. Problem at time compilation in traslation of language
    By thomasjoy in forum Qt Programming
    Replies: 3
    Last Post: 22nd May 2007, 14:18

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.