-#ifndef WIN32
-#include <errno.h>
-#include <fcntl.h>
-#include <unistd.h>
-#endif
-#include <msp/strings/format.h>
-#include <msp/core/systemerror.h>
#include "file.h"
#include "handle_private.h"
mode = m;
-#ifdef WIN32
- int flags = 0;
- int create_flags = OPEN_EXISTING;
-
- if(mode&M_READ)
- flags |= GENERIC_READ;
- else if(mode&M_WRITE)
- {
- flags |= GENERIC_WRITE;
-
- switch(static_cast<int>(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;
- }
- }
-
- *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)
- 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()
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
+ adjust_mode(mode, M_NONBLOCK, !b);
+ sys_set_blocking(handle, b);
+}
+
+void File::set_inherit(bool i)
+{
+ adjust_mode(mode, M_INHERIT, i);
+ sys_set_inherit(handle, i);
}
unsigned File::do_write(const char *buf, unsigned size)
if(size==0)
return 0;
-#ifdef WIN32
+#ifdef _WIN32
if(mode&M_APPEND)
seek(0, S_END);
#endif
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();
return handle;
}
+
+BufferedFile::BufferedFile(const string &fn, Mode m, File::CreateMode cm):
+ file(fn, m, cm),
+ buffer(file),
+ position(0)
+{
+ mode = m;
+ file.signal_end_of_file.connect(sigc::mem_fun(this, &BufferedFile::set_eof));
+}
+
+void BufferedFile::set_block(bool b)
+{
+ file.set_block(b);
+}
+
+void BufferedFile::set_inherit(bool)
+{
+ throw logic_error("BufferedFile::set_inherit");
+}
+
+unsigned BufferedFile::do_write(const char *buf, unsigned size)
+{
+ unsigned ret = buffer.write(buf, size);
+ position += ret;
+ return ret;
+}
+
+unsigned BufferedFile::do_read(char *buf, unsigned size)
+{
+ unsigned ret = buffer.read(buf, size);
+ position += ret;
+ return ret;
+}
+
+unsigned BufferedFile::put(char c)
+{
+ unsigned ret = buffer.put(c);
+ position += ret;
+ return ret;
+}
+
+bool BufferedFile::getline(string &line)
+{
+ bool ret = buffer.getline(line);
+ if(ret)
+ position += line.size();
+ return ret;
+}
+
+int BufferedFile::get()
+{
+ int ret = buffer.get();
+ if(ret>=0)
+ ++position;
+ return ret;
+}
+
+const Handle &BufferedFile::get_handle(Mode)
+{
+ throw logic_error("BufferedFile::get_handle");
+}
+
+SeekOffset BufferedFile::seek(SeekOffset offset, SeekType type)
+{
+ if(type==S_CUR)
+ {
+ offset += position;
+ type = S_BEG;
+ }
+
+ signal_flush_required.emit();
+ position = file.seek(offset, type);
+ eof_flag = false;
+ return position;
+}
+
} // namespace IO
} // namespace Msp