Main Page   Namespace List   Compound List   File List   Namespace Members   Compound Members   Related Pages  

filesPAL.hpp

Go to the documentation of this file.
00001 
00002 #ifndef __FILES_WIN32_PAL_HPP__
00003 #define __FILES_WIN32_PAL_HPP__
00004 
00005 //#include <winbase.h>
00006 #include <assert.h>
00007 #include <windows.h>
00008 
00009 namespace lfc
00010 {
00011 namespace win32PAL
00012 {
00013 
00015 
00019 namespace files
00020 {
00021 
00022 const int MAX_ERROR_CODE = 4;
00023 
00024 enum ErrorCodes
00025 {
00026     errOk,
00027     errGeneric,
00028     errNotSupported,
00029     errAlreadyExists,
00030     errCantTruncate,
00031 };
00032 
00033 extern const char *messagesTable[MAX_ERROR_CODE + 1];
00034 
00035 
00037 typedef ::HANDLE Handle;
00038 
00040 const Handle NULL_HANDLE = INVALID_HANDLE_VALUE;
00041 
00043 const int
00044     seekSet         = 0x0001,
00045     seekCurrent     = 0x0002,
00046     seekEnd         = 0x0004;
00047 
00049 const int
00050     flAppend        = 0x0001,
00051     flRead          = 0x0002,
00052     flWrite         = 0x0004,
00053     flTruncate      = 0x0008,
00054     flOpenExisting  = 0x0010,
00055     flCreateNew     = 0x0020,
00056     flTemp          = 0x0040;
00057 
00059 const int
00060     shRead      = 0x0001,
00061     shWrite     = 0x0002;
00062 
00063 
00064 // forward declarations
00065 inline int close(Handle handle);
00066 inline int seek(Handle handle, long offset, int mode, long &pos);
00067 inline int setEof(Handle handle);
00068 
00069 
00071 
00074 inline int init()
00075 {
00076     return errOk;
00077 }
00078 
00080 
00083 inline int cleanup()
00084 {
00085     return errOk;
00086 }
00087 
00089 
00097 inline int open(Handle &handle, const char *name, int flags, int shareFlags)
00098 {
00099     DWORD dwAccess = 0;
00100     dwAccess |= (flags & flRead ? GENERIC_READ : 0);
00101     dwAccess |= (flags & flWrite ? GENERIC_WRITE : 0);
00102 
00103     DWORD dwShare = 0;
00104     dwShare |= (shareFlags & shRead ? FILE_SHARE_READ : 0);
00105     dwShare |= (shareFlags & shWrite ? FILE_SHARE_WRITE : 0);
00106 
00107     DWORD dwCreation = 0;
00108     if(flags & flOpenExisting)
00109         dwCreation = OPEN_EXISTING;
00110     else if(flags & flCreateNew)
00111         dwCreation = CREATE_NEW;
00112     else if(flags & flTruncate)
00113         dwCreation = CREATE_ALWAYS;
00114     else
00115         dwCreation = OPEN_ALWAYS;
00116 
00117     DWORD dwFlagsAndAttrs = FILE_ATTRIBUTE_NORMAL;
00118     dwFlagsAndAttrs |= (flags & flTemp ? FILE_FLAG_DELETE_ON_CLOSE : 0);
00119 
00120     ::SetLastError(NO_ERROR);
00121     HANDLE h = ::CreateFile(name, dwAccess, dwShare, NULL, dwCreation,
00122         FILE_ATTRIBUTE_NORMAL, NULL);
00123 
00124     if(h != INVALID_HANDLE_VALUE)
00125     {
00126         handle = h;
00127         return errOk;
00128     }
00129     else
00130         switch(::GetLastError())
00131         {
00132         case ERROR_NOT_SUPPORTED:
00133             return errNotSupported;
00134 
00135         case ERROR_ALREADY_EXISTS:
00136             return errAlreadyExists;
00137 
00138         default:
00139             return errGeneric;
00140         }
00141 }
00142 
00144 
00148 inline int close(Handle handle)
00149 {
00150     assert(handle != NULL_HANDLE);
00151     return ::CloseHandle(handle) ? errOk : errGeneric;
00152 }
00153 
00155 
00162 inline int seek(Handle handle, long offset, int mode, long &pos)
00163 {
00164     DWORD dwMode = 0;
00165     switch(mode)
00166     {
00167     case seekSet: dwMode = FILE_BEGIN; break;
00168     case seekCurrent: dwMode = FILE_CURRENT; break;
00169     case seekEnd: dwMode = FILE_END; break;
00170     default:
00171         assert(false);
00172     }
00173 
00174     ::SetLastError(NO_ERROR);
00175     long newPos = ::SetFilePointer(handle, offset, NULL, dwMode);
00176 
00177     if(::GetLastError() == NO_ERROR)
00178     {
00179         pos = newPos;
00180         return errOk;
00181     }
00182     else
00183         return errGeneric;
00184 }
00185 
00187 
00191 inline int setEof(Handle handle)
00192 {
00193     return ::SetEndOfFile(handle) ? errOk : errGeneric;
00194 }
00195 
00197 
00204 inline int write(Handle handle, const void *buff, long count, long &written)
00205 {
00206     DWORD dwCount = static_cast<DWORD>(count);
00207     DWORD dwWritten = 0;
00208 
00209     if(::WriteFile(handle, buff, dwCount, &dwWritten, NULL))
00210     {
00211         written = static_cast<long>(dwWritten);
00212         return errOk;
00213     }
00214     else
00215         return errGeneric;
00216 }
00217 
00219 
00226 inline int read(Handle handle, void *buff, long count, long &read)
00227 {
00228     DWORD dwCount = static_cast<DWORD>(count);
00229     DWORD dwRead = 0;
00230 
00231     if(::ReadFile(handle, buff, dwCount, &dwRead, NULL))
00232     {
00233         read = static_cast<long>(dwRead);
00234         return errOk;
00235     }
00236     else
00237         return errGeneric;
00238 }
00239 
00240 
00242 
00246 inline int flush(Handle handle)
00247 {
00248     return ::FlushFileBuffers(handle) ? errOk : errGeneric;
00249 }
00250 
00251 
00253 
00256 inline const char *message(int index)
00257 {
00258     assert(index >= 0 && index <= MAX_ERROR_CODE);
00259     return messagesTable[index];
00260 }
00261 
00262 
00263 } // namespace win32PAL::files
00264 } // namespace win32PAL
00265 
00266 namespace pal = win32PAL;
00267 
00268 } // namespace lfc
00269 
00270 
00271 #endif  // __FILES_WIN32_PAL_HPP__

Generated on Sat Jan 26 00:34:57 2002 for LFC2 PAL by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001