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 "FileSystemPathBuilder.h"
25 
26 #include <ctime>
27 
29 
30 #include "../exceptions/local/ExpressionException.h"
31 #include "../system/ArmarXDataPath.h"
32 #include "../system/cmake/CMakePackageFinder.h"
33 #include "Preprocessor.h"
34 
35 namespace armarx
36 {
37  const boost::regex FileSystemPathBuilder::RawPathRegex{"([^{}]+|\\{[^{}]+\\})+"};
38 
39  std::map<std::string, FileSystemPathBuilder::FormatStringOption>
41  {
42  std::map<std::string, FormatStringOption> descr;
44  {
46  }
47  return descr;
48  }
49 
50  std::string
51  FileSystemPathBuilder::ApplyFormatting(const std::string& rawPath)
52  {
53  ARMARX_CHECK_EXPRESSION(boost::regex_match(rawPath, RawPathRegex)) << VAROUT(rawPath);
54 
55  std::stringstream strstr;
56 
57  boost::regex expression{"([^{}]+|\\{([^{}]+)\\})"};
58  std::string::const_iterator start = rawPath.begin();
59  const std::string::const_iterator end = rawPath.end();
60  boost::match_results<std::string::const_iterator> match;
61  while (boost::regex_search(start, end, match, expression))
62  {
63  if (match[2].matched)
64  {
65  const std::string matched{match[2].first, match[2].second};
66  bool hit = false;
68  {
69  const FormatStringOption& opt =
71  if (boost::regex_match(matched, opt.patternRegex))
72  {
73  strstr << opt.patternReplacer(matched);
74  hit = true;
75  break;
76  }
77  }
79  << "pattern '{" << matched << "}' did not match any known FormatString";
80  }
81  else
82  {
83  strstr << match[0];
84  }
85  //advance
86  start = match[0].second;
87  }
88  return strstr.str();
89  }
90 
91  std::string
93  {
94  auto val = rawPath;
97  return ApplyFormatting(val);
98  }
99 
101  std::string name,
102  boost::regex patternRegex,
103  std::function<std::string(const std::string&)> replacer,
104  std::string description)
105  {
107  name, {name, std::move(description), std::move(patternRegex), std::move(replacer)});
108  }
109 
110  //chars
112  "LEFT CURLY",
113  boost::regex{"LEFT CURLY"},
114  [](const std::string&) { return "{"; },
115  "prints {"};
116  FileSystemPathBuilder::RegisterFormatStringOption ARMARX_ANONYMOUS_VARIABLE{
117  "RIGHT CURLY",
118  boost::regex{"RIGHT CURLY"},
119  [](const std::string&) { return "}"; },
120  "prints }"};
121  //date&time
122  FileSystemPathBuilder::RegisterFormatStringOption ARMARX_ANONYMOUS_VARIABLE{
123  "year",
124  boost::regex{"year"},
125  [](const std::string&)
126  {
127  std::time_t t = std::time(nullptr);
128  char buff[8];
129  ARMARX_CHECK_EXPRESSION(std::strftime(buff, sizeof(buff), "%y", std::localtime(&t)));
130  return std::string{buff};
131  },
132  "year (last two digits)"};
133  FileSystemPathBuilder::RegisterFormatStringOption ARMARX_ANONYMOUS_VARIABLE{
134  "Year",
135  boost::regex{"Year"},
136  [](const std::string&)
137  {
138  std::time_t t = std::time(nullptr);
139  char buff[5];
140  ARMARX_CHECK_EXPRESSION(std::strftime(buff, sizeof(buff), "%Y", std::localtime(&t)));
141  return std::string{buff};
142  },
143  "Year (all four digits)"};
144  FileSystemPathBuilder::RegisterFormatStringOption ARMARX_ANONYMOUS_VARIABLE{
145  "month",
146  boost::regex{"month"},
147  [](const std::string&)
148  {
149  std::time_t t = std::time(nullptr);
150  char buff[3];
151  ARMARX_CHECK_EXPRESSION(std::strftime(buff, sizeof(buff), "%m", std::localtime(&t)));
152  return std::string{buff};
153  },
154  "month (01-12)"};
155  FileSystemPathBuilder::RegisterFormatStringOption ARMARX_ANONYMOUS_VARIABLE{
156  "day",
157  boost::regex{"day"},
158  [](const std::string&)
159  {
160  std::time_t t = std::time(nullptr);
161  char buff[3];
162  ARMARX_CHECK_EXPRESSION(std::strftime(buff, sizeof(buff), "%d", std::localtime(&t)));
163  return std::string{buff};
164  },
165  "day (01-31)"};
166  FileSystemPathBuilder::RegisterFormatStringOption ARMARX_ANONYMOUS_VARIABLE{
167  "hour",
168  boost::regex{"hour"},
169  [](const std::string&)
170  {
171  std::time_t t = std::time(nullptr);
172  char buff[3];
173  ARMARX_CHECK_EXPRESSION(std::strftime(buff, sizeof(buff), "%H", std::localtime(&t)));
174  return std::string{buff};
175  },
176  "hour (00-23)"};
177  FileSystemPathBuilder::RegisterFormatStringOption ARMARX_ANONYMOUS_VARIABLE{
178  "minute",
179  boost::regex{"minute"},
180  [](const std::string&)
181  {
182  std::time_t t = std::time(nullptr);
183  char buff[3];
184  ARMARX_CHECK_EXPRESSION(std::strftime(buff, sizeof(buff), "%M", std::localtime(&t)));
185  return std::string{buff};
186  },
187  "minute (00-59)"};
188  FileSystemPathBuilder::RegisterFormatStringOption ARMARX_ANONYMOUS_VARIABLE{
189  "second",
190  boost::regex{"second"},
191  [](const std::string&)
192  {
193  std::time_t t = std::time(nullptr);
194  char buff[3];
195  ARMARX_CHECK_EXPRESSION(std::strftime(buff, sizeof(buff), "%S", std::localtime(&t)));
196  return std::string{buff};
197  },
198  "second (00-59)"};
199  FileSystemPathBuilder::RegisterFormatStringOption ARMARX_ANONYMOUS_VARIABLE{
200  "Time",
201  boost::regex{"Time"},
202  [](const std::string&)
203  {
204  std::time_t t = std::time(nullptr);
205  char buff[9];
207  std::strftime(buff, sizeof(buff), "%H-%M-%S", std::localtime(&t)));
208  return std::string{buff};
209  },
210  "HH-MM-SS"};
211  FileSystemPathBuilder::RegisterFormatStringOption ARMARX_ANONYMOUS_VARIABLE{
212  "Date",
213  boost::regex{"Date"},
214  [](const std::string&)
215  {
216  std::time_t t = std::time(nullptr);
217  char buff[11];
219  std::strftime(buff, sizeof(buff), "%Y-%m-%d", std::localtime(&t)));
220  return std::string{buff};
221  },
222  "YYYY-MM-DD"};
223  FileSystemPathBuilder::RegisterFormatStringOption ARMARX_ANONYMOUS_VARIABLE{
224  "DateTime",
225  boost::regex{"DateTime"},
226  [](const std::string&)
227  {
228  std::time_t t = std::time(nullptr);
229  char buff[20];
231  std::strftime(buff, sizeof(buff), "%Y-%m-%d_%H-%M-%S", std::localtime(&t)));
232  return std::string{buff};
233  },
234  "YYYY-MM-DD_HH-MM-SS"};
235  FileSystemPathBuilder::RegisterFormatStringOption ARMARX_ANONYMOUS_VARIABLE{
236  "time-since-epoch",
237  boost::regex{"time-since-epoch"},
238  [](const std::string&)
239  { return to_string(std::chrono::high_resolution_clock::now().time_since_epoch().count()); },
240  "time since epoch in nanoseconds (can be used as UUID)"};
241  //package paths
242  FileSystemPathBuilder::RegisterFormatStringOption ARMARX_ANONYMOUS_VARIABLE{
243  "PackageDir",
244  boost::regex{"PackageDir:.+"},
245  [](const std::string& s)
246  {
247  const auto pkg = s.substr(11);
248  CMakePackageFinder pf{pkg};
249  ARMARX_CHECK_EXPRESSION(pf.packageFound()) << "package '" + pkg + "' not found!";
250  return pf.getPackageDir();
251  },
252  "PackageDir:<PACKAGE>: Package dir of the armarx Package <PACKAGE>."};
253  FileSystemPathBuilder::RegisterFormatStringOption ARMARX_ANONYMOUS_VARIABLE{
254  "ScenarioDir",
255  boost::regex{"ScenarioDir:.+"},
256  [](const std::string& s)
257  {
258  const auto pkg = s.substr(12);
259  CMakePackageFinder pf{pkg};
260  ARMARX_CHECK_EXPRESSION(pf.packageFound()) << "package '" + pkg + "' not found!";
261  return pf.getScenariosDir();
262  },
263  "ScenarioDir:<PACKAGE>: Scenario dir of the armarx Package <PACKAGE>."};
264  FileSystemPathBuilder::RegisterFormatStringOption ARMARX_ANONYMOUS_VARIABLE{
265  "DataDir",
266  boost::regex{"DataDir:.+"},
267  [](const std::string& s)
268  {
269  const auto pkg = s.substr(8);
270  CMakePackageFinder pf{pkg};
271  ARMARX_CHECK_EXPRESSION(pf.packageFound()) << "package '" + pkg + "' not found!";
272  return pf.getDataDir();
273  },
274  "DataDir:<PACKAGE>: Data dir of the armarx Package <PACKAGE>."};
275  FileSystemPathBuilder::RegisterFormatStringOption ARMARX_ANONYMOUS_VARIABLE{
276  "BuildDir",
277  boost::regex{"BuildDir:.+"},
278  [](const std::string& s)
279  {
280  const auto pkg = s.substr(9);
281  CMakePackageFinder pf{pkg};
282  ARMARX_CHECK_EXPRESSION(pf.packageFound()) << "package '" + pkg + "' not found!";
283  return pf.getBuildDir();
284  },
285  "BuildDir:<PACKAGE>: Build dir of the armarx Package <PACKAGE>."};
286  FileSystemPathBuilder::RegisterFormatStringOption ARMARX_ANONYMOUS_VARIABLE{
287  "BinaryDir",
288  boost::regex{"BinaryDir:.+"},
289  [](const std::string& s)
290  {
291  const auto pkg = s.substr(10);
292  CMakePackageFinder pf{pkg};
293  ARMARX_CHECK_EXPRESSION(pf.packageFound()) << "package '" + pkg + "' not found!";
294  return pf.getBinaryDir();
295  },
296  "BinaryDir:<PACKAGE>: Binary dir of the armarx Package <PACKAGE>."};
297  FileSystemPathBuilder::RegisterFormatStringOption ARMARX_ANONYMOUS_VARIABLE{
298  "CMakeDir",
299  boost::regex{"CMakeDir:.+"},
300  [](const std::string& s)
301  {
302  const auto pkg = s.substr(9);
303  CMakePackageFinder pf{pkg};
304  ARMARX_CHECK_EXPRESSION(pf.packageFound()) << "package '" + pkg + "' not found!";
305  return pf.getCMakeDir();
306  },
307  "CMakeDir:<PACKAGE>: CMake dir of the armarx Package <PACKAGE>."};
308  FileSystemPathBuilder::RegisterFormatStringOption ARMARX_ANONYMOUS_VARIABLE{
309  "SourceDir",
310  boost::regex{"SourceDir:.+"},
311  [](const std::string& s)
312  {
313  const auto pkg = s.substr(10);
314  CMakePackageFinder pf{pkg};
315  ARMARX_CHECK_EXPRESSION(pf.packageFound()) << "package '" + pkg + "' not found!";
316  return pf.getPackageDir() + "/source/" + pkg;
317  },
318  "SourceDir:<PACKAGE>: CMake dir of the armarx Package <PACKAGE>."};
319 
320 } // namespace armarx
armarx::Registrar::get
static const RegisteredType & get(const KeyType &key)
Returns the registered object for the given key.
Definition: Registrar.h:85
armarx::FileSystemPathBuilder::FormatStringOption
Replacer for a pattern.
Definition: FileSystemPathBuilder.h:73
armarx::Registrar::registerElement
static void registerElement(const KeyType &key, RegisteredType element)
Registers an element.
Definition: Registrar.h:108
StringHelpers.h
armarx::Registrar::getKeys
static std::vector< KeyType > getKeys()
Retrieves the list of all registered elements.
Definition: Registrar.h:119
armarx::FileSystemPathBuilder::rawPath
const std::string rawPath
Definition: FileSystemPathBuilder.h:139
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:100
armarx::to_string
const std::string & to_string(const std::string &s)
Definition: StringHelpers.h:41
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:198
armarx::FileSystemPathBuilder::FormatStringOption::patternRegex
boost::regex patternRegex
Definition: FileSystemPathBuilder.h:77
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:480
armarx::FileSystemPathBuilder::RegisterFormatStringOption
Definition: FileSystemPathBuilder.h:81
armarx::CMakePackageFinder::ReplaceCMakePackageFinderVars
static bool ReplaceCMakePackageFinderVars(std::string &string)
Replaces occurrences like $C{PACKAGE_NAME:VAR_NAME} with their CMakePackageFinder value.
Definition: CMakePackageFinder.cpp:664
armarx::FileSystemPathBuilder::GetFormatStringOptions
static std::map< std::string, FormatStringOption > GetFormatStringOptions()
Definition: FileSystemPathBuilder.cpp:40
armarx::FileSystemPathBuilder::FormatStringOption::patternReplacer
std::function< std::string(const std::string &)> patternReplacer
Definition: FileSystemPathBuilder.h:78
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:92
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27