X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Fio%2Ffile.h;h=85075d0cca6e8709db663012ced369dd5f0cda52;hp=47111609e8adb238f5bfef24132f81c6e8a9b8a2;hb=ea8bf8f588310b0d7fd3297d74907602705bba1d;hpb=8f2711fba7a2817840038630d9cf9a2060ecbe8e diff --git a/source/io/file.h b/source/io/file.h index 4711160..85075d0 100644 --- a/source/io/file.h +++ b/source/io/file.h @@ -3,10 +3,10 @@ #include #include -#include "base.h" #include "buffered.h" #include "filtered.h" -#include "seek.h" +#include "handle.h" +#include "seekable.h" namespace Msp { namespace IO { @@ -15,6 +15,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() { } +}; + +class file_already_exists: public std::runtime_error +{ +public: + file_already_exists(const std::string &fn): std::runtime_error(fn) { } + ~file_already_exists() throw() { } }; @@ -23,14 +31,17 @@ A class for reading and writing files. Non-blocking mode is not supported on Win32. */ -class File: public Base +class File: public Seekable { public: enum CreateMode { 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 +51,14 @@ 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(); - /** Closes the file. Any attempt to access the file after this will cause - an exception to be thrown. */ - void close(); - - void set_block(bool); + virtual void set_block(bool); + virtual void set_inherit(bool); protected: virtual unsigned do_write(const char *, unsigned); @@ -56,16 +67,10 @@ protected: public: virtual void sync(); - /** Changes the read/write offset of the file. Returns the new offset. */ - virtual int seek(int, SeekType); - - /** Returns the current read/write offset of the file. */ - virtual int tell() const; - - virtual Handle get_event_handle() { return handle; } + virtual SeekOffset seek(SeekOffset, SeekType); + virtual SeekOffset tell() const; -private: - void check_access(Mode) const; + virtual const Handle &get_handle(Mode); }; inline File::CreateMode operator|(File::CreateMode m, File::CreateMode n) @@ -77,7 +82,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 unsigned do_write(const char *, unsigned); + virtual unsigned do_read(char *, unsigned); +public: + virtual unsigned 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