1 /*
2  Copyright (C) 2010-2014 Kristian Duske
3 
4  This file is part of TrenchBroom.
5 
6  TrenchBroom is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  TrenchBroom is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with TrenchBroom. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef TrenchBroom_MappedFile
21 #define TrenchBroom_MappedFile
22 
23 #include "SharedPointer.h"
24 
25 #include "Exceptions.h"
26 
27 #include <vector>
28 
29 #ifdef _WIN32
30 // can't include Windows.h here
31 typedef void *HANDLE;
32 #endif
33 
34 namespace TrenchBroom {
35     namespace IO {
36         class Path;
37 
38         class MappedFile {
39         public:
40             typedef std::shared_ptr<MappedFile> Ptr;
41             typedef std::vector<Ptr> List;
42         private:
43             const char* m_begin;
44             const char* m_end;
45             size_t m_size;
46         public:
47             MappedFile();
48             virtual ~MappedFile();
49 
50             size_t size() const;
51             const char* begin() const;
52             const char* end() const;
53         protected:
54             void init(const char* begin, const char* end);
55         };
56 
57         class MappedFileView : public MappedFile {
58         public:
59             MappedFileView(const char* begin, const char* end);
60         };
61 
62 #ifdef _WIN32
63         class WinMappedFile : public MappedFile {
64         private:
65             HANDLE m_fileHandle;
66 	        HANDLE m_mappingHandle;
67             char* m_address;
68         public:
69             WinMappedFile(const Path& path, std::ios_base::openmode mode);
70             ~WinMappedFile();
71         };
72 #else
73         class PosixMappedFile : public MappedFile {
74         private:
75             char* m_address;
76             size_t m_size;
77             int m_filedesc;
78         public:
79             PosixMappedFile(const Path& path, std::ios_base::openmode mode);
80             ~PosixMappedFile();
81         };
82 #endif
83     }
84 }
85 
86 #endif /* defined(TrenchBroom_MappedFile) */
87