]> git.tdb.fi Git - libs/core.git/blob - source/io/file.h
Remove the broken IO::Filtered class
[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 "handle.h"
8 #include "seekable.h"
9
10 namespace Msp {
11 namespace IO {
12
13 class file_not_found: public std::runtime_error
14 {
15 public:
16         file_not_found(const std::string &fn): std::runtime_error(fn) { }
17         virtual ~file_not_found() throw() = default;
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         virtual ~file_already_exists() throw() = default;
25 };
26
27
28 /**
29 A class for reading and writing files.
30
31 Non-blocking mode is not supported on Win32.
32 */
33 class File: public Seekable
34 {
35 public:
36         enum CreateMode
37         {
38                 C_NONE = 0,
39                 C_CREATE = 1,
40                 C_TRUNCATE = 2,
41                 C_OVERWRITE = C_CREATE+C_TRUNCATE,
42                 C_EXCLUSIVE = 4,
43                 C_NEW = C_CREATE+C_EXCLUSIVE
44         };
45
46 private:
47         Handle handle;
48
49 public:
50         /** Creates a new file object and opens it.  If the create flag is set and
51         write access is requested and the file does exist, it is created.  Otherwise
52         a missing file is an error. */
53         File(const std::string &, Mode = M_READ, CreateMode = C_OVERWRITE);
54 private:
55         void platform_init(const std::string &, CreateMode);
56 public:
57         virtual ~File();
58
59         virtual void set_block(bool);
60         virtual void set_inherit(bool);
61
62 protected:
63         virtual std::size_t do_write(const char *, std::size_t);
64         virtual std::size_t do_read(char *, std::size_t);
65
66 public:
67         virtual void sync();
68
69         virtual SeekOffset seek(SeekOffset, SeekType);
70         virtual SeekOffset tell() const;
71
72         virtual const Handle &get_handle(Mode);
73 };
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, File::CreateMode n)
79 { return File::CreateMode(static_cast<int>(m)&static_cast<int>(n)); }
80
81 inline File::CreateMode operator~(File::CreateMode m)
82 { return File::CreateMode(~static_cast<int>(m)); }
83
84
85 class BufferedFile: public Seekable
86 {
87 private:
88         File file;
89         Buffered buffer;
90         SeekOffset position;
91
92 public:
93         BufferedFile(const std::string &, Mode = M_READ, File::CreateMode = File::C_OVERWRITE);
94
95         virtual void set_block(bool);
96         virtual void set_inherit(bool);
97
98 protected:
99         virtual std::size_t do_write(const char *, std::size_t);
100         virtual std::size_t do_read(char *, std::size_t);
101 public:
102         virtual std::size_t put(char);
103
104         virtual bool getline(std::string &);
105         virtual int get();
106
107         virtual const Handle &get_handle(Mode);
108
109         virtual SeekOffset seek(SeekOffset, SeekType);
110         virtual SeekOffset tell() const { return position; }
111 };
112
113 } // namespace IO
114 } // namespace Msp
115
116 #endif