foobar2000 SDK  2015-08-03
Public Member Functions | Static Public Member Functions | Protected Attributes
file_win32_wrapper_t< p_seekable, p_writeable >

#include <file_win32_wrapper.h>

+ Inheritance diagram for file_win32_wrapper_t< p_seekable, p_writeable >:

Public Member Functions

 file_win32_wrapper_t (HANDLE p_handle)
 
 ~file_win32_wrapper_t ()
 
bool can_seek ()
 
bool get_content_type (pfc::string_base &out)
 
t_filesize get_position (abort_callback &p_abort)
 
t_filesize get_size (abort_callback &p_abort)
 
t_filetimestamp get_timestamp (abort_callback &p_abort)
 
bool is_in_memory ()
 
bool is_remote ()
 
void on_idle (abort_callback &p_abort)
 
t_size read (void *p_buffer, t_size p_bytes, abort_callback &p_abort)
 
void reopen (abort_callback &p_abort)
 
void resize (t_filesize p_size, abort_callback &p_abort)
 
void seek (t_filesize p_position, abort_callback &p_abort)
 
void write (const void *p_buffer, t_size p_bytes, abort_callback &p_abort)
 

Static Public Member Functions

static service_ptr_t< fileg_create_from_handle (HANDLE p_handle)
 
static file::ptr g_CreateFile (const char *p_path, DWORD p_access, DWORD p_sharemode, LPSECURITY_ATTRIBUTES p_security_attributes, DWORD p_createmode, DWORD p_flags, HANDLE p_template)
 

Protected Attributes

HANDLE m_handle
 
t_filesize m_position
 

Detailed Description

template<bool p_seekable, bool p_writeable>
class file_win32_wrapper_t< p_seekable, p_writeable >

Definition at line 15 of file file_win32_wrapper.h.

Constructor & Destructor Documentation

template<bool p_seekable, bool p_writeable>
file_win32_wrapper_t< p_seekable, p_writeable >::file_win32_wrapper_t ( HANDLE  p_handle)
inline

Definition at line 17 of file file_win32_wrapper.h.

17  : m_handle(p_handle), m_position(0)
18  {
19  }
template<bool p_seekable, bool p_writeable>
file_win32_wrapper_t< p_seekable, p_writeable >::~file_win32_wrapper_t ( )
inline

Definition at line 147 of file file_win32_wrapper.h.

147 {CloseHandle(m_handle);}

Member Function Documentation

template<bool p_seekable, bool p_writeable>
bool file_win32_wrapper_t< p_seekable, p_writeable >::can_seek ( )
inline

Definition at line 132 of file file_win32_wrapper.h.

132 {return p_seekable;}
template<bool p_seekable, bool p_writeable>
static service_ptr_t<file> file_win32_wrapper_t< p_seekable, p_writeable >::g_create_from_handle ( HANDLE  p_handle)
inlinestatic

Definition at line 34 of file file_win32_wrapper.h.

34  {
36  }
Template implementing reference-counting features of service_base. Intended for dynamic instantiation...
Definition: service_impl.h:4
template<bool p_seekable, bool p_writeable>
static file::ptr file_win32_wrapper_t< p_seekable, p_writeable >::g_CreateFile ( const char *  p_path,
DWORD  p_access,
DWORD  p_sharemode,
LPSECURITY_ATTRIBUTES  p_security_attributes,
DWORD  p_createmode,
DWORD  p_flags,
HANDLE  p_template 
)
inlinestatic

Definition at line 21 of file file_win32_wrapper.h.

21  {
22  SetLastError(NO_ERROR);
23  HANDLE handle = uCreateFile(p_path,p_access,p_sharemode,p_security_attributes,p_createmode,p_flags,p_template);
24  if (handle == INVALID_HANDLE_VALUE) {
25  const DWORD code = GetLastError();
26  if (p_access & GENERIC_WRITE) win32_file_write_failure(code, p_path);
27  else exception_io_from_win32(code);
28  }
29  try {
30  return g_create_from_handle(handle);
31  } catch(...) {CloseHandle(handle); throw;}
32  }
PFC_NORETURN void win32_file_write_failure(DWORD p_code, const char *path)
Definition: filesystem.cpp:765
PFC_NORETURN void exception_io_from_win32(DWORD p_code)
Definition: filesystem.cpp:773
HANDLE SHARED_EXPORT uCreateFile(const char *p_path, DWORD p_access, DWORD p_sharemode, LPSECURITY_ATTRIBUTES p_security_attributes, DWORD p_createmode, DWORD p_flags, HANDLE p_template)
typedef HANDLE(WINAPI *pPowerCreateRequest_t)(__in void *Context)
static service_ptr_t< file > g_create_from_handle(HANDLE p_handle)
template<bool p_seekable, bool p_writeable>
bool file_win32_wrapper_t< p_seekable, p_writeable >::get_content_type ( pfc::string_base out)
inline

Definition at line 133 of file file_win32_wrapper.h.

133 {return false;}
template<bool p_seekable, bool p_writeable>
t_filesize file_win32_wrapper_t< p_seekable, p_writeable >::get_position ( abort_callback p_abort)
inline

Definition at line 102 of file file_win32_wrapper.h.

102  {
103  p_abort.check_e();
104  return m_position;
105  }
template<bool p_seekable, bool p_writeable>
t_filesize file_win32_wrapper_t< p_seekable, p_writeable >::get_size ( abort_callback p_abort)
inline

Definition at line 97 of file file_win32_wrapper.h.

97  {
98  p_abort.check_e();
100  }
t_filesize get_size(HANDLE p_handle)
template<bool p_seekable, bool p_writeable>
t_filetimestamp file_win32_wrapper_t< p_seekable, p_writeable >::get_timestamp ( abort_callback p_abort)
inline

Definition at line 137 of file file_win32_wrapper.h.

137  {
138  p_abort.check_e();
139  FlushFileBuffers(m_handle);
140  SetLastError(ERROR_SUCCESS);
141  t_filetimestamp temp;
142  if (!GetFileTime(m_handle,0,0,(FILETIME*)&temp)) exception_io_from_win32(GetLastError());
143  return temp;
144  }
PFC_NORETURN void exception_io_from_win32(DWORD p_code)
Definition: filesystem.cpp:773
t_uint64 t_filetimestamp
Type used for file timestamp related variables. 64-bit value representing the number of 100-nanosecon...
Definition: filesystem.h:12
template<bool p_seekable, bool p_writeable>
bool file_win32_wrapper_t< p_seekable, p_writeable >::is_in_memory ( )
inline

Definition at line 134 of file file_win32_wrapper.h.

134 {return false;}
template<bool p_seekable, bool p_writeable>
bool file_win32_wrapper_t< p_seekable, p_writeable >::is_remote ( )
inline

Definition at line 146 of file file_win32_wrapper.h.

146 {return false;}
template<bool p_seekable, bool p_writeable>
void file_win32_wrapper_t< p_seekable, p_writeable >::on_idle ( abort_callback p_abort)
inline

Definition at line 135 of file file_win32_wrapper.h.

135 {p_abort.check_e();}
template<bool p_seekable, bool p_writeable>
t_size file_win32_wrapper_t< p_seekable, p_writeable >::read ( void *  p_buffer,
t_size  p_bytes,
abort_callback p_abort 
)
inline

Definition at line 70 of file file_win32_wrapper.h.

70  {
71  PFC_STATIC_ASSERT(sizeof(t_size) >= sizeof(DWORD));
72 
73  t_size bytes_read_total = 0;
74  if (sizeof(t_size) == sizeof(DWORD)) {
75  p_abort.check_e();
76  DWORD bytes_read = 0;
77  SetLastError(ERROR_SUCCESS);
78  if (!ReadFile(m_handle,p_buffer,pfc::downcast_guarded<DWORD>(p_bytes),&bytes_read,0)) exception_io_from_win32(GetLastError());
79  bytes_read_total = bytes_read;
80  m_position += bytes_read;
81  } else {
82  while(bytes_read_total < p_bytes) {
83  p_abort.check_e();
84  DWORD bytes_read = 0;
85  DWORD delta = (DWORD) pfc::min_t<t_size>(p_bytes - bytes_read_total, 0x80000000);
86  SetLastError(ERROR_SUCCESS);
87  if (!ReadFile(m_handle,(t_uint8*)p_buffer + bytes_read_total,delta,&bytes_read,0)) exception_io_from_win32(GetLastError());
88  bytes_read_total += bytes_read;
89  m_position += bytes_read;
90  if (bytes_read != delta) break;
91  }
92  }
93  return bytes_read_total;
94  }
uint8_t t_uint8
Definition: int_types.h:9
PFC_NORETURN void exception_io_from_win32(DWORD p_code)
Definition: filesystem.cpp:773
size_t t_size
Definition: int_types.h:48
template<bool p_seekable, bool p_writeable>
void file_win32_wrapper_t< p_seekable, p_writeable >::reopen ( abort_callback p_abort)
inline

Definition at line 39 of file file_win32_wrapper.h.

39 {seek(0,p_abort);}
void seek(t_filesize p_position, abort_callback &p_abort)
template<bool p_seekable, bool p_writeable>
void file_win32_wrapper_t< p_seekable, p_writeable >::resize ( t_filesize  p_size,
abort_callback p_abort 
)
inline

Definition at line 107 of file file_win32_wrapper.h.

107  {
108  if (!p_writeable) throw exception_io_denied();
109  p_abort.check_e();
110  if (m_position != p_size) {
111  file_win32_helpers::seek(m_handle,p_size,file::seek_from_beginning);
112  }
113  SetLastError(ERROR_SUCCESS);
114  if (!SetEndOfFile(m_handle)) {
115  DWORD code = GetLastError();
116  if (m_position != p_size) try {file_win32_helpers::seek(m_handle,m_position,file::seek_from_beginning);} catch(...) {}
118  }
119  if (m_position > p_size) m_position = p_size;
120  if (m_position != p_size) file_win32_helpers::seek(m_handle,m_position,file::seek_from_beginning);
121  }
void seek(HANDLE p_handle, t_sfilesize p_position, file::t_seek_mode p_mode)
PFC_NORETURN void exception_io_from_win32(DWORD p_code)
Definition: filesystem.cpp:773
template<bool p_seekable, bool p_writeable>
void file_win32_wrapper_t< p_seekable, p_writeable >::seek ( t_filesize  p_position,
abort_callback p_abort 
)
inline

Definition at line 124 of file file_win32_wrapper.h.

124  {
125  if (!p_seekable) throw exception_io_object_not_seekable();
126  p_abort.check_e();
127  if (p_position > file_win32_helpers::get_size(m_handle)) throw exception_io_seek_out_of_range();
128  file_win32_helpers::seek(m_handle,p_position,file::seek_from_beginning);
129  m_position = p_position;
130  }
t_filesize get_size(HANDLE p_handle)
void seek(HANDLE p_handle, t_sfilesize p_position, file::t_seek_mode p_mode)
template<bool p_seekable, bool p_writeable>
void file_win32_wrapper_t< p_seekable, p_writeable >::write ( const void *  p_buffer,
t_size  p_bytes,
abort_callback p_abort 
)
inline

Definition at line 41 of file file_win32_wrapper.h.

41  {
42  if (!p_writeable) throw exception_io_denied();
43 
44  PFC_STATIC_ASSERT(sizeof(t_size) >= sizeof(DWORD));
45 
46  t_size bytes_written_total = 0;
47 
48  if (sizeof(t_size) == sizeof(DWORD)) {
49  p_abort.check_e();
50  DWORD bytes_written = 0;
51  SetLastError(ERROR_SUCCESS);
52  if (!WriteFile(m_handle,p_buffer,(DWORD)p_bytes,&bytes_written,0)) exception_io_from_win32(GetLastError());
53  if (bytes_written != p_bytes) throw exception_io("Write failure");
54  bytes_written_total = bytes_written;
55  m_position += bytes_written;
56  } else {
57  while(bytes_written_total < p_bytes) {
58  p_abort.check_e();
59  DWORD bytes_written = 0;
60  DWORD delta = (DWORD) pfc::min_t<t_size>(p_bytes - bytes_written_total, 0x80000000);
61  SetLastError(ERROR_SUCCESS);
62  if (!WriteFile(m_handle,(const t_uint8*)p_buffer + bytes_written_total,delta,&bytes_written,0)) exception_io_from_win32(GetLastError());
63  if (bytes_written != delta) throw exception_io("Write failure");
64  bytes_written_total += bytes_written;
65  m_position += bytes_written;
66  }
67  }
68  }
uint8_t t_uint8
Definition: int_types.h:9
PFC_NORETURN void exception_io_from_win32(DWORD p_code)
Definition: filesystem.cpp:773
size_t t_size
Definition: int_types.h:48

Field Documentation

template<bool p_seekable, bool p_writeable>
HANDLE file_win32_wrapper_t< p_seekable, p_writeable >::m_handle
protected

Definition at line 149 of file file_win32_wrapper.h.

template<bool p_seekable, bool p_writeable>
t_filesize file_win32_wrapper_t< p_seekable, p_writeable >::m_position
protected

Definition at line 150 of file file_win32_wrapper.h.


The documentation for this class was generated from the following file: