FileSystemPathBuilder.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5  *
6  * ArmarX is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * ArmarX is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * @package ArmarXCore
19  * @author Raphael Grimm ( raphael dot grimm at kit dot edu)
20  * @date 2017
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 #include <ctime>
25 
27 
28 #include "FileSystemPathBuilder.h"
29 #include "../system/cmake/CMakePackageFinder.h"
30 #include "../system/ArmarXDataPath.h"
31 #include "../exceptions/local/ExpressionException.h"
32 #include "Preprocessor.h"
33 
34 namespace armarx
35 {
36  const boost::regex FileSystemPathBuilder::RawPathRegex
37  {
38  "([^{}]+|\\{[^{}]+\\})+"
39  };
40 
41  std::map<std::string, FileSystemPathBuilder::FormatStringOption> FileSystemPathBuilder::GetFormatStringOptions()
42  {
43  std::map<std::string, FormatStringOption> descr;
45  {
47  }
48  return descr;
49  }
50 
51  std::string FileSystemPathBuilder::ApplyFormatting(const std::string& rawPath)
52  {
53  ARMARX_CHECK_EXPRESSION(boost::regex_match(rawPath, RawPathRegex))
54  << VAROUT(rawPath);
55 
56  std::stringstream strstr;
57 
58  boost::regex expression {"([^{}]+|\\{([^{}]+)\\})"};
59  std::string::const_iterator start = rawPath.begin();
60  const std::string::const_iterator end = rawPath.end();
61  boost::match_results<std::string::const_iterator> match;
62  while (boost::regex_search(start, end, match, expression))
63  {
64  if (match[2].matched)
65  {
66  const std::string matched
67  {
68  match[2].first, match[2].second
69  };
70  bool hit = false;
72  {
74  if (boost::regex_match(matched, opt.patternRegex))
75  {
76  strstr << opt.patternReplacer(matched);
77  hit = true;
78  break;
79  }
80  }
82  << "pattern '{" << matched
83  << "}' did not match any known FormatString";
84  }
85  else
86  {
87  strstr << match[0];
88  }
89  //advance
90  start = match[0].second;
91  }
92  return strstr.str();
93  }
95  {
96  auto val = rawPath;
99  return ApplyFormatting(val);
100  }
101 
103  std::string name, boost::regex patternRegex, std::function<std::string(const std::string&)> replacer, std::string description)
104  {
106  name, {name, std::move(description), std::move(patternRegex), std::move(replacer)});
107  }
108 
109  //chars
111  {
112  "LEFT CURLY",
113  boost::regex{"LEFT CURLY"},
114  [](const std::string&){return "{";},
115  "prints {"
116  };
117  FileSystemPathBuilder::RegisterFormatStringOption ARMARX_ANONYMOUS_VARIABLE
118  {
119  "RIGHT CURLY",
120  boost::regex{"RIGHT CURLY"},
121  [](const std::string&){return "}";},
122  "prints }"
123  };
124  //date&time
125  FileSystemPathBuilder::RegisterFormatStringOption ARMARX_ANONYMOUS_VARIABLE
126  {
127  "year",
128  boost::regex{"year"},
129  [](const std::string&)
130  {
131  std::time_t t = std::time(nullptr);
132  char buff[8];
133  ARMARX_CHECK_EXPRESSION(std::strftime(buff, sizeof(buff), "%y", std::localtime(&t)));
134  return std::string {buff};
135  },
136  "year (last two digits)"
137  };
138  FileSystemPathBuilder::RegisterFormatStringOption ARMARX_ANONYMOUS_VARIABLE
139  {
140  "Year",
141  boost::regex{"Year"},
142  [](const std::string&)
143  {
144  std::time_t t = std::time(nullptr);
145  char buff[5];
146  ARMARX_CHECK_EXPRESSION(std::strftime(buff, sizeof(buff), "%Y", std::localtime(&t)));
147  return std::string {buff};
148  },
149  "Year (all four digits)"
150  };
151  FileSystemPathBuilder::RegisterFormatStringOption ARMARX_ANONYMOUS_VARIABLE
152  {
153  "month",
154  boost::regex{"month"},
155  [](const std::string&)
156  {
157  std::time_t t = std::time(nullptr);
158  char buff[3];
159  ARMARX_CHECK_EXPRESSION(std::strftime(buff, sizeof(buff), "%m", std::localtime(&t)));
160  return std::string {buff};
161  },
162  "month (01-12)"
163  };
164  FileSystemPathBuilder::RegisterFormatStringOption ARMARX_ANONYMOUS_VARIABLE
165  {
166  "day",
167  boost::regex{"day"},
168  [](const std::string&)
169  {
170  std::time_t t = std::time(nullptr);
171  char buff[3];
172  ARMARX_CHECK_EXPRESSION(std::strftime(buff, sizeof(buff), "%d", std::localtime(&t)));
173  return std::string {buff};
174  },
175  "day (01-31)"
176  };
177  FileSystemPathBuilder::RegisterFormatStringOption ARMARX_ANONYMOUS_VARIABLE
178  {
179  "hour",
180  boost::regex{"hour"},
181  [](const std::string&)
182  {
183  std::time_t t = std::time(nullptr);
184  char buff[3];
185  ARMARX_CHECK_EXPRESSION(std::strftime(buff, sizeof(buff), "%H", std::localtime(&t)));
186  return std::string {buff};
187  },
188  "hour (00-23)"
189  };
190  FileSystemPathBuilder::RegisterFormatStringOption ARMARX_ANONYMOUS_VARIABLE
191  {
192  "minute",
193  boost::regex{"minute"},
194  [](const std::string&)
195  {
196  std::time_t t = std::time(nullptr);
197  char buff[3];
198  ARMARX_CHECK_EXPRESSION(std::strftime(buff, sizeof(buff), "%M", std::localtime(&t)));
199  return std::string {buff};
200  },
201  "minute (00-59)"
202  };
203  FileSystemPathBuilder::RegisterFormatStringOption ARMARX_ANONYMOUS_VARIABLE
204  {
205  "second",
206  boost::regex{"second"},
207  [](const std::string&)
208  {
209  std::time_t t = std::time(nullptr);
210  char buff[3];
211  ARMARX_CHECK_EXPRESSION(std::strftime(buff, sizeof(buff), "%S", std::localtime(&t)));
212  return std::string {buff};
213  },
214  "second (00-59)"
215  };
216  FileSystemPathBuilder::RegisterFormatStringOption ARMARX_ANONYMOUS_VARIABLE
217  {
218  "Time",
219  boost::regex{"Time"},
220  [](const std::string&)
221  {
222  std::time_t t = std::time(nullptr);
223  char buff[9];
224  ARMARX_CHECK_EXPRESSION(std::strftime(buff, sizeof(buff), "%H-%M-%S", std::localtime(&t)));
225  return std::string {buff};
226  },
227  "HH-MM-SS"
228  };
229  FileSystemPathBuilder::RegisterFormatStringOption ARMARX_ANONYMOUS_VARIABLE
230  {
231  "Date",
232  boost::regex{"Date"},
233  [](const std::string&)
234  {
235  std::time_t t = std::time(nullptr);
236  char buff[11];
237  ARMARX_CHECK_EXPRESSION(std::strftime(buff, sizeof(buff), "%Y-%m-%d", std::localtime(&t)));
238  return std::string {buff};
239  },
240  "YYYY-MM-DD"
241  };
242  FileSystemPathBuilder::RegisterFormatStringOption ARMARX_ANONYMOUS_VARIABLE
243  {
244  "DateTime",
245  boost::regex{"DateTime"},
246  [](const std::string&)
247  {
248  std::time_t t = std::time(nullptr);
249  char buff[20];
250  ARMARX_CHECK_EXPRESSION(std::strftime(buff, sizeof(buff), "%Y-%m-%d_%H-%M-%S", std::localtime(&t)));
251  return std::string {buff};
252  },
253  "YYYY-MM-DD_HH-MM-SS"
254  };
255  FileSystemPathBuilder::RegisterFormatStringOption ARMARX_ANONYMOUS_VARIABLE
256  {
257  "time-since-epoch",
258  boost::regex{"time-since-epoch"},
259  [](const std::string&)
260  {
261  return to_string(std::chrono::high_resolution_clock::now().time_since_epoch().count());
262  },
263  "time since epoch in nanoseconds (can be used as UUID)"
264  };
265  //package paths
266  FileSystemPathBuilder::RegisterFormatStringOption ARMARX_ANONYMOUS_VARIABLE
267  {
268  "PackageDir",
269  boost::regex{"PackageDir:.+"},
270  [](const std::string & s)
271  {
272  const auto pkg = s.substr(11);
273  CMakePackageFinder pf {pkg};
274  ARMARX_CHECK_EXPRESSION(pf.packageFound()) << "package '" + pkg + "' not found!";
275  return pf.getPackageDir();
276  },
277  "PackageDir:<PACKAGE>: Package dir of the armarx Package <PACKAGE>."
278  };
279  FileSystemPathBuilder::RegisterFormatStringOption ARMARX_ANONYMOUS_VARIABLE
280  {
281  "ScenarioDir",
282  boost::regex{"ScenarioDir:.+"},
283  [](const std::string & s)
284  {
285  const auto pkg = s.substr(12);
286  CMakePackageFinder pf {pkg};
287  ARMARX_CHECK_EXPRESSION(pf.packageFound()) << "package '" + pkg + "' not found!";
288  return pf.getScenariosDir();
289  },
290  "ScenarioDir:<PACKAGE>: Scenario dir of the armarx Package <PACKAGE>."
291  };
292  FileSystemPathBuilder::RegisterFormatStringOption ARMARX_ANONYMOUS_VARIABLE
293  {
294  "DataDir",
295  boost::regex{"DataDir:.+"},
296  [](const std::string & s)
297  {
298  const auto pkg = s.substr(8);
299  CMakePackageFinder pf {pkg};
300  ARMARX_CHECK_EXPRESSION(pf.packageFound()) << "package '" + pkg + "' not found!";
301  return pf.getDataDir();
302  },
303  "DataDir:<PACKAGE>: Data dir of the armarx Package <PACKAGE>."
304  };
305  FileSystemPathBuilder::RegisterFormatStringOption ARMARX_ANONYMOUS_VARIABLE
306  {
307  "BuildDir",
308  boost::regex{"BuildDir:.+"},
309  [](const std::string & s)
310  {
311  const auto pkg = s.substr(9);
312  CMakePackageFinder pf {pkg};
313  ARMARX_CHECK_EXPRESSION(pf.packageFound()) << "package '" + pkg + "' not found!";
314  return pf.getBuildDir();
315  },
316  "BuildDir:<PACKAGE>: Build dir of the armarx Package <PACKAGE>."
317  };
318  FileSystemPathBuilder::RegisterFormatStringOption ARMARX_ANONYMOUS_VARIABLE
319  {
320  "BinaryDir",
321  boost::regex{"BinaryDir:.+"},
322  [](const std::string & s)
323  {
324  const auto pkg = s.substr(10);
325  CMakePackageFinder pf {pkg};
326  ARMARX_CHECK_EXPRESSION(pf.packageFound()) << "package '" + pkg + "' not found!";
327  return pf.getBinaryDir();
328  },
329  "BinaryDir:<PACKAGE>: Binary dir of the armarx Package <PACKAGE>."
330  };
331  FileSystemPathBuilder::RegisterFormatStringOption ARMARX_ANONYMOUS_VARIABLE
332  {
333  "CMakeDir",
334  boost::regex{"CMakeDir:.+"},
335  [](const std::string & s)
336  {
337  const auto pkg = s.substr(9);
338  CMakePackageFinder pf {pkg};
339  ARMARX_CHECK_EXPRESSION(pf.packageFound()) << "package '" + pkg + "' not found!";
340  return pf.getCMakeDir();
341  },
342  "CMakeDir:<PACKAGE>: CMake dir of the armarx Package <PACKAGE>."
343  };
344  FileSystemPathBuilder::RegisterFormatStringOption ARMARX_ANONYMOUS_VARIABLE
345  {
346  "SourceDir",
347  boost::regex{"SourceDir:.+"},
348  [](const std::string & s)
349  {
350  const auto pkg = s.substr(10);
351  CMakePackageFinder pf {pkg};
352  ARMARX_CHECK_EXPRESSION(pf.packageFound()) << "package '" + pkg + "' not found!";
353  return pf.getPackageDir() + "/source/" + pkg;
354  },
355  "SourceDir:<PACKAGE>: CMake dir of the armarx Package <PACKAGE>."
356  };
357 
358 }
armarx::Registrar::get
static const RegisteredType & get(const KeyType &key)
Returns the registered object for the given key.
Definition: Registrar.h:84
armarx::FileSystemPathBuilder::FormatStringOption
Replacer for a pattern.
Definition: FileSystemPathBuilder.h:72
armarx::Registrar::registerElement
static void registerElement(const KeyType &key, RegisteredType element)
Registers an element.
Definition: Registrar.h:105
StringHelpers.h
armarx::Registrar::getKeys
static std::vector< KeyType > getKeys()
Retrieves the list of all registered elements.
Definition: Registrar.h:115
armarx::FileSystemPathBuilder::rawPath
const std::string rawPath
Definition: FileSystemPathBuilder.h:129
armarx::FileSystemPathBuilder::RawPathRegex
static const boost::regex RawPathRegex
Definition: FileSystemPathBuilder.h:65
armarx::FileSystemPathBuilder::ApplyFormatting
static std::string ApplyFormatting(const std::string &rawPath)
Definition: FileSystemPathBuilder.cpp:51
armarx::FileSystemPathBuilder::RegisterFormatStringOption::RegisterFormatStringOption
RegisterFormatStringOption(std::string name, boost::regex patternRegex, std::function< std::string(const std::string &)> replacer, std::string description="")
Definition: FileSystemPathBuilder.cpp:102
armarx::to_string
const std::string & to_string(const std::string &s)
Definition: StringHelpers.h:40
armarx::ARMARX_ANONYMOUS_VARIABLE
FileSystemPathBuilder::RegisterFormatStringOption ARMARX_ANONYMOUS_VARIABLE
Definition: FileSystemPathBuilder.cpp:111
ARMARX_CHECK_EXPRESSION
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
Definition: ExpressionException.h:73
Preprocessor.h
VAROUT
#define VAROUT(x)
Definition: StringHelpers.h:182
armarx::FileSystemPathBuilder::FormatStringOption::patternRegex
boost::regex patternRegex
Definition: FileSystemPathBuilder.h:76
armarx::ArmarXDataPath::ReplaceEnvVars
static bool ReplaceEnvVars(std::string &string)
ReplaceEnvVars replaces environment variables in a string with their values, if the env.
Definition: ArmarXDataPath.cpp:483
armarx::FileSystemPathBuilder::RegisterFormatStringOption
Definition: FileSystemPathBuilder.h:80
armarx::CMakePackageFinder::ReplaceCMakePackageFinderVars
static bool ReplaceCMakePackageFinderVars(std::string &string)
Replaces occurrences like $C{PACKAGE_NAME:VAR_NAME} with their CMakePackageFinder value.
Definition: CMakePackageFinder.cpp:630
armarx::FileSystemPathBuilder::GetFormatStringOptions
static std::map< std::string, FormatStringOption > GetFormatStringOptions()
Definition: FileSystemPathBuilder.cpp:41
armarx::FileSystemPathBuilder::FormatStringOption::patternReplacer
std::function< std::string(const std::string &)> patternReplacer
Definition: FileSystemPathBuilder.h:77
armarx::ctrlutil::s
double s(double t, double s0, double v0, double a0, double j)
Definition: CtrlUtil.h:33
FileSystemPathBuilder.h
armarx::FileSystemPathBuilder::ApplyFormattingAndResolveEnvAndCMakeVars
static std::string ApplyFormattingAndResolveEnvAndCMakeVars(const std::string &rawPath)
Definition: FileSystemPathBuilder.cpp:94
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28