From ddc1ff555604543cc2a3b1b797ff61b67abb0fe4 Mon Sep 17 00:00:00 2001 From: Oswald Buddenhagen Date: Wed, 25 Mar 2009 20:23:06 +0100 Subject: proper encoding and escaping --- tools/linguist/shared/qph.cpp | 41 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/tools/linguist/shared/qph.cpp b/tools/linguist/shared/qph.cpp index 18f1634..799bf7d 100644 --- a/tools/linguist/shared/qph.cpp +++ b/tools/linguist/shared/qph.cpp @@ -133,14 +133,47 @@ static bool loadQPH(Translator &translator, QIODevice &dev, ConversionData &cd) return reader.read(translator); } -static bool saveQPH(const Translator &translator, QIODevice &dev, ConversionData &cd) +static QString protect(const QString &str) +{ + QString result; + result.reserve(str.length() * 12 / 10); + for (int i = 0; i != str.size(); ++i) { + uint c = str.at(i).unicode(); + switch (c) { + case '\"': + result += QLatin1String("""); + break; + case '&': + result += QLatin1String("&"); + break; + case '>': + result += QLatin1String(">"); + break; + case '<': + result += QLatin1String("<"); + break; + case '\'': + result += QLatin1String("'"); + break; + default: + if (c < 0x20 && c != '\r' && c != '\n' && c != '\t') + result += QString(QLatin1String("&#%1;")).arg(c); + else // this also covers surrogates + result += QChar(c); + } + } + return result; +} + +static bool saveQPH(const Translator &translator, QIODevice &dev, ConversionData &) { QTextStream t(&dev); - t << "\n"; + t.setCodec(QTextCodec::codecForName("UTF-8")); + t << "\n\n"; foreach (const TranslatorMessage &msg, translator.messages()) { t << "\n"; - t << " " << msg.sourceText() << "\n"; - t << " " << msg.translations().join(QLatin1String("@")) + t << " " << protect(msg.sourceText()) << "\n"; + t << " " << protect(msg.translations().join(QLatin1String("@"))) << "\n"; if (!msg.context().isEmpty() || !msg.comment().isEmpty()) t << " " << msg.context() << msg.comment() -- cgit v0.12