PageRenderTime 116ms CodeModel.GetById 110ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

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