9 #include "../package_registry.h" 11 #include <boost/algorithm/string/trim.hpp> 12 #include <boost/filesystem.hpp> 16 namespace fs = boost::filesystem;
23 using Handler = std::function<std::string(const std::string&, const std::string&)>;
33 namespace substitutions
37 std::string base = name;
49 std::string value = it->second;
54 "$(arg {}): Accessing unset argument '{}', please specify as parameter.", name, name
63 fs::path launch_file = context.
filename();
64 return fs::absolute(launch_file).parent_path().string();
67 std::string
env(
const std::string& name)
69 const char* envval = getenv(name.c_str());
76 std::string
optenv(
const std::string& name,
const std::string& defaultValue)
78 const char* envval = getenv(name.c_str());
98 std::size_t begin = 0;
100 for(std::size_t i = 0; i < input.size(); ++i)
123 std::string contents = input.substr(begin, i - begin);
124 std::string after = input.substr(i+1);
127 auto pos = contents.find(
' ');
128 std::string name = contents.substr(0, pos);
130 if(pos != std::string::npos)
131 args = contents.substr(pos+1);
137 auto it = handlers.find(name);
138 if(it != handlers.end())
140 std::string replacement = it->second(args, after);
142 std::stringstream ss;
143 ss << input.substr(0, begin-2);
169 std::string buffer = input;
172 if(buffer.size() > 6 && buffer.substr(0,6) ==
"$(eval" && buffer[buffer.size()-1] ==
')')
174 return evaluatePython(buffer.substr(7, buffer.size() - 7 - 1), context);
178 {
"anon", [&context](
const std::string& args,
const std::string&) -> std::string{
181 {
"arg", [&context](
const std::string& args,
const std::string&) -> std::string{
184 {
"dirname", [&context](
const std::string&,
const std::string&) -> std::string{
187 {
"env", [](
const std::string& args,
const std::string&) -> std::string{
190 {
"optenv", [](
const std::string& args,
const std::string&) -> std::string{
191 auto pos = args.find(
' ');
192 std::string defaultValue;
193 std::string name = args;
194 if(pos != std::string::npos)
196 defaultValue = args.substr(pos + 1);
197 name = args.substr(0, pos);
211 {
"find", [](
const std::string& args,
const std::string& after) -> std::string{
213 auto pos = after.find(
' ');
214 std::string filename = after.substr(0, pos);
215 boost::trim(filename);
217 if(!filename.empty())
std::string anon(const std::string &name, ParseContext &context)
std::string simplifyWhitespace(const std::string &input)
Compress any sequence of whitespace to single spaces.
static std::string parseOneElement(const std::string &input, const HandlerMap &handlers, bool strict, bool *found)
std::map< std::string, Handler > HandlerMap
std::function< std::string(const std::string &, const std::string &)> Handler
std::string evaluatePython(const std::string &input, ParseContext &context)
Evaluate a $(eval ...) python expression.
static SubstitutionException format(const char *format, const Args &...args)
const char * UNSET_MARKER
std::string anonName(const std::string &base)
std::string dirname(const ParseContext &context)
const std::string & filename() const
static std::string getPath(const std::string &package)
static std::string findPathToFile(const std::string &package, const std::string &name)
Find path to a package file.
std::string optenv(const std::string &name, const std::string &defaultValue)
std::string env(const std::string &name)
std::string arg(const std::string &name, const ParseContext &context)
std::string parseSubstitutionArgs(const std::string &input, ParseContext &context)
std::string find_stupid(const std::string &name)
$(find ...) which always gives rospack find results
const std::map< std::string, std::string > & arguments() const