X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fio%2Ffile.h;fp=source%2Fio%2Ffile.h;h=05aaf9565a08653f278773e010fee4acb6361779;hb=6e0fd758970bcb5bad5e3f2454b694cc4d7b4b66;hp=0000000000000000000000000000000000000000;hpb=b97d4e9f86e90254ab9edef7ee62a910f6333c78;p=libs%2Fcore.git diff --git a/source/io/file.h b/source/io/file.h new file mode 100644 index 0000000..05aaf95 --- /dev/null +++ b/source/io/file.h @@ -0,0 +1,77 @@ +#ifndef MSP_IO_FILE_H_ +#define MSP_IO_FILE_H_ + +#include +#include "base.h" +#include "buffered.h" +#include "filtered.h" +#include "seek.h" + +namespace Msp { +namespace IO { + +/** +A class for reading and writing files. + +Non-blocking mode is not supported on Win32. +*/ +class File: public Base +{ +public: + enum CreateMode + { + C_NONE = 0, + C_CREATE = 1, + C_TRUNCATE = 2 + }; + +private: + Handle handle; + +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(); + + void set_block(bool); + +protected: + virtual unsigned do_write(const char *, unsigned); + virtual unsigned do_read(char *, unsigned); + +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; } + +private: + void check_access(Mode) const; +}; + +inline File::CreateMode operator|(File::CreateMode m, File::CreateMode n) +{ return File::CreateMode(static_cast(m)|static_cast(n)); } + +inline File::CreateMode operator&(File::CreateMode m, File::CreateMode n) +{ return File::CreateMode(static_cast(m)&static_cast(n)); } + +inline File::CreateMode operator~(File::CreateMode m) +{ return File::CreateMode(~static_cast(m)); } + +typedef Filtered BufferedFile; + +} // namespace IO +} // namespace Msp + +#endif