| 
									
										
										
										
											2013-04-17 23:09:55 -04:00
										 |  |  | // Copyright 2013 Dolphin Emulator Project
 | 
					
						
							|  |  |  | // Licensed under GPLv2
 | 
					
						
							|  |  |  | // Refer to the license.txt file included.
 | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "Common.h"
 | 
					
						
							| 
									
										
										
										
											2010-07-20 03:23:25 +00:00
										 |  |  | #include "CommonPaths.h"
 | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | #ifndef _WIN32
 | 
					
						
							|  |  |  | #include <sys/types.h>
 | 
					
						
							|  |  |  | #include <dirent.h>
 | 
					
						
							| 
									
										
										
										
											2009-01-16 02:58:34 +00:00
										 |  |  | #else
 | 
					
						
							|  |  |  | #include <windows.h>
 | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <string>
 | 
					
						
							| 
									
										
										
										
											2013-01-13 15:32:26 -06:00
										 |  |  | #include <algorithm>
 | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include "FileSearch.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "StringUtil.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | CFileSearch::CFileSearch(const CFileSearch::XStringVector& _rSearchStrings, const CFileSearch::XStringVector& _rDirectories) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	// Reverse the loop order for speed?
 | 
					
						
							|  |  |  | 	for (size_t j = 0; j < _rSearchStrings.size(); j++) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		for (size_t i = 0; i < _rDirectories.size(); i++) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			FindFiles(_rSearchStrings[j], _rDirectories[i]); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void CFileSearch::FindFiles(const std::string& _searchString, const std::string& _strPath) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	std::string GCMSearchPath; | 
					
						
							|  |  |  | 	BuildCompleteFilename(GCMSearchPath, _strPath, _searchString); | 
					
						
							|  |  |  | #ifdef _WIN32
 | 
					
						
							|  |  |  | 	WIN32_FIND_DATA findData; | 
					
						
							| 
									
										
										
										
											2013-02-27 18:51:02 -06:00
										 |  |  | 	HANDLE FindFirst = FindFirstFile(UTF8ToTStr(GCMSearchPath).c_str(), &findData); | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (FindFirst != INVALID_HANDLE_VALUE) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		bool bkeepLooping = true; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		while (bkeepLooping) | 
					
						
							|  |  |  | 		{			 | 
					
						
							|  |  |  | 			if (findData.cFileName[0] != '.') | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				std::string strFilename; | 
					
						
							| 
									
										
										
										
											2013-02-27 18:51:02 -06:00
										 |  |  | 				BuildCompleteFilename(strFilename, _strPath, TStrToUTF8(findData.cFileName)); | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 				m_FileNames.push_back(strFilename); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			bkeepLooping = FindNextFile(FindFirst, &findData) ? true : false; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	FindClose(FindFirst); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2013-01-13 15:32:26 -06:00
										 |  |  | 	// TODO: super lame/broken
 | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-01-13 15:32:26 -06:00
										 |  |  | 	auto end_match(_searchString); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// assuming we have a "*.blah"-like pattern
 | 
					
						
							|  |  |  | 	if (!end_match.empty() && end_match[0] == '*') | 
					
						
							|  |  |  | 		end_match.erase(0, 1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// ugly
 | 
					
						
							|  |  |  | 	if (end_match == ".*") | 
					
						
							|  |  |  | 		end_match.clear(); | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	DIR* dir = opendir(_strPath.c_str()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (!dir) | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-01-13 15:32:26 -06:00
										 |  |  | 	while (auto const dp = readdir(dir)) | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2013-01-13 15:32:26 -06:00
										 |  |  | 		std::string found(dp->d_name); | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-01-13 15:32:26 -06:00
										 |  |  | 		if ((found != ".") && (found != "..") | 
					
						
							|  |  |  | 			&& (found.size() >= end_match.size()) | 
					
						
							|  |  |  | 			&& std::equal(end_match.rbegin(), end_match.rend(), found.rbegin())) | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2010-04-08 16:59:35 +00:00
										 |  |  | 			std::string full_name; | 
					
						
							|  |  |  | 			if (_strPath.c_str()[_strPath.size()-1] == DIR_SEP_CHR) | 
					
						
							| 
									
										
										
										
											2013-01-13 15:32:26 -06:00
										 |  |  | 				full_name = _strPath + found; | 
					
						
							| 
									
										
										
										
											2010-04-08 16:59:35 +00:00
										 |  |  | 			else | 
					
						
							| 
									
										
										
										
											2013-01-13 15:32:26 -06:00
										 |  |  | 				full_name = _strPath + DIR_SEP + found; | 
					
						
							| 
									
										
										
										
											2010-01-13 21:09:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 			m_FileNames.push_back(full_name); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	closedir(dir); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const CFileSearch::XStringVector& CFileSearch::GetFileNames() const | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2010-04-08 16:59:35 +00:00
										 |  |  | 	return m_FileNames; | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | } |