XmlSettings.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright (C) 2015 by Ralf Kaestner *
3  * ralf.kaestner@gmail.com *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the Lesser GNU General Public License as published by*
7  * the Free Software Foundation; either version 3 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * Lesser GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the Lesser GNU General Public License *
16  * along with this program. If not, see <http://www.gnu.org/licenses/>. *
17  ******************************************************************************/
18 
19 #include <QMap>
20 #include <QList>
21 #include <QSharedPointer>
22 #include <QStringList>
23 #include <QXmlStreamReader>
24 #include <QXmlStreamWriter>
25 
27 
28 namespace rqt_multiplot {
29 
30 /*****************************************************************************/
31 /* Static Initializations */
32 /*****************************************************************************/
33 
34 const QSettings::Format XmlSettings::format = QSettings::registerFormat("xml",
36 
37 /*****************************************************************************/
38 /* Methods */
39 /*****************************************************************************/
40 
41 bool XmlSettings::read(QIODevice& device, QSettings::SettingsMap& map) {
42  QXmlStreamReader xmlReader(&device);
43 
44  QStringList groups;
45 
46  while (!xmlReader.atEnd()) {
47  xmlReader.readNext();
48 
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())
54  groups.removeLast();
55  }
56 
57  return !xmlReader.hasError();
58 }
59 
60 bool XmlSettings::write(QIODevice& device, const QSettings::SettingsMap& map) {
61  struct NestedMap;
62  struct NestedMap : QMap<QString, QSharedPointer<NestedMap> > {};
63 
64  QSharedPointer<NestedMap> nestedMap(new NestedMap());
65 
66  for (QSettings::SettingsMap::const_iterator it= map.begin();
67  it != map.end(); ++it) {
68  QSharedPointer<NestedMap> currentMap = nestedMap;
69 
70  QStringList groups = it.key().split("/");
71 
72  for (QStringList::const_iterator jt = groups.begin();
73  jt != groups.end(); ++jt) {
74  NestedMap::iterator kt = currentMap->find(*jt);
75 
76  if (kt == currentMap->end()) {
77  kt = currentMap->insert(*jt, QSharedPointer<NestedMap>(
78  new NestedMap()));
79  currentMap = kt.value();
80  }
81  else
82  currentMap = kt.value();
83  }
84  }
85 
86  QXmlStreamWriter xmlWriter(&device);
87 
88  xmlWriter.setAutoFormatting(true);
89  xmlWriter.writeStartDocument();
90 
91  QStringList groups;
92  QList<QSharedPointer<NestedMap> > nestedMaps;
93  QList<NestedMap::iterator> nestedMapIterators;
94 
95  nestedMaps.append(nestedMap);
96  nestedMapIterators.append(nestedMap->begin());
97 
98  while (!nestedMaps.isEmpty()) {
99  QSharedPointer<NestedMap> currentMap = nestedMaps.last();
100  NestedMap::iterator it = nestedMapIterators.last();
101 
102  if (it != currentMap->end()) {
103  xmlWriter.writeStartElement(it.key());
104 
105  groups.append(it.key());
106  nestedMaps.append(it.value());
107  nestedMapIterators.append(it.value()->begin());
108  }
109  else {
110  if (currentMap->isEmpty())
111  xmlWriter.writeCharacters(map[groups.join("/")].toString());
112 
113  xmlWriter.writeEndElement();
114 
115  if (!groups.isEmpty())
116  groups.removeLast();
117  nestedMaps.removeLast();
118  nestedMapIterators.removeLast();
119 
120  if (!nestedMaps.isEmpty())
121  ++nestedMapIterators.last();
122  }
123  }
124 
125  xmlWriter.writeEndDocument();
126 
127  return true;
128 }
129 
130 }
static bool write(QIODevice &device, const QSettings::SettingsMap &map)
Definition: XmlSettings.cpp:60
static const QSettings::Format format
Definition: XmlSettings.h:28
static bool read(QIODevice &device, QSettings::SettingsMap &map)
Definition: XmlSettings.cpp:41


rqt_multiplot_plugin
Author(s): Ralf Kaestner
autogenerated on Fri Jan 15 2021 03:47:53