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.

90 lines
3.3 KiB

  1. /*******************************************************************************
  2. *
  3. * (C) COPYRIGHT MICROSOFT CORPORATION, 1998, 1999, 2000
  4. *
  5. * TITLE: FINDFILE.CPP
  6. *
  7. * VERSION: 1.0
  8. *
  9. * AUTHOR: ShaunIv
  10. *
  11. * DATE: 1/13/1999
  12. *
  13. * DESCRIPTION: Directory recursing class. A derived class should be created,
  14. * which overrides FoundFile, or you can pass in a callback function
  15. * that is called for each file and directory found. A cancel callback
  16. * is also provided.
  17. *
  18. *******************************************************************************/
  19. #include "precomp.h"
  20. #pragma hdrstop
  21. #include "findfile.h"
  22. #include "simtok.h"
  23. static CSimpleString EnsureTrailingBackslash( const CSimpleString &filename )
  24. {
  25. if (!filename.Length())
  26. return (filename + CSimpleString(TEXT("\\")));
  27. else if (!filename.MatchLastCharacter(TEXT('\\')))
  28. return (filename + CSimpleString(TEXT("\\")));
  29. else return filename;
  30. }
  31. bool RecursiveFindFiles( CSimpleString strDirectory, const CSimpleString &strMask, FindFilesCallback pfnFindFilesCallback, PVOID pvParam, int nStackLevel, const int cnMaxDepth )
  32. {
  33. //
  34. // Prevent stack overflows
  35. //
  36. if (nStackLevel >= cnMaxDepth)
  37. {
  38. return true;
  39. }
  40. WIA_PUSH_FUNCTION((TEXT("RecursiveFindFiles( %s, %s )"), strDirectory.String(), strMask.String() ));
  41. bool bFindResult = true;
  42. bool bContinue = true;
  43. WIN32_FIND_DATA FindData;
  44. HANDLE hFind = FindFirstFile( EnsureTrailingBackslash(strDirectory) + TEXT("*"), &FindData );
  45. if (hFind != INVALID_HANDLE_VALUE)
  46. {
  47. while (bFindResult && bContinue)
  48. {
  49. if ((FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && lstrcmp(FindData.cFileName,TEXT("..")) && lstrcmp(FindData.cFileName,TEXT(".")))
  50. {
  51. if (pfnFindFilesCallback)
  52. bContinue = pfnFindFilesCallback( false, EnsureTrailingBackslash(strDirectory)+FindData.cFileName, &FindData, pvParam );
  53. if (bContinue)
  54. bContinue = RecursiveFindFiles( EnsureTrailingBackslash(strDirectory) + FindData.cFileName, strMask, pfnFindFilesCallback, pvParam, nStackLevel+1 );
  55. }
  56. bFindResult = (FindNextFile(hFind,&FindData) != FALSE);
  57. }
  58. FindClose(hFind);
  59. }
  60. CSimpleStringToken<CSimpleString> strMasks(strMask);
  61. while (bContinue)
  62. {
  63. CSimpleString TempMask = strMasks.Tokenize(TEXT(";"));
  64. if (!TempMask.Length())
  65. break;
  66. TempMask.TrimLeft();
  67. TempMask.TrimRight();
  68. if (TempMask.Length())
  69. {
  70. hFind = FindFirstFile( EnsureTrailingBackslash(strDirectory)+TempMask, &FindData );
  71. if (hFind != INVALID_HANDLE_VALUE)
  72. {
  73. bFindResult = true;
  74. while (bFindResult && bContinue)
  75. {
  76. if (pfnFindFilesCallback)
  77. {
  78. bContinue = pfnFindFilesCallback( true, EnsureTrailingBackslash(strDirectory)+FindData.cFileName, &FindData, pvParam );
  79. }
  80. bFindResult = (FindNextFile(hFind,&FindData) != FALSE);
  81. }
  82. FindClose(hFind);
  83. }
  84. }
  85. }
  86. return bContinue;
  87. }