34 #include <boost/regex.hpp> 39 boost::filesystem::path
const path,
40 boost::filesystem::path
const base)
47 #if BOOST_FILESYSTEM_VERSION == 3 48 const boost::filesystem::path _dot = boost::filesystem::path(
".").native();
49 const boost::filesystem::path _dot_sep = boost::filesystem::path(
"./").native();
50 const boost::filesystem::path _dots = boost::filesystem::path(
"..").native();
51 const boost::filesystem::path _dots_sep = boost::filesystem::path(
"../").native();
52 const boost::filesystem::path _sep = boost::filesystem::path(
"/").native();
54 const boost::filesystem::path _dot = std::string(1, boost::filesystem::dot<boost::filesystem::path>::value);
55 const boost::filesystem::path _sep = std::string(1, boost::filesystem::slash<boost::filesystem::path>::value);
56 const boost::filesystem::path _dot_sep = _dot.string() + _sep.string();
57 const boost::filesystem::path _dots = std::string(2, boost::filesystem::dot<boost::filesystem::path>::value);
58 const boost::filesystem::path _dots_sep = _dots.string() + _sep.string();
59 #endif // BOOST_FILESYSTE_VERSION == 3 61 if (path == base)
return _dot_sep;
63 boost::filesystem::path from_path;
64 boost::filesystem::path from_base;
65 boost::filesystem::path output;
67 boost::filesystem::path::iterator path_it = path.begin();
68 boost::filesystem::path::iterator base_it = base.begin();
70 if ((path_it == path.end()) || (base_it == base.end()))
77 if (*path_it != *base_it)
79 for (; base_it != base.end(); ++base_it)
83 else if (*base_it == _sep)
88 boost::filesystem::path::iterator path_it_start = path_it;
89 for (; path_it != path.end(); ++path_it)
91 if (path_it != path_it_start)
105 from_path /= boost::filesystem::path(*path_it);
106 from_base /= boost::filesystem::path(*base_it);
108 ++path_it, ++base_it;
114 std::vector<std::string>
load_all_files(
const std::string& path, std::string& directory)
116 std::vector< std::string > all_matching_files;
118 std::string direct = path.substr(0, path.find_last_of(
"/\\"));
120 std::string filename = path.substr(path.find_last_of(
"/\\")+1);
121 boost::filesystem::path p(direct);
124 printf(
"Path %s does not exists\n", p.string().c_str());
125 return all_matching_files;
127 const boost::regex my_filter(filename.replace(filename.find(
"*"), std::string(
"*").length(),
".*\\") );
129 boost::filesystem::directory_iterator end_itr;
130 for( boost::filesystem::directory_iterator i( p ); i != end_itr; ++i )
136 if ( boost::filesystem::is_directory(i->status()) )
138 std::string path2 = i->path().string() + std::string(
"/") + path.substr(path.find_last_of(
"/\\")+1);
139 std::string directory2;
140 std::vector<std::string> matching_files =
load_all_files( path2, directory2 );
141 all_matching_files.insert(all_matching_files.end(), matching_files.begin(), matching_files.end());
143 else if (boost::filesystem::is_regular_file( i->status()))
147 #if BOOST_FILESYSTEM_VERSION == 3 148 if( !boost::regex_match( i->path().filename().string(), what, my_filter ) )
continue;
150 if( !boost::regex_match( i->leaf(), what, my_filter ) )
continue;
151 #endif // BOOST_FILESYSTE_VERSION == 3 154 all_matching_files.push_back( i->path().string() );
156 std::sort(all_matching_files.begin(), all_matching_files.end());
159 return all_matching_files;
163 const std::string& path,
164 const std::string& expression,
167 std::vector<std::string> files;
169 boost::filesystem::path root(path);
170 if(!boost::filesystem::exists(root) || !boost::filesystem::is_directory(root))
175 boost::regex filter(expression);
176 boost::filesystem::recursive_directory_iterator it(root);
177 boost::filesystem::recursive_directory_iterator end_it;
180 if (max_depth >= 0 && it.level() >= max_depth)
186 std::string filename = it->path().filename().string();
187 if (boost::regex_match(filename, what, filter))
189 files.push_back(it->path().string());
std::vector< std::string > Find(const std::string &path, const std::string &expression, int max_depth=-1)
boost::filesystem::path NaiveUncomplete(boost::filesystem::path const path, boost::filesystem::path const base)
std::vector< std::string > load_all_files(const std::string &path, std::string &directory)
ROSCPP_DECL bool exists(const std::string &service_name, bool print_failure_reason)