ders::file Class Reference

Provides file I/O operations. More...

#include <file.hpp>

Inheritance diagram for ders::file:

ders::readable ders::writable ders::destroyable ders::destroyable

List of all members.

Public Types

enum  mode { rdo, wro, rdwr }
 file open modes More...
enum  flags { app = 1, crt = 2, excl = 4, trnc = 8 }
 file open flags More...
enum  whence { set, cur, end }
 seek whence More...

Public Member Functions

 file (mem_pool &m)
 file (mem_pool &m, const ch_rng &path, mode md, int fls)
 file (mem_pool &m, int fd)
 ~file ()
virtual void destroy (mem_pool &mp2)
mem_poolpool () const
bool is_ok () const
bool is_opened () const
bool is_owned () const
int get_fd () const
const sh_textget_name () const
error get_err () const
bool open (const ch_rng &path, mode md, int fls)
bool close ()
bool attach (int fd)
bool detach ()
virtual int read (void *buf, int n)
int read (sh_text &buf, int n)
virtual int write (const void *buf, int n)
int write (const ch_rng &r)
int tell ()
int seek (int off, whence wh)
void ex_is_ok () const
void ex_open (const ch_rng &path, mode md, int fls)
void ex_close ()
void ex_attach (int fd)
void ex_detach ()
int ex_read (void *buf, int n)
int ex_read (sh_text &buf, int n)
int ex_write (const void *buf, int n)
int ex_write (const ch_rng &r)
int ex_tell ()
int ex_seek (int off, whence wh)

Protected Member Functions

void operator delete (void *ptr, size_t size)

Static Protected Member Functions

template<class T>
static void destroy_this (T *ptr, mem_pool &mp2)


Detailed Description

Provides file I/O operations.

Definition at line 31 of file file.hpp.


Member Enumeration Documentation

file open modes

Enumerator:
rdo 
wro 
rdwr 

Definition at line 34 of file file.hpp.

00034 { rdo, wro, rdwr };

file open flags

Enumerator:
app 
crt 
excl 
trnc 

Definition at line 36 of file file.hpp.

00036 { app=1, crt=2, excl=4, trnc=8 };

seek whence

Enumerator:
set 
cur 
end 

Definition at line 38 of file file.hpp.

00038 { set, cur, end };


Constructor & Destructor Documentation

ders::file::file ( mem_pool m  ) 

Definition at line 33 of file file.cpp.

00033                       : mp(m), name(nt(m))
00034 {
00035  init();
00036 }

ders::file::file ( mem_pool m,
const ch_rng path,
mode  md,
int  fls 
)

Definition at line 38 of file file.cpp.

00038                                                             :
00039   mp(m), name(nt(m))
00040 {
00041  init();
00042  ex_open(path, md, fls);
00043 }

ders::file::file ( mem_pool m,
int  fd 
)

Definition at line 45 of file file.cpp.

00045                               : mp(m), name(nt(m))
00046 {
00047  init();
00048  ex_attach(fd);
00049 }

ders::file::~file (  ) 

Definition at line 51 of file file.cpp.

00052 {
00053  if (is_owned() && is_opened()) close();
00054 }


Member Function Documentation

virtual void ders::file::destroy ( mem_pool mp2  )  [inline, virtual]

Implements ders::destroyable.

Definition at line 58 of file file.hpp.

00058 { destroy_this(this, mp2); }

mem_pool& ders::file::pool (  )  const [inline]

Definition at line 60 of file file.hpp.

00060 { return mp; }

bool ders::file::is_ok (  )  const [inline]

Definition at line 62 of file file.hpp.

00062 { return get_err()==ezero; }

bool ders::file::is_opened (  )  const [inline]

Definition at line 63 of file file.hpp.

00063 { return get_fd()!=-1; }

bool ders::file::is_owned (  )  const [inline]

Definition at line 64 of file file.hpp.

00064 { return own; }

int ders::file::get_fd (  )  const [inline]

Definition at line 66 of file file.hpp.

00066 { return fdr; }

const sh_text& ders::file::get_name (  )  const [inline]

Definition at line 67 of file file.hpp.

00067 { return name; }

error ders::file::get_err (  )  const [inline]

Definition at line 68 of file file.hpp.

00068 { return err; }

bool ders::file::open ( const ch_rng path,
mode  md,
int  fls 
)

Definition at line 56 of file file.cpp.

00057 {
00058  assert(!is_opened());
00059 
00060  fdr=fd::open(nt(mp, path)->c_str(), md, fls, err);
00061  if (fdr==-1) return false;
00062 
00063  own=true;
00064  *name=path;
00065 
00066  return true;
00067 }

bool ders::file::close (  ) 

Definition at line 69 of file file.cpp.

00070 {
00071  assert(is_owned());
00072 
00073  bool rc=fd::close(fdr, err);
00074  if (!rc) return false;
00075 
00076  init();
00077  return true;
00078 }

bool ders::file::attach ( int  fd  ) 

Definition at line 80 of file file.cpp.

00081 {
00082  assert(!is_opened());
00083 
00084  if (fd<0) {
00085     err=ebadf;
00086     return false;
00087  }
00088 
00089  fdr=fd;
00090  own=false;
00091  *name=text_buf(mp)+"(attached to "+fdr+')';
00092  err=ezero;
00093 
00094  return true;
00095 }

bool ders::file::detach (  ) 

Definition at line 97 of file file.cpp.

00098 {
00099  assert(is_opened());
00100 
00101  init();
00102  return true;
00103 }

int ders::file::read ( void *  buf,
int  n 
) [virtual]

Implements ders::readable.

Definition at line 105 of file file.cpp.

00106 {
00107  assert(n>=0);
00108 
00109  if (n==0) {
00110     err=ezero;
00111     return 0;
00112  }
00113 
00114  return fd::read(fdr, buf, n, err);
00115 }

int ders::file::read ( sh_text buf,
int  n 
)

Definition at line 117 of file file.cpp.

00118 {
00119  assert(n>=0);
00120 
00121  buf->clear();
00122  buf->reserve(n);
00123 
00124  int rc=read(buf->begin(), n);
00125  assert(rc<=n);
00126  if (rc!=-1) buf->uninitialized_resize(rc);
00127 
00128  return rc;
00129 }

int ders::file::write ( const void *  buf,
int  n 
) [virtual]

Implements ders::writable.

Definition at line 131 of file file.cpp.

00132 {
00133  assert(n>=0);
00134 
00135  if (n==0) {
00136     err=ezero;
00137     return 0;
00138  }
00139 
00140  return fd::write(fdr, buf, n, err);
00141 }

int ders::file::write ( const ch_rng r  )  [inline]

Definition at line 80 of file file.hpp.

00080 { return write(r.beg, r.size()); }

int ders::file::tell (  )  [inline]

Definition at line 82 of file file.hpp.

00082 { return seek(0, cur); }

int ders::file::seek ( int  off,
whence  wh 
)

Definition at line 143 of file file.cpp.

00144 {
00145  return fd::seek(fdr, off, wh, err);
00146 }

void ders::file::ex_is_ok (  )  const

Definition at line 148 of file file.cpp.

00149 {
00150  if (!is_ok()) {
00151     throw newFileErrorException(mp, _FLINE_, "File error", get_err(),
00152       get_name());
00153  }
00154 }

void ders::file::ex_open ( const ch_rng path,
mode  md,
int  fls 
)

Definition at line 156 of file file.cpp.

00157 {
00158  open(path, md, fls);
00159  if (!is_ok()) {
00160     throw newFileErrorException(mp, _FLINE_, "Can't open file", get_err(),
00161       path);
00162  }
00163 }

void ders::file::ex_close (  ) 

Definition at line 165 of file file.cpp.

00166 {
00167  close();
00168  if (!is_ok()) {
00169     throw newFileErrorException(mp, _FLINE_, "Can't close file", get_err(),
00170       get_name());
00171  }
00172 }

void ders::file::ex_attach ( int  fd  ) 

Definition at line 174 of file file.cpp.

00175 {
00176  attach(fd);
00177  if (!is_ok()) {
00178     throw newFileErrorException(mp, _FLINE_, text_buf(mp)+"Can't attach to \""+
00179       fd+"\"", get_err(), get_name());
00180  }
00181 }

void ders::file::ex_detach (  ) 

Definition at line 183 of file file.cpp.

00184 {
00185  detach();
00186  if (!is_ok())
00187     throw newFileErrorException(mp, _FLINE_, "Can't detach", get_err(),
00188       get_name());
00189 }

int ders::file::ex_read ( void *  buf,
int  n 
)

Definition at line 191 of file file.cpp.

00192 {
00193  int rc=read(buf, n);
00194  if (!is_ok()) {
00195     throw newFileErrorException(mp, _FLINE_, "Can't read from file", get_err(),
00196       get_name());
00197  }
00198 
00199  return rc;
00200 }

int ders::file::ex_read ( sh_text buf,
int  n 
)

Definition at line 202 of file file.cpp.

00203 {
00204  int rc=read(buf, n);
00205  if (!is_ok()) {
00206     throw newFileErrorException(mp, _FLINE_, "Can't read from file", get_err(),
00207       get_name());
00208  }
00209 
00210  return rc;
00211 }

int ders::file::ex_write ( const void *  buf,
int  n 
)

Definition at line 213 of file file.cpp.

00214 {
00215  int rc=write(buf, n);
00216  if (!is_ok()) {
00217     throw newFileErrorException(mp, _FLINE_, "Can't write to file", get_err(),
00218       get_name());
00219  }
00220 
00221  return rc;
00222 }

int ders::file::ex_write ( const ch_rng r  )  [inline]

Definition at line 93 of file file.hpp.

00093 { return ex_write(r.beg, r.size()); }

int ders::file::ex_tell (  ) 

Definition at line 224 of file file.cpp.

00225 {
00226  int rc=tell();
00227  if (!is_ok()) {
00228     throw newFileErrorException(mp, _FLINE_, "Can't tell on file", get_err(),
00229       get_name());
00230  }
00231 
00232  return rc;
00233 }

int ders::file::ex_seek ( int  off,
whence  wh 
)

Definition at line 235 of file file.cpp.

00236 {
00237  int rc=seek(off, wh);
00238  if (!is_ok()) {
00239     throw newFileErrorException(mp, _FLINE_, "Can't seek on file", get_err(),
00240       get_name());
00241  }
00242 
00243  return rc;
00244 }

template<class T>
void ders::destroyable::destroy_this ( T *  ptr,
mem_pool mp2 
) [inline, static, protected, inherited]

Definition at line 47 of file destroy.hpp.

00048 {
00049  assert(typeid(*ptr)==typeid(T));
00050 
00051  ptr->~T();
00052  mp2.deallocate(ptr, sizeof(T));
00053 }

void ders::destroyable::operator delete ( void *  ptr,
size_t  size 
) [inline, protected, inherited]

Definition at line 39 of file destroy.hpp.

00039 { hard_assert(false); }


The documentation for this class was generated from the following files:

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