Main Page | Class Hierarchy | Class List | File List | Class Members | File Members

File.h

Go to the documentation of this file.
00001 /*  File.h - standard Win32 file functions wrapper
00002     Copyright (C) 2001-2004 Mark Weaver
00003     Written by Mark Weaver <mark@npsl.co.uk>
00004 
00005     Part of the Open-Win32 library.
00006     This library is free software; you can redistribute it and/or
00007     modify it under the terms of the GNU Library General Public
00008     License as published by the Free Software Foundation; either
00009     version 2 of the License, or (at your option) any later version.
00010 
00011     This library is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014     Library General Public License for more details.
00015 
00016     You should have received a copy of the GNU Library General Public
00017     License along with this library; if not, write to the
00018     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00019     Boston, MA  02111-1307, USA.
00020 */
00021 
00027 #ifndef OW32_File_h
00028 #define OW32_File_h
00029 
00030 #include <OW32/XHANDLE.h>
00031 
00032 namespace OW32
00033 {
00034 
00043 class CFile
00044 {
00045 public:
00049     CFile() {}
00050 
00054     CFile(HANDLE hFile) :
00055         m_Handle(hFile)
00056     {
00057     }
00058 
00067     HANDLE Detach() 
00068     {
00069         return m_Handle.Detach();
00070     }
00071 
00077     void Attach(HANDLE h)
00078     {
00079         m_Handle = h;
00080     }
00081 
00085     operator HANDLE() const { return m_Handle; }
00086 
00090     HANDLE* operator&() { return &m_Handle; }
00091 
00095     HANDLE operator=(HANDLE h) { m_Handle = h; return m_Handle; }
00096 
00100     BOOL CreateA(LPCSTR lpFileName, DWORD dwDesiredAccess = 0, DWORD dwShareMode = 0, 
00101               LPSECURITY_ATTRIBUTES lpSecurityAttributes = NULL, DWORD dwCreationDisposition = 0, DWORD dwFlagsAndAttributes = 0, 
00102               HANDLE hTemplateFile = NULL)
00103     {
00104         m_Handle = ::CreateFileA(lpFileName, dwDesiredAccess, dwShareMode, lpSecurityAttributes,
00105                                 dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile);
00106         return (m_Handle != INVALID_HANDLE_VALUE);
00107     }
00108     
00112     BOOL CreateW(LPCWSTR lpFileName, DWORD dwDesiredAccess = 0, DWORD dwShareMode = 0, 
00113               LPSECURITY_ATTRIBUTES lpSecurityAttributes = NULL, DWORD dwCreationDisposition = 0, DWORD dwFlagsAndAttributes = 0, 
00114               HANDLE hTemplateFile = NULL)
00115     {
00116         m_Handle = ::CreateFileW(lpFileName, dwDesiredAccess, dwShareMode, lpSecurityAttributes,
00117                                 dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile);
00118         return (m_Handle != INVALID_HANDLE_VALUE);
00119     }
00120 
00124     BOOL Create(LPCTSTR lpFileName, DWORD dwDesiredAccess = 0, DWORD dwShareMode = 0, 
00125               LPSECURITY_ATTRIBUTES lpSecurityAttributes = NULL, DWORD dwCreationDisposition = 0, DWORD dwFlagsAndAttributes = 0, 
00126               HANDLE hTemplateFile = NULL)
00127     {
00128         m_Handle = ::CreateFile(lpFileName, dwDesiredAccess, dwShareMode, lpSecurityAttributes,
00129                                 dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile);
00130         return (m_Handle != INVALID_HANDLE_VALUE);
00131     }
00132 
00136     void Close()
00137     {
00138         m_Handle.Close();
00139     }
00140 
00144     BOOL CreateAlwaysA(LPCSTR lpFileName, DWORD dwDesiredAccess = GENERIC_WRITE|GENERIC_READ, DWORD dwShareMode = 0, 
00145               LPSECURITY_ATTRIBUTES lpSecurityAttributes = NULL, DWORD dwFlagsAndAttributes = 0, 
00146               HANDLE hTemplateFile = NULL)
00147     {
00148         return CreateA(lpFileName, dwDesiredAccess, dwShareMode, lpSecurityAttributes, CREATE_ALWAYS, 
00149                       dwFlagsAndAttributes, hTemplateFile);
00150     }
00151 
00155     BOOL CreateAlwaysW(LPCWSTR lpFileName, DWORD dwDesiredAccess = GENERIC_WRITE|GENERIC_READ, DWORD dwShareMode = 0, 
00156               LPSECURITY_ATTRIBUTES lpSecurityAttributes = NULL, DWORD dwFlagsAndAttributes = 0, 
00157               HANDLE hTemplateFile = NULL)
00158     {
00159         return CreateW(lpFileName, dwDesiredAccess, dwShareMode, lpSecurityAttributes, CREATE_ALWAYS, 
00160                       dwFlagsAndAttributes, hTemplateFile);
00161     }
00162 
00166     BOOL CreateAlways(LPCTSTR lpFileName, DWORD dwDesiredAccess = GENERIC_WRITE|GENERIC_READ, DWORD dwShareMode = 0, 
00167               LPSECURITY_ATTRIBUTES lpSecurityAttributes = NULL, DWORD dwFlagsAndAttributes = 0, 
00168               HANDLE hTemplateFile = NULL)
00169     {
00170         return Create(lpFileName, dwDesiredAccess, dwShareMode, lpSecurityAttributes, CREATE_ALWAYS, 
00171                       dwFlagsAndAttributes, hTemplateFile);
00172     }
00173 
00177     BOOL OpenExistingA(LPCSTR lpFileName, DWORD dwDesiredAccess = GENERIC_WRITE|GENERIC_READ, DWORD dwShareMode = 0, 
00178               LPSECURITY_ATTRIBUTES lpSecurityAttributes = NULL, DWORD dwFlagsAndAttributes = 0, 
00179               HANDLE hTemplateFile = NULL)
00180     {
00181         return CreateA(lpFileName, dwDesiredAccess, dwShareMode, lpSecurityAttributes, OPEN_EXISTING, 
00182                       dwFlagsAndAttributes, hTemplateFile);
00183     }
00184 
00188     BOOL OpenExistingW(LPCWSTR lpFileName, DWORD dwDesiredAccess = GENERIC_WRITE|GENERIC_READ, DWORD dwShareMode = 0, 
00189               LPSECURITY_ATTRIBUTES lpSecurityAttributes = NULL, DWORD dwFlagsAndAttributes = 0, 
00190               HANDLE hTemplateFile = NULL)
00191     {
00192         return CreateW(lpFileName, dwDesiredAccess, dwShareMode, lpSecurityAttributes, OPEN_EXISTING, 
00193                       dwFlagsAndAttributes, hTemplateFile);
00194     }
00195 
00199     BOOL OpenExisting(LPCTSTR lpFileName, DWORD dwDesiredAccess = GENERIC_WRITE|GENERIC_READ, DWORD dwShareMode = 0, 
00200               LPSECURITY_ATTRIBUTES lpSecurityAttributes = NULL, DWORD dwFlagsAndAttributes = 0, 
00201               HANDLE hTemplateFile = NULL)
00202     {
00203         return Create(lpFileName, dwDesiredAccess, dwShareMode, lpSecurityAttributes, OPEN_EXISTING, 
00204                       dwFlagsAndAttributes, hTemplateFile);
00205     }
00206 
00210     BOOL OpenAlwaysA(LPCSTR lpFileName, DWORD dwDesiredAccess = GENERIC_WRITE|GENERIC_READ, DWORD dwShareMode = 0, 
00211               LPSECURITY_ATTRIBUTES lpSecurityAttributes = NULL, DWORD dwFlagsAndAttributes = 0, 
00212               HANDLE hTemplateFile = NULL)
00213     {
00214         return CreateA(lpFileName, dwDesiredAccess, dwShareMode, lpSecurityAttributes, OPEN_ALWAYS, 
00215                       dwFlagsAndAttributes, hTemplateFile);
00216     }
00217     
00221     BOOL OpenAlwaysW(LPCWSTR lpFileName, DWORD dwDesiredAccess = GENERIC_WRITE|GENERIC_READ, DWORD dwShareMode = 0, 
00222               LPSECURITY_ATTRIBUTES lpSecurityAttributes = NULL, DWORD dwFlagsAndAttributes = 0, 
00223               HANDLE hTemplateFile = NULL)
00224     {
00225         return CreateW(lpFileName, dwDesiredAccess, dwShareMode, lpSecurityAttributes, OPEN_ALWAYS, 
00226                       dwFlagsAndAttributes, hTemplateFile);
00227     }
00228 
00232     BOOL OpenAlways(LPCTSTR lpFileName, DWORD dwDesiredAccess = GENERIC_WRITE|GENERIC_READ, DWORD dwShareMode = 0, 
00233               LPSECURITY_ATTRIBUTES lpSecurityAttributes = NULL, DWORD dwFlagsAndAttributes = 0, 
00234               HANDLE hTemplateFile = NULL)
00235     {
00236         return Create(lpFileName, dwDesiredAccess, dwShareMode, lpSecurityAttributes, OPEN_ALWAYS, 
00237                       dwFlagsAndAttributes, hTemplateFile);
00238     }
00239 
00243     BOOL Read(LPVOID lpBuffer, DWORD nNumberOfBytesToRead, LPDWORD lpNumberOfBytesRead=NULL, LPOVERLAPPED lpOverlapped=NULL)
00244     {
00245         DWORD dwNumberOfBytesRead;
00246         if (lpNumberOfBytesRead == NULL)
00247             lpNumberOfBytesRead = &dwNumberOfBytesRead;
00248         return ::ReadFile(m_Handle, lpBuffer, nNumberOfBytesToRead, lpNumberOfBytesRead, lpOverlapped);
00249     }
00250     
00254     BOOL Write(LPCVOID lpBuffer, DWORD nNumberOfBytesToWrite, LPDWORD lpNumberOfBytesWritten=NULL, LPOVERLAPPED lpOverlapped=NULL)
00255     {
00256         DWORD dwNumberOfBytesWritten;
00257         if (lpNumberOfBytesWritten == NULL)
00258             lpNumberOfBytesWritten = &dwNumberOfBytesWritten;
00259         return ::WriteFile(m_Handle, lpBuffer, nNumberOfBytesToWrite, lpNumberOfBytesWritten, lpOverlapped);
00260     }
00261 
00266     BOOL GetSize(PULARGE_INTEGER pSize)
00267     {
00268         pSize->LowPart = ::GetFileSize(m_Handle, &pSize->HighPart);
00269         if (pSize->LowPart == INVALID_FILE_SIZE && GetLastError() != ERROR_SUCCESS)
00270             return FALSE;
00271         return TRUE;
00272     }
00273 
00274 #if (_WIN32_WINNT >= 0x0500)
00275 
00278     BOOL GetSizeEx(PLARGE_INTEGER pSize)
00279     {
00280         return ::GetFileSizeEx(m_Handle, pSize);
00281     }
00282 
00286     BOOL SetPointerEx(const LARGE_INTEGER& liDistanceToMove, DWORD dwMoveMethod = FILE_BEGIN, PLARGE_INTEGER lpNewFilePointer = NULL)
00287     {
00288         return ::SetFilePointerEx(m_Handle, liDistanceToMove, lpNewFilePointer, dwMoveMethod);
00289     }
00290 
00294     BOOL SetPointerEx(__int64 i64DistanceToMove, DWORD dwMoveMethod = FILE_BEGIN, PLARGE_INTEGER lpNewFilePointer = NULL)
00295     {
00296         LARGE_INTEGER liDistanceToMove;
00297         liDistanceToMove.QuadPart = i64DistanceToMove;
00298         return ::SetFilePointerEx(m_Handle, liDistanceToMove, lpNewFilePointer, dwMoveMethod);
00299     }
00300 #endif
00301 
00306     BOOL SetPointer(const LARGE_INTEGER& liDistanceToMove, DWORD dwMoveMethod = FILE_BEGIN, PLARGE_INTEGER lpNewFilePointer = NULL)
00307     {
00308         LONG dwPtrHigh = liDistanceToMove.HighPart;
00309         DWORD dwPtrLow = ::SetFilePointer(m_Handle, (LONG)liDistanceToMove.LowPart, &dwPtrHigh, dwMoveMethod);
00310         if (dwPtrLow == INVALID_SET_FILE_POINTER && GetLastError () != ERROR_SUCCESS)
00311             return FALSE;
00312         if (lpNewFilePointer)
00313         {
00314             lpNewFilePointer->LowPart = dwPtrLow;
00315             lpNewFilePointer->HighPart = dwPtrHigh;
00316         }
00317         return TRUE;
00318     }
00319 
00323     BOOL SetPointer(__int64 i64DistanceToMove, DWORD dwMoveMethod = FILE_BEGIN, PLARGE_INTEGER lpNewFilePointer = NULL)
00324     {
00325         LARGE_INTEGER li;
00326         li.QuadPart = i64DistanceToMove;
00327         return SetPointer(li, dwMoveMethod, lpNewFilePointer);
00328     }
00329 
00333     BOOL SetEnd()
00334     {
00335         return ::SetEndOfFile(m_Handle);
00336     }
00337 
00343     BOOL IsOpen()
00344     {
00345         return (m_Handle != INVALID_HANDLE_VALUE && m_Handle != NULL);
00346     }
00347 
00353     BOOL GetInformation(LPBY_HANDLE_FILE_INFORMATION lpFileInformation)
00354     {
00355         return GetFileInformationByHandle(m_Handle, lpFileInformation);
00356     }
00357 
00363     BOOL FlushBuffers()
00364     {
00365         return ::FlushFileBuffers(m_Handle);
00366     }
00367     
00375     BOOL SetTime(const FILETIME *lpCreationTime, const FILETIME *lpLastAccessTime, const FILETIME *lpLastWriteTime)
00376     {
00377         return SetFileTime(m_Handle, lpCreationTime, lpLastAccessTime, lpLastWriteTime);
00378     }
00379 
00380 private:
00382     XHANDLE m_Handle;
00384     CFile& operator= (const CFile& );
00385     CFile(const CFile& );
00386 };
00387 
00388 }; // namespace OW32
00389 
00390 #endif // OW32_File_h

Generated on Sun Jun 5 01:29:17 2005 for OW32 by  doxygen 1.3.9.1