00001
00002
00003
00004
00005
00006 #include "stdafx.h"
00007 #include "vmsFile.h"
00008 using namespace vmsFDM;
00009
00010 vmsFile::vmsFile(HANDLE hFile, bool bAutoClose)
00011 {
00012 m_hFile = hFile;
00013 m_bAutoClose = bAutoClose;
00014 m_bThrowOnPartialRead = true;
00015 }
00016
00017 vmsFile::~vmsFile()
00018 {
00019 if (m_bAutoClose)
00020 Close ();
00021 }
00022
00023 void vmsFile::Create(LPCTSTR pszFileName, DWORD dwAccess, DWORD dwShareMode, DWORD dwCreationDisposition, DWORD dwFlags)
00024 {
00025 Close ();
00026
00027 m_hFile = CreateFile (pszFileName, dwAccess, dwShareMode, NULL, dwCreationDisposition,
00028 dwFlags, NULL);
00029
00030 if (m_hFile == INVALID_HANDLE_VALUE)
00031 throw GetLastError ();
00032 }
00033
00034 void vmsFile::Close()
00035 {
00036 if (m_hFile != INVALID_HANDLE_VALUE)
00037 {
00038 if (FALSE == CloseHandle (Detach ()))
00039 throw GetLastError ();
00040 }
00041 }
00042
00043 void vmsFile::Read(LPVOID pvBuffer, DWORD dwToRead, DWORD *pdwRead)
00044 {
00045 if (m_hFile == INVALID_HANDLE_VALUE)
00046 throw ERROR_INVALID_HANDLE;
00047
00048 DWORD dwRead = 0;
00049
00050 if (FALSE == ReadFile (m_hFile, pvBuffer, dwToRead, &dwRead, NULL))
00051 throw GetLastError ();
00052
00053 if (pdwRead)
00054 *pdwRead = dwRead;
00055
00056 if (m_bThrowOnPartialRead && dwRead != dwToRead)
00057 throw ERROR_BAD_FORMAT;
00058 }
00059
00060 void vmsFile::Write(LPCVOID pvBuffer, DWORD dwToWrite)
00061 {
00062 if (m_hFile == INVALID_HANDLE_VALUE)
00063 throw ERROR_INVALID_HANDLE;
00064
00065 DWORD dwWritten = 0;
00066
00067 if (FALSE == WriteFile (m_hFile, pvBuffer, dwToWrite, &dwWritten, NULL))
00068 throw GetLastError ();
00069
00070 if (dwWritten != dwToWrite)
00071 throw ERROR_BAD_LENGTH;
00072 }
00073
00074 void vmsFile::Attach(HANDLE hFile, bool bAutoClose)
00075 {
00076 Close ();
00077 m_hFile = hFile;
00078 m_bAutoClose = bAutoClose;
00079 }
00080
00081 HANDLE vmsFile::Detach()
00082 {
00083 HANDLE h = m_hFile;
00084 m_hFile = INVALID_HANDLE_VALUE;
00085 return h;
00086 }
00087
00088 DWORD vmsFile::GetSize()
00089 {
00090 if (m_hFile == INVALID_HANDLE_VALUE)
00091 throw ERROR_INVALID_HANDLE;
00092
00093 return GetFileSize (m_hFile, NULL);
00094 }
00095
00096 void vmsFile::Seek(DWORD dwDistance, DWORD dwMethod)
00097 {
00098 SetFilePointer (m_hFile, dwDistance, NULL, dwMethod);
00099 }
00100
00101 void vmsFile::WriteString(LPCSTR psz)
00102 {
00103 if (FALSE == fsSaveStrToFile (psz, m_hFile))
00104 throw GetLastError ();
00105 }
00106
00107 void vmsFile::ReadString(fsString& str)
00108 {
00109 LPSTR psz;
00110 if (FALSE == fsReadStrFromFile (&psz, m_hFile))
00111 throw GetLastError ();
00112 str = psz;
00113 delete [] psz;
00114 }
00115
00116 void vmsFile::WriteDouble(double f)
00117 {
00118 Write (&f, sizeof (f));
00119 }
00120
00121 void vmsFile::WriteInt64(__int64 i)
00122 {
00123 Write (&i, sizeof (i));
00124 }
00125
00126 void vmsFile::WriteInt(int i)
00127 {
00128 Write (&i, sizeof (i));
00129 }
00130
00131 void vmsFile::ReadInt(int &i)
00132 {
00133 Read (&i, sizeof (i));
00134 }
00135
00136 void vmsFile::ReadInt64(__int64 &i)
00137 {
00138 Read (&i, sizeof (i));
00139 }
00140
00141 void vmsFile::ReadDouble(double &f)
00142 {
00143 Read (&f, sizeof (f));
00144 }