diff options
author | Oswald Buddenhagen <oswald.buddenhagen@nokia.com> | 2009-03-25 19:23:06 (GMT) |
---|---|---|
committer | Oswald Buddenhagen <oswald.buddenhagen@nokia.com> | 2009-03-27 11:08:30 (GMT) |
commit | ddc1ff555604543cc2a3b1b797ff61b67abb0fe4 (patch) | |
tree | 832eea7b56e6f74b4da18984f245630a2f34e2ca /tools/linguist/shared/qph.cpp | |
parent | b61933e6dfadd70655d1fad5344ba77da34587fc (diff) | |
download | Qt-ddc1ff555604543cc2a3b1b797ff61b67abb0fe4.zip Qt-ddc1ff555604543cc2a3b1b797ff61b67abb0fe4.tar.gz Qt-ddc1ff555604543cc2a3b1b797ff61b67abb0fe4.tar.bz2 |
proper encoding and escaping
Diffstat (limited to 'tools/linguist/shared/qph.cpp')
-rw-r--r-- | tools/linguist/shared/qph.cpp | 41 |
1 files 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 << "<!DOCTYPE QPH><QPH>\n"; + t.setCodec(QTextCodec::codecForName("UTF-8")); + t << "<!DOCTYPE QPH>\n<QPH>\n"; foreach (const TranslatorMessage &msg, translator.messages()) { t << "<phrase>\n"; - t << " <source>" << msg.sourceText() << "</source>\n"; - t << " <target>" << msg.translations().join(QLatin1String("@")) + t << " <source>" << protect(msg.sourceText()) << "</source>\n"; + t << " <target>" << protect(msg.translations().join(QLatin1String("@"))) << "</target>\n"; if (!msg.context().isEmpty() || !msg.comment().isEmpty()) t << " <definition>" << msg.context() << msg.comment() |