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.

170 lines
4.7 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 2000.
  5. //
  6. // File: U P F I L E . C P P
  7. //
  8. // Contents: File utility functions.
  9. //
  10. // Notes: This is separate from ncfile.h so we don't bring the shell
  11. // dependencies into upnphost.
  12. //
  13. // Author: mbend 18 Aug 2000
  14. //
  15. //----------------------------------------------------------------------------
  16. #include <pch.h>
  17. #pragma hdrstop
  18. #include "upfile.h"
  19. #include "trace.h"
  20. #include "ncbase.h"
  21. #include "ComUtility.h"
  22. //+---------------------------------------------------------------------------
  23. //
  24. // Function: HrCreateFile
  25. //
  26. // Purpose: Wrapper for CreateFile
  27. //
  28. // Arguments:
  29. // szFilename [in] Filename
  30. // dwDesiredAccess [in] 0 / GENERIC_READ / GENERIC_WRITE
  31. // dwShareMode [in] 0 / FILE_SHARE_READ / FILE_SHARE_WRITE
  32. // lpSecurityAttributes [in] Security
  33. // dwCreationDisposition [in] CREATE_NEW / CREATE_ALWAYS / OPEN_EXISTING
  34. // OPEN_ALWAYS / TRUNCATE_EXISTING
  35. // dwFlagsAndAttributes [in] FILE_ATTRIBUTE_NORMAL / ...
  36. // phandle [out]
  37. //
  38. // Returns: S_OK on success or COM error code on failure.
  39. //
  40. // Author: mbend 18 Aug 2000
  41. //
  42. // Notes:
  43. //
  44. HRESULT HrCreateFile(
  45. const wchar_t * szFilename,
  46. DWORD dwDesiredAccess,
  47. DWORD dwShareMode,
  48. LPSECURITY_ATTRIBUTES lpSecurityAttributes,
  49. DWORD dwCreationDisposition,
  50. DWORD dwFlagsAndAttributes,
  51. HANDLE * phandle)
  52. {
  53. HRESULT hr = S_OK;
  54. *phandle = CreateFile(szFilename, dwDesiredAccess, dwShareMode, lpSecurityAttributes,
  55. dwCreationDisposition, dwFlagsAndAttributes, NULL);
  56. if(INVALID_HANDLE_VALUE == *phandle)
  57. {
  58. hr = HrFromLastWin32Error();
  59. }
  60. TraceHr(ttidError, FAL, hr, FALSE, "HrCreateFile");
  61. return hr;
  62. }
  63. //+---------------------------------------------------------------------------
  64. //
  65. // Function: HrLoadFileFromDisk
  66. //
  67. // Purpose: Loads a file's contents from disk into memory.
  68. //
  69. // Arguments:
  70. // szFilename [in] Name of file to load
  71. // pnFileSize [out] Size of file loaded
  72. // parBytes [out] Bytes of file loaded
  73. //
  74. // Returns: S_OK on success and COM error code on failure.
  75. //
  76. // Author: mbend 18 Aug 2000
  77. //
  78. // Notes:
  79. //
  80. HRESULT HrLoadFileFromDisk(const wchar_t * szFilename, long * pnFileSize, byte ** parBytes)
  81. {
  82. CHECK_POINTER(szFilename);
  83. CHECK_POINTER(pnFileSize);
  84. CHECK_POINTER(parBytes);
  85. HRESULT hr = S_OK;
  86. HANDLE h;
  87. hr = HrCreateFile(szFilename, GENERIC_READ, FILE_SHARE_READ,
  88. NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, &h);
  89. if(SUCCEEDED(hr))
  90. {
  91. *pnFileSize = GetFileSize(h, NULL);
  92. hr = HrCoTaskMemAllocArray(*pnFileSize, parBytes);
  93. if(SUCCEEDED(hr))
  94. {
  95. DWORD dwDummy = 0;
  96. if(!ReadFile(h, *parBytes, *pnFileSize, &dwDummy, NULL))
  97. {
  98. hr = HrFromLastWin32Error();
  99. }
  100. if(FAILED(hr))
  101. {
  102. if(*parBytes)
  103. {
  104. CoTaskMemFree(*parBytes);
  105. }
  106. }
  107. }
  108. CloseHandle(h);
  109. }
  110. if(FAILED(hr))
  111. {
  112. *pnFileSize = 0;
  113. *parBytes = NULL;
  114. }
  115. TraceHr(ttidError, FAL, hr, FALSE, "HrLoadFileFromDisk");
  116. return hr;
  117. }
  118. //+---------------------------------------------------------------------------
  119. //
  120. // Function: HrGetFileExtension
  121. //
  122. // Purpose: Extracts an extension from a filename.
  123. //
  124. // Arguments:
  125. // szFilename [in] Filename to process.
  126. // szExt [out] Charachter buffer of size UH_MAX_EXTENSION.
  127. //
  128. // Returns: S_OK on success or E_INVALIDARG if filename doesn't have a valid extension.
  129. //
  130. // Author: mbend 18 Aug 2000
  131. //
  132. // Notes:
  133. //
  134. HRESULT HrGetFileExtension(const wchar_t * szFilename, wchar_t szExt[UH_MAX_EXTENSION])
  135. {
  136. CHECK_POINTER(szFilename);
  137. CHECK_POINTER(szExt);
  138. HRESULT hr = E_INVALIDARG;
  139. long nLength = lstrlen(szFilename);
  140. long nMaxExt = UH_MAX_EXTENSION;
  141. // Check that extension size to search for is not greater than string size
  142. if(nLength < nMaxExt)
  143. {
  144. nMaxExt = nLength;
  145. }
  146. // Walk back until we hit a . or reach nMaxExt
  147. for(long n = 0; n < nMaxExt; ++n)
  148. {
  149. if(L'.' == szFilename[nLength - (n+1)])
  150. {
  151. // Copy string and return success
  152. hr = S_OK;
  153. lstrcpy(szExt, &szFilename[nLength - n]);
  154. break;
  155. }
  156. }
  157. TraceHr(ttidError, FAL, hr, FALSE, "HrGetFileExtension");
  158. return hr;
  159. }