00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00015 #include <ders/file.hpp>
00016 #include <errno.h>
00017 #include <fcntl.h>
00018 #include <io.h>
00019 #include <stdio.h>
00020 #include <sys/stat.h>
00021
00022 namespace {
00023
00024 using namespace ders;
00025
00026 inline int setErr(int rc, error& err)
00027 {
00028 err=(rc==-1) ? convert_errno(errno) : ezero;
00029 return rc;
00030 }
00031
00032 }
00033
00034 namespace ders {
00035
00036 int fd::open(const char* path, file::mode md, int fls, error& err)
00037 {
00038 int m=0;
00039 switch (md) {
00040 case file::rdo: m=O_RDONLY; break;
00041 case file::wro: m=O_WRONLY; break;
00042 case file::rdwr: m=O_RDWR; break;
00043 default: assert(false);
00044 }
00045
00046 if (fls&file::app) m|=O_APPEND;
00047 if (fls&file::crt) m|=O_CREAT;
00048 if (fls&file::excl) m|=O_EXCL;
00049 if (fls&file::trnc) m|=O_TRUNC;
00050
00051 return setErr(::open(path, m|O_BINARY, S_IREAD|S_IWRITE), err);
00052 }
00053
00054 bool fd::close(int fd, error& err)
00055 {
00056 return setErr(::close(fd), err)==0;
00057 }
00058
00059 int fd::read(int fd, void* buf, int n, error& err)
00060 {
00061 assert(n>0);
00062 int rc=::read(fd, buf, n);
00063 assert(rc<=n);
00064
00065 return setErr(rc, err);
00066 }
00067
00068 int fd::write(int fd, const void* buf, int n, error& err)
00069 {
00070 assert(n>0);
00071 int rc=::write(fd, buf, n);
00072 assert(rc<=n);
00073
00074 return setErr(rc, err);
00075 }
00076
00077 int fd::seek(int fd, int off, file::whence wh, error& err)
00078 {
00079 int w=0;
00080 switch (wh) {
00081 case file::set: w=SEEK_SET; break;
00082 case file::cur: w=SEEK_CUR; break;
00083 case file::end: w=SEEK_END; break;
00084 default: assert(false);
00085 }
00086
00087 return setErr(::lseek(fd, off, w), err);
00088 }
00089
00090 }
00091