DirectoryIterator_WIN32.cpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. //
  2. // DirectoryIterator_WIN32.cpp
  3. //
  4. // $Id: //poco/1.4/Foundation/src/DirectoryIterator_WIN32.cpp#1 $
  5. //
  6. // Library: Foundation
  7. // Package: Filesystem
  8. // Module: DirectoryIterator
  9. //
  10. // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #include "Poco/DirectoryIterator_WIN32.h"
  16. #include "Poco/File_WIN32.h"
  17. #include "Poco/Path.h"
  18. namespace Poco {
  19. DirectoryIteratorImpl::DirectoryIteratorImpl(const std::string& path): _fh(INVALID_HANDLE_VALUE), _rc(1)
  20. {
  21. Path p(path);
  22. p.makeDirectory();
  23. std::string findPath = p.toString();
  24. findPath.append("*");
  25. _fh = FindFirstFile(findPath.c_str(), &_fd);
  26. if (_fh == INVALID_HANDLE_VALUE)
  27. {
  28. if (GetLastError() != ERROR_NO_MORE_FILES)
  29. File::handleLastError(path);
  30. }
  31. else
  32. {
  33. _current = _fd.cFileName;
  34. if (_current == "." || _current == "..")
  35. next();
  36. }
  37. }
  38. DirectoryIteratorImpl::~DirectoryIteratorImpl()
  39. {
  40. if (_fh != INVALID_HANDLE_VALUE)
  41. FindClose(_fh);
  42. }
  43. const std::string& DirectoryIteratorImpl::next()
  44. {
  45. do
  46. {
  47. if (FindNextFile(_fh, &_fd) != 0)
  48. _current = _fd.cFileName;
  49. else
  50. _current.clear();
  51. }
  52. while (_current == "." || _current == "..");
  53. return _current;
  54. }
  55. } // namespace Poco