21 #include <QSharedPointer> 22 #include <QStringList> 23 #include <QXmlStreamReader> 24 #include <QXmlStreamWriter> 42 QXmlStreamReader xmlReader(&device);
46 while (!xmlReader.atEnd()) {
49 if (xmlReader.isStartElement())
50 groups.append(xmlReader.name().toString());
51 else if (xmlReader.isCharacters() && !xmlReader.isWhitespace())
52 map[groups.join(
"/")] = xmlReader.text().toString();
53 else if (xmlReader.isEndElement())
57 return !xmlReader.hasError();
62 struct NestedMap : QMap<QString, QSharedPointer<NestedMap> > {};
64 QSharedPointer<NestedMap> nestedMap(
new NestedMap());
66 for (QSettings::SettingsMap::const_iterator it= map.begin();
67 it != map.end(); ++it) {
68 QSharedPointer<NestedMap> currentMap = nestedMap;
70 QStringList groups = it.key().split(
"/");
72 for (QStringList::const_iterator jt = groups.begin();
73 jt != groups.end(); ++jt) {
74 NestedMap::iterator kt = currentMap->find(*jt);
76 if (kt == currentMap->end()) {
77 kt = currentMap->insert(*jt, QSharedPointer<NestedMap>(
79 currentMap = kt.value();
82 currentMap = kt.value();
86 QXmlStreamWriter xmlWriter(&device);
88 xmlWriter.setAutoFormatting(
true);
89 xmlWriter.writeStartDocument();
92 QList<QSharedPointer<NestedMap> > nestedMaps;
93 QList<NestedMap::iterator> nestedMapIterators;
95 nestedMaps.append(nestedMap);
96 nestedMapIterators.append(nestedMap->begin());
98 while (!nestedMaps.isEmpty()) {
99 QSharedPointer<NestedMap> currentMap = nestedMaps.last();
100 NestedMap::iterator it = nestedMapIterators.last();
102 if (it != currentMap->end()) {
103 xmlWriter.writeStartElement(it.key());
105 groups.append(it.key());
106 nestedMaps.append(it.value());
107 nestedMapIterators.append(it.value()->begin());
110 if (currentMap->isEmpty())
111 xmlWriter.writeCharacters(map[groups.join(
"/")].toString());
113 xmlWriter.writeEndElement();
115 if (!groups.isEmpty())
117 nestedMaps.removeLast();
118 nestedMapIterators.removeLast();
120 if (!nestedMaps.isEmpty())
121 ++nestedMapIterators.last();
125 xmlWriter.writeEndDocument();
static bool write(QIODevice &device, const QSettings::SettingsMap &map)
static const QSettings::Format format
static bool read(QIODevice &device, QSettings::SettingsMap &map)