DetectorGraph  2.0
nodenameutils.cpp
Go to the documentation of this file.
1 // Copyright 2018 Nest Labs, Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "nodenameutils.hpp"
16 
17 #include "dgassert.hpp"
18 #include <cxxabi.h>
19 #include <string.h>
20 #include <stdlib.h>
21 
22 std::string DetectorGraph::NodeNameUtils::GetDemangledName(const std::string& aMangledString)
23 {
24  int demanglingStatus;
25  char* demangledChars = abi::__cxa_demangle(aMangledString.c_str(), 0, 0, &demanglingStatus);
26  std::string demangledName = demangledChars;
27  free(demangledChars);
28 
29  return demangledName;
30 }
31 
32 std::string DetectorGraph::NodeNameUtils::GetMinimalName(const std::string& aMangledString)
33 {
34  static const char* elimStrings[] = {
35  "DetectorGraph::",
36  "TopicState",
37  "Topic",
38  "<",
39  ">",
40  "\n",
41  NULL };
42 
43  std::string demangledName = GetDemangledName(aMangledString);
44  std::string minimalName = RemoveSubstrings(demangledName, elimStrings);
45  return minimalName;
46 }
47 
48 std::string DetectorGraph::NodeNameUtils::WrapOnCommonEndings(const std::string& aNodeName)
49 {
50  static const char* wrapStrings[] = {
51  "State",
52  "Detector",
53  "Set",
54  "Request",
55  "Response",
56  "Sample",
57  "Vote",
58  "Timeout",
59  "Update",
60  NULL };
61 
62  std::string wrappedName = WrapOnSubStrings(aNodeName, wrapStrings);
63 
64  return wrappedName;
65 }
66 
67 std::string DetectorGraph::NodeNameUtils::RemoveSubstrings(const std::string& aInStr, const char* elimStrings[])
68 {
69  std::string retString = aInStr;
70 
71  size_t elimStrIt = 0;
72  while (elimStrings[elimStrIt] != NULL)
73  {
74  std::string::size_type i = retString.find(elimStrings[elimStrIt]);
75 
76  if (i != std::string::npos)
77  {
78  retString.erase(i, strlen(elimStrings[elimStrIt]));
79  elimStrIt = 0; // restart again
80  }
81  else
82  {
83  elimStrIt++;
84  }
85  }
86 
87  return retString;
88 }
89 
90 std::string DetectorGraph::NodeNameUtils::WrapOnSubStrings(const std::string& aInStr, const char* wrapStrings[])
91 {
92  std::string retString = aInStr;
93 
94  size_t wrapStrIt = 0;
95  while (wrapStrings[wrapStrIt] != NULL)
96  {
97  std::string::size_type wrapPos = retString.rfind(wrapStrings[wrapStrIt]);
98 
99  if (wrapPos != std::string::npos && wrapPos != 0)
100  {
101  retString.insert(wrapPos, "\\n");
102  }
103 
104  wrapStrIt++;
105  }
106 
107  return retString;
108 }
109 
std::string GetDemangledName(const std::string &aMangledString)
Provides utilities for manipulating DetectorGraph node names.
std::string WrapOnCommonEndings(const std::string &aNodeName)
Adds a \n to the input name before common suffixes.
std::string WrapOnSubStrings(const std::string &aInStr, const char *wrapStrings[])
Adds a \n to aInStr before each sub-string in wrapStrings.
std::string RemoveSubstrings(const std::string &aInStr, const char *elimStrings[])
Removes all elimStrings from aInStr and returns new string.
std::string GetMinimalName(const std::string &aMangledString)
Returns a readable name with redundant prefixes/suffixes removed.