pinocchio  UNKNOWN
file-explorer.hpp
1 //
2 // Copyright (c) 2016 CNRS
3 //
4 // This file is part of Pinocchio
5 // Pinocchio is free software: you can redistribute it
6 // and/or modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation, either version
8 // 3 of the License, or (at your option) any later version.
9 //
10 // Pinocchio is distributed in the hope that it will be
11 // useful, but WITHOUT ANY WARRANTY; without even the implied warranty
12 // of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // General Lesser Public License for more details. You should have
14 // received a copy of the GNU Lesser General Public License along with
15 // Pinocchio If not, see
16 // <http://www.gnu.org/licenses/>.
17 
18 #ifndef __se3_file_explorer_hpp__
19 #define __se3_file_explorer_hpp__
20 
21 #include <string>
22 #include <iostream>
23 #include <vector>
24 
25 namespace se3
26 {
27 
36  inline std::vector<std::string> extractPathFromEnvVar(const std::string & env_var_name, const std::string & delimiter = ":")
37  {
38  const char * env_var_value = std::getenv(env_var_name.c_str());
39  std::vector<std::string> env_var_paths;
40 
41  if (env_var_value != NULL)
42  {
43  std::string policyStr (env_var_value);
44  // Add a separator at the end so that last path is also retrieved
45  policyStr += std::string (":");
46  size_t lastOffset = 0;
47 
48  while(true)
49  {
50  size_t offset = policyStr.find_first_of(delimiter, lastOffset);
51  if (offset < policyStr.size())
52  env_var_paths.push_back(policyStr.substr(lastOffset, offset - lastOffset));
53  if (offset == std::string::npos)
54  break;
55  else
56  lastOffset = offset + 1; // add one to skip the delimiter
57  }
58  }
59 
60  return env_var_paths;
61  }
62 
63 
69  inline std::vector<std::string> rosPaths()
70  {
71  return extractPathFromEnvVar("ROS_PACKAGE_PATH");
72  }
73 
74 }
75 
76 #endif // __se3_file_explorer_hpp__
std::vector< std::string > extractPathFromEnvVar(const std::string &env_var_name, const std::string &delimiter=":")
Parse an environment variable if exists and extract paths according to the delimiter.
std::vector< std::string > rosPaths()
Parse the environment variable ROS_PACKAGE_PATH and extract paths.