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_MapParser_h
21 #define TrenchBroom_MapParser_h
22 
23 #include "TrenchBroom.h"
24 #include "VecMath.h"
25 #include "Model/EntityAttributes.h"
26 #include "Model/MapFormat.h"
27 #include "Model/ModelTypes.h"
28 
29 #include <map>
30 #include <vector>
31 
32 namespace TrenchBroom {
33     namespace Model {
34         class BrushFaceAttributes;
35     }
36 
37     namespace IO {
38         class MapParser {
39         protected:
40             class ExtraAttribute {
41             public:
42                 typedef enum {
43                     Type_String,
44                     Type_Integer
45                 } Type;
46             private:
47                 Type m_type;
48                 String m_name;
49                 String m_value;
50                 size_t m_line;
51                 size_t m_column;
52             public:
53                 ExtraAttribute(Type type, const String& name, const String& value, size_t line, size_t column);
54 
55                 Type type() const;
56                 const String& name() const;
57                 const String& strValue() const;
58 
59                 void assertType(Type expected) const;
60 
61                 template <typename T>
intValue()62                 T intValue() const {
63                     assert(m_type == Type_Integer);
64                     return static_cast<T>(std::atoi(m_value.c_str()));
65                 }
66             };
67 
68             typedef std::map<String, ExtraAttribute> ExtraAttributes;
69         public:
70             virtual ~MapParser();
71         protected:
72             void formatSet(Model::MapFormat::Type format);
73             void beginEntity(size_t line, const Model::EntityAttribute::List& attributes, const ExtraAttributes& extraAttributes);
74             void endEntity(size_t startLine, size_t lineCount);
75             void beginBrush(size_t line);
76             void endBrush(size_t startLine, size_t lineCount, const ExtraAttributes& extraAttributes);
77             void brushFace(size_t line, const Vec3& point1, const Vec3& point2, const Vec3& point3, const Model::BrushFaceAttributes& attribs, const Vec3& texAxisX, const Vec3& texAxisY);
78         private: // subclassing interface for users of the parser
79             virtual void onFormatSet(Model::MapFormat::Type format) = 0;
80             virtual void onBeginEntity(size_t line, const Model::EntityAttribute::List& attributes, const ExtraAttributes& extraAttributes) = 0;
81             virtual void onEndEntity(size_t startLine, size_t lineCount) = 0;
82             virtual void onBeginBrush(size_t line) = 0;
83             virtual void onEndBrush(size_t startLine, size_t lineCount, const ExtraAttributes& extraAttributes) = 0;
84             virtual void onBrushFace(size_t line, const Vec3& point1, const Vec3& point2, const Vec3& point3, const Model::BrushFaceAttributes& attribs, const Vec3& texAxisX, const Vec3& texAxisY) = 0;
85         };
86     }
87 }
88 
89 #endif
90