]> git.tdb.fi Git - libs/core.git/blob - source/io/file.h
5fb0353211b2e99d23384afa2924e87ee78acc10
[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_OVERWRITE = C_CREATE+C_TRUNCATE,
41                 C_EXCLUSIVE = 4,
42                 C_NEW = C_CREATE+C_EXCLUSIVE
43         };
44
45 private:
46         Handle handle;
47
48 public:
49         /** Creates a new file object and opens it.  If the create flag is set and
50         write access is requested and the file does exist, it is created.  Otherwise
51         a missing file is an error. */
52         File(const std::string &, Mode = M_READ, CreateMode = C_OVERWRITE);
53         virtual ~File();
54
55         void set_block(bool);
56
57 protected:
58         virtual unsigned do_write(const char *, unsigned);
59         virtual unsigned do_read(char *, unsigned);
60
61 public:
62         virtual void sync();
63
64         virtual SeekOffset seek(SeekOffset, SeekType);
65         virtual SeekOffset tell() const;
66
67         virtual const Handle &get_handle(Mode);
68 };
69
70 inline File::CreateMode operator|(File::CreateMode m, File::CreateMode n)
71 { return File::CreateMode(static_cast<int>(m)|static_cast<int>(n)); }
72
73 inline File::CreateMode operator&(File::CreateMode m, File::CreateMode n)
74 { return File::CreateMode(static_cast<int>(m)&static_cast<int>(n)); }
75
76 inline File::CreateMode operator~(File::CreateMode m)
77 { return File::CreateMode(~static_cast<int>(m)); }
78
79 typedef Filtered<File, Buffered> BufferedFile;
80
81 } // namespace IO
82 } // namespace Msp
83
84 #endif