Leaked source code of windows server 2003
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.

221 lines
5.2 KiB

  1. /******************************************************************************
  2. *
  3. * Copyright (c) 2000 Microsoft Corporation
  4. *
  5. * Module Name:
  6. * findfile.cpp
  7. *
  8. * Abstract:
  9. * CFindFile functions.
  10. *
  11. * Revision History:
  12. * Brijesh Krishnaswami (brijeshk) 03/17/2000
  13. * created
  14. *
  15. *****************************************************************************/
  16. #include "precomp.h"
  17. #ifdef THIS_FILE
  18. #undef THIS_FILE
  19. #endif
  20. static char __szTraceSourceFile[] = __FILE__;
  21. #define THIS_FILE __szTraceSourceFile
  22. // constructor
  23. CFindFile::CFindFile()
  24. {
  25. m_ulCurID = 0;
  26. m_ulMaxID = 0;
  27. m_ulMinID = 0;
  28. m_fForward = FALSE;
  29. }
  30. // returns oldest/youngest file
  31. // <prefix>n<suffix> is older than <prefix>n+1<suffix>
  32. BOOL
  33. CFindFile::_FindFirstFile(
  34. LPCWSTR pszPrefix,
  35. LPCWSTR pszSuffix,
  36. PWIN32_FIND_DATA pData,
  37. BOOL fForward,
  38. BOOL fSkipLast
  39. )
  40. {
  41. BOOL fRc = FALSE;
  42. HANDLE hdl = INVALID_HANDLE_VALUE;
  43. ULONG ulCurID = 0;
  44. WCHAR szSrch[MAX_PATH];
  45. TENTER("CFindFile::_FindFirstFile");
  46. m_fForward = fForward;
  47. m_ulCurID = 1;
  48. m_ulMaxID = 0;
  49. m_ulMinID = 0xFFFFFFF7;
  50. if(NULL == pData || NULL == pszPrefix || NULL == pszSuffix)
  51. {
  52. SetLastError(ERROR_INVALID_PARAMETER);
  53. goto done;
  54. }
  55. // enumerate all files with wildcard search
  56. // record the first and last files in numerical order
  57. // then return in order requested
  58. wsprintf(szSrch, L"%s*%s", pszPrefix, pszSuffix);
  59. hdl = FindFirstFile(szSrch, pData);
  60. if(INVALID_HANDLE_VALUE == hdl)
  61. {
  62. goto done;
  63. }
  64. do
  65. {
  66. ulCurID = GetID(pData->cFileName);
  67. if (0 == ulCurID) // always skip 0
  68. continue;
  69. if (ulCurID < m_ulMinID)
  70. m_ulMinID = ulCurID;
  71. if (ulCurID > m_ulMaxID)
  72. m_ulMaxID = ulCurID;
  73. } while (FindNextFile(hdl, pData));
  74. FindClose(hdl);
  75. hdl = INVALID_HANDLE_VALUE;
  76. if (m_ulMaxID == 0) // no file really
  77. goto done;
  78. if (fSkipLast) // skip the last file if needed
  79. m_ulMaxID--;
  80. if (m_ulMaxID == 0) // no file again
  81. goto done;
  82. // start at beginning or end
  83. m_ulCurID = m_fForward ? m_ulMinID : m_ulMaxID;
  84. wsprintf(szSrch, L"%s%d%s", pszPrefix, m_ulCurID, pszSuffix);
  85. // get the first existing file
  86. while (m_ulCurID >= m_ulMinID && m_ulCurID <= m_ulMaxID &&
  87. INVALID_HANDLE_VALUE == (hdl = FindFirstFile(szSrch, pData)))
  88. {
  89. // try again with leading zeros
  90. wsprintf(szSrch, L"%s%07d%s", pszPrefix, m_ulCurID, pszSuffix);
  91. if (INVALID_HANDLE_VALUE == (hdl = FindFirstFile (szSrch, pData)))
  92. {
  93. m_fForward ? m_ulCurID++ : m_ulCurID--;
  94. wsprintf(szSrch, L"%s%d%s", pszPrefix, m_ulCurID, pszSuffix);
  95. }
  96. else
  97. {
  98. break;
  99. }
  100. }
  101. if (INVALID_HANDLE_VALUE != hdl)
  102. {
  103. FindClose(hdl);
  104. fRc = TRUE;
  105. }
  106. done:
  107. TLEAVE();
  108. return fRc;
  109. }
  110. // returns next/prev oldest file
  111. // <prefix>n<suffix> is older than <prefix>n+1<suffix>
  112. BOOL
  113. CFindFile::_FindNextFile(
  114. LPCWSTR pszPrefix,
  115. LPCWSTR pszSuffix,
  116. PWIN32_FIND_DATA pData // [out] Next file info
  117. )
  118. {
  119. BOOL fRc = FALSE;
  120. WCHAR szSrch[MAX_PATH];
  121. HANDLE hdl = INVALID_HANDLE_VALUE;
  122. TENTER("CFindFile::_FindNextFile");
  123. if(NULL == pData || NULL == pszPrefix || NULL == pszSuffix)
  124. {
  125. SetLastError(ERROR_INVALID_PARAMETER);
  126. goto done;
  127. }
  128. // get the next/prev oldest existing file
  129. do
  130. {
  131. m_fForward ? m_ulCurID++ : m_ulCurID--;
  132. wsprintf(szSrch, L"%s%d%s", pszPrefix, m_ulCurID, pszSuffix);
  133. if (m_ulCurID >= m_ulMinID && m_ulCurID <= m_ulMaxID &&
  134. INVALID_HANDLE_VALUE == (hdl = FindFirstFile(szSrch, pData)))
  135. {
  136. // try again with leading zeros
  137. wsprintf(szSrch, L"%s%07d%s", pszPrefix, m_ulCurID, pszSuffix);
  138. }
  139. else if (INVALID_HANDLE_VALUE != hdl)
  140. break;
  141. } while (m_ulCurID >= m_ulMinID && m_ulCurID <= m_ulMaxID &&
  142. INVALID_HANDLE_VALUE == (hdl = FindFirstFile(szSrch, pData)));
  143. if (INVALID_HANDLE_VALUE != hdl) // no more files?
  144. {
  145. fRc = TRUE;
  146. FindClose(hdl);
  147. }
  148. done:
  149. TLEAVE();
  150. return fRc;
  151. }
  152. // returns n+1 for the max n for which file <Prefix>n<Suffix> exists
  153. ULONG
  154. CFindFile::GetNextFileID(
  155. LPCWSTR pszPrefix,
  156. LPCWSTR pszSuffix)
  157. {
  158. HANDLE hFile = INVALID_HANDLE_VALUE;
  159. CFindFile FindFile;
  160. WIN32_FIND_DATA FindData;
  161. TENTER("CFindFile::GetNextFileID");
  162. // step thru all files in order of increasing id
  163. if (FindFile._FindFirstFile(pszPrefix, pszSuffix, &FindData, TRUE))
  164. {
  165. while (FindFile._FindNextFile(pszPrefix, pszSuffix, &FindData));
  166. }
  167. TLEAVE();
  168. return FindFile.m_ulCurID;
  169. }