FindFile.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /******************************************************************************
  2. |* THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
  3. |* ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  4. |* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  5. |* PARTICULAR PURPOSE.
  6. |*
  7. |* Copyright 1995-2004 Ahead Software AG. All Rights Reserved.
  8. |*-----------------------------------------------------------------------------
  9. |* NeroSDK / NeroCmd
  10. |*
  11. |* PROGRAM: FindFile.cpp
  12. |*
  13. |* PURPOSE: Implementation of helper class for ISO tree handling
  14. ******************************************************************************/
  15. #include "stdafx.h"
  16. #include "FindFile.h"
  17. #include <string>
  18. using namespace std;
  19. // Constructor
  20. CFindFiles::CFindFiles (LPCSTR psPath)
  21. {
  22. // Make sure the path is not NULL.
  23. //
  24. _ASSERTE (psPath != NULL);
  25. // Store the path in std::string for ease of use.
  26. //
  27. const string sOriginalPath (psPath);
  28. string sPathBeforeWildcard;
  29. // Find the last occurrence of backslash...
  30. //
  31. string::size_type pos = sOriginalPath.rfind ("\\");
  32. if (pos == string::npos)
  33. {
  34. // Let the wildcard be the whole path as no backslash was found
  35. // and the path-before-wildcard is a DOT, which means the current
  36. // directory.
  37. //
  38. m_sWildcard = sOriginalPath;
  39. sPathBeforeWildcard = ".";
  40. }
  41. else
  42. {
  43. // If there is a backslash, let the wildcard be everything after
  44. // the backslash and path-before-wildcard is everything up to the
  45. // backslash (excluding).
  46. //
  47. m_sWildcard.assign (sOriginalPath.c_str () + pos + 1);
  48. sPathBeforeWildcard = sOriginalPath;
  49. sPathBeforeWildcard.erase (pos);
  50. }
  51. // Now that we have extracted what we believe is a wildcard, make
  52. // sure it really is. It must contain at least one of * or ? (asterisk
  53. // and question mark). Depending on whether it is a wildcard or not
  54. // we will use a different path for directory search.
  55. //
  56. string sDirectorySearchPath;
  57. if (m_sWildcard.find ("*") == string::npos &&
  58. m_sWildcard.find ("?") == string::npos)
  59. {
  60. // If asterisk and question mark are not found, then it wasn't a
  61. // wildcard at all. Let the wildcard be a single asterisk which
  62. // means ALL FILES.
  63. //
  64. m_sWildcard = "*";
  65. sDirectorySearchPath = sOriginalPath;
  66. }
  67. else
  68. {
  69. sDirectorySearchPath = sPathBeforeWildcard + "\\*";
  70. }
  71. // Obtain a find handle.
  72. //
  73. m_lHandleFile = _findfirst ((char*)sOriginalPath.c_str (), &m_fdFile);
  74. // The operation succeeded if the returned handle is not -1.
  75. //
  76. m_bValidFile = (-1 != m_lHandleFile);
  77. // Search for a first non-directory entry.
  78. //
  79. while (m_bValidFile && 0 != (m_fdFile.attrib & _A_SUBDIR))
  80. {
  81. m_bValidFile = (0 == _findnext (m_lHandleFile, &m_fdFile));
  82. }
  83. m_lHandleDirectory = _findfirst ((char*)sDirectorySearchPath.c_str (), &m_fdDirectory);
  84. m_bValidDirectory = (m_lHandleDirectory != -1);
  85. // Search for a first non-file entry.
  86. //
  87. while (m_bValidDirectory && 0 == (m_fdDirectory.attrib & _A_SUBDIR))
  88. {
  89. m_bValidDirectory = (0 == _findnext (m_lHandleDirectory, &m_fdDirectory));
  90. }
  91. }
  92. // Destructor
  93. CFindFiles::~CFindFiles ()
  94. {
  95. // If the handle is valid, make sure it is released.
  96. //
  97. if (m_lHandleFile != -1)
  98. {
  99. _findclose (m_lHandleFile);
  100. }
  101. if (m_lHandleDirectory != -1)
  102. {
  103. _findclose (m_lHandleDirectory);
  104. }
  105. }
  106. // Find the next entry and set valid flag
  107. void CFindFiles::FindNext (void)
  108. {
  109. // _findnext returns 0 for success
  110. if (m_bValidFile)
  111. {
  112. // We will now seach for a valid file and skip all directories
  113. // found as we don't need them here. We have a separate directory
  114. // search.
  115. //
  116. do
  117. {
  118. m_bValidFile = (0 == _findnext (m_lHandleFile, &m_fdFile));
  119. }
  120. while (m_bValidFile && 0 != (m_fdFile.attrib & _A_SUBDIR));
  121. }
  122. else if (m_bValidDirectory)
  123. {
  124. // If there are no more files, we search for directories only
  125. // with a wildcard of *.*
  126. //
  127. do
  128. {
  129. m_bValidDirectory = (0 == _findnext (m_lHandleDirectory, &m_fdDirectory));
  130. }
  131. while (m_bValidDirectory && 0 == (m_fdDirectory.attrib & _A_SUBDIR));
  132. }
  133. // If both flags are false on entry to this function, no files or
  134. // directories can be found as both lists are exausted.
  135. //
  136. }