crn_find_files.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. // File: crn_win32_find_files.cpp
  2. // See Copyright Notice and license at the end of inc/crnlib.h
  3. #include "crn_core.h"
  4. #include "crn_find_files.h"
  5. #include "crn_file_utils.h"
  6. #include "crn_strutils.h"
  7. #ifdef CRNLIB_USE_WIN32_API
  8. #include "crn_winhdr.h"
  9. #elif defined(__GNUC__)
  10. #include <fnmatch.h>
  11. #include <dirent.h>
  12. #endif
  13. namespace crnlib
  14. {
  15. #ifdef CRNLIB_USE_WIN32_API
  16. bool find_files::find(const char* pBasepath, const char* pFilespec, uint flags)
  17. {
  18. m_last_error = S_OK;
  19. m_files.resize(0);
  20. return find_internal(pBasepath, "", pFilespec, flags, 0);
  21. }
  22. bool find_files::find(const char* pSpec, uint flags)
  23. {
  24. dynamic_string find_name(pSpec);
  25. if (!file_utils::full_path(find_name))
  26. return false;
  27. dynamic_string find_pathname, find_filename;
  28. if (!file_utils::split_path(find_name.get_ptr(), find_pathname, find_filename))
  29. return false;
  30. return find(find_pathname.get_ptr(), find_filename.get_ptr(), flags);
  31. }
  32. bool find_files::find_internal(const char* pBasepath, const char* pRelpath, const char* pFilespec, uint flags, int level)
  33. {
  34. WIN32_FIND_DATAA find_data;
  35. dynamic_string filename;
  36. dynamic_string_array child_paths;
  37. if (flags & cFlagRecursive)
  38. {
  39. if (strlen(pRelpath))
  40. file_utils::combine_path(filename, pBasepath, pRelpath, "*");
  41. else
  42. file_utils::combine_path(filename, pBasepath, "*");
  43. HANDLE handle = FindFirstFileA(filename.get_ptr(), &find_data);
  44. if (handle == INVALID_HANDLE_VALUE)
  45. {
  46. HRESULT hres = GetLastError();
  47. if ((level == 0) && (hres != NO_ERROR) && (hres != ERROR_FILE_NOT_FOUND))
  48. {
  49. m_last_error = hres;
  50. return false;
  51. }
  52. }
  53. else
  54. {
  55. do
  56. {
  57. const bool is_dir = (find_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
  58. bool skip = !is_dir;
  59. if (is_dir)
  60. skip = (strcmp(find_data.cFileName, ".") == 0) || (strcmp(find_data.cFileName, "..") == 0);
  61. if (find_data.dwFileAttributes & (FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_TEMPORARY))
  62. skip = true;
  63. if (find_data.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN)
  64. {
  65. if ((flags & cFlagAllowHidden) == 0)
  66. skip = true;
  67. }
  68. if (!skip)
  69. {
  70. dynamic_string child_path(find_data.cFileName);
  71. if ((!child_path.count_char('?')) && (!child_path.count_char('*')))
  72. child_paths.push_back(child_path);
  73. }
  74. } while (FindNextFileA(handle, &find_data) != 0);
  75. HRESULT hres = GetLastError();
  76. FindClose(handle);
  77. handle = INVALID_HANDLE_VALUE;
  78. if (hres != ERROR_NO_MORE_FILES)
  79. {
  80. m_last_error = hres;
  81. return false;
  82. }
  83. }
  84. }
  85. if (strlen(pRelpath))
  86. file_utils::combine_path(filename, pBasepath, pRelpath, pFilespec);
  87. else
  88. file_utils::combine_path(filename, pBasepath, pFilespec);
  89. HANDLE handle = FindFirstFileA(filename.get_ptr(), &find_data);
  90. if (handle == INVALID_HANDLE_VALUE)
  91. {
  92. HRESULT hres = GetLastError();
  93. if ((level == 0) && (hres != NO_ERROR) && (hres != ERROR_FILE_NOT_FOUND))
  94. {
  95. m_last_error = hres;
  96. return false;
  97. }
  98. }
  99. else
  100. {
  101. do
  102. {
  103. const bool is_dir = (find_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
  104. bool skip = false;
  105. if (is_dir)
  106. skip = (strcmp(find_data.cFileName, ".") == 0) || (strcmp(find_data.cFileName, "..") == 0);
  107. if (find_data.dwFileAttributes & (FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_TEMPORARY))
  108. skip = true;
  109. if (find_data.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN)
  110. {
  111. if ((flags & cFlagAllowHidden) == 0)
  112. skip = true;
  113. }
  114. if (!skip)
  115. {
  116. if (((is_dir) && (flags & cFlagAllowDirs)) || ((!is_dir) && (flags & cFlagAllowFiles)))
  117. {
  118. m_files.resize(m_files.size() + 1);
  119. file_desc& file = m_files.back();
  120. file.m_is_dir = is_dir;
  121. file.m_base = pBasepath;
  122. file.m_name = find_data.cFileName;
  123. file.m_rel = pRelpath;
  124. if (strlen(pRelpath))
  125. file_utils::combine_path(file.m_fullname, pBasepath, pRelpath, find_data.cFileName);
  126. else
  127. file_utils::combine_path(file.m_fullname, pBasepath, find_data.cFileName);
  128. }
  129. }
  130. } while (FindNextFileA(handle, &find_data) != 0);
  131. HRESULT hres = GetLastError();
  132. FindClose(handle);
  133. if (hres != ERROR_NO_MORE_FILES)
  134. {
  135. m_last_error = hres;
  136. return false;
  137. }
  138. }
  139. for (uint i = 0; i < child_paths.size(); i++)
  140. {
  141. dynamic_string child_path;
  142. if (strlen(pRelpath))
  143. file_utils::combine_path(child_path, pRelpath, child_paths[i].get_ptr());
  144. else
  145. child_path = child_paths[i];
  146. if (!find_internal(pBasepath, child_path.get_ptr(), pFilespec, flags, level + 1))
  147. return false;
  148. }
  149. return true;
  150. }
  151. #elif defined(__GNUC__)
  152. bool find_files::find(const char* pBasepath, const char* pFilespec, uint flags)
  153. {
  154. m_files.resize(0);
  155. return find_internal(pBasepath, "", pFilespec, flags, 0);
  156. }
  157. bool find_files::find(const char* pSpec, uint flags)
  158. {
  159. dynamic_string find_name(pSpec);
  160. if (!file_utils::full_path(find_name))
  161. return false;
  162. dynamic_string find_pathname, find_filename;
  163. if (!file_utils::split_path(find_name.get_ptr(), find_pathname, find_filename))
  164. return false;
  165. return find(find_pathname.get_ptr(), find_filename.get_ptr(), flags);
  166. }
  167. bool find_files::find_internal(const char* pBasepath, const char* pRelpath, const char* pFilespec, uint flags, int level)
  168. {
  169. dynamic_string pathname;
  170. if (strlen(pRelpath))
  171. file_utils::combine_path(pathname, pBasepath, pRelpath);
  172. else
  173. pathname = pBasepath;
  174. if (!pathname.is_empty())
  175. {
  176. char c = pathname.back();
  177. if (c != '/')
  178. pathname += "/";
  179. }
  180. DIR *dp = opendir(pathname.get_ptr());
  181. if (!dp)
  182. return level ? true : false;
  183. dynamic_string_array paths;
  184. for ( ; ; )
  185. {
  186. struct dirent *ep = readdir(dp);
  187. if (!ep)
  188. break;
  189. if ((strcmp(ep->d_name, ".") == 0) || (strcmp(ep->d_name, "..") == 0))
  190. continue;
  191. const bool is_directory = (ep->d_type & DT_DIR) != 0;
  192. const bool is_file = (ep->d_type & DT_REG) != 0;
  193. dynamic_string filename(ep->d_name);
  194. if (is_directory)
  195. {
  196. if (flags & cFlagRecursive)
  197. {
  198. paths.push_back(filename);
  199. }
  200. }
  201. if (((is_file) && (flags & cFlagAllowFiles)) || ((is_directory) && (flags & cFlagAllowDirs)))
  202. {
  203. if (0 == fnmatch(pFilespec, filename.get_ptr(), 0))
  204. {
  205. m_files.resize(m_files.size() + 1);
  206. file_desc& file = m_files.back();
  207. file.m_is_dir = is_directory;
  208. file.m_base = pBasepath;
  209. file.m_rel = pRelpath;
  210. file.m_name = filename;
  211. file.m_fullname = pathname + filename;
  212. }
  213. }
  214. }
  215. closedir(dp);
  216. dp = NULL;
  217. if (flags & cFlagRecursive)
  218. {
  219. for (uint i = 0; i < paths.size(); i++)
  220. {
  221. dynamic_string childpath;
  222. if (strlen(pRelpath))
  223. file_utils::combine_path(childpath, pRelpath, paths[i].get_ptr());
  224. else
  225. childpath = paths[i];
  226. if (!find_internal(pBasepath, childpath.get_ptr(), pFilespec, flags, level + 1))
  227. return false;
  228. }
  229. }
  230. return true;
  231. }
  232. #else
  233. #error Unimplemented
  234. #endif
  235. } // namespace crnlib