X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Fio%2Ffile.h;h=f23cc93e7d013b71dc33cbe060cd83ce939fbe4a;hp=d59cd577fc0f33e1acbcbb62bcaa9fa0bb31c141;hb=991fabc1956b73a4007859058fb44171000b452e;hpb=86e0004bd195bc7f7478cf736871339bddf38127 diff --git a/source/io/file.h b/source/io/file.h index d59cd57..f23cc93 100644 --- a/source/io/file.h +++ b/source/io/file.h @@ -4,7 +4,7 @@ #include #include #include "buffered.h" -#include "filtered.h" +#include "handle.h" #include "seekable.h" namespace Msp { @@ -14,6 +14,14 @@ class file_not_found: public std::runtime_error { public: file_not_found(const std::string &fn): std::runtime_error(fn) { } + ~file_not_found() throw() override = default; +}; + +class file_already_exists: public std::runtime_error +{ +public: + file_already_exists(const std::string &fn): std::runtime_error(fn) { } + ~file_already_exists() throw() override = default; }; @@ -29,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: @@ -39,29 +50,26 @@ 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)); - virtual ~File(); - - /** Closes the file. Any attempt to access the file after this will cause - an exception to be thrown. */ - void close(); + File(const std::string &, Mode = M_READ, CreateMode = C_OVERWRITE); +private: + void platform_init(const std::string &, CreateMode); +public: + ~File() override; - void set_block(bool); + void set_block(bool) override; + void set_inherit(bool) override; protected: - virtual unsigned do_write(const char *, unsigned); - virtual unsigned do_read(char *, unsigned); + std::size_t do_write(const char *, std::size_t) override; + std::size_t do_read(char *, std::size_t) override; public: - virtual void sync(); - - virtual unsigned seek(int, SeekType); - virtual unsigned tell() const; + void sync(); - virtual Handle get_event_handle() { return handle; } + SeekOffset seek(SeekOffset, SeekType) override; + SeekOffset tell() const override; -private: - void check_access(Mode) const; + const Handle &get_handle(Mode) override; }; inline File::CreateMode operator|(File::CreateMode m, File::CreateMode n) @@ -73,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); + + void set_block(bool) override; + void set_inherit(bool) override; + +protected: + std::size_t do_write(const char *, std::size_t) override; + std::size_t do_read(char *, std::size_t) override; +public: + std::size_t put(char) override; + + bool getline(std::string &) override; + int get() override; + + const Handle &get_handle(Mode) override; + + SeekOffset seek(SeekOffset, SeekType) override; + SeekOffset tell() const override { return position; } +}; } // namespace IO } // namespace Msp