YARP
Yet Another Robot Platform
xmlresloader.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  * All rights reserved.
4  *
5  * This software may be modified and distributed under the terms of the
6  * BSD-3-Clause license. See the accompanying LICENSE file for details.
7  */
8 
10 #include <yarp/conf/filesystem.h>
11 #include <yarp/manager/utility.h>
12 #include <dirent.h>
15 
16 #include <algorithm>
17 #include <cctype>
18 #include <string>
19 #include <fstream>
21 #include <tinyxml.h>
22 
23 
24 using namespace std;
25 using namespace yarp::manager;
26 
27 
28 XmlResLoader::XmlResLoader(const char* szPath, const char* szName)
29 {
30  parser = new(TextParser);
31  if(strlen(szPath))
32  {
33  const std::string directorySeparator{yarp::conf::filesystem::preferred_separator};
34  strPath = szPath;
35  if((strPath.rfind(directorySeparator)==string::npos) ||
36  (strPath.rfind(directorySeparator)!=strPath.size()-1))
37  strPath = strPath + string(directorySeparator);
38  }
39 
40  if(szName)
41  strName = szName;
42 }
43 
47 XmlResLoader::XmlResLoader(const char* szFileName)
48 {
49  parser = new(TextParser);
50  if(szFileName)
51  strFileName = szFileName;
52 }
53 
54 
55 
56 XmlResLoader::~XmlResLoader()
57 {
58  if(parser)
59  {
60  delete parser;
61  }
62 }
63 
64 
65 bool XmlResLoader::init()
66 {
67  fileNames.clear();
68  ErrorLogger* logger = ErrorLogger::Instance();
69 
73  if(!strFileName.empty())
74  {
75  fileNames.push_back(strFileName);
76  return true;
77  }
78 
79  if(strPath.empty())
80  {
81  logger->addError("No module path is introduced.");
82  return false;
83  }
84 
85  DIR *dir;
86  struct dirent *entry;
87  if ((dir = opendir(strPath.c_str())) == nullptr)
88  {
89  OSTRINGSTREAM err;
90  err<<"Cannot access "<<strPath;
91  logger->addError(err);
92  return false;
93  }
94 
95  /* we need to load all xml files */
96  while((entry = readdir(dir)))
97  {
98  string name = entry->d_name;
99  if(name.size() > 3)
100  {
101  string ext = name.substr(name.size()-3,3);
102  if(compareString(ext.c_str(), "xml"))
103  fileNames.push_back(strPath+name);
104  }
105  }
106  closedir(dir);
107 
108  /*
109  if(fileNames.empty())
110  {
111  OSTRINGSTREAM err;
112  err<<"No xml resource file found in "<<strPath;
113  logger->addWarning(err);
114  //return true;
115  }
116  */
117  return true;
118 }
119 
120 
121 void XmlResLoader::reset()
122 {
123  fini();
124  init();
125 }
126 
127 
128 void XmlResLoader::fini()
129 {
130  fileNames.clear();
131 }
132 
133 
134 GenericResource* XmlResLoader::getNextResource()
135 {
136  if(strName.empty())
137  {
138  if(computers.size())
139  {
140  dummyComputer = computers.back();
141  computers.pop_back();
142  return &dummyComputer;
143  }
144  else
145  {
146  bool ret = false;
147  do
148  {
149  if(fileNames.empty())
150  return nullptr;
151 
152  string fname = fileNames.back();
153  fileNames.pop_back();
154  ret = parsXml(fname.c_str());
155  } while(!ret);
156 
157  dummyComputer = computers.back();
158  computers.pop_back();
159  return &dummyComputer;
160  }
161  }
162  else
163  {
167  vector<string>::iterator itr;
168  for(itr=fileNames.begin(); itr<fileNames.end(); itr++)
169  {
170  if(parsXml((*itr).c_str()))
171  {
172  for(auto& computer : computers)
173  if(string(computer.getName()) == strName)
174  return &computer;
175  }
176  }
177  }
178  return nullptr;
179 }
180 
181 
182 
183 bool XmlResLoader::parsXml(const char* szFile)
184 {
185  computers.clear();
186 
187  ErrorLogger* logger = ErrorLogger::Instance();
188 
189  TiXmlDocument doc(szFile);
190  if(!doc.LoadFile())
191  {
192  OSTRINGSTREAM err;
193  err<<"Syntax error while loading "<<szFile<<" at line "\
194  <<doc.ErrorRow()<<": ";
195  err<<doc.ErrorDesc();
196  logger->addError(err);
197  return false;
198  }
199  /* retrieving root module */
200  TiXmlElement *root = doc.RootElement();
201  if(!root)
202  {
203  OSTRINGSTREAM err;
204  err<<"Syntax error while loading "<<szFile<<" . ";
205  err<<"No root element.";
206  logger->addError(err);
207  return false;
208  }
209 
210 
211 
212  if(!compareString(root->Value(), "resources"))
213  {
214  /*
215  OSTRINGSTREAM msg;
216  msg<<szFile<<" is not a resource descriptor file.";
217  logger->addWarning(msg);
218  */
219  return false;
220  }
221 
222  for(TiXmlElement* var = root->FirstChildElement("var"); var; var = var->NextSiblingElement())
223  {
224  if(var->Attribute("name") && var->GetText())
225  {
226  parser->addVariable(var->Attribute("name"), var->GetText());
227  }
228  }
229 
230  /* retrieving all computers descriptions */
231  for(TiXmlElement* restag = root->FirstChildElement();
232  restag; restag = restag->NextSiblingElement())
233  {
234  /* retrieving a computer resource */
235  if(compareString(restag->Value(), "computer"))
236  {
237  Computer computer;
238  computer.setXmlFile(szFile);
239 
240  for(TiXmlElement* comptag = restag->FirstChildElement();
241  comptag; comptag = comptag->NextSiblingElement())
242  {
243  /* retrieving name */
244  if(compareString(comptag->Value(), "name"))
245  computer.setName(parser->parseText(comptag->GetText()).c_str());
246 
247  /* retrieving description */
248  if(compareString(comptag->Value(), "description"))
249  computer.setDescription(parser->parseText(comptag->GetText()).c_str());
250 
251  /* retrieving disablility */
252  if(compareString(comptag->Value(), "disable"))
253  {
254  if(compareString(parser->parseText(comptag->GetText()).c_str(), "yes"))
255  computer.setDisable(true);
256  }
257 
258  // platform
259  if(compareString(comptag->Value(), "platform"))
260  {
261  Platform os;
262  TiXmlElement* element;
263  if((element = (TiXmlElement*) comptag->FirstChild("name")))
264  os.setName(parser->parseText(element->GetText()).c_str());
265  else
266  {
267  OSTRINGSTREAM war;
268  war<<"Platform from "<<szFile<<" at line "\
269  <<comptag->Row()<<" has no name.";
270  logger->addWarning(war);
271  }
272 
273  if((element = (TiXmlElement*) comptag->FirstChild("distribution")))
274  os.setDistribution(parser->parseText(element->GetText()).c_str());
275 
276  if((element = (TiXmlElement*) comptag->FirstChild("release")))
277  os.setRelease(parser->parseText(element->GetText()).c_str());
278 
279  computer.setPlatform(os);
280  } // end of platform tag
281 
282  // memory
283  if(compareString(comptag->Value(), "memory"))
284  {
285  Memory mem;
286  TiXmlElement* element;
287  if((element = (TiXmlElement*) comptag->FirstChild("total_space")))
288  mem.setTotalSpace((Capacity)atol(parser->parseText(element->GetText()).c_str()));
289  computer.setMemory(mem);
290  } // end of memory tag
291 
292  // storage
293  if(compareString(comptag->Value(), "storage"))
294  {
295  Storage stg;
296  TiXmlElement* element;
297  if((element = (TiXmlElement*) comptag->FirstChild("total_space")))
298  stg.setTotalSpace((Capacity)atol(parser->parseText(element->GetText()).c_str()));
299  computer.setStorage(stg);
300  } // end of storage tag
301 
302  // processor
303  if(compareString(comptag->Value(), "processor"))
304  {
305  Processor proc;
306  TiXmlElement* element;
307  if((element = (TiXmlElement*) comptag->FirstChild("architecture")))
308  proc.setArchitecture(parser->parseText(element->GetText()).c_str());
309  if((element = (TiXmlElement*) comptag->FirstChild("model")))
310  proc.setModel(parser->parseText(element->GetText()).c_str());
311  if((element = (TiXmlElement*) comptag->FirstChild("cores")))
312  proc.setCores((size_t)atoi(parser->parseText(element->GetText()).c_str()));
313  if((element = (TiXmlElement*) comptag->FirstChild("frequency")))
314  proc.setFrequency(atof(parser->parseText(element->GetText()).c_str()));
315  computer.setProcessor(proc);
316  } // end of processor tag
317 
318  // network
319  if(compareString(comptag->Value(), "network"))
320  {
321  Network net;
322  TiXmlElement* element;
323  if((element = (TiXmlElement*) comptag->FirstChild("ip4")))
324  net.setIP4(parser->parseText(element->GetText()).c_str());
325  if((element = (TiXmlElement*) comptag->FirstChild("ip6")))
326  net.setIP6(parser->parseText(element->GetText()).c_str());
327  if((element = (TiXmlElement*) comptag->FirstChild("mac")))
328  net.setMAC(parser->parseText(element->GetText()).c_str());
329  computer.setNetwork(net);
330  } // end of network tag
331 
332 
333  // gpu
334  if(compareString(comptag->Value(), "gpu"))
335  {
336  GPU gpu;
337  TiXmlElement* element;
338  if((element = (TiXmlElement*) comptag->FirstChild("name")))
339  gpu.setName(parser->parseText(element->GetText()).c_str());
340  if((element = (TiXmlElement*) comptag->FirstChild("capability")))
341  gpu.setCompCompatibility(parser->parseText(element->GetText()).c_str());
342  if((element = (TiXmlElement*) comptag->FirstChild("cores")))
343  gpu.setCores((size_t)atoi(parser->parseText(element->GetText()).c_str()));
344  if((element = (TiXmlElement*) comptag->FirstChild("frequency")))
345  gpu.setFrequency(atof(parser->parseText(element->GetText()).c_str()));
346  if((element = (TiXmlElement*) comptag->FirstChild("register_block")))
347  gpu.setResgisterPerBlock((size_t)atoi(parser->parseText(element->GetText()).c_str()));
348  if((element = (TiXmlElement*) comptag->FirstChild("thread_block")))
349  gpu.setThreadPerBlock((size_t)atoi(parser->parseText(element->GetText()).c_str()));
350  if((element = (TiXmlElement*) comptag->FirstChild("overlap")))
351  {
352  if(compareString(parser->parseText(element->GetText()).c_str(), "yes"))
353  gpu.setOverlap(true);
354  else
355  gpu.setOverlap(false);
356  }
357 
358  // global memory
359  if(comptag->FirstChild("global_memory"))
360  {
361  TiXmlElement* element;
362  element = (TiXmlElement*) comptag->FirstChild("global_memory");
363  if((element = (TiXmlElement*) element->FirstChild("total_space")))
364  gpu.setGlobalMemory((Capacity)atol(parser->parseText(element->GetText()).c_str()));
365  } // end of global memory tag
366 
367  // shared memory
368  if(comptag->FirstChild("shared_memory"))
369  {
370  TiXmlElement* element;
371  element = (TiXmlElement*) comptag->FirstChild("shared_memory");
372  if((element = (TiXmlElement*) element->FirstChild("total_space")))
373  gpu.setSharedMemory((Capacity)atol(parser->parseText(element->GetText()).c_str()));
374  } // end of shared memory tag
375 
376  // constant memory
377  if(comptag->FirstChild("constant_memory"))
378  {
379  TiXmlElement* element;
380  element = (TiXmlElement*) comptag->FirstChild("constant_memory");
381  if((element = (TiXmlElement*) element->FirstChild("total_space")))
382  gpu.setConstantMemory((Capacity)atol(parser->parseText(element->GetText()).c_str()));
383  } // end of shared memory tag
384 
385  computer.addPeripheral(gpu);
386  } // end of gpu tag
387  } // end of computer loop
388 
389  computers.push_back(computer);
390  } // end of if computer
391  } // end of resources
392  return true;
393 }
yarp::manager::Processor::setModel
void setModel(const char *model)
Definition: primresource.h:120
yarp::manager::GPU::setFrequency
void setFrequency(double f)
Definition: physicresource.h:33
filesystem.h
yarp::manager::GPU::setThreadPerBlock
void setThreadPerBlock(size_t val)
Definition: physicresource.h:39
yarp::manager::GenericResource::setDisable
void setDisable(bool flag)
Definition: resource.h:28
yarp::manager::Processor
Definition: primresource.h:111
yarp::manager::Computer::setProcessor
void setProcessor(Processor &proc)
Definition: primresource.h:167
yarp::manager::Processor::setArchitecture
void setArchitecture(const char *arch)
Definition: primresource.h:119
yarp::manager::Computer::setMemory
void setMemory(Memory &mem)
Definition: primresource.h:165
yarp::manager::OSTRINGSTREAM
std::stringstream OSTRINGSTREAM
Definition: utility.h:52
yarp::manager::Computer
Definition: primresource.h:155
yarp::manager::Network::setIP6
void setIP6(const char *ip)
Definition: primresource.h:85
yarp::manager
Definition: application.h:24
yarp::manager::Memory::setTotalSpace
void setTotalSpace(Capacity c)
Definition: primresource.h:35
yarp::manager::Processor::setFrequency
void setFrequency(double f)
Definition: primresource.h:123
yarp::manager::ErrorLogger
Singleton class ErrorLogger.
Definition: utility.h:60
utility.h
ret
bool ret
Definition: ImplementAxisInfo.cpp:72
yarp::manager::compareString
bool compareString(const char *szFirst, const char *szSecond)
Definition: utility.cpp:305
yarp::manager::GenericResource::setXmlFile
void setXmlFile(const char *szFilename)
Definition: resource.h:39
yarp::manager::GenericResource::setDescription
void setDescription(const char *szDesc)
Definition: resource.h:33
yarp::manager::Network
Definition: primresource.h:76
yarp::manager::Storage
Definition: primresource.h:52
yarp::manager::Platform
Definition: logicresource.h:23
yarp::manager::Memory
Class Memory.
Definition: primresource.h:27
yarp::manager::TextParser
Definition: textparser.h:24
yarp::manager::GPU
Definition: physicresource.h:22
yarp::manager::Network::setMAC
void setMAC(const char *mac)
Definition: primresource.h:86
yarp::manager::Platform::setRelease
void setRelease(const char *str)
Definition: logicresource.h:32
yarp::manager::ErrorLogger::addError
void addError(const char *szError)
Definition: utility.cpp:121
textparser.h
yarp::manager::Capacity
size_t Capacity
Definition: primresource.h:22
yarp::manager::GPU::setSharedMemory
void setSharedMemory(Capacity c)
Definition: physicresource.h:36
yarp::manager::Platform::setDistribution
void setDistribution(const char *str)
Definition: logicresource.h:31
yarp::manager::ErrorLogger::addWarning
void addWarning(const char *szWarning)
Definition: utility.cpp:108
yarp::manager::Computer::addPeripheral
bool addPeripheral(GenericResource &res)
Definition: primresource.cpp:221
yarp::manager::Computer::setPlatform
void setPlatform(Platform &os)
Definition: primresource.h:169
yarp::manager::GenericResource
Definition: resource.h:21
yarp::manager::GPU::setCompCompatibility
void setCompCompatibility(const char *cap)
Definition: physicresource.h:34
yarp::manager::Computer::setNetwork
void setNetwork(Network &net)
Definition: primresource.h:168
yarp::conf::filesystem::preferred_separator
static constexpr value_type preferred_separator
Definition: filesystem.h:28
yarp::manager::Processor::setCores
void setCores(size_t n)
Definition: primresource.h:121
logicresource.h
yarp::manager::GPU::setCores
void setCores(size_t n)
Definition: physicresource.h:32
yarp::manager::GPU::setGlobalMemory
void setGlobalMemory(Capacity c)
Definition: physicresource.h:35
yarp::manager::Computer::setStorage
void setStorage(Storage &stg)
Definition: primresource.h:166
yarp::manager::GPU::setConstantMemory
void setConstantMemory(Capacity c)
Definition: physicresource.h:37
physicresource.h
yarp::manager::GPU::setOverlap
void setOverlap(bool flag)
Definition: physicresource.h:40
yarp::manager::GenericResource::setName
void setName(const char *szName)
Definition: resource.h:31
yarp::manager::GPU::setResgisterPerBlock
void setResgisterPerBlock(size_t val)
Definition: physicresource.h:38
xmlresloader.h
yarp::manager::Network::setIP4
void setIP4(const char *ip)
Definition: primresource.h:84
yarp::manager::Storage::setTotalSpace
void setTotalSpace(Capacity c)
Definition: primresource.h:60