Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include <QMap>
00020 #include <QList>
00021 #include <QSharedPointer>
00022 #include <QStringList>
00023 #include <QXmlStreamReader>
00024 #include <QXmlStreamWriter>
00025
00026 #include "rqt_multiplot/XmlSettings.h"
00027
00028 namespace rqt_multiplot {
00029
00030
00031
00032
00033
00034 const QSettings::Format XmlSettings::format = QSettings::registerFormat("xml",
00035 XmlSettings::read, XmlSettings::write);
00036
00037
00038
00039
00040
00041 bool XmlSettings::read(QIODevice& device, QSettings::SettingsMap& map) {
00042 QXmlStreamReader xmlReader(&device);
00043
00044 QStringList groups;
00045
00046 while (!xmlReader.atEnd()) {
00047 xmlReader.readNext();
00048
00049 if (xmlReader.isStartElement())
00050 groups.append(xmlReader.name().toString());
00051 else if (xmlReader.isCharacters() && !xmlReader.isWhitespace())
00052 map[groups.join("/")] = xmlReader.text().toString();
00053 else if (xmlReader.isEndElement())
00054 groups.removeLast();
00055 }
00056
00057 return !xmlReader.hasError();
00058 }
00059
00060 bool XmlSettings::write(QIODevice& device, const QSettings::SettingsMap& map) {
00061 struct NestedMap;
00062 struct NestedMap : QMap<QString, QSharedPointer<NestedMap> > {};
00063
00064 QSharedPointer<NestedMap> nestedMap(new NestedMap());
00065
00066 for (QSettings::SettingsMap::const_iterator it= map.begin();
00067 it != map.end(); ++it) {
00068 QSharedPointer<NestedMap> currentMap = nestedMap;
00069
00070 QStringList groups = it.key().split("/");
00071
00072 for (QStringList::const_iterator jt = groups.begin();
00073 jt != groups.end(); ++jt) {
00074 NestedMap::iterator kt = currentMap->find(*jt);
00075
00076 if (kt == currentMap->end()) {
00077 kt = currentMap->insert(*jt, QSharedPointer<NestedMap>(
00078 new NestedMap()));
00079 currentMap = kt.value();
00080 }
00081 else
00082 currentMap = kt.value();
00083 }
00084 }
00085
00086 QXmlStreamWriter xmlWriter(&device);
00087
00088 xmlWriter.setAutoFormatting(true);
00089 xmlWriter.writeStartDocument();
00090
00091 QStringList groups;
00092 QList<QSharedPointer<NestedMap> > nestedMaps;
00093 QList<NestedMap::iterator> nestedMapIterators;
00094
00095 nestedMaps.append(nestedMap);
00096 nestedMapIterators.append(nestedMap->begin());
00097
00098 while (!nestedMaps.isEmpty()) {
00099 QSharedPointer<NestedMap> currentMap = nestedMaps.last();
00100 NestedMap::iterator it = nestedMapIterators.last();
00101
00102 if (it != currentMap->end()) {
00103 xmlWriter.writeStartElement(it.key());
00104
00105 groups.append(it.key());
00106 nestedMaps.append(it.value());
00107 nestedMapIterators.append(it.value()->begin());
00108 }
00109 else {
00110 if (currentMap->isEmpty())
00111 xmlWriter.writeCharacters(map[groups.join("/")].toString());
00112
00113 xmlWriter.writeEndElement();
00114
00115 if (!groups.isEmpty())
00116 groups.removeLast();
00117 nestedMaps.removeLast();
00118 nestedMapIterators.removeLast();
00119
00120 if (!nestedMaps.isEmpty())
00121 ++nestedMapIterators.last();
00122 }
00123 }
00124
00125 xmlWriter.writeEndDocument();
00126
00127 return true;
00128 }
00129
00130 }