| 
									
										
										
										
											2015-05-24 06:55:12 +02:00
										 |  |  | // Copyright 2008 Dolphin Emulator Project
 | 
					
						
							| 
									
										
										
										
											2015-05-18 01:08:10 +02:00
										 |  |  | // Licensed under GPLv2+
 | 
					
						
							| 
									
										
										
										
											2013-04-17 23:09:55 -04:00
										 |  |  | // Refer to the license.txt file included.
 | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-15 15:46:40 -05:00
										 |  |  | #if !__clang__ && __GNUC__ == 4 && __GNUC_MINOR__ < 9
 | 
					
						
							|  |  |  | #error <regex> is broken in GCC < 4.9; please upgrade
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-17 05:18:15 -05:00
										 |  |  | #include <algorithm>
 | 
					
						
							| 
									
										
										
										
											2014-11-15 15:46:40 -05:00
										 |  |  | #include <functional>
 | 
					
						
							|  |  |  | #include <regex>
 | 
					
						
							| 
									
										
										
										
											2014-02-17 05:18:15 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include "Common/CommonPaths.h"
 | 
					
						
							|  |  |  | #include "Common/FileSearch.h"
 | 
					
						
							| 
									
										
										
										
											2014-11-15 15:46:40 -05:00
										 |  |  | #include "Common/FileUtil.h"
 | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-15 15:46:40 -05:00
										 |  |  | static std::vector<std::string> FileSearchWithTest(const std::vector<std::string>& directories, bool recursive, std::function<bool(const File::FSTEntry &)> callback) | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2014-11-15 15:46:40 -05:00
										 |  |  | 	std::vector<std::string> result; | 
					
						
							|  |  |  | 	for (const std::string& directory : directories) | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2014-11-15 15:46:40 -05:00
										 |  |  | 		File::FSTEntry top = File::ScanDirectoryTree(directory, recursive); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		std::function<void(File::FSTEntry&)> DoEntry; | 
					
						
							|  |  |  | 		DoEntry = [&](File::FSTEntry& entry) { | 
					
						
							|  |  |  | 			if (callback(entry)) | 
					
						
							|  |  |  | 				result.push_back(entry.physicalName); | 
					
						
							|  |  |  | 			for (auto& child : entry.children) | 
					
						
							|  |  |  | 				DoEntry(child); | 
					
						
							|  |  |  | 		}; | 
					
						
							| 
									
										
										
										
											2015-06-21 16:03:32 -04:00
										 |  |  | 		for (auto& child : top.children) | 
					
						
							|  |  |  | 			DoEntry(child); | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-11-15 15:46:40 -05:00
										 |  |  | 	// remove duplicates
 | 
					
						
							|  |  |  | 	std::sort(result.begin(), result.end()); | 
					
						
							|  |  |  | 	result.erase(std::unique(result.begin(), result.end()), result.end()); | 
					
						
							|  |  |  | 	return result; | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-15 15:46:40 -05:00
										 |  |  | std::vector<std::string> DoFileSearch(const std::vector<std::string>& globs, const std::vector<std::string>& directories, bool recursive) | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2014-11-15 15:46:40 -05:00
										 |  |  | 	std::string regex_str = "^("; | 
					
						
							|  |  |  | 	for (const auto& str : globs) | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2014-11-15 15:46:40 -05:00
										 |  |  | 		if (regex_str.size() != 2) | 
					
						
							|  |  |  | 			regex_str += "|"; | 
					
						
							|  |  |  | 		// convert glob to regex
 | 
					
						
							|  |  |  | 		regex_str += std::regex_replace(std::regex_replace(str, std::regex("\\."), "\\."), std::regex("\\*"), ".*"); | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-11-15 15:46:40 -05:00
										 |  |  | 	regex_str += ")$"; | 
					
						
							| 
									
										
										
										
											2015-06-08 21:39:00 -04:00
										 |  |  | 	std::regex regex(regex_str, std::regex_constants::icase); | 
					
						
							| 
									
										
										
										
											2014-11-15 15:46:40 -05:00
										 |  |  | 	return FileSearchWithTest(directories, recursive, [&](const File::FSTEntry& entry) { | 
					
						
							|  |  |  | 		return std::regex_match(entry.virtualName, regex); | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-21 16:03:32 -04:00
										 |  |  | // Result includes the passed directories themselves as well as their subdirectories.
 | 
					
						
							| 
									
										
										
										
											2014-11-15 15:46:40 -05:00
										 |  |  | std::vector<std::string> FindSubdirectories(const std::vector<std::string>& directories, bool recursive) | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2014-11-15 15:46:40 -05:00
										 |  |  | 	return FileSearchWithTest(directories, true, [&](const File::FSTEntry& entry) { | 
					
						
							|  |  |  | 		return entry.isDirectory; | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | } |