]> git.tdb.fi Git - libs/core.git/blobdiff - source/io/file.cpp
Use the new Handle class to hide platform details from public headers
[libs/core.git] / source / io / file.cpp
index 74930baaf497286f32925ee395641147813daf58..25e3b4af115855a6b332e99ebcf947599590cbc8 100644 (file)
@@ -3,9 +3,10 @@
 #include <fcntl.h>
 #include <unistd.h>
 #endif
+#include <msp/strings/format.h>
 #include <msp/core/systemerror.h>
-#include <msp/strings/formatter.h>
 #include "file.h"
+#include "handle_private.h"
 
 using namespace std;
 
@@ -40,8 +41,8 @@ File::File(const string &fn, Mode m, CreateMode cm)
                }
        }
 
-       handle = CreateFile(fn.c_str(), flags, 0, 0, create_flags, FILE_ATTRIBUTE_NORMAL, 0);
-       if(handle==INVALID_HANDLE_VALUE)
+       *handle = CreateFile(fn.c_str(), flags, 0, 0, create_flags, FILE_ATTRIBUTE_NORMAL, 0);
+       if(!handle)
        {
                int err = GetLastError();
                if(err==ERROR_FILE_NOT_FOUND)
@@ -71,8 +72,8 @@ File::File(const string &fn, Mode m, CreateMode cm)
        if(mode&M_NONBLOCK)
                flags |= O_NONBLOCK;
 
-       handle = ::open(fn.c_str(), flags, 0666);
-       if(handle==-1)
+       *handle = ::open(fn.c_str(), flags, 0666);
+       if(!handle)
        {
                int err = errno;
                if(err==ENOENT)
@@ -92,7 +93,7 @@ File::~File()
 
 void File::close()
 {
-       if(handle==MSP_IO_INVALID_HANDLE)
+       if(!handle)
                return;
 
        set_events(P_NONE);
@@ -100,12 +101,12 @@ void File::close()
        signal_flush_required.emit();
 
 #ifdef WIN32
-       CloseHandle(handle);
+       CloseHandle(*handle);
 #else
-       ::close(handle);
+       ::close(*handle);
 #endif
 
-       handle = MSP_IO_INVALID_HANDLE;
+       handle = Handle();
        signal_closed.emit();
 }
 
@@ -117,8 +118,8 @@ void File::set_block(bool b)
        if(b)
                mode = (mode|M_NONBLOCK);
 #ifndef WIN32
-       int flags = fcntl(handle, F_GETFD);
-       fcntl(handle, F_SETFL, (flags&O_NONBLOCK)|(b?0:O_NONBLOCK));
+       int flags = fcntl(*handle, F_GETFD);
+       fcntl(*handle, F_SETFL, (flags&O_NONBLOCK)|(b?0:O_NONBLOCK));
 #endif
 }
 
@@ -133,10 +134,10 @@ unsigned File::do_write(const char *buf, unsigned size)
        if(mode&M_APPEND)
                seek(0, S_END);
        DWORD ret;
-       if(WriteFile(handle, buf, size, &ret, 0)==0)
+       if(WriteFile(*handle, buf, size, &ret, 0)==0)
                throw system_error("WriteFile");
 #else
-       int ret = ::write(handle, buf, size);
+       int ret = ::write(*handle, buf, size);
        if(ret==-1)
        {
                if(errno==EAGAIN)
@@ -158,10 +159,10 @@ unsigned File::do_read(char *buf, unsigned size)
 
 #ifdef WIN32
        DWORD ret;
-       if(ReadFile(handle, buf, size, &ret, 0)==0)
+       if(ReadFile(*handle, buf, size, &ret, 0)==0)
                throw system_error("ReadFile");
 #else
-       int ret = ::read(handle, buf, size);
+       int ret = ::read(*handle, buf, size);
        if(ret==-1)
        {
                if(errno==EAGAIN)
@@ -185,7 +186,7 @@ void File::sync()
 #ifndef WIN32
        signal_flush_required.emit();
 
-       fsync(handle);
+       fsync(*handle);
 #endif
 }
 
@@ -197,11 +198,11 @@ unsigned File::seek(int off, SeekType st)
 
        int type = sys_seek_type(st);
 #ifdef WIN32
-       DWORD ret = SetFilePointer(handle, off, 0, type);
+       DWORD ret = SetFilePointer(*handle, off, 0, type);
        if(ret==INVALID_SET_FILE_POINTER)
                throw system_error("SetFilePointer");
 #else
-       off_t ret = lseek(handle, off, type);
+       off_t ret = lseek(*handle, off, type);
        if(ret==(off_t)-1)
                throw system_error("lseek");
 #endif
@@ -216,11 +217,11 @@ unsigned File::tell() const
        check_access(M_NONE);
 
 #ifdef WIN32
-       DWORD ret = SetFilePointer(handle, 0, 0, FILE_CURRENT);
+       DWORD ret = SetFilePointer(*handle, 0, 0, FILE_CURRENT);
        if(ret==INVALID_SET_FILE_POINTER)
                throw system_error("SetFilePointer");
 #else
-       off_t ret = lseek(handle, 0, SEEK_CUR);
+       off_t ret = lseek(*handle, 0, SEEK_CUR);
        if(ret==(off_t)-1)
                throw system_error("lseek");
 #endif
@@ -230,7 +231,7 @@ unsigned File::tell() const
 
 void File::check_access(Mode m) const
 {
-       if(handle==MSP_IO_INVALID_HANDLE || (m && !(mode&m)))
+       if(!handle || (m && !(mode&m)))
                throw invalid_access(m);
 }