Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include <swri_system_util/file_util.h>
00031
00032 #include <string>
00033
00034 #include <boost/regex.hpp>
00035
00036 namespace swri_system_util
00037 {
00038 boost::filesystem::path NaiveUncomplete(
00039 boost::filesystem::path const path,
00040 boost::filesystem::path const base)
00041 {
00042
00043
00044
00045
00046
00047 #if BOOST_FILESYSTEM_VERSION == 3
00048 const boost::filesystem::path _dot = boost::filesystem::path(".").native();
00049 const boost::filesystem::path _dot_sep = boost::filesystem::path("./").native();
00050 const boost::filesystem::path _dots = boost::filesystem::path("..").native();
00051 const boost::filesystem::path _dots_sep = boost::filesystem::path("../").native();
00052 const boost::filesystem::path _sep = boost::filesystem::path("/").native();
00053 #else
00054 const boost::filesystem::path _dot = std::string(1, boost::filesystem::dot<boost::filesystem::path>::value);
00055 const boost::filesystem::path _sep = std::string(1, boost::filesystem::slash<boost::filesystem::path>::value);
00056 const boost::filesystem::path _dot_sep = _dot.string() + _sep.string();
00057 const boost::filesystem::path _dots = std::string(2, boost::filesystem::dot<boost::filesystem::path>::value);
00058 const boost::filesystem::path _dots_sep = _dots.string() + _sep.string();
00059 #endif // BOOST_FILESYSTE_VERSION == 3
00060
00061 if (path == base) return _dot_sep;
00062
00063 boost::filesystem::path from_path;
00064 boost::filesystem::path from_base;
00065 boost::filesystem::path output;
00066
00067 boost::filesystem::path::iterator path_it = path.begin();
00068 boost::filesystem::path::iterator base_it = base.begin();
00069
00070 if ((path_it == path.end()) || (base_it == base.end()))
00071 {
00072 return "";
00073 }
00074
00075 while (true)
00076 {
00077 if (*path_it != *base_it)
00078 {
00079 for (; base_it != base.end(); ++base_it)
00080 {
00081 if (*base_it == _dot)
00082 continue;
00083 else if (*base_it == _sep)
00084 continue;
00085 output /= _dots_sep;
00086 }
00087
00088 boost::filesystem::path::iterator path_it_start = path_it;
00089 for (; path_it != path.end(); ++path_it)
00090 {
00091 if (path_it != path_it_start)
00092 output /= _sep;
00093
00094 if (*path_it == _dot)
00095 continue;
00096
00097 if (*path_it == _sep)
00098 continue;
00099
00100 output /= *path_it;
00101 }
00102 break;
00103 }
00104
00105 from_path /= boost::filesystem::path(*path_it);
00106 from_base /= boost::filesystem::path(*base_it);
00107
00108 ++path_it, ++base_it;
00109 }
00110
00111 return output;
00112 }
00113
00114 std::vector<std::string> load_all_files(const std::string& path, std::string& directory)
00115 {
00116 std::vector< std::string > all_matching_files;
00117
00118 std::string direct = path.substr(0, path.find_last_of("/\\"));
00119
00120 std::string filename = path.substr(path.find_last_of("/\\")+1);
00121 boost::filesystem::path p(direct);
00122 if ( !exists( p ) )
00123 {
00124 printf("Path %s does not exists\n", p.string().c_str());
00125 return all_matching_files;
00126 }
00127 const boost::regex my_filter(filename.replace(filename.find("*"), std::string("*").length(), ".*\\") );
00128
00129 boost::filesystem::directory_iterator end_itr;
00130 for( boost::filesystem::directory_iterator i( p ); i != end_itr; ++i )
00131 {
00132
00133
00134
00135
00136 if ( boost::filesystem::is_directory(i->status()) )
00137 {
00138 std::string path2 = i->path().string() + std::string("/") + path.substr(path.find_last_of("/\\")+1);
00139 std::string directory2;
00140 std::vector<std::string> matching_files = load_all_files( path2, directory2 );
00141 all_matching_files.insert(all_matching_files.end(), matching_files.begin(), matching_files.end());
00142 }
00143 else if (boost::filesystem::is_regular_file( i->status()))
00144 {
00145 boost::smatch what;
00146
00147 #if BOOST_FILESYSTEM_VERSION == 3
00148 if( !boost::regex_match( i->path().filename().string(), what, my_filter ) ) continue;
00149 #else
00150 if( !boost::regex_match( i->leaf(), what, my_filter ) ) continue;
00151 #endif // BOOST_FILESYSTE_VERSION == 3
00152
00153
00154 all_matching_files.push_back( i->path().string() );
00155 }
00156 std::sort(all_matching_files.begin(), all_matching_files.end());
00157 }
00158 directory = direct;
00159 return all_matching_files;
00160 }
00161
00162 std::vector<std::string> Find(
00163 const std::string& path,
00164 const std::string& expression,
00165 int max_depth)
00166 {
00167 std::vector<std::string> files;
00168
00169 boost::filesystem::path root(path);
00170 if(!boost::filesystem::exists(root) || !boost::filesystem::is_directory(root))
00171 {
00172 return files;
00173 }
00174
00175 boost::regex filter(expression);
00176 boost::filesystem::recursive_directory_iterator it(root);
00177 boost::filesystem::recursive_directory_iterator end_it;
00178 while (it != end_it)
00179 {
00180 if (max_depth >= 0 && it.level() >= max_depth)
00181 {
00182 it.no_push();
00183 }
00184
00185 boost::smatch what;
00186 std::string filename = it->path().filename().string();
00187 if (boost::regex_match(filename, what, filter))
00188 {
00189 files.push_back(it->path().string());
00190 }
00191
00192 ++it;
00193 }
00194
00195 return files;
00196 }
00197 }