X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Fio%2Ffile.h;h=292897b5251a1710bc7802a9a9ae1c76914d7f55;hp=430571f909d872854e64d11ac43917442541f121;hb=f832af5e832a5be2804e5613a73e7cc75428956c;hpb=f041a31f9a6e19da86a63912e5a8050f216e5bc5 diff --git a/source/io/file.h b/source/io/file.h index 430571f..292897b 100644 --- a/source/io/file.h +++ b/source/io/file.h @@ -4,7 +4,6 @@ #include #include #include "buffered.h" -#include "filtered.h" #include "handle.h" #include "seekable.h" @@ -15,6 +14,14 @@ class file_not_found: public std::runtime_error { public: file_not_found(const std::string &fn): std::runtime_error(fn) { } + virtual ~file_not_found() throw() = default; +}; + +class file_already_exists: public std::runtime_error +{ +public: + file_already_exists(const std::string &fn): std::runtime_error(fn) { } + virtual ~file_already_exists() throw() = default; }; @@ -30,7 +37,10 @@ public: { C_NONE = 0, C_CREATE = 1, - C_TRUNCATE = 2 + C_TRUNCATE = 2, + C_OVERWRITE = C_CREATE+C_TRUNCATE, + C_EXCLUSIVE = 4, + C_NEW = C_CREATE+C_EXCLUSIVE }; private: @@ -40,14 +50,18 @@ public: /** Creates a new file object and opens it. If the create flag is set and write access is requested and the file does exist, it is created. Otherwise a missing file is an error. */ - File(const std::string &, Mode = M_READ, CreateMode = CreateMode(C_CREATE+C_TRUNCATE)); + File(const std::string &, Mode = M_READ, CreateMode = C_OVERWRITE); +private: + void platform_init(const std::string &, CreateMode); +public: virtual ~File(); - void set_block(bool); + virtual void set_block(bool); + virtual void set_inherit(bool); protected: - virtual unsigned do_write(const char *, unsigned); - virtual unsigned do_read(char *, unsigned); + virtual std::size_t do_write(const char *, std::size_t); + virtual std::size_t do_read(char *, std::size_t); public: virtual void sync(); @@ -67,7 +81,34 @@ inline File::CreateMode operator&(File::CreateMode m, File::CreateMode n) inline File::CreateMode operator~(File::CreateMode m) { return File::CreateMode(~static_cast(m)); } -typedef Filtered BufferedFile; + +class BufferedFile: public Seekable +{ +private: + File file; + Buffered buffer; + SeekOffset position; + +public: + BufferedFile(const std::string &, Mode = M_READ, File::CreateMode = File::C_OVERWRITE); + + virtual void set_block(bool); + virtual void set_inherit(bool); + +protected: + virtual std::size_t do_write(const char *, std::size_t); + virtual std::size_t do_read(char *, std::size_t); +public: + virtual std::size_t put(char); + + virtual bool getline(std::string &); + virtual int get(); + + virtual const Handle &get_handle(Mode); + + virtual SeekOffset seek(SeekOffset, SeekType); + virtual SeekOffset tell() const { return position; } +}; } // namespace IO } // namespace Msp