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