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.

304 lines
9.8 KiB

  1. /*******************************************************************************
  2. *
  3. * (C) COPYRIGHT MICROSOFT CORPORATION, 1998
  4. *
  5. * TITLE: FLNFILE.CPP
  6. *
  7. * VERSION: 1.0
  8. *
  9. * AUTHOR: ShaunIv
  10. *
  11. * DATE: 10/13/1999
  12. *
  13. * DESCRIPTION: Find the lowest numbered files in a given directory with a given
  14. * root filename.
  15. *
  16. *******************************************************************************/
  17. #include "precomp.h"
  18. #pragma hdrstop
  19. #include "flnfile.h"
  20. bool NumberedFileName::DoesFileExist( LPCTSTR pszFilename )
  21. {
  22. WIA_PUSH_FUNCTION((TEXT("NumberedFileName::DoesFileExist(%s"), pszFilename ));
  23. bool bExists = false;
  24. WIN32_FIND_DATA FindFileData;
  25. ZeroMemory( &FindFileData, sizeof(FindFileData));
  26. HANDLE hFindFiles = FindFirstFile( pszFilename, &FindFileData );
  27. if (hFindFiles != INVALID_HANDLE_VALUE)
  28. {
  29. bExists = true;
  30. FindClose(hFindFiles);
  31. }
  32. return bExists;
  33. }
  34. bool NumberedFileName::ConstructFilename( LPTSTR szFile, LPCTSTR pszDirectory, LPCTSTR pszFilename, LPCTSTR pszNumberFormat, LPCTSTR pszExtension )
  35. {
  36. *szFile = TEXT('\0');
  37. if (pszDirectory && *pszDirectory)
  38. {
  39. //
  40. // Start with the directory name
  41. //
  42. lstrcpyn( szFile + lstrlen(szFile), pszDirectory, MAX_PATH-lstrlen(szFile) );
  43. //
  44. // Ensure there is a trailing slash on the filename
  45. //
  46. if (!CSimpleString(szFile).MatchLastCharacter(TEXT('\\')))
  47. {
  48. lstrcpyn( szFile + lstrlen(szFile), TEXT("\\"), MAX_PATH-lstrlen(szFile) );
  49. }
  50. }
  51. if (pszFilename && *pszFilename)
  52. {
  53. // Append the filename
  54. lstrcpyn( szFile + lstrlen(szFile), pszFilename, MAX_PATH-lstrlen(szFile) );
  55. }
  56. if (pszNumberFormat && *pszNumberFormat)
  57. {
  58. // Append a space
  59. lstrcpyn( szFile + lstrlen(szFile), TEXT(" "), MAX_PATH-lstrlen(szFile) );
  60. // Append the printf-style number format string
  61. lstrcpyn( szFile + lstrlen(szFile), pszNumberFormat, MAX_PATH-lstrlen(szFile) );
  62. }
  63. if (pszExtension && *pszExtension)
  64. {
  65. // Append the extension's . if necessary
  66. if (*pszExtension != TEXT('.'))
  67. {
  68. lstrcpyn( szFile + lstrlen(szFile), TEXT("."), MAX_PATH-lstrlen(szFile) );
  69. }
  70. // Append the extension
  71. lstrcpyn( szFile + lstrlen(szFile), pszExtension, MAX_PATH-lstrlen(szFile) );
  72. }
  73. return(lstrlen(szFile) != 0);
  74. }
  75. int NumberedFileName::FindLowestAvailableFileSequence( LPCTSTR pszDirectory, LPCTSTR pszFilename, LPCTSTR pszNumberFormat, bool bAllowUnnumberedFile, int nCount, int nStart )
  76. {
  77. WIA_PUSH_FUNCTION((TEXT("NumberedFileName::FindLowestAvailableFileSequence(%s, %s, %s, %d, %d, %d"), pszDirectory, pszFilename, pszNumberFormat, bAllowUnnumberedFile, nCount, nStart ));
  78. if (!pszDirectory || !pszFilename || !pszNumberFormat || !nCount || !*pszDirectory || !*pszFilename || !*pszNumberFormat)
  79. return -1;
  80. TCHAR szFile[MAX_PATH + 10]=TEXT("");
  81. if (nCount == 1 && bAllowUnnumberedFile)
  82. {
  83. if (ConstructFilename(szFile,pszDirectory,pszFilename,NULL,TEXT("*")))
  84. {
  85. if (!DoesFileExist(szFile))
  86. {
  87. // 0 is a special return value that says "Don't put a number on this file"
  88. return 0;
  89. }
  90. }
  91. }
  92. int i=nStart;
  93. //
  94. // Make sure i is a valid number
  95. //
  96. if (i <= 0)
  97. {
  98. i = 1;
  99. }
  100. while (i<0x7FFFFFFF)
  101. {
  102. //
  103. // Assume we'll be able to store the sequence
  104. //
  105. bool bEnoughRoom = true;
  106. for (int j=0;j<nCount && bEnoughRoom;j++)
  107. {
  108. TCHAR szNumber[24];
  109. if (wnsprintf( szNumber, ARRAYSIZE(szNumber), pszNumberFormat, i+j ) >= 0)
  110. {
  111. if (ConstructFilename(szFile,pszDirectory,pszFilename,szNumber,TEXT("*")))
  112. {
  113. if (DoesFileExist(szFile))
  114. {
  115. //
  116. // Didn't make it
  117. //
  118. bEnoughRoom = false;
  119. //
  120. // Skip this series. No need to start at the bottom.
  121. //
  122. i += j;
  123. }
  124. }
  125. }
  126. }
  127. //
  128. // If we made it through, return the base number, otherwise increment by one
  129. //
  130. if (bEnoughRoom)
  131. {
  132. return i;
  133. }
  134. else i++;
  135. }
  136. return -1;
  137. }
  138. bool NumberedFileName::CreateNumberedFileName( DWORD dwFlags, LPTSTR pszPathName, LPCTSTR pszDirectory, LPCTSTR pszFilename, LPCTSTR pszNumberFormat, LPCTSTR pszExtension, int nNumber )
  139. {
  140. if (nNumber == 0)
  141. {
  142. return ConstructFilename(pszPathName,
  143. (dwFlags&FlagOmitDirectory) ? NULL : pszDirectory,
  144. pszFilename,
  145. NULL,
  146. (dwFlags&FlagOmitExtension) ? NULL : pszExtension);
  147. }
  148. else
  149. {
  150. TCHAR szNumber[24];
  151. if (wnsprintf( szNumber, ARRAYSIZE(szNumber), pszNumberFormat, nNumber ) >= 0)
  152. {
  153. return ConstructFilename(pszPathName,
  154. (dwFlags&FlagOmitDirectory) ? NULL : pszDirectory,
  155. pszFilename,
  156. szNumber,
  157. (dwFlags&FlagOmitExtension) ? NULL : pszExtension);
  158. }
  159. }
  160. return false;
  161. }
  162. int NumberedFileName::GenerateLowestAvailableNumberedFileName( DWORD dwFlags, LPTSTR pszPathName, LPCTSTR pszDirectory, LPCTSTR pszFilename, LPCTSTR pszNumberFormat, LPCTSTR pszExtension, bool bAllowUnnumberedFile, int nStart )
  163. {
  164. //
  165. // -1 is an error. Default to failure
  166. //
  167. int nResult = -1;
  168. //
  169. // Find the lowest available file number
  170. //
  171. int nLowest = FindLowestAvailableFileSequence( pszDirectory, pszFilename, pszNumberFormat, bAllowUnnumberedFile, 1, nStart );
  172. if (nLowest >= 0)
  173. {
  174. //
  175. // If we can create the filename, return the number of the file
  176. //
  177. if (CreateNumberedFileName( dwFlags, pszPathName, pszDirectory, pszFilename, pszNumberFormat, pszExtension, nLowest ))
  178. {
  179. //
  180. // Return the file's number
  181. //
  182. nResult = nLowest;
  183. }
  184. }
  185. return nResult;
  186. }
  187. int NumberedFileName::FindHighestNumberedFile( LPCTSTR pszDirectory, LPCTSTR pszFilename )
  188. {
  189. WIA_PUSH_FUNCTION((TEXT("NumberedFileName::FindHighestNumberedFile( %s, %s )"), pszDirectory, pszFilename ));
  190. //
  191. // Make sure we have reasonable args
  192. //
  193. if (!pszFilename || !pszDirectory || !*pszFilename || !*pszDirectory)
  194. {
  195. return -1;
  196. }
  197. //
  198. // Assume we won't be finding any files
  199. //
  200. int nHighest = 0;
  201. //
  202. // Construct a filename that looks like this: c:\path\file*.*
  203. //
  204. TCHAR szFile[MAX_PATH*2] = TEXT("");
  205. if (ConstructFilename(szFile,pszDirectory,pszFilename,TEXT("*"),TEXT("*")))
  206. {
  207. //
  208. // Find the first file which matches the path and wildcards
  209. //
  210. WIN32_FIND_DATA FindFileData = {0};
  211. HANDLE hFindFiles = FindFirstFile( szFile, &FindFileData );
  212. if (hFindFiles != INVALID_HANDLE_VALUE)
  213. {
  214. //
  215. // Loop while there are more matching files
  216. //
  217. BOOL bSuccess = TRUE;
  218. while (bSuccess)
  219. {
  220. //
  221. // Make sure the filename is long enough
  222. //
  223. WIA_TRACE((TEXT("FindFileData.cFileName: %s"), FindFileData.cFileName ));
  224. if (lstrlen(FindFileData.cFileName) >= lstrlen(pszFilename))
  225. {
  226. //
  227. // Copy the filename to a temp buffer MINUS the filename portion,
  228. // so "c:\path\file 001.jpg" becomes " 001.jpg"
  229. //
  230. TCHAR szFoundFile[MAX_PATH] = TEXT("");
  231. if (lstrcpyn( szFoundFile, FindFileData.cFileName+lstrlen(pszFilename), ARRAYSIZE(szFoundFile)-lstrlen(pszFilename)))
  232. {
  233. //
  234. // Remove the extension, so
  235. // " 001.jpg" becomes " 001"
  236. //
  237. PathRemoveExtension(szFoundFile);
  238. //
  239. // Remove spaces, so " 001" becomes "001"
  240. //
  241. StrTrim(szFoundFile,TEXT(" "));
  242. WIA_TRACE((TEXT("szFoundFile: %s"), szFoundFile ));
  243. //
  244. // Convert the string to a number
  245. //
  246. int nCurrNumber = 0;
  247. if (StrToIntEx(szFoundFile,STIF_DEFAULT,&nCurrNumber))
  248. {
  249. //
  250. // Replace our current high if this one exceeds it
  251. //
  252. if (nCurrNumber > nHighest)
  253. {
  254. nHighest = nCurrNumber;
  255. }
  256. }
  257. }
  258. }
  259. //
  260. // Continue finding files
  261. //
  262. bSuccess = FindNextFile( hFindFiles, &FindFileData );
  263. }
  264. //
  265. // Prevent handle leaks
  266. //
  267. FindClose(hFindFiles);
  268. }
  269. }
  270. WIA_TRACE((TEXT("nHighest: %d"), nHighest ));
  271. return nHighest+1;
  272. }