summaryrefslogtreecommitdiffstats
path: root/tools
diff options
context:
space:
mode:
authorOswald Buddenhagen <oswald.buddenhagen@nokia.com>2009-03-25 19:23:06 (GMT)
committerOswald Buddenhagen <oswald.buddenhagen@nokia.com>2009-03-27 11:08:30 (GMT)
commitddc1ff555604543cc2a3b1b797ff61b67abb0fe4 (patch)
tree832eea7b56e6f74b4da18984f245630a2f34e2ca /tools
parentb61933e6dfadd70655d1fad5344ba77da34587fc (diff)
downloadQt-ddc1ff555604543cc2a3b1b797ff61b67abb0fe4.zip
Qt-ddc1ff555604543cc2a3b1b797ff61b67abb0fe4.tar.gz
Qt-ddc1ff555604543cc2a3b1b797ff61b67abb0fe4.tar.bz2
proper encoding and escaping
Diffstat (limited to 'tools')
-rw-r--r--tools/linguist/shared/qph.cpp41
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("&quot;");
+ break;
+ case '&':
+ result += QLatin1String("&amp;");
+ break;
+ case '>':
+ result += QLatin1String("&gt;");
+ break;
+ case '<':
+ result += QLatin1String("&lt;");
+ break;
+ case '\'':
+ result += QLatin1String("&apos;");
+ 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()