]> git.tdb.fi Git - libs/core.git/blob - source/io/file.h
Move non-oneliner functions out of RefPtr class declaration
[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 private:
56         void platform_init(const std::string &, CreateMode);
57 public:
58         virtual ~File();
59
60         virtual void set_block(bool);
61         virtual void set_inherit(bool);
62
63 protected:
64         virtual unsigned do_write(const char *, unsigned);
65         virtual unsigned do_read(char *, unsigned);
66
67 public:
68         virtual void sync();
69
70         virtual SeekOffset seek(SeekOffset, SeekType);
71         virtual SeekOffset tell() const;
72
73         virtual const Handle &get_handle(Mode);
74 };
75
76 inline File::CreateMode operator|(File::CreateMode m, File::CreateMode n)
77 { return File::CreateMode(static_cast<int>(m)|static_cast<int>(n)); }
78
79 inline File::CreateMode operator&(File::CreateMode m, File::CreateMode n)
80 { return File::CreateMode(static_cast<int>(m)&static_cast<int>(n)); }
81
82 inline File::CreateMode operator~(File::CreateMode m)
83 { return File::CreateMode(~static_cast<int>(m)); }
84
85
86 class BufferedFile: public Seekable
87 {
88 private:
89         File file;
90         Buffered buffer;
91         SeekOffset position;
92
93 public:
94         BufferedFile(const std::string &, Mode = M_READ, File::CreateMode = File::C_OVERWRITE);
95
96         virtual void set_block(bool);
97         virtual void set_inherit(bool);
98
99 protected:
100         virtual unsigned do_write(const char *, unsigned);
101         virtual unsigned do_read(char *, unsigned);
102 public:
103         virtual unsigned put(char);
104
105         virtual bool getline(std::string &);
106         virtual int get();
107
108         virtual const Handle &get_handle(Mode);
109
110         virtual SeekOffset seek(SeekOffset, SeekType);
111         virtual SeekOffset tell() const { return position; }
112 };
113
114 } // namespace IO
115 } // namespace Msp
116
117 #endif