6 #include <msp/strings/format.h>
7 #include <msp/core/systemerror.h>
9 #include "handle_private.h"
16 File::File(const string &fn, Mode m, CreateMode cm)
19 throw invalid_argument("File::File mode");
20 if(cm&~(C_CREATE|C_TRUNCATE))
21 throw invalid_argument("File::File create");
27 int create_flags = OPEN_EXISTING;
30 flags |= GENERIC_READ;
33 flags |= GENERIC_WRITE;
35 switch(static_cast<int>(cm))
37 case C_NONE: create_flags = OPEN_EXISTING; break;
38 case C_CREATE: create_flags = OPEN_ALWAYS; break;
39 case C_TRUNCATE: create_flags = TRUNCATE_EXISTING; break;
40 case C_CREATE+C_TRUNCATE: create_flags = CREATE_ALWAYS; break;
44 *handle = CreateFile(fn.c_str(), flags, 0, 0, create_flags, FILE_ATTRIBUTE_NORMAL, 0);
47 int err = GetLastError();
48 if(err==ERROR_FILE_NOT_FOUND)
49 throw file_not_found(fn);
51 throw system_error(format("CreateFile(%s)", fn), err);
57 case M_READ: flags |= O_RDONLY; break;
58 case M_WRITE: flags |= O_WRONLY; break;
59 case M_RDWR: flags |= O_RDWR; break;
75 *handle = ::open(fn.c_str(), flags, 0666);
80 throw file_not_found(fn);
82 throw system_error(format("open(%s)", fn), err);
97 signal_flush_required.emit();
102 signal_closed.emit();
105 void File::set_block(bool b)
107 check_access(M_NONE);
109 mode = (mode&~M_NONBLOCK);
111 mode = (mode|M_NONBLOCK);
113 int flags = fcntl(*handle, F_GETFD);
114 fcntl(*handle, F_SETFL, (flags&O_NONBLOCK)|(b?0:O_NONBLOCK));
118 unsigned File::do_write(const char *buf, unsigned size)
120 check_access(M_WRITE);
130 return sys_write(handle, buf, size);
133 unsigned File::do_read(char *buf, unsigned size)
135 check_access(M_READ);
140 unsigned ret = sys_read(handle, buf, size);
144 signal_end_of_file.emit();
153 signal_flush_required.emit();
159 unsigned File::seek(int off, SeekType st)
161 check_access(M_NONE);
163 signal_flush_required.emit();
165 int type = sys_seek_type(st);
167 DWORD ret = SetFilePointer(*handle, off, 0, type);
168 if(ret==INVALID_SET_FILE_POINTER)
169 throw system_error("SetFilePointer");
171 off_t ret = lseek(*handle, off, type);
173 throw system_error("lseek");
181 unsigned File::tell() const
183 check_access(M_NONE);
186 DWORD ret = SetFilePointer(*handle, 0, 0, FILE_CURRENT);
187 if(ret==INVALID_SET_FILE_POINTER)
188 throw system_error("SetFilePointer");
190 off_t ret = lseek(*handle, 0, SEEK_CUR);
192 throw system_error("lseek");
198 void File::check_access(Mode m) const
200 if(!handle || (m && !(mode&m)))
201 throw invalid_access(m);