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