]> git.tdb.fi Git - libs/core.git/blob - source/file.h
4e86e29642c47dd35b52b0978b6b7be7b38d05f8
[libs/core.git] / source / file.h
1 /* $Id$
2
3 This file is part of libmspio
4 Copyright © 2007 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7 #ifndef MSP_IO_FILE_H_
8 #define MSP_IO_FILE_H_
9
10 #include <string>
11 #include "base.h"
12 #include "buffered.h"
13 #include "filtered.h"
14 #include "seek.h"
15
16 namespace Msp {
17 namespace IO {
18
19 /**
20 A class for reading and writing files.
21
22 Non-blocking mode is not supported on Win32.
23 */
24 class File: public Base
25 {
26 public:
27         enum CreateMode
28         {
29                 C_NONE=0,
30                 C_CREATE=1,
31                 C_TRUNCATE=2
32         };
33
34         File(const std::string &, Mode=M_READ, CreateMode =CreateMode(C_CREATE+C_TRUNCATE));
35
36         void close();
37
38         void set_block(bool);
39
40         virtual void sync();
41
42         virtual int  seek(int, SeekType);
43         virtual int  tell() const;
44
45         virtual Handle get_event_handle() { return handle; }
46
47         virtual ~File();
48 private:
49         Handle handle;
50
51         void              check_access(Mode) const;
52 protected:
53         virtual unsigned  do_write(const char *, unsigned);
54         virtual unsigned  do_read(char *, unsigned);
55 };
56
57 inline File::CreateMode operator|(File::CreateMode m, File::CreateMode n)
58 { return File::CreateMode(static_cast<int>(m)|static_cast<int>(n)); }
59
60 inline File::CreateMode operator&(File::CreateMode m, File::CreateMode n)
61 { return File::CreateMode(static_cast<int>(m)&static_cast<int>(n)); }
62
63 inline File::CreateMode operator~(File::CreateMode m)
64 { return File::CreateMode(~static_cast<int>(m)); }
65
66 typedef Filtered<File, Buffered> BufferedFile;
67
68 } // namespace IO
69 } // namespace Msp
70
71 #endif