Debian Codegraph @

src/lib/QXPDetector.cpp

raw
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /*
3 * This file is part of the libqxp project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
9
10 #include "QXPDetector.h"
11
12 #include "QXP1Header.h"
13 #include "QXP33Header.h"
14 #include "QXP3HeaderBase.h"
15 #include "QXP4Header.h"
16 #include "QXPMacFileParser.h"
17 #include "QXPParser.h"
18
19 namespace libqxp
20 {
21
22 namespace
23 {
24
25 class QXP3Detector : public QXP3HeaderBase
26 {
27 public:
28 bool isSupported()
29 {
30 switch (m_version)
31 {
32 case QXPVersion::QXP_31_MAC:
33 case QXPVersion::QXP_31:
34 case QXPVersion::QXP_33:
35 case QXPVersion::QXP_4:
36 return m_signature == "XPR";
37 default:
38 return false;
39 }
40 }
41
42 std::shared_ptr<QXPHeader> createHeader(const boost::optional<QXPDocument::Type> type = boost::none)
43 {
44 switch (m_version)
45 {
46 case QXPVersion::QXP_31_MAC:
47 case QXPVersion::QXP_31:
48 case QXPVersion::QXP_33:
49 return std::make_shared<QXP33Header>(type);
50 case QXPVersion::QXP_4:
51 return std::make_shared<QXP4Header>(type);
52 default:
53 return nullptr;
54 }
55 }
56
57 private:
58 QXPDocument::Type getType() const override
59 {
60 return QXPDocument::TYPE_UNKNOWN;
61 }
62
63 std::unique_ptr<QXPParser> createParser(const std::shared_ptr<librevenge::RVNGInputStream> &, librevenge::RVNGDrawingInterface *) override
64 {
65 return nullptr;
66 }
67 };
68
69 }
70
71 QXPDetector::QXPDetector()
72 : m_input()
73 , m_header()
74 , m_type(QXPDocument::TYPE_UNKNOWN)
75 , m_supported(false)
76 {
77 }
78
79 void QXPDetector::detect(const std::shared_ptr<librevenge::RVNGInputStream> &input)
80 {
81 boost::optional<QXPDocument::Type> docType;
82
83 std::shared_ptr<librevenge::RVNGInputStream> docStream;
84 std::string type;
85 std::string creator;
86 QXPMacFileParser macFile(docStream, type, creator);
87 if (macFile.parse(input))
88 {
89 if (creator == "XPR3")
90 {
91 if (type == "XDOC")
92 docType = QXPDocument::TYPE_DOCUMENT;
93 else if (type == "XTMP")
94 docType = QXPDocument::TYPE_TEMPLATE;
95 else if (type == "XBOK")
96 docType = QXPDocument::TYPE_BOOK;
97 else if (type == "XLIB")
98 docType = QXPDocument::TYPE_LIBRARY;
99 }
100 else if (creator == "XPRS" && type == "XDOC")
101 {
102 m_input = docStream;
103 m_header = std::make_shared<QXP1Header>();
104 }
105 }
106 else
107 {
108 docStream = input;
109 }
110
111 if (!m_header)
112 {
113 QXP3Detector detector;
114 if (detector.load(docStream) && detector.isSupported())
115 {
116 m_input = docStream;
117 m_header = detector.createHeader(docType);
118 }
119 }
120
121 if (bool(m_header))
122 {
123 m_input->seek(0, librevenge::RVNG_SEEK_SET);
124 m_header->load(m_input);
125 m_type = m_header->getType();
126 m_supported = m_type != QXPDocument::TYPE_UNKNOWN;
127 }
128 }
129
130 const std::shared_ptr<librevenge::RVNGInputStream> &QXPDetector::input() const
131 {
132 return m_input;
133 }
134
135 const std::shared_ptr<QXPHeader> &QXPDetector::header() const
136 {
137 return m_header;
138 }
139
140 bool QXPDetector::isSupported() const
141 {
142 return m_supported;
143 }
144
145 QXPDocument::Type QXPDetector::type() const
146 {
147 return m_type;
148 }
149
150 } // namespace libqxp
151
152 /* vim:set shiftwidth=2 softtabstop=2 expandtab: */