FileIOHelper.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // //
  3. // FileIOHelper.h //
  4. // Copyright (C) Microsoft Corporation. All rights reserved. //
  5. // This file is distributed under the University of Illinois Open Source //
  6. // License. See LICENSE.TXT for details. //
  7. // //
  8. // Provides utitlity functions to work with files. //
  9. // //
  10. ///////////////////////////////////////////////////////////////////////////////
  11. #pragma once
  12. // Forward declarations.
  13. struct IDxcBlob;
  14. struct IDxcBlobEncoding;
  15. namespace hlsl {
  16. void ReadBinaryFile(_In_z_ LPCWSTR pFileName,
  17. _Outptr_result_bytebuffer_(*pDataSize) void **ppData,
  18. _Out_ DWORD *pDataSize);
  19. void WriteBinaryFile(_In_z_ LPCWSTR pFileName,
  20. _In_reads_bytes_(DataSize) const void *pData,
  21. _In_ DWORD DataSize);
  22. ///////////////////////////////////////////////////////////////////////////////
  23. // Blob and encoding manipulation functions.
  24. UINT32 DxcCodePageFromBytes(_In_count_(byteLen) const char *bytes,
  25. size_t byteLen) throw();
  26. HRESULT DxcCreateBlobFromFile(LPCWSTR pFileName, _In_opt_ UINT32 *pCodePage,
  27. _COM_Outptr_ IDxcBlobEncoding **pBlobEncoding) throw();
  28. // Given a blob, creates a subrange view.
  29. HRESULT DxcCreateBlobFromBlob(_In_ IDxcBlob *pBlob, UINT32 offset,
  30. UINT32 length,
  31. _COM_Outptr_ IDxcBlob **ppResult) throw();
  32. HRESULT
  33. DxcCreateBlobOnHeap(_In_bytecount_(size) LPCVOID pData, UINT32 size,
  34. _COM_Outptr_ IDxcBlob **ppResult) throw();
  35. HRESULT
  36. DxcCreateBlobOnHeapCopy(_In_bytecount_(size) LPCVOID pData, UINT32 size,
  37. _COM_Outptr_ IDxcBlob **ppResult) throw();
  38. // Given a blob, creates a new instance with a specific code page set.
  39. HRESULT
  40. DxcCreateBlobWithEncodingSet(_In_ IDxcBlob *pBlob, UINT32 codePage,
  41. _COM_Outptr_ IDxcBlobEncoding **pBlobEncoding) throw();
  42. HRESULT DxcCreateBlobWithEncodingFromPinned(
  43. _In_bytecount_(size) LPCVOID pText, UINT32 size, UINT32 codePage,
  44. _COM_Outptr_ IDxcBlobEncoding **pBlobEncoding) throw();
  45. HRESULT
  46. DxcCreateBlobWithEncodingFromStream(
  47. IStream *pStream, bool newInstanceAlways, UINT32 codePage,
  48. _COM_Outptr_ IDxcBlobEncoding **pBlobEncoding) throw();
  49. HRESULT
  50. DxcCreateBlobWithEncodingOnHeap(_In_bytecount_(size) LPCVOID pText, UINT32 size,
  51. UINT32 codePage,
  52. _COM_Outptr_ IDxcBlobEncoding **pBlobEncoding) throw();
  53. HRESULT
  54. DxcCreateBlobWithEncodingOnHeapCopy(
  55. _In_bytecount_(size) LPCVOID pText, UINT32 size, UINT32 codePage,
  56. _COM_Outptr_ IDxcBlobEncoding **pBlobEncoding) throw();
  57. HRESULT
  58. DxcCreateBlobWithEncodingOnMalloc(
  59. _In_bytecount_(size) LPCVOID pText, IMalloc *pIMalloc, UINT32 size, UINT32 codePage,
  60. _COM_Outptr_ IDxcBlobEncoding **pBlobEncoding) throw();
  61. HRESULT DxcGetBlobAsUtf8(_In_ IDxcBlob *pBlob,
  62. _COM_Outptr_ IDxcBlobEncoding **pBlobEncoding) throw();
  63. HRESULT
  64. DxcGetBlobAsUtf8NullTerm(
  65. _In_ IDxcBlob *pBlob,
  66. _COM_Outptr_ IDxcBlobEncoding **ppBlobEncoding) throw();
  67. HRESULT DxcGetBlobAsUtf16(_In_ IDxcBlob *pBlob,
  68. _COM_Outptr_ IDxcBlobEncoding **pBlobEncoding) throw();
  69. bool IsBlobNullOrEmpty(_In_opt_ IDxcBlob *pBlob) throw();
  70. ///////////////////////////////////////////////////////////////////////////////
  71. // Stream implementations.
  72. class AbstractMemoryStream : public IStream {
  73. public:
  74. virtual LPBYTE GetPtr() throw() = 0;
  75. virtual ULONG GetPtrSize() throw() = 0;
  76. virtual LPBYTE Detach() throw() = 0;
  77. virtual UINT64 GetPosition() throw() = 0;
  78. virtual HRESULT Reserve(ULONG targetSize) throw() = 0;
  79. };
  80. HRESULT CreateMemoryStream(_In_ IMalloc *pMalloc, _COM_Outptr_ AbstractMemoryStream** ppResult) throw();
  81. HRESULT CreateReadOnlyBlobStream(_In_ IDxcBlob *pSource, _COM_Outptr_ IStream** ppResult) throw();
  82. template <typename T>
  83. HRESULT WriteStreamValue(AbstractMemoryStream *pStream, const T& value) {
  84. ULONG cb;
  85. return pStream->Write(&value, sizeof(value), &cb);
  86. }
  87. } // namespace hlsl