LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
groupConfigurationStructure.cpp
Go to the documentation of this file.
1// Copyright 2002-2020 CEA LIST
2// SPDX-FileCopyrightText: 2022 CEA LIST <gael.de-chalendar@cea.fr>
3//
4// SPDX-License-Identifier: MIT
5
15#include "common/LimaCommon.h"
16
19
20#include <iostream>
21#include <stdlib.h>
22
23#include <QJsonArray>
24#include <QJsonObject>
25
26using namespace std;
27
28namespace Lima
29{
30namespace Common
31{
32namespace XMLConfigurationFiles
33{
34
39typedef std::map< std::string, std::string > MSS;
40std::ostream& operator<<(std::ostream& os, const MSS& mss);
41
42typedef std::map< std::string, std::deque<std::string > > MSDS;
43std::ostream& operator<<(std::ostream& os, const MSDS& mss);
44
45typedef std::map< std::string, std::map<std::string,std::string > > MSMSS;
46std::ostream& operator<<(std::ostream& os, const MSMSS& msmss);
47
49{
51 friend std::ostream& operator<<(std::ostream& os,
52 const GroupConfigurationStructure& dgcs);
53
55 GroupConfigurationStructurePrivate(const QJsonObject& group);
56 GroupConfigurationStructurePrivate(const std::string& name);
62
63 MSS m_params;
64 MSDS m_lists;
65 MSS m_attributes;
66 MSMSS m_maps;
67 std::string m_groupName;
68
70};
79GroupConfigurationStructurePrivate::GroupConfigurationStructurePrivate(
80 const QJsonObject& group) :
81 m_params(),
82 m_lists(),
83 m_attributes(),
84 m_maps(),
85 m_groupName()
86{
87 m_groupName = group["name"].toString().toStdString();
88
89 for(const QString& key: group.keys())
90 {
91 if (key == "name")
92 continue;
93
94 auto val = group[key];
95 // Param
96 if (val.isString())
97 {
98 m_params[key.toStdString()] = val.toString().toStdString();
99 }
100 // List
101 else if (val.isArray())
102 {
103 m_lists[key.toStdString()] = {};
104 auto list = val.toArray();
105 for (const auto& item: list)
106 {
107 m_lists[key.toStdString()].push_back(item.toString().toStdString());
108 }
109 }
110 // Map
111 else if (val.isObject())
112 {
113 m_maps[key.toStdString()] = {};
114 auto map = val.toObject();
115 for(const QString& mapKey: map.keys())
116 {
117 m_maps[key.toStdString()][mapKey.toStdString()] =
118 map[mapKey].toString().toStdString();
119 }
120 }
121 }
122}
123
124GroupConfigurationStructurePrivate::GroupConfigurationStructurePrivate(
125 const std::string& name) :
126 m_params(),
127 m_lists(),
128 m_attributes(),
129 m_maps(),
130 m_groupName(name)
131{}
132
133GroupConfigurationStructurePrivate::GroupConfigurationStructurePrivate(
134 const GroupConfigurationStructurePrivate& group) :
135 m_params(group.m_params),
136 m_lists(group.m_lists),
137 m_attributes(group.m_attributes),
138 m_maps(group.m_maps),
139 m_groupName(group.m_groupName)
140{}
141
142GroupConfigurationStructurePrivate& GroupConfigurationStructurePrivate::operator=(
143 const GroupConfigurationStructurePrivate& group)
144{
145 m_params=group.m_params;
146 m_lists=group.m_lists;
147 m_attributes=group.m_attributes;
148 m_maps=group.m_maps;
149 m_groupName=group.m_groupName;
150 return *this;
151}
152
153GroupConfigurationStructurePrivate::~GroupConfigurationStructurePrivate()
154{
155
156}
157
158
164
167{
168
169}
170
174
179
181 const GroupConfigurationStructure& group)
182{
183 *m_d = *group.m_d;
184 return *this;
185}
186
191
193{
194 return m_d->m_groupName;
195}
196
197
199 const std::string& key)
200{
201 MSS::iterator it=m_d->m_attributes.find(key);
202 if (it == m_d->m_attributes.end())
203 throw NoSuchAttribute(m_d->m_groupName+"["+key+"]");
204 return (it->second);
205}
206
208{
209 MSS::iterator it = m_d->m_params.find(key);
210 if (it == m_d->m_params.end())
211 throw NoSuchParam(m_d->m_groupName+"["+key+"]");
212 // cerr << "Found param value " << ((*it).second) << endl;
213 return ((*it).second);
214}
215
217 string& value)
218{
219 MSS::iterator it = m_d->m_params.find(key);
220 if (it == m_d->m_params.end())
221 {
222 return false;
223 }
224 value = it->second;
225 return true;
226}
227
228// QString GroupConfigurationStructure::getNameQ()
229// {
230// return QString::fromStdString(m_d->m_groupName);
231// }
232//
233//
234// QString GroupConfigurationStructure::getAttribute(const QString& key)
235// {
236// return QString::fromStdString(getAttribute(key.toStdString()));
237// }
238//
239// QString GroupConfigurationStructure::getParamsValueAtKey(const QString& key)
240// {
241// return QString::fromStdString(getParamsValueAtKey(key.toStdString()));
242// }
243//
244// bool GroupConfigurationStructure::getParamsValueAtKey(const QString& key, QString& value)
245// {
246// std::string svalue;
247// if (!getParamsValueAtKey(key.toStdString(), svalue))
248// {
249// return false;
250// }
251// value = QString::fromStdString(svalue);
252// return true;
253// }
254
256{
257 string& str=getParamsValueAtKey(key);
258 if (str=="yes" || str=="true" || str=="1") {
259 return true;
260 }
261 return false;
262}
263
264uint32_t GroupConfigurationStructure::getIntParameter(const std::string& key)
265{
266 return atoi(getParamsValueAtKey(key).c_str());
267}
268
270{
271 return QString::fromUtf8(getParamsValueAtKey(key).c_str()).toDouble();
272}
273
275 const std::string& key)
276{
277 return getParamsValueAtKey(key);
278}
279
280// bool GroupConfigurationStructure::getBooleanParameter(const QString& key)
281// {
282// auto& str = getParamsValueAtKey(key);
283// if (str=="yes" || str=="true" || str=="1")
284// {
285// return true;
286// }
287// return false;
288// }
289//
290// uint32_t GroupConfigurationStructure::getIntParameter(const QString& key)
291// {
292// return getParamsValueAtKey(key).toUInt();
293// }
294//
295// double GroupConfigurationStructure::getDoubleParameter(const QString& key)
296// {
297// return getParamsValueAtKey(key).toDouble();
298// }
299//
300// QString GroupConfigurationStructure::getStringParameter(const QString& key)
301// {
302// return getParamsValueAtKey(key);
303// }
304
306 const std::string& key)
307{
308 auto it = m_d->m_lists.find(key);
309 if (it == m_d->m_lists.end())
310 throw NoSuchList(m_d->m_groupName+"["+key+"]");
311 return (*it).second;
312}
313
314std::map<std::string, std::string>& GroupConfigurationStructure::getMapAtKey(
315 const std::string& key)
316{
317 MSMSS::iterator it = m_d->m_maps.find(key);
318 if (it == m_d->m_maps.end())
319 {
321 LWARN << "GroupConfigurationStructure::getMapAtKey No such map"
322 << m_d->m_groupName+"["+key+"]";
323 throw NoSuchMap(m_d->m_groupName+"["+key+"]");
324 }
325 return it->second;
326}
327
328// std::deque<std::string>& GroupConfigurationStructure::getListsValueAtKey(const QString& key)
329// {
330// return getListsValueAtKey(key.toStdString());
331// }
332//
333// std::map<std::string, std::string>& GroupConfigurationStructure::getMapAtKey(const QString& key)
334// {
335// return getMapAtKey(key.toStdString());
336// }
337
338const map<string,string>& GroupConfigurationStructure::getParams()
339{ return m_d->m_params;}
340
341const std::map<std::string,std::map<std::string,std::string> >&
343{ return m_d->m_maps; }
344
345const std::map<std::string,std::deque<std::string> >&
347{ return m_d->m_lists; }
348
350 const std::string& value)
351{
352 m_d->m_attributes[key]=value;
353}
354
356 const string& value)
357{
358 m_d->m_params.insert(make_pair(key,value));
359}
360
362{
363 m_d->m_lists.insert(make_pair(listName,deque<string>()));
364}
365
367 const string& listName)
368{
369 MSDS::iterator it = m_d->m_lists.find(listName);
370 if (it == m_d->m_lists.end())
371 throw NoSuchList(m_d->m_groupName+"["+listName+"]");
372 /*
373 deque<string> list((*it).second);
374 list.push_back(item);
375 m_lists.erase(listName);
376 m_lists.insert(make_pair(listName, list));
377 */
378 (*it).second.push_back(item);
379}
380
381void GroupConfigurationStructure::addMap(const std::string& mapName)
382{
383 m_d->m_maps.insert(make_pair(mapName,MSS()));
384}
385
387 const std::string& mapName,const std::string& key,const std::string& value)
388{
389 MSMSS::iterator it=m_d->m_maps.find(mapName);
390 if (it == m_d->m_maps.end())
391 throw NoSuchMap(m_d->m_groupName+"["+mapName+"]");
392 (it->second)[key]=value;
393}
394
395// void GroupConfigurationStructure::addAttribute(const QString& key,const QString& value)
396// {
397// addAttribute(key.toStdString(), value.toStdString());
398// }
399//
400// void GroupConfigurationStructure::addParamValuePair(const QString& key, const QString& value)
401// {
402// addParamValuePair(key.toStdString(), value.toStdString());
403// }
404//
405// void GroupConfigurationStructure::addListNamed(const QString& listName)
406// {
407// addListNamed(listName.toStdString());
408// }
409//
410// void GroupConfigurationStructure::addItemInListNamed(const QString& item, const QString& listName)
411// {
412// addItemInListNamed(item.toStdString(), listName.toStdString());
413// }
414//
415// void GroupConfigurationStructure::addMap(const QString& mapName)
416// {
417// addMap(mapName.toStdString());;
418// }
419//
420// void GroupConfigurationStructure::addEntryInMap(const QString& mapName,const QString& key,const QString& value)
421// {
422// addEntryInMap(mapName.toStdString(), key.toStdString(), value.toStdString());
423// }
424
425std::ostream& operator<<(std::ostream& os,
426 const GroupConfigurationStructure& dgcs)
427{
428 return os << "Group : " << dgcs.m_d->m_groupName << endl
429 << "Attributes : " << dgcs.m_d->m_attributes << endl
430 << "Params : " << dgcs.m_d->m_params << endl
431 << "Lists : " << dgcs.m_d->m_lists << endl
432 << "Maps : " << dgcs.m_d->m_maps << endl;
433}
434
435std::ostream& operator<<(std::ostream& os, const MSS& mss)
436{
437 for (auto it = mss.begin(); it != mss.end(); it++)
438 {
439 os << (*it).first << " : " << (*it).second << endl;
440 }
441 return os;
442}
443
444std::ostream& operator<<(std::ostream& os, const MSDS& msds)
445{
446 for (MSDS::const_iterator it = msds.begin(); it != msds.end(); it++)
447 {
448 os << (*it).first << " : [" ;
449 const deque<string>& ds = (*it).second;
450 for (auto dit = ds.begin(); dit != ds.end(); dit++)
451 {
452 os << (*dit) << ", ";
453 }
454 os << "]" << endl;
455 }
456 return os;
457}
458
459std::ostream& operator<<(std::ostream& os, const MSMSS& msmss)
460{
461 for (MSMSS::const_iterator it=msmss.begin();it!=msmss.end();it++)
462 {
463 os << it->first << " { ";
464 const MSS& mss=it->second;
465 for (auto it2=mss.begin();it2!=mss.end();it2++)
466 {
467 os << "[" << it2->first << "->" << it2->second << "]";
468 }
469 os << "}" << endl;
470 }
471 return os;
472}
473
474} // closing namespace XMLConfigurationFiles
475} // closing namespace Common
476} // closing namespace Lima
#define XMLCFGLOGINIT
Definition LimaCommon.h:217
#define LWARN
Definition LimaCommon.h:160
friend std::ostream & operator<<(std::ostream &os, const GroupConfigurationStructure &dgcs)
const std::map< std::string, std::deque< std::string > > & getLists()
std::deque< std::string > & getListsValueAtKey(const std::string &key)
std::map< std::string, std::string > & getMapAtKey(const std::string &key)
const std::map< std::string, std::map< std::string, std::string > > & getMaps()
void addEntryInMap(const std::string &mapName, const std::string &key, const std::string &value)
void addAttribute(const std::string &key, const std::string &value)
void addParamValuePair(const std::string &key, const std::string &value)
void addItemInListNamed(const std::string &item, const std::string &listName)
GroupConfigurationStructure & operator=(const GroupConfigurationStructure &group)
return a message when an 'attribute' was not found
return a message when a 'param' was not found
This is a SAX document handler the configuration files.
originally detectGroupConfigurationStructure.h in detectlibraries
std::map< std::string, std::string > MSS
The objects of this class hold the configuration of one module in the form of a list of group names a...
std::ostream & operator<<(std::ostream &os, const ConfigurationStructure &conf)
std::map< std::string, std::deque< std::string > > MSDS
std::map< std::string, std::map< std::string, std::string > > MSMSS
NAUTITIA.
STL namespace.
launch exception related to the configuration file parsing