]> git.tdb.fi Git - libs/core.git/blob - source/io/file.h
Move files to prepare for assimilation into core
[libs/core.git] / source / io / file.h
1 #ifndef MSP_IO_FILE_H_
2 #define MSP_IO_FILE_H_
3
4 #include <string>
5 #include "base.h"
6 #include "buffered.h"
7 #include "filtered.h"
8 #include "seek.h"
9
10 namespace Msp {
11 namespace IO {
12
13 /**
14 A class for reading and writing files.
15
16 Non-blocking mode is not supported on Win32.
17 */
18 class File: public Base
19 {
20 public:
21         enum CreateMode
22         {
23                 C_NONE = 0,
24                 C_CREATE = 1,
25                 C_TRUNCATE = 2
26         };
27
28 private:
29         Handle handle;
30
31 public:
32         /** Creates a new file object and opens it.  If the create flag is set and
33         write access is requested and the file does exist, it is created.  Otherwise
34         a missing file is an error. */
35         File(const std::string &, Mode = M_READ, CreateMode = CreateMode(C_CREATE+C_TRUNCATE));
36         virtual ~File();
37
38         /** Closes the file.  Any attempt to access the file after this will cause
39         an exception to be thrown. */
40         void close();
41
42         void set_block(bool);
43
44 protected:
45         virtual unsigned do_write(const char *, unsigned);
46         virtual unsigned do_read(char *, unsigned);
47
48 public:
49         virtual void sync();
50
51         /** Changes the read/write offset of the file.  Returns the new offset. */
52         virtual int seek(int, SeekType);
53         
54         /** Returns the current read/write offset of the file. */
55         virtual int tell() const;
56
57         virtual Handle get_event_handle() { return handle; }
58
59 private:
60         void check_access(Mode) const;
61 };
62
63 inline File::CreateMode operator|(File::CreateMode m, File::CreateMode n)
64 { return File::CreateMode(static_cast<int>(m)|static_cast<int>(n)); }
65
66 inline File::CreateMode operator&(File::CreateMode m, File::CreateMode n)
67 { return File::CreateMode(static_cast<int>(m)&static_cast<int>(n)); }
68
69 inline File::CreateMode operator~(File::CreateMode m)
70 { return File::CreateMode(~static_cast<int>(m)); }
71
72 typedef Filtered<File, Buffered> BufferedFile;
73
74 } // namespace IO
75 } // namespace Msp
76
77 #endif