]> git.tdb.fi Git - libs/core.git/blob - source/io/file.h
4f8e5ddf8612b559f82ce5bfc7c2623ba5860cc9
[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 class file_already_exists: public std::runtime_error
21 {
22 public:
23         file_already_exists(const std::string &fn): std::runtime_error(fn) { }
24 };
25
26
27 /**
28 A class for reading and writing files.
29
30 Non-blocking mode is not supported on Win32.
31 */
32 class File: public Seekable
33 {
34 public:
35         enum CreateMode
36         {
37                 C_NONE = 0,
38                 C_CREATE = 1,
39                 C_TRUNCATE = 2,
40                 C_EXCLUSIVE = 4
41         };
42
43 private:
44         Handle handle;
45
46 public:
47         /** Creates a new file object and opens it.  If the create flag is set and
48         write access is requested and the file does exist, it is created.  Otherwise
49         a missing file is an error. */
50         File(const std::string &, Mode = M_READ, CreateMode = CreateMode(C_CREATE+C_TRUNCATE));
51         virtual ~File();
52
53         void set_block(bool);
54
55 protected:
56         virtual unsigned do_write(const char *, unsigned);
57         virtual unsigned do_read(char *, unsigned);
58
59 public:
60         virtual void sync();
61
62         virtual SeekOffset seek(SeekOffset, SeekType);
63         virtual SeekOffset tell() const;
64
65         virtual const Handle &get_handle(Mode);
66 };
67
68 inline File::CreateMode operator|(File::CreateMode m, File::CreateMode n)
69 { return File::CreateMode(static_cast<int>(m)|static_cast<int>(n)); }
70
71 inline File::CreateMode operator&(File::CreateMode m, File::CreateMode n)
72 { return File::CreateMode(static_cast<int>(m)&static_cast<int>(n)); }
73
74 inline File::CreateMode operator~(File::CreateMode m)
75 { return File::CreateMode(~static_cast<int>(m)); }
76
77 typedef Filtered<File, Buffered> BufferedFile;
78
79 } // namespace IO
80 } // namespace Msp
81
82 #endif