1 // Copyright (C) 2009 Davis E. King (davis@dlib.net) 2 // License: Boost Software License See LICENSE.txt for the full license. 3 #ifndef DLIB_DIR_NAV_EXTENSIONs_CPP_ 4 #define DLIB_DIR_NAV_EXTENSIONs_CPP_ 5 6 #include "dir_nav_extensions.h" 7 8 namespace dlib 9 { 10 11 // ---------------------------------------------------------------------------------------- 12 13 namespace implementation_details 14 { get_all_sub_dirs(const directory & top_of_tree,unsigned long max_depth,std::vector<directory> & result,std::vector<directory> & temp)15 void get_all_sub_dirs ( 16 const directory& top_of_tree, 17 unsigned long max_depth, 18 std::vector<directory>& result, 19 std::vector<directory>& temp 20 ) 21 { 22 if (max_depth > 0) 23 { 24 top_of_tree.get_dirs(temp); 25 const unsigned long start = result.size(); 26 result.insert(result.end(), temp.begin(), temp.end()); 27 const unsigned long end = start + temp.size(); 28 29 for (unsigned long i = start; i < end; ++i) 30 { 31 get_all_sub_dirs(result[i], max_depth-1, result, temp); 32 } 33 } 34 } 35 } 36 37 // ---------------------------------------------------------------------------------------- 38 file_exists(const std::string & filename)39 bool file_exists ( 40 const std::string& filename 41 ) 42 { 43 try 44 { 45 dlib::file temp(filename); 46 return true; 47 } 48 catch (file::file_not_found&) 49 { 50 return false; 51 } 52 } 53 54 // ---------------------------------------------------------------------------------------- 55 get_parent_directory(const directory & dir)56 directory get_parent_directory ( 57 const directory& dir 58 ) 59 { 60 return dir.get_parent(); 61 } 62 63 // ---------------------------------------------------------------------------------------- 64 get_parent_directory(const file & f)65 directory get_parent_directory ( 66 const file& f 67 ) 68 { 69 if (f.full_name().size() == 0) 70 return directory(); 71 72 std::string::size_type pos = f.full_name().find_last_of("\\/"); 73 74 if (pos == std::string::npos) 75 return directory(); 76 77 return directory(f.full_name().substr(0,pos)); 78 } 79 80 // ---------------------------------------------------------------------------------------- 81 select_oldest_file(const std::string & filename1,const std::string & filename2)82 std::string select_oldest_file ( 83 const std::string& filename1, 84 const std::string& filename2 85 ) 86 { 87 file f1, f2; 88 try{f1 = file(filename1);} catch(file::file_not_found&) { return filename1; } 89 try{f2 = file(filename2);} catch(file::file_not_found&) { return filename2; } 90 91 if (f1.last_modified() < f2.last_modified()) 92 return filename1; 93 else 94 return filename2; 95 } 96 97 // ---------------------------------------------------------------------------------------- 98 select_newest_file(const std::string & filename1,const std::string & filename2)99 std::string select_newest_file ( 100 const std::string& filename1, 101 const std::string& filename2 102 ) 103 { 104 file f1, f2; 105 try{f1 = file(filename1);} catch(file::file_not_found&) { return filename2; } 106 try{f2 = file(filename2);} catch(file::file_not_found&) { return filename1; } 107 108 if (f1.last_modified() > f2.last_modified()) 109 return filename1; 110 else 111 return filename2; 112 } 113 114 // ---------------------------------------------------------------------------------------- 115 116 } 117 118 #endif // DLIB_DIR_NAV_EXTENSIONs_CPP_ 119 120 121 122