00001
00002
00003
00004
00005
00006 #include "stdafx.h"
00007 #include "fsArchiveFileStream.h"
00008
00009 #ifdef _DEBUG
00010 #undef THIS_FILE
00011 static char THIS_FILE[]=__FILE__;
00012 #define new DEBUG_NEW
00013 #endif
00014
00015 using namespace fsArchive;
00016
00017 fsArchiveFileStream::fsArchiveFileStream()
00018 {
00019 m_hFile = INVALID_HANDLE_VALUE;
00020 }
00021
00022 fsArchiveFileStream::~fsArchiveFileStream()
00023 {
00024 Close ();
00025 }
00026
00027 int fsArchiveFileStream::Read(LPVOID buf, int cBytes)
00028 {
00029 DWORD dwRead;
00030
00031 if (FALSE == ReadFile (m_hFile, buf, cBytes, &dwRead,
00032 NULL))
00033 return -1;
00034
00035 return dwRead;
00036 }
00037
00038 int fsArchiveFileStream::Write(LPVOID buf, int cBytes)
00039 {
00040 DWORD dwWritten;
00041
00042 if (FALSE == WriteFile (m_hFile, buf, cBytes, &dwWritten,
00043 NULL))
00044 return -1;
00045
00046 return dwWritten;
00047 }
00048
00049 BOOL fsArchiveFileStream::Seek(UINT64 dist, fsSeekType met)
00050 {
00051 DWORD dwMet;
00052 switch (met)
00053 {
00054 case ST_CURRENT:
00055 dwMet = FILE_CURRENT;
00056 break;
00057
00058 case ST_END:
00059 dwMet = FILE_END;
00060 break;
00061
00062 case ST_BEGIN:
00063 dwMet = FILE_BEGIN;
00064 break;
00065
00066 default:
00067 return FALSE;
00068 }
00069
00070 return fsSetFilePointer (m_hFile, dist, dwMet);
00071 }
00072
00073 DWORD fsArchiveFileStream::Open(LPCSTR pszFile, DWORD dwGenFlags)
00074 {
00075 m_hFile = CreateFile (pszFile, dwGenFlags, FILE_SHARE_READ | FILE_SHARE_WRITE,
00076 NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
00077
00078 if (m_hFile == INVALID_HANDLE_VALUE)
00079 return GetLastError ();
00080
00081 return NOERROR;
00082 }
00083
00084 void fsArchiveFileStream::Close()
00085 {
00086 if (m_hFile != INVALID_HANDLE_VALUE)
00087 {
00088 CloseHandle (m_hFile);
00089 m_hFile = INVALID_HANDLE_VALUE;
00090 }
00091 }
00092
00093 void fsArchiveFileStream::Attach(HANDLE hFile)
00094 {
00095 Close ();
00096 m_hFile = hFile;
00097 }
00098
00099 HANDLE fsArchiveFileStream::Detach()
00100 {
00101 HANDLE h = m_hFile;
00102 m_hFile = INVALID_HANDLE_VALUE;
00103 return h;
00104 }