Path.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2012-2016, High Performance Humanoid Technologies (H2T),
5  * Karlsruhe Institute of Technology (KIT), all rights reserved.
6  *
7  * ArmarX is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  *
11  * ArmarX is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  *
19  * @author Fabian Peller-Konrad (fabian dot peller-konrad at kit dot edu)
20  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
21  * GNU General Public License
22  */
23 
24 // Header
25 #include "Path.h"
26 
27 #include <SimoxUtility/algorithm/string/string_tools.h>
28 
29 // ArmarX
31 
32 namespace armarx::aron
33 {
34  Path::Path() : rootIdentifier(DEFAULT_ROOT_IDENTIFIER), delimeter(DEFAULT_DELIMETER)
35  {
36  }
37 
38  Path::Path(const std::vector<std::string>& p) :
39  rootIdentifier(DEFAULT_ROOT_IDENTIFIER), delimeter(DEFAULT_DELIMETER), path(p)
40  {
41  }
42 
43  Path::Path(const Path& p) :
44  rootIdentifier(p.getRootIdentifier()), delimeter(p.getDelimeter()), path(p.getPath())
45  {
46  }
47 
48  Path::Path(const Path& pa, const std::vector<std::string>& p) : Path(pa)
49  {
50  for (const std::string& s : p)
51  {
52  append(s);
53  }
54  }
55 
56  void
57  Path::setRootIdentifier(const std::string& s)
58  {
59  rootIdentifier = s;
60  }
61 
62  std::string
64  {
65  return rootIdentifier;
66  }
67 
68  void
69  Path::setDelimeter(const std::string& d)
70  {
71  delimeter = d;
72  }
73 
74  std::string
76  {
77  return delimeter;
78  }
79 
80  void
81  Path::append(const std::string& str)
82  {
83  path.push_back(str);
84  }
85 
86  std::vector<std::string>
87  Path::getPath() const
88  {
89  return path;
90  }
91 
92  std::string
94  {
95  if (!hasElement())
96  {
97  throw error::AronException(__PRETTY_FUNCTION__,
98  "Try to access last element of empty vector.");
99  }
100  return path.back();
101  }
102 
103  std::string
105  {
106  if (!hasElement())
107  {
108  throw error::AronException(__PRETTY_FUNCTION__,
109  "Try to access last element of empty vector.");
110  }
111  return path[0];
112  }
113 
114  bool
116  {
117  return path.size() > 0;
118  }
119 
120  size_t
121  Path::size() const
122  {
123  return path.size();
124  }
125 
126  std::string
128  {
129  std::stringstream ss;
130  ss << rootIdentifier;
131  for (const std::string& s : path)
132  {
133  ss << delimeter << s;
134  }
135  return ss.str();
136  }
137 
138  Path
139  Path::FromString(const std::string& s,
140  const std::string& rootIdentifier,
141  const std::string& delimeter)
142  {
143  std::vector<std::string> elements = simox::alg::split(s, delimeter);
144  if (elements.size())
145  {
146  elements[0] = simox::alg::remove_prefix(elements[0], rootIdentifier);
147  }
148  return Path(elements);
149  }
150 
151  Path
152  Path::withIndex(int i, bool escape) const
153  {
154  std::string el = std::to_string(i);
155  if (escape)
156  {
157  el = "\"" + el + "\"";
158  }
159  return Path(*this, {el});
160  }
161 
162  Path
163  Path::withElement(const std::string& s, bool escape) const
164  {
165  std::string el = s;
166  if (escape)
167  {
168  el = "\"" + el + "\"";
169  }
170  return Path(*this, {el});
171  }
172 
173  Path
174  Path::withAcceptedType(bool escape) const
175  {
176  std::string el = "::accepted_type";
177  if (escape)
178  {
179  el = "\"" + el + "\"";
180  }
181  return Path(*this, {el});
182  }
183 
184  Path
185  Path::withAcceptedTypeIndex(int i, bool escape) const
186  {
187  std::string el = "::accepted_type_" + std::to_string(i);
188  if (escape)
189  {
190  el = "\"" + el + "\"";
191  }
192  return Path(*this, {el});
193  }
194 
195  Path
197  {
198  std::vector<std::string> p = path;
199  p.pop_back();
200  auto ret = Path(p);
201  ret.setRootIdentifier(rootIdentifier);
202  ret.setDelimeter(delimeter);
203  return ret;
204  }
205 
206  Path
208  {
209  std::vector<std::string> p = path;
210  p.erase(p.begin());
211  auto ret = Path(p);
212  ret.setRootIdentifier(rootIdentifier);
213  ret.setDelimeter(delimeter);
214  return ret;
215  }
216 
217  Path
218  Path::getWithoutPrefix(const Path& pref) const
219  {
220  unsigned int firstWithoutMatch = 0;
221  for (const std::string& el : pref.getPath())
222  {
223  if (path.size() <= firstWithoutMatch || el != path[firstWithoutMatch])
224  {
225  break;
226  }
227  else
228  {
229  firstWithoutMatch++;
230  }
231  }
232  std::vector<std::string> elementsWithoutPrefix(path.begin() + firstWithoutMatch,
233  path.end());
234 
235  auto ret = Path(elementsWithoutPrefix);
236  ret.setRootIdentifier(rootIdentifier);
237  ret.setDelimeter(delimeter);
238  return ret;
239  }
240 
241  bool
242  Path::hasPrefix(const Path& e) const
243  {
244  const auto& p = e.getPath();
245 
246  if (path.size() < p.size())
247  {
248  // prefix is impossible
249  return false;
250  }
251 
252  int i = 0;
253  for (const auto& el : path)
254  {
255  if (el != path[i++])
256  {
257  return false;
258  }
259  }
260 
261  return true;
262  }
263 
264  bool
265  Path::hasDirectPrefix(const Path& e) const
266  {
267  const auto& p = e.getPath();
268  if (p.size() + 1 != path.size())
269  {
270  return false;
271  }
272  return hasPrefix(e);
273  }
274 } // namespace armarx::aron
armarx::aron::Path::withElement
Path withElement(const std::string &, bool escape=false) const
Definition: Path.cpp:163
armarx::aron::Path::getFirstElement
std::string getFirstElement() const
Definition: Path.cpp:104
armarx::aron::error::AronException
A base class for aron exceptions.
Definition: Exception.h:36
str
std::string str(const T &t)
Definition: UserAssistedSegmenterGuiWidgetController.cpp:43
armarx::aron::ret
ReaderT::InputType T & ret
Definition: rw.h:13
armarx::aron::Path::hasElement
bool hasElement() const
Definition: Path.cpp:115
Path.h
armarx::aron::Path::withAcceptedTypeIndex
Path withAcceptedTypeIndex(int, bool escape=false) const
Definition: Path.cpp:185
armarx::aron::Path::size
size_t size() const
Definition: Path.cpp:121
armarx::aron::Path::getDelimeter
std::string getDelimeter() const
Definition: Path.cpp:75
armarx::aron::Path::withIndex
Path withIndex(int, bool escape=false) const
Definition: Path.cpp:152
armarx::aron::Path
The Path class.
Definition: Path.h:35
armarx::aron::Path::hasPrefix
bool hasPrefix(const Path &) const
Definition: Path.cpp:242
armarx::aron::Path::setRootIdentifier
void setRootIdentifier(const std::string &)
Definition: Path.cpp:57
armarx::aron::Path::setDelimeter
void setDelimeter(const std::string &)
Definition: Path.cpp:69
armarx::aron
Definition: DataDisplayVisitor.cpp:5
armarx::aron::Path::FromString
static Path FromString(const std::string &, const std::string &rootIdentifier=DEFAULT_ROOT_IDENTIFIER, const std::string &delimeter=DEFAULT_DELIMETER)
Definition: Path.cpp:139
armarx::aron::Path::Path
Path()
default constructor
Definition: Path.cpp:34
armarx::aron::Path::hasDirectPrefix
bool hasDirectPrefix(const Path &) const
Definition: Path.cpp:265
armarx::to_string
const std::string & to_string(const std::string &s)
Definition: StringHelpers.h:41
armarx::aron::Path::getLastElement
std::string getLastElement() const
Definition: Path.cpp:93
armarx::aron::Path::withAcceptedType
Path withAcceptedType(bool escape=false) const
Definition: Path.cpp:174
Exception.h
armarx::aron::Path::withDetachedLastElement
Path withDetachedLastElement() const
Definition: Path.cpp:196
armarx::aron::Path::withDetachedFirstElement
Path withDetachedFirstElement() const
Definition: Path.cpp:207
armarx::aron::Path::getRootIdentifier
std::string getRootIdentifier() const
Definition: Path.cpp:63
armarx::ctrlutil::s
double s(double t, double s0, double v0, double a0, double j)
Definition: CtrlUtil.h:33
armarx::aron::Path::getWithoutPrefix
Path getWithoutPrefix(const Path &) const
Definition: Path.cpp:218
armarx::aron::Path::getPath
std::vector< std::string > getPath() const
Definition: Path.cpp:87
armarx::aron::Path::toString
std::string toString() const
Definition: Path.cpp:127
armarx::split
std::vector< std::string > split(const std::string &source, const std::string &splitBy, bool trimElements=false, bool removeEmptyElements=false)
Definition: StringHelpers.cpp:38