Debian Codegraph @

src/lib/QXPDocument.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 <algorithm>
11 #include <cstring>
12 #include <memory>
13
14 #include <librevenge-stream/librevenge-stream.h>
15
16 #include <libqxp/libqxp.h>
17
18 #include "libqxp_utils.h"
19 #include "QXPDetector.h"
20 #include "QXPHeader.h"
21 #include "QXPParser.h"
22
23 using librevenge::RVNGInputStream;
24
25 using std::equal;
26 using std::shared_ptr;
27
28 namespace libqxp
29 {
30
31 QXPAPI bool QXPDocument::isSupported(librevenge::RVNGInputStream *const input, Type *const type) try
32 {
33 QXPDetector detector;
34 detector.detect(std::shared_ptr<librevenge::RVNGInputStream>(input, QXPDummyDeleter()));
35 if (type)
36 *type = detector.type();
37 return detector.isSupported();
38 }
39 catch (...)
40 {
41 return false;
42 }
43
44 QXPAPI QXPDocument::Result QXPDocument::parse(librevenge::RVNGInputStream *const input, librevenge::RVNGDrawingInterface *const document, QXPPathResolver * /*resolver*/) try
45 {
46 QXPDetector detector;
47 detector.detect(std::shared_ptr<librevenge::RVNGInputStream>(input, QXPDummyDeleter()));
48 if (!detector.isSupported())
49 return RESULT_UNSUPPORTED_FORMAT;
50
51 if (detector.type() != QXPDocument::TYPE_DOCUMENT && detector.type() != QXPDocument::TYPE_TEMPLATE)
52 return QXPDocument::RESULT_UNSUPPORTED_FORMAT;
53
54 auto parser = detector.header()->createParser(detector.input(), document);
55
56 return parser->parse() ? RESULT_OK : RESULT_UNKNOWN_ERROR;
57 }
58 catch (const FileAccessError &)
59 {
60 return RESULT_FILE_ACCESS_ERROR;
61 }
62 catch (const UnsupportedFormat &)
63 {
64 return RESULT_UNSUPPORTED_FORMAT;
65 }
66 catch (...)
67 {
68 return RESULT_UNKNOWN_ERROR;
69 }
70
71 } // namespace libqxp
72
73 /* vim:set shiftwidth=2 softtabstop=2 expandtab: */