posix/fdimpl.cpp

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) Sergey P. Derevyago, 2008-2009.
00003  *
00004  * Permission to copy, use, modify, sell and distribute this software is granted
00005  * provided this copyright notice appears in all copies.
00006  * This software is provided "as is" without express or implied warranty, and
00007  * with no claim as to its suitability for any purpose.
00008  *
00009  */
00010 
00015 #include <ders/file.hpp>
00016 #include <errno.h>
00017 #include <fcntl.h>
00018 #include <unistd.h>
00019 #include <sys/stat.h>
00020 
00021 namespace {
00022 
00023 using namespace ders;
00024 
00025 inline int setErr(int rc, error& err)
00026 {
00027  err=(rc==-1) ? convert_errno(errno) : ezero;
00028  return rc;
00029 }
00030 
00031 }  // unnamed
00032 
00033 namespace ders {  // ::ders
00034 
00035 int fd::open(const char* path, file::mode md, int fls, error& err)
00036 {
00037  int m=0;
00038  switch (md) {
00039         case file::rdo:  m=O_RDONLY; break;
00040         case file::wro:  m=O_WRONLY; break;
00041         case file::rdwr: m=O_RDWR;   break;
00042         default:         assert(false);
00043  }
00044 
00045  if (fls&file::app)  m|=O_APPEND;
00046  if (fls&file::crt)  m|=O_CREAT;
00047  if (fls&file::excl) m|=O_EXCL;
00048  if (fls&file::trnc) m|=O_TRUNC;
00049 
00050  return setErr(::open(path, m, S_IREAD|S_IWRITE), err);
00051 }
00052 
00053 bool fd::close(int fd, error& err)
00054 {
00055  return setErr(::close(fd), err)==0;
00056 }
00057 
00058 int fd::read(int fd, void* buf, int n, error& err)
00059 {
00060  assert(n>0);
00061  int rc=::read(fd, buf, n);
00062  assert(rc<=n);
00063 
00064  return setErr(rc, err);
00065 }
00066 
00067 int fd::write(int fd, const void* buf, int n, error& err)
00068 {
00069  assert(n>0);
00070  int rc=::write(fd, buf, n);
00071  assert(rc<=n);
00072 
00073  return setErr(rc, err);
00074 }
00075 
00076 int fd::seek(int fd, int off, file::whence wh, error& err)
00077 {
00078  int w=0;
00079  switch (wh) {
00080         case file::set: w=SEEK_SET; break;
00081         case file::cur: w=SEEK_CUR; break;
00082         case file::end: w=SEEK_END; break;
00083         default:        assert(false);
00084  }
00085 
00086  return setErr(::lseek(fd, off, w), err);
00087 }
00088 
00089 }  // namespace ::ders
00090 

Generated on Tue Dec 8 11:35:32 2009 for derslib by  doxygen 1.5.5