📄 pfwinfilestream.cpp
字号:
/////////////////////////////////////////////////////////////////////////////////////////////////////// PowerFish core library// Copyright (C) 1997-2001 Camilla Drefvenborg <elmindreda@home.se>//// This program is free software; you can redistribute it and/or modify// it under the terms of the GNU General Public License as published by// the Free Software Foundation; either version 2 of the License, or// (at your option) any later version.//// This program is distributed in the hope that it will be useful,// but WITHOUT ANY WARRANTY; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the// GNU General Public License for more details.//// You should have received a copy of the GNU General Public License// along with this program; if not, write to the Free Software// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA/////////////////////////////////////////////////////////////////////////////////////////////////////#include <PfBase.h>#include <PfTemplate.h>#include <PfString.h>#include <PfStream.h>#include <windows.h>#include "PfWinFileStream.h"///////////////////////////////////////////////////////////////////////////////////////////////////// PfWinFileStream constructors ----------------------------------------------------------------------PfWinFileStream::PfWinFileStream(void){ m_bOpen = false; m_hFile = NULL; m_bRead = false; m_bWrite = false;}PfWinFileStream::~PfWinFileStream(void){ Close();}// PfWinFileStream methods ---------------------------------------------------------------------------bool PfWinFileStream::Create(const char* szFileName, bool bAllowExist, bool bAllowRead, bool bAllowWrite){ Close(); if (!szFileName) return false; m_hFile = CreateFile(szFileName, (bAllowRead ? GENERIC_READ : 0) | (bAllowWrite ? GENERIC_WRITE : 0), FILE_SHARE_READ, NULL, bAllowExist ? CREATE_ALWAYS : CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL); if (m_hFile == INVALID_HANDLE_VALUE) { m_hFile = NULL; return false; } m_bRead = true; m_bWrite = true; return m_bOpen = true;}bool PfWinFileStream::Open(const char* szFileName, bool bAllowCreate, bool bAllowRead, bool bAllowWrite){ Close(); if (!szFileName) return false; m_hFile = CreateFile(szFileName, (bAllowRead ? GENERIC_READ : 0) | (bAllowWrite ? GENERIC_WRITE : 0), FILE_SHARE_READ, NULL, bAllowCreate ? OPEN_ALWAYS : OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (m_hFile == INVALID_HANDLE_VALUE) { m_hFile = NULL; return false; } m_bRead = true; m_bWrite = true; return m_bOpen = true;}void PfWinFileStream::Close(void){ m_bOpen = false; if (m_hFile) { CloseHandle(m_hFile); m_hFile = NULL; } m_bRead = false; m_bWrite = false;}// PfWinFileStream interface methods --------------------------------------------------------------unsigned int PfWinFileStream::Read(void* pData, unsigned int nBytes){ if (!m_bOpen || !m_bRead) return 0; DWORD dwBytesRead; if (!ReadFile(m_hFile, pData, nBytes, &dwBytesRead, NULL)) return 0; return (unsigned int) dwBytesRead;}unsigned int PfWinFileStream::Peek(void* pData, unsigned int nBytes){ if (!m_bOpen || !m_bRead) return 0; unsigned int nBytesRead; unsigned int uPosition = GetPosition(); if (nBytesRead = Read(pData, nBytes)) SetPosition(uPosition); return nBytesRead;}unsigned int PfWinFileStream::Write(const void* pData, unsigned int nBytes){ if (!m_bOpen || !m_bWrite) return 0; DWORD dwBytesWritten; if (!WriteFile(m_hFile, pData, nBytes, &dwBytesWritten, NULL)) return 0; return (unsigned int) dwBytesWritten;}void PfWinFileStream::Skip(unsigned int nBytes){ if (!m_bOpen) return; SetFilePointer(m_hFile, nBytes, NULL, FILE_CURRENT);}void PfWinFileStream::Truncate(void){ if (!m_bOpen || !m_bWrite) return; SetEndOfFile(m_hFile);}void PfWinFileStream::Flush(void){ if (!m_bOpen) return; FlushFileBuffers(m_hFile);}// PfWinFileStream interface accessors ------------------------------------------------------------bool PfWinFileStream::IsEOF(void) const{ if (!m_bOpen) return false; if (GetPosition() < GetSize()) return false; return true;}bool PfWinFileStream::AllowsRead(void) const{ return m_bRead;}bool PfWinFileStream::AllowsWrite(void) const{ return m_bWrite;}unsigned int PfWinFileStream::GetSize(void) const{ if (!m_bOpen) return 0; DWORD dwFileSize = GetFileSize(m_hFile, NULL); if (dwFileSize == (unsigned int) -1) return 0; return (unsigned int) dwFileSize;}unsigned int PfWinFileStream::GetPosition(void) const{ if (!m_bOpen) return 0; DWORD dwFilePointer = SetFilePointer(m_hFile, 0, NULL, FILE_CURRENT);#ifdef INVALID_SET_FILE_POINTER // this is not defined in older Platform SDKs if (dwFilePointer == INVALID_SET_FILE_POINTER) return 0;#endif return (unsigned int) dwFilePointer;}void PfWinFileStream::SetPosition(unsigned int uPosition){ if (!m_bOpen) return; DWORD dwFilePointer = SetFilePointer(m_hFile, uPosition, NULL, FILE_BEGIN);#ifdef INVALID_SET_FILE_POINTER // this is not defined in older Platform SDKs if (dwFilePointer == INVALID_SET_FILE_POINTER) return;#endif}unsigned int PfWinFileStream::GetType(void) const{ return TYPE_FILE;}///////////////////////////////////////////////////////////////////////////////////////////////////
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -