]> git.tdb.fi Git - libs/core.git/blob - source/io/file.h
Merge branch 'fs-master'
[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         void set_block(bool);
47
48 protected:
49         virtual unsigned do_write(const char *, unsigned);
50         virtual unsigned do_read(char *, unsigned);
51
52 public:
53         virtual void sync();
54
55         virtual SeekOffset seek(SeekOffset, SeekType);
56         virtual SeekOffset tell() const;
57 };
58
59 inline File::CreateMode operator|(File::CreateMode m, File::CreateMode n)
60 { return File::CreateMode(static_cast<int>(m)|static_cast<int>(n)); }
61
62 inline File::CreateMode operator&(File::CreateMode m, File::CreateMode n)
63 { return File::CreateMode(static_cast<int>(m)&static_cast<int>(n)); }
64
65 inline File::CreateMode operator~(File::CreateMode m)
66 { return File::CreateMode(~static_cast<int>(m)); }
67
68 typedef Filtered<File, Buffered> BufferedFile;
69
70 } // namespace IO
71 } // namespace Msp
72
73 #endif