X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Fio%2Ffile.cpp;fp=source%2Fio%2Ffile.cpp;h=90694324aa1d6e52f8c59b9f649135725926e031;hp=805a9e8dba0072270d90a8cb695faa681e710e3a;hb=609c9a508cfdc7b42c46c4f21d17639204165a00;hpb=b4806214e905752617691f851717033fd3f266c2 diff --git a/source/io/file.cpp b/source/io/file.cpp index 805a9e8..9069432 100644 --- a/source/io/file.cpp +++ b/source/io/file.cpp @@ -1,10 +1,3 @@ -#ifndef WIN32 -#include -#include -#include -#endif -#include -#include #include "file.h" #include "handle_private.h" @@ -24,77 +17,7 @@ File::File(const string &fn, Mode m, CreateMode cm) mode = m; -#ifdef WIN32 - int flags = 0; - int share_flags = 0; - int create_flags = OPEN_EXISTING; - - if(mode&M_READ) - flags |= GENERIC_READ; - - if(mode&M_WRITE) - { - flags |= GENERIC_WRITE; - - switch(static_cast(cm)) - { - case C_NONE: create_flags = OPEN_EXISTING; break; - case C_CREATE: create_flags = OPEN_ALWAYS; break; - case C_TRUNCATE: create_flags = TRUNCATE_EXISTING; break; - case C_OVERWRITE: create_flags = CREATE_ALWAYS; break; - case C_NEW: create_flags = CREATE_NEW; break; - } - } - else - share_flags = FILE_SHARE_READ; - - *handle = CreateFile(fn.c_str(), flags, share_flags, 0, create_flags, FILE_ATTRIBUTE_NORMAL, 0); - if(!handle) - { - int err = GetLastError(); - if(err==ERROR_FILE_NOT_FOUND) - throw file_not_found(fn); - else if(err==ERROR_FILE_EXISTS) - throw file_already_exists(fn); - else - throw system_error(format("CreateFile(%s)", fn), err); - } -#else - int flags = 0; - switch(mode&M_RDWR) - { - case M_READ: flags |= O_RDONLY; break; - case M_WRITE: flags |= O_WRONLY; break; - case M_RDWR: flags |= O_RDWR; break; - default:; - } - - if(mode&M_WRITE) - { - if(cm&C_CREATE) - flags |= O_CREAT; - if(cm&C_TRUNCATE) - flags |= O_TRUNC; - if(cm&C_EXCLUSIVE) - flags |= O_EXCL; - } - if(mode&M_APPEND) - flags |= O_APPEND; - if(mode&M_NONBLOCK) - flags |= O_NONBLOCK; - - *handle = ::open(fn.c_str(), flags, 0666); - if(!handle) - { - int err = errno; - if(err==ENOENT) - throw file_not_found(fn); - else if(err==EEXIST) - throw file_already_exists(fn); - else - throw system_error(format("open(%s)", fn), err); - } -#endif + platform_init(fn, cm); } File::~File() @@ -105,13 +28,8 @@ File::~File() void File::set_block(bool b) { - mode = (mode&~M_NONBLOCK); - 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)); -#endif + mode = b?(mode&~M_NONBLOCK):(mode|M_NONBLOCK); + sys_set_blocking(handle, b); } unsigned File::do_write(const char *buf, unsigned size) @@ -143,15 +61,6 @@ unsigned File::do_read(char *buf, unsigned size) return ret; } -void File::sync() -{ -#ifndef WIN32 - signal_flush_required.emit(); - - fsync(*handle); -#endif -} - SeekOffset File::seek(SeekOffset off, SeekType type) { signal_flush_required.emit();