/src/FreeImage/Source/LibRawLite/libraw/librawwindows.h

https://bitbucket.org/cabalistic/ogredeps/ · C Header · 91 lines · 51 code · 16 blank · 24 comment · 11 complexity · c7324d5c3a3b33af13f4438c271583c4 MD5 · raw file

  1. #pragma once
  2. // LibRawWindows.h
  3. //
  4. // Windows specific classes and routines for LibRaw
  5. //
  6. // Copyright(c) 2011 Linc Brookes
  7. // LibRawApp is free software. The source code, documentation and artwork are distributed under the terms of the
  8. // GNU LESSER GENERAL PUBLIC LICENSE version 2.1
  9. // see http://www.gnu.org/licenses/lgpl-2.1.html for details
  10. //
  11. // LibRawApp requires the LibRaw library
  12. // Information and source code are available here: http://www.libraw.org/
  13. //#include <windows.h>
  14. //#include "../libraw/libraw_datastream.h"
  15. //#include <stdint.h>
  16. #include <exception>
  17. // class LibRaw_windows_datastream
  18. // a windows file mapping encapsulated in a LibRaw_buffer_datastream
  19. // against LibRaw convention, this class may throw a std::runtime_exception
  20. class LibRaw_windows_datastream : public LibRaw_buffer_datastream
  21. {
  22. public:
  23. // ctor: high level constructor opens a file by name
  24. LibRaw_windows_datastream(const TCHAR* sFile)
  25. : LibRaw_buffer_datastream(NULL, 0)
  26. , hMap_(0)
  27. , pView_(NULL)
  28. {
  29. HANDLE hFile = CreateFile(sFile, GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
  30. if (hFile == INVALID_HANDLE_VALUE)
  31. throw std::runtime_error("failed to open the file");
  32. try { Open(hFile); } catch(...) { CloseHandle(hFile); throw; }
  33. CloseHandle(hFile); // windows will defer the actual closing of this handle until the hMap_ is closed
  34. reconstruct_base();
  35. }
  36. // ctor: construct with a file handle - caller is responsible for closing the file handle
  37. LibRaw_windows_datastream(HANDLE hFile)
  38. : LibRaw_buffer_datastream(NULL, 0)
  39. , hMap_(0)
  40. , pView_(NULL)
  41. {
  42. Open(hFile);
  43. reconstruct_base();
  44. }
  45. // dtor: unmap and close the mapping handle
  46. virtual ~LibRaw_windows_datastream()
  47. {
  48. if (pView_ != NULL)
  49. ::UnmapViewOfFile(pView_);
  50. if (hMap_ != 0)
  51. ::CloseHandle(hMap_);
  52. }
  53. protected:
  54. void Open(HANDLE hFile)
  55. {
  56. // create a file mapping handle on the file handle
  57. hMap_ = ::CreateFileMapping(hFile, 0, PAGE_READONLY, 0, 0, 0);
  58. if (hMap_ == NULL) throw std::runtime_error("failed to create file mapping");
  59. // now map the whole file base view
  60. if (!::GetFileSizeEx(hFile, (PLARGE_INTEGER)&cbView_))
  61. throw std::runtime_error("failed to get the file size");
  62. pView_ = ::MapViewOfFile(hMap_, FILE_MAP_READ, 0, 0, (size_t)cbView_);
  63. if (pView_ == NULL)
  64. throw std::runtime_error("failed to map the file");
  65. }
  66. inline void reconstruct_base()
  67. {
  68. // this subterfuge is to overcome the private-ness of LibRaw_buffer_datastream
  69. (LibRaw_buffer_datastream&)*this = LibRaw_buffer_datastream(pView_, (size_t)cbView_);
  70. }
  71. HANDLE hMap_; // handle of the file mapping
  72. void* pView_; // pointer to the mapped memory
  73. // uint64_t cbView_; // size of the mapping in bytes
  74. __int64 cbView_; // size of the mapping in bytes
  75. };