00001
00002
00003
00004
00005
00006 #include "vmsFile.h"
00007 #include <string>
00008
00009 vmsFile::vmsFile()
00010 {
00011 m_hFile = INVALID_HANDLE_VALUE;
00012 }
00013
00014 vmsFile::~vmsFile()
00015 {
00016 Close ();
00017 }
00018
00019 void vmsFile::Close()
00020 {
00021 if (m_hFile != INVALID_HANDLE_VALUE)
00022 {
00023 CloseHandle (m_hFile);
00024 m_hFile = INVALID_HANDLE_VALUE;
00025 }
00026 }
00027
00028 void vmsFile::Open(LPCSTR pszFile, BOOL bRead)
00029 {
00030 Close ();
00031
00032 m_hFile = CreateFile (pszFile, bRead ? GENERIC_READ : GENERIC_WRITE,
00033 bRead ? FILE_SHARE_READ : 0, NULL, OPEN_EXISTING, 0, NULL);
00034 if (m_hFile == INVALID_HANDLE_VALUE)
00035 throw "failed to open file";
00036 }
00037
00038 void vmsFile::Create(LPCSTR pszFile)
00039 {
00040 Close ();
00041
00042 m_hFile = CreateFile (pszFile, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
00043 FILE_ATTRIBUTE_NORMAL, NULL);
00044 if (m_hFile == INVALID_HANDLE_VALUE)
00045 throw "failed to create file";
00046 }
00047
00048 UINT64 vmsFile::get_Size()
00049 {
00050 DWORD dwSizeHigh = 0;
00051 DWORD dwSizeLow;
00052
00053 dwSizeLow = GetFileSize (m_hFile, &dwSizeHigh);
00054
00055 ULARGE_INTEGER uli;
00056 uli.LowPart = dwSizeLow; uli.HighPart = dwSizeHigh;
00057
00058 return uli.QuadPart;
00059 }
00060
00061 DWORD vmsFile::Write(LPCVOID pb, DWORD dwSize)
00062 {
00063 DWORD dw;
00064 if (FALSE == WriteFile (m_hFile, pb, dwSize, &dw, NULL))
00065 throw "failed to write to file";
00066 return dw;
00067 }
00068
00069 DWORD vmsFile::Read(LPVOID lp, DWORD dwSize)
00070 {
00071 DWORD dw;
00072 if (FALSE == ReadFile (m_hFile, lp, dwSize, &dw, NULL))
00073 throw "failed to read from file";
00074 return dw;
00075 }
00076
00077 vmsFile::operator HANDLE() const
00078 {
00079 return m_hFile;
00080 }
00081
00082 DWORD vmsFile::get_Pos()
00083 {
00084 return SetFilePointer (m_hFile, 0, NULL, FILE_CURRENT);
00085 }
00086
00087 void vmsFile::set_Pos(DWORD dwPos, DWORD dwMethod)
00088 {
00089 SetFilePointer (m_hFile, dwPos, NULL, dwMethod);
00090 }