| 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
| 2 | /* |
| 3 | * This file is part of the libcdr 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 <libcdr/libcdr.h> |
| 11 | #include "CDRDocumentStructure.h" |
| 12 | #include "CMXParser.h" |
| 13 | #include "CDRContentCollector.h" |
| 14 | #include "CDRStylesCollector.h" |
| 15 | #include "libcdr_utils.h" |
| 16 | |
| 17 | /** |
| 18 | Analyzes the content of an input stream to see if it can be parsed |
| 19 | \param input The input stream |
| 20 | \return A value that indicates whether the content from the input |
| 21 | stream is a Corel Draw Document that libcdr is able to parse |
| 22 | */ |
| 23 | CDRAPI bool libcdr::CMXDocument::isSupported(librevenge::RVNGInputStream *input) |
| 24 | try |
| 25 | { |
| 26 | if (!input) |
| 27 | return false; |
| 28 | |
| 29 | input->seek(0, librevenge::RVNG_SEEK_SET); |
| 30 | unsigned riff = readU32(input); |
| 31 | if (riff != CDR_FOURCC_RIFF && riff != CDR_FOURCC_RIFX) |
| 32 | return false; |
| 33 | input->seek(4, librevenge::RVNG_SEEK_CUR); |
| 34 | auto signature_c = (char)readU8(input); |
| 35 | if (signature_c != 'C' && signature_c != 'c') |
| 36 | return false; |
| 37 | auto signature_d = (char)readU8(input); |
| 38 | if (signature_d != 'M' && signature_d != 'm') |
| 39 | return false; |
| 40 | auto signature_r = (char)readU8(input); |
| 41 | if (signature_r != 'X' && signature_r != 'x') |
| 42 | return false; |
| 43 | return true; |
| 44 | } |
| 45 | catch (...) |
| 46 | { |
| 47 | return false; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | Parses the input stream content. It will make callbacks to the functions provided by a |
| 52 | CDRPaintInterface class implementation when needed. This is often commonly called the |
| 53 | 'main parsing routine'. |
| 54 | \param input The input stream |
| 55 | \param painter A CDRPainterInterface implementation |
| 56 | \return A value that indicates whether the parsing was successful |
| 57 | */ |
| 58 | CDRAPI bool libcdr::CMXDocument::parse(librevenge::RVNGInputStream *input, librevenge::RVNGDrawingInterface *painter) |
| 59 | { |
| 60 | if (!input || !painter) |
| 61 | return false; |
| 62 | |
| 63 | input->seek(0, librevenge::RVNG_SEEK_SET); |
| 64 | CDRParserState ps; |
| 65 | CDRStylesCollector stylesCollector(ps); |
| 66 | CMXParserState parserState; |
| 67 | CMXParser stylesParser(&stylesCollector, parserState); |
| 68 | bool retVal = stylesParser.parseRecords(input); |
| 69 | if (ps.m_pages.empty()) |
| 70 | retVal = false; |
| 71 | if (retVal) |
| 72 | { |
| 73 | input->seek(0, librevenge::RVNG_SEEK_SET); |
| 74 | CDRContentCollector contentCollector(ps, painter, false); |
| 75 | CMXParser contentParser(&contentCollector, parserState); |
| 76 | retVal = contentParser.parseRecords(input); |
| 77 | } |
| 78 | return retVal; |
| 79 | } |
| 80 | |
| 81 | /* vim:set shiftwidth=2 softtabstop=2 expandtab: */ |