]> git.tdb.fi Git - libs/core.git/blob - source/file.h
49b596bb943077411db57b0bc66044de8de87a92
[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 "seek.h"
13
14 namespace Msp {
15 namespace IO {
16
17 /**
18 A class for reading and writing files.
19
20 Non-blocking mode is not supported on Win32.
21 */
22 class File: public Base
23 {
24 public:
25         enum CreateMode
26         {
27                 C_NONE=0,
28                 C_CREATE=1,
29                 C_TRUNCATE=2
30         };
31
32         File(const std::string &, Mode=M_READ, CreateMode =CreateMode(C_CREATE+C_TRUNCATE));
33
34         void close();
35
36         void set_block(bool);
37         void enable_events();
38
39         void sync();
40
41         int  seek(int, SeekType);
42         int  tell() const;
43
44         Handle get_event_handle() { return handle; }
45
46         ~File();
47 private:
48         Handle handle;
49
50         void      check_access(Mode) const;
51         unsigned  do_write(const char *, unsigned);
52         unsigned  do_read(char *, unsigned);
53 };
54
55 inline File::CreateMode operator|(File::CreateMode m, File::CreateMode n)
56 { return File::CreateMode((int)m|(int)n); }
57
58 inline File::CreateMode operator&(File::CreateMode m, File::CreateMode n)
59 { return File::CreateMode((int)m&(int)n); }
60
61 inline File::CreateMode operator~(File::CreateMode m)
62 { return File::CreateMode(~(int)m); }
63
64 } // namespace IO
65 } // namespace Msp
66
67 #endif