| 1 |
#ifndef SOURCETOOLS_READ_WINDOWS_FILE_CONNECTION_H |
| 2 |
#define SOURCETOOLS_READ_WINDOWS_FILE_CONNECTION_H |
| 3 |
|
| 4 |
#undef Realloc |
| 5 |
#undef Free |
| 6 |
#include <windows.h> |
| 7 |
|
| 8 |
namespace sourcetools { |
| 9 |
namespace detail { |
| 10 |
|
| 11 |
class FileConnection |
| 12 |
{ |
| 13 |
public: |
| 14 |
typedef HANDLE FileDescriptor; |
| 15 |
|
| 16 |
FileConnection(const char* path, int flags = GENERIC_READ) |
| 17 |
{ |
| 18 |
handle_ = ::CreateFile(path, flags, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL); |
| 19 |
} |
| 20 |
|
| 21 |
~FileConnection() |
| 22 |
{ |
| 23 |
if (open()) |
| 24 |
::CloseHandle(handle_); |
| 25 |
} |
| 26 |
|
| 27 |
bool open() |
| 28 |
{ |
| 29 |
return handle_ != INVALID_HANDLE_VALUE; |
| 30 |
} |
| 31 |
|
| 32 |
bool size(std::size_t* pSize) |
| 33 |
{ |
| 34 |
*pSize = ::GetFileSize(handle_, NULL); |
| 35 |
return true; |
| 36 |
} |
| 37 |
|
| 38 |
operator FileDescriptor() const |
| 39 |
{ |
| 40 |
return handle_; |
| 41 |
} |
| 42 |
|
| 43 |
private: |
| 44 |
FileDescriptor handle_; |
| 45 |
}; |
| 46 |
|
| 47 |
} // namespace detail |
| 48 |
} // namespace sourcetools |
| 49 |
|
| 50 |
#endif /* SOURCETOOLS_READ_WINDOWS_FILE_CONNECTION_H */ |