file.hpp

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 
00019 #ifndef __DERS__FILE_HPP__
00020 #define __DERS__FILE_HPP__
00021 
00022 #include <ders/config.hpp>
00023 #include <ders/buf_rdwr.hpp>
00024 #include <ders/error.hpp>
00025 
00026 namespace ders {  // ::ders
00027 
00031 class file : public readable, public writable {
00032  public:
00034       enum mode { rdo, wro, rdwr };
00036       enum flags { app=1, crt=2, excl=4, trnc=8 };
00038       enum whence { set, cur, end };
00039 
00040  private:
00041       mem_pool& mp;
00042       int fdr;
00043       bool own;
00044       sh_text name;
00045       error err;
00046 
00047       void init();
00048 
00049       file(const file&);
00050       file& operator=(const file&);
00051 
00052  public:
00053       file(mem_pool& m);
00054       file(mem_pool& m, const ch_rng& path, mode md, int fls);
00055       file(mem_pool& m, int fd);
00056       ~file();
00057 
00058       virtual void destroy(mem_pool& mp2) { destroy_this(this, mp2); }
00059 
00060       mem_pool& pool() const { return mp; }
00061 
00062       bool is_ok() const { return get_err()==ezero; }
00063       bool is_opened() const { return get_fd()!=-1; }
00064       bool is_owned() const { return own; }
00065 
00066       int get_fd() const { return fdr; }
00067       const sh_text& get_name() const { return name; }
00068       error get_err() const { return err; }
00069 
00070       bool open(const ch_rng& path, mode md, int fls);
00071       bool close();
00072 
00073       bool attach(int fd);
00074       bool detach();
00075 
00076       virtual int read(void* buf, int n);
00077       int read(sh_text& buf, int n);
00078 
00079       virtual int write(const void* buf, int n);
00080       int write(const ch_rng& r) { return write(r.beg, r.size()); }
00081 
00082       int tell() { return seek(0, cur); }
00083       int seek(int off, whence wh);
00084 
00085       void ex_is_ok() const;
00086       void ex_open(const ch_rng& path, mode md, int fls);
00087       void ex_close();
00088       void ex_attach(int fd);
00089       void ex_detach();
00090       int ex_read(void* buf, int n);
00091       int ex_read(sh_text& buf, int n);
00092       int ex_write(const void* buf, int n);
00093       int ex_write(const ch_rng& r) { return ex_write(r.beg, r.size()); }
00094       int ex_tell();
00095       int ex_seek(int off, whence wh);
00096 };
00097 
00098 typedef sh_ptr<file> sh_file;
00099 
00105 sh_file new_file(mem_pool& mp);
00106 
00115 sh_file new_file(mem_pool& mp, const ch_rng& path, file::mode md, int fls);
00116 
00123 sh_file new_file(mem_pool& mp, int fd);
00124 
00125 shException newFileErrorException(mem_pool& mp, const FileLine& loc, const
00126   ch_rng& msg, error err, const ch_rng& fname);
00127 shException newFileErrorException(const FileLine& loc, const ch_rng& msg, error
00128   err, const ch_rng& fname, shException nest);
00129 
00133 class FileErrorException : public ErrorException {
00134       friend shException newFileErrorException(const FileLine& loc, const
00135         ch_rng& msg, error err, const ch_rng& fname, shException nest);
00136 
00137       FileErrorException(const FileErrorException&);
00138       FileErrorException& operator=(const FileErrorException&);
00139 
00140  protected:
00141       FileErrorException(const FileLine& loc, const ch_rng& msg, error err,
00142         const ch_rng& fname, shException nest);
00143 
00144  public:
00145       const sh_text fileName;
00146 
00147       virtual sh_text getClassName() const;
00148       virtual sh_text toText() const;
00149 
00150       virtual void destroy(mem_pool& mp2) { destroy_this(this, mp2); }
00151 };
00152 
00153 error remove(mem_pool& mp, const ch_rng& fname);
00154 void ex_remove(mem_pool& mp, const ch_rng& fname);
00155 
00160 class fd {
00161       fd();
00162 
00163  public:
00164       static const int in =0;
00165       static const int out=1;
00166       static const int err=2;
00167 
00168       static int open(const char* path, file::mode md, int fls, error& err);
00169       static bool close(int fd, error& err);
00170 
00171       static int read(int fd, void* buf, int n, error& err);
00172       static int write(int fd, const void* buf, int n, error& err);
00173 
00174       static int seek(int fd, int off, file::whence wh, error& err);
00175 };
00176 
00177 }  // namespace ::ders
00178 
00179 #endif  // __DERS__FILE_HPP__
00180 

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