Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

87 lines
3.0 KiB

  1. #include "precomp.h"
  2. #include "findfile.h"
  3. #include "simtok.h"
  4. static CSimpleString EnsureTrailingBackslash( const CSimpleString &filename )
  5. {
  6. if (!filename.Length())
  7. {
  8. return (filename + CSimpleString(TEXT("\\")));
  9. }
  10. else if (!filename.MatchLastCharacter(TEXT('\\')))
  11. {
  12. return (filename + CSimpleString(TEXT("\\")));
  13. }
  14. else
  15. {
  16. return filename;
  17. }
  18. }
  19. bool RecursiveFindFiles( CSimpleString strDirectory, const CSimpleString &strMask, FindFilesCallback pfnFindFilesCallback, PVOID pvParam )
  20. {
  21. bool bFindResult = true;
  22. bool bContinue = true;
  23. WIN32_FIND_DATA FindData = {0};
  24. HANDLE hFind = ::FindFirstFile( EnsureTrailingBackslash(strDirectory) + TEXT("*"), &FindData );
  25. if (hFind != INVALID_HANDLE_VALUE)
  26. {
  27. while (bFindResult && bContinue)
  28. {
  29. if ( (FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) &&
  30. lstrcmp(FindData.cFileName,TEXT("..")) &&
  31. lstrcmp(FindData.cFileName,TEXT(".")))
  32. {
  33. if (pfnFindFilesCallback)
  34. {
  35. bContinue = pfnFindFilesCallback( false,
  36. EnsureTrailingBackslash(strDirectory)+FindData.cFileName,
  37. &FindData,
  38. pvParam );
  39. }
  40. if (bContinue)
  41. {
  42. bContinue = ::RecursiveFindFiles( EnsureTrailingBackslash(strDirectory) + FindData.cFileName,
  43. strMask,
  44. pfnFindFilesCallback,
  45. pvParam );
  46. }
  47. }
  48. bFindResult = (::FindNextFile(hFind,&FindData) != FALSE);
  49. }
  50. FindClose(hFind);
  51. }
  52. CSimpleStringToken<CSimpleString> strMasks(strMask);
  53. while (bContinue)
  54. {
  55. CSimpleString TempMask = strMasks.Tokenize(TEXT(";"));
  56. if (!TempMask.Length())
  57. {
  58. break;
  59. }
  60. TempMask.TrimLeft();
  61. TempMask.TrimRight();
  62. if (TempMask.Length())
  63. {
  64. hFind = ::FindFirstFile( EnsureTrailingBackslash(strDirectory)+TempMask, &FindData );
  65. if (hFind != INVALID_HANDLE_VALUE)
  66. {
  67. bFindResult = true;
  68. while (bFindResult && bContinue)
  69. {
  70. if (pfnFindFilesCallback)
  71. {
  72. bContinue = pfnFindFilesCallback( true,
  73. EnsureTrailingBackslash(strDirectory)+FindData.cFileName,
  74. &FindData,
  75. pvParam );
  76. }
  77. bFindResult = (FindNextFile(hFind,&FindData) != FALSE);
  78. }
  79. FindClose(hFind);
  80. }
  81. }
  82. }
  83. return bContinue;
  84. }