YARP
Yet Another Robot Platform
utility.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 
9 #include <yarp/manager/utility.h>
10 #include <yarp/manager/graph.h>
11 #include <yarp/manager/module.h>
13 #include <yarp/manager/resource.h>
14 
15 #include <cstdio>
16 #include <fstream>
17 
18 
19 using namespace yarp::manager;
20 using namespace std;
21 
22 
23 //#if defined(_MSC_VER) && (_MSC_VER == 1600)
24 
25 StrStream::StrStream() = default;
26 
27 StrStream::StrStream(const std::string str) {
28  dummyStr = str;
29 }
30 
31 StrStream::~StrStream() = default;
32 
33 std::string StrStream::str() {
34  return dummyStr;
35 }
36 
38  dummyStr += oss.str();
39  return *this;
40 }
41 
42 StrStream& StrStream::operator<<(const std::string &str) {
43  dummyStr += str;
44  return *this;
45 }
46 
48  char buff[64];
49  sprintf(buff, "%d", n);
50  dummyStr += std::string(buff);
51  return *this;
52 }
53 
55  char buff[64];
56  sprintf(buff, "%.2f", n);
57  dummyStr += std::string(buff);
58  return *this;
59 }
60 
61 
63  dummyStr = std::string(sz);
64  return *this;
65 }
66 
68  dummyStr = std::string(sz);
69  return *this;
70 }
71 
72 StrStream& StrStream::operator = (const std::string &str) {
73  dummyStr = str;
74  return *this;
75 }
76 
78  dummyStr = oss.str();
79  return *this;
80 }
81 
83  char buff[64];
84  sprintf(buff, "%d", n);
85  dummyStr = std::string(buff);
86  return *this;
87 }
88 
89 std::ostream& operator << (std::ostream &os , StrStream& sstr)
90 {
91  std::cout<<sstr.str();
92  return os;
93 }
94 
95 //#endif
96 
97 
103 {
104  static ErrorLogger instance;
105  return &instance;
106 }
107 
108 void ErrorLogger::addWarning(const char* szWarning) {
109  if(szWarning)
110  warnings.emplace_back(szWarning);
111 }
112 
113 void ErrorLogger::addWarning(const string &str) {
114  warnings.push_back(str);
115 }
116 
118  addWarning(stream.str());
119 }
120 
121 void ErrorLogger::addError(const char* szError) {
122  if(szError)
123  errors.emplace_back(szError);
124 }
125 
126 void ErrorLogger::addError(const string &str) {
127  errors.push_back(str);
128 }
129 
131  addError(stream.str());
132 }
133 
135  if(errors.empty())
136  return nullptr;
137  static string msg;
138  msg = errors.back();
139  errors.pop_back();
140  return msg.c_str();
141 }
142 
144  static string msgs;
145  char* err;
146  while((err=(char*)getLastError()) != nullptr)
147  msgs += string(err) + " ";
148  return msgs.c_str();
149 }
150 
152  if(warnings.empty())
153  return nullptr;
154  static string msg;
155  msg = warnings.back();
156  warnings.pop_back();
157  return msg.c_str();
158 }
159 
161  static string msgs;
162  char* err;
163  while((err=(char*)getLastWarning()) != nullptr)
164  msgs += string(err) + " ";
165  return msgs.c_str();
166 }
167 
168 
170  errors.clear(); warnings.clear();
171 }
172 
174  return errors.size();
175 }
176 
178 
179  return warnings.size();
180 }
181 
182 
183 const string GRAPH_LEGEND =
184 "{"
185 " rank=sink;"
186 " style=filled;"
187 " color=lightgrey;"
188 " Legend [shape=none, margin=0, label=<"
189 " <TABLE BORDER=\"0\" CELLBORDER=\"1\" CELLSPACING=\"0\" CELLPADDING=\"4\" bgcolor=\"white\">"
190 " <TR>"
191 " <TD COLSPAN=\"2\"><B>Legend</B></TD>"
192 " </TR>"
193 " <TR>"
194 " <TD align=\"left\">Application</TD>"
195 " <TD CELLPADDING=\"4\">"
196 " <TABLE BORDER=\"1\" CELLBORDER=\"0\" CELLSPACING=\"0\" CELLPADDING=\"0\">"
197 " <TR>"
198 " <TD BGCOLOR=\"darkseagreen\"> </TD>"
199 " </TR>"
200 " </TABLE>"
201 " </TD>"
202 " </TR>"
203 " <TR>"
204 " <TD align=\"left\">Module</TD>"
205 " <TD CELLPADDING=\"4\">"
206 " <TABLE BORDER=\"1\" CELLBORDER=\"0\" CELLSPACING=\"0\" CELLPADDING=\"0\">"
207 " <TR>"
208 " <TD BGCOLOR=\"lightslategrey\"> </TD>"
209 " </TR>"
210 " </TABLE>"
211 " </TD>"
212 " </TR>"
213 " <TR>"
214 " <TD align=\"left\">Res. Dependency</TD>"
215 " <TD CELLPADDING=\"4\">"
216 " <TABLE BORDER=\"1\" CELLBORDER=\"0\" CELLSPACING=\"0\" CELLPADDING=\"0\">"
217 " <TR>"
218 " <TD BGCOLOR=\"salmon\"> </TD>"
219 " </TR>"
220 " </TABLE>"
221 " </TD>"
222 " </TR>"
223 " <TR>"
224 " <TD align=\"left\">Res. Provider</TD>"
225 " <TD CELLPADDING=\"4\">"
226 " <TABLE BORDER=\"1\" CELLBORDER=\"0\" CELLSPACING=\"0\" CELLPADDING=\"0\">"
227 " <TR>"
228 " <TD BGCOLOR=\"indianred\"> </TD>"
229 " </TR>"
230 " </TABLE>"
231 " </TD>"
232 " </TR>"
233 " <TR>"
234 " <TD align=\"left\">Input data</TD>"
235 " <TD CELLPADDING=\"4\">"
236 " <TABLE BORDER=\"1\" CELLBORDER=\"0\" CELLSPACING=\"0\" CELLPADDING=\"0\">"
237 " <TR>"
238 " <TD BGCOLOR=\"lightgrey\"> </TD>"
239 " </TR>"
240 " </TABLE>"
241 " </TD>"
242 " </TR>"
243 " <TR>"
244 " <TD align=\"left\">Output data</TD>"
245 " <TD CELLPADDING=\"4\">"
246 " <TABLE BORDER=\"1\" CELLBORDER=\"0\" CELLSPACING=\"0\" CELLPADDING=\"0\">"
247 " <TR>"
248 " <TD BGCOLOR=\"wheat\"> </TD>"
249 " </TR>"
250 " </TABLE>"
251 " </TD>"
252 " </TR>"
253 " </TABLE>"
254 " >];"
255 "}";
256 
257 
258 /*
259 Carrier strToCarrier(const char* szCar)
260 {
261  if(szCar)
262  {
263  if(compareString(szCar, "TCP"))
264  return TCP;
265  if(compareString(szCar, "UDP"))
266  return UDP;
267  if(compareString(szCar, "MCAST"))
268  return MCAST;
269  if(compareString(szCar, "SHMEM"))
270  return SHMEM;
271  if(compareString(szCar, "TEXT"))
272  return TEXT;
273  }
274  return UNKNOWN;
275 }
276 
277 const char* carrierToStr(Carrier cr)
278 {
279  switch(cr){
280  case TCP:{return("tcp");}
281  case UDP:{return("udp");}
282  case MCAST:{return("mcast");}
283  case SHMEM:{return("shmem");}
284  case TEXT:{return("text");}
285  default:{return("tcp");}
286  };
287 }
288 */
289 
290 OS yarp::manager::strToOS(const char* szOS)
291 {
292  if(szOS)
293  {
294  if(compareString(szOS, "LINUX"))
295  return LINUX;
296  if (compareString(szOS, "WINDOWS"))
297  return WINDOWS;
298  if (compareString(szOS, "MAC"))
299  return MAC;
300  }
301  return OTHER;
302 }
303 
304 
305 bool yarp::manager::compareString(const char* szFirst, const char* szSecond)
306 {
307  if(!szFirst && !szSecond)
308  return true;
309  if( !szFirst || !szSecond)
310  return false;
311 
312  string strFirst(szFirst);
313  string strSecond(szSecond);
314  transform(strFirst.begin(), strFirst.end(), strFirst.begin(),
315  (int(*)(int))toupper);
316  transform(strSecond.begin(), strSecond.end(), strSecond.begin(),
317  (int(*)(int))toupper);
318  if(strFirst == strSecond)
319  return true;
320  return false;
321 }
322 
323 void yarp::manager::trimString(string& str)
324 {
325  string::size_type pos = str.find_last_not_of(' ');
326  if(pos != string::npos)
327  {
328  str.erase(pos + 1);
329  pos = str.find_first_not_of(' ');
330  if(pos != string::npos)
331  str.erase(0, pos);
332  }
333  else str.erase(str.begin(), str.end());
334 }
335 
336 
337 bool yarp::manager::exportDotGraph(Graph& graph, const char* szFileName)
338 {
339  ofstream dot;
340  dot.open(szFileName);
341  if(!dot.is_open())
342  return false;
343 
344  dot<<"digraph G {"<<endl;
345  dot<<"rankdir=LR;"<<endl;
346  dot<<"ranksep=0.0;"<<endl;
347  dot<<"nodesep=0.2;"<<endl;
348 
349  for(GraphIterator itr=graph.begin(); itr!=graph.end(); itr++)
350  {
351  switch((*itr)->getType()) {
352  case MODULE: {
353  auto* mod = (Module*)(*itr);
354  dot<<"\""<<mod->getLabel()<<"\"";
355  dot<<" [label=\""<< mod->getName()<<"\"";
356  dot<<" shape=component, color=midnightblue, fillcolor=lightslategrey, peripheries=1, style=filled, penwidth=2];"<<endl;
357  for(int i=0; i<mod->sucCount(); i++)
358  {
359  Link l = mod->getLinkAt(i);
360  auto* in = (InputData*)l.to();
361  dot<<"\""<<mod->getLabel()<<"\" -> ";
362  dot<<"\""<<in->getLabel()<<"\"";
363  if(!l.isVirtual())
364  dot<<" [label=\"\"];"<<endl;
365  else
366  dot<<" [label=\"\" style=dashed];"<<endl;
367 
368  }
369 
370  break;
371  }
372  case INPUTD:{
373  auto* in = (InputData*)(*itr);
374  dot<<"\""<<in->getLabel()<<"\"";
375  if(in->withPriority())
376  {
377  dot<<" [color=red, fillcolor=lightgrey, peripheries=1, style=filled";
378  dot<<" label=\""<< in->getName()<<"\\n"<<in->getPort()<<"\"];"<<endl;
379  }
380  else
381  {
382  dot<<" [color=black, fillcolor=lightgrey, peripheries=1, style=filled";
383  dot<<" label=\""<< in->getName()<<"\\n"<<in->getPort()<<"\"];"<<endl;
384  }
385  for(int i=0; i<in->sucCount(); i++)
386  {
387  Link l = in->getLinkAt(i);
388  auto* out = (OutputData*)l.to();
389  dot<<"\""<<in->getLabel()<<"\" -> ";
390  dot<<"\""<<out->getLabel()<<"\"";
391  if(!l.isVirtual())
392  dot<<" [label=\""<<l.weight()<<"\"];"<<endl;
393  else
394  dot<<" [label=\""<<l.weight()<<"\" style=dashed];"<<endl;
395  }
396 
397  break;
398  }
399  case OUTPUTD:{
400  auto* out = (OutputData*)(*itr);
401  dot<<"\""<<out->getLabel()<<"\"";
402  dot<<" [color=black, fillcolor=wheat, peripheries=1, style=filled";
403  dot<<" label=\""<< out->getName()<<"\\n"<<out->getPort()<<"\"];"<<endl;
404  for(int i=0; i<out->sucCount(); i++)
405  {
406  Link l = out->getLinkAt(i);
407  auto* mod = (Module*)l.to();
408  dot<<"\""<<out->getLabel()<<"\" -> ";
409  dot<<"\""<<mod->getLabel()<<"\"";
410  dot<<" [label=\"\" arrowhead=none];"<<endl;
411  }
412 
413  break;
414  }
415 
416  case APPLICATION:{
417  auto* app = (Application*)(*itr);
418  dot<<"\""<<app->getLabel()<<"\"";
419  dot<<" [shape=folder, color=darkgreen, fillcolor=darkseagreen, peripheries=1, style=filled, penwidth=2";
420  dot<<" label=\""<<app->getLabel()<<"\""<<"];"<<endl;
421  for(int i=0; i<app->sucCount(); i++)
422  {
423  Link l = app->getLinkAt(i);
424  auto* mod = (Module*)l.to();
425  dot<<"\""<<app->getLabel()<<"\" -> ";
426  dot<<"\""<<mod->getLabel()<<"\"";
427  if(!l.isVirtual())
428  dot<<" [label=\"\"];"<<endl;
429  else
430  dot<<" [label=\"\" style=dashed];"<<endl;
431  }
432  break;
433  }
434 
435  case RESOURCE:{
436  auto* res = (GenericResource*)(*itr);
437  dot<<"\""<<res->getLabel()<<"\"";
438  if(res->owner())
439  dot<<" [shape=rect, color=black, fillcolor=salmon, peripheries=1, style=filled ";
440  else
441  dot<<" [shape=house, color=maroon, fillcolor=indianred, peripheries=1, style=filled, penwidth=2";
442  dot<<" label=\""<<res->getName()<<"\""<<"];"<<endl;
443  for(int i=0; i<res->sucCount(); i++)
444  {
445  Link l = res->getLinkAt(i);
446  Node* prov = l.to();
447  dot<<"\""<<res->getLabel()<<"\" -> ";
448  dot<<"\""<<prov->getLabel()<<"\"";
449  dot<<" [label=\""<<l.weight()<<"\"];"<<endl;
450  }
451 
452  break;
453  }
454 
455  default:
456  break;
457  };
458  }
459 
460  dot<<GRAPH_LEGEND;
461  dot<<"}"<<endl;
462  dot.close();
463  return true;
464 }
yarp::manager::OUTPUTD
@ OUTPUTD
Definition: ymm-types.h:24
yarp::manager::Application
Class Application.
Definition: application.h:292
yarp::manager::Graph::end
GraphIterator end()
Definition: graph.cpp:164
operator<<
std::ostream & operator<<(std::ostream &os, StrStream &sstr)
Definition: utility.cpp:89
yarp::manager::GraphIterator
Class GraphIterator.
Definition: graph.h:70
yarp::manager::ErrorLogger::getLastWarning
const char * getLastWarning()
Definition: utility.cpp:151
yarp::manager::ErrorLogger::clear
void clear()
Definition: utility.cpp:169
yarp::manager::ErrorLogger::getLastError
const char * getLastError()
Definition: utility.cpp:134
GRAPH_LEGEND
const string GRAPH_LEGEND
Definition: utility.cpp:183
yarp::manager::Node::getLabel
const char * getLabel()
Definition: node.h:97
yarp::manager::OSTRINGSTREAM
std::stringstream OSTRINGSTREAM
Definition: utility.h:52
yarp::manager
Definition: application.h:24
module.h
yarp::manager::ErrorLogger
Singleton class ErrorLogger.
Definition: utility.h:60
utility.h
yarp::manager::Graph
Class Graph.
Definition: graph.h:31
yarp::manager::WINDOWS
@ WINDOWS
Definition: ymm-types.h:40
yarp::manager::compareString
bool compareString(const char *szFirst, const char *szSecond)
Definition: utility.cpp:305
yarp::manager::trimString
void trimString(std::string &str)
Definition: utility.cpp:323
yarp::manager::Module
Class Module.
Definition: module.h:103
yarp::manager::RESOURCE
@ RESOURCE
Definition: ymm-types.h:29
yarp::math::dot
double dot(const yarp::sig::Vector &a, const yarp::sig::Vector &b)
Scalar product between vectors (defined in Math.h).
Definition: math.cpp:461
yarp::manager::StrStream::operator=
StrStream & operator=(const char *sz)
Definition: utility.cpp:62
yarp::manager::OutputData
Definition: data.h:72
yarp::manager::ErrorLogger::Instance
static ErrorLogger * Instance()
Singleton class ErrorLogger.
Definition: utility.cpp:102
yarp::manager::MODULE
@ MODULE
Definition: ymm-types.h:22
yarp::manager::Node
a Node of a Graph
Definition: node.h:68
resource.h
yarp::manager::StrStream
Definition: utility.h:27
yarp::manager::ErrorLogger::addError
void addError(const char *szError)
Definition: utility.cpp:121
yarp::manager::StrStream::~StrStream
~StrStream()
yarp::manager::OS
enum yarp::manager::__OS OS
yarp::manager::StrStream::str
std::string str()
Definition: utility.cpp:33
yarp::manager::StrStream::StrStream
StrStream()
yarp::manager::LINUX
@ LINUX
Definition: ymm-types.h:39
yarp::manager::ErrorLogger::addWarning
void addWarning(const char *szWarning)
Definition: utility.cpp:108
yarp::manager::exportDotGraph
bool exportDotGraph(Graph &graph, const char *szFileName)
Definition: utility.cpp:337
yarp::manager::ErrorLogger::warningCount
int warningCount()
Definition: utility.cpp:177
yarp::manager::GenericResource
Definition: resource.h:21
yarp::manager::StrStream::operator<<
StrStream & operator<<(StrStream &oss)
Definition: utility.cpp:37
graph.h
yarp::manager::APPLICATION
@ APPLICATION
Definition: ymm-types.h:28
yarp::manager::ErrorLogger::errorCount
int errorCount()
Definition: utility.cpp:173
yarp::manager::Graph::begin
GraphIterator begin()
Definition: graph.cpp:157
yarp::manager::ErrorLogger::getFormatedErrorString
const char * getFormatedErrorString()
Definition: utility.cpp:143
yarp::manager::ErrorLogger::getFormatedWarningString
const char * getFormatedWarningString()
Definition: utility.cpp:160
yarp::manager::INPUTD
@ INPUTD
Definition: ymm-types.h:23
yarp::manager::InputData
Class InputData.
Definition: data.h:25
yarp::manager::strToOS
OS strToOS(const char *szOS)
Definition: utility.cpp:290
yarp::manager::OTHER
@ OTHER
Definition: ymm-types.h:42
yarp::manager::MAC
@ MAC
Definition: ymm-types.h:41
application.h