/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /* * This file is part of the libsw602 project. * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ #include #include #include #include #include #include "T602Header.h" #include "T602Parser.h" #include "WinText602Header.h" #include "WinText602Parser.h" using librevenge::RVNGInputStream; using std::string; namespace libsw602 { namespace { struct DetectionInfo { DetectionInfo(); SW602Document::Kind m_kind; SW602Document::Type m_type; SW602Document::Confidence m_confidence; bool m_needsEncoding; }; DetectionInfo::DetectionInfo() : m_kind(SW602Document::KIND_UNKNOWN) , m_type(SW602Document::TYPE_UNKNOWN) , m_confidence(SW602Document::CONFIDENCE_NONE) , m_needsEncoding(false) { } template void fillDetectionInfo(const Header &header, DetectionInfo &info); void fillDetectionInfo(const WinText602Header &/*header*/, DetectionInfo &info) { info.m_kind = SW602Document::KIND_TEXT; info.m_type = SW602Document::TYPE_WINTEXT602; info.m_needsEncoding = false; // TODO: handle encryption info.m_confidence = SW602Document::CONFIDENCE_EXCELLENT; } template bool probe(RVNGInputStream &input, DetectionInfo &info) try { input.seek(0, librevenge::RVNG_SEEK_SET); Header header; header.construct(input); if (header.isValid()) { fillDetectionInfo(header, info); assert(info.m_kind != SW602Document::KIND_UNKNOWN); assert(info.m_type != SW602Document::TYPE_UNKNOWN); } return info.m_confidence != SW602Document::CONFIDENCE_NONE; } catch (...) { return false; } SW602Document::Confidence pass(const DetectionInfo &info, SW602Document::Kind &kind, SW602Document::Type *const type, bool *const needsEncoding) { kind = info.m_kind; if (type) *type = info.m_type; if (needsEncoding) *needsEncoding = info.m_needsEncoding; return info.m_confidence; } } SW602Document::Confidence SW602Document::isSupported(librevenge::RVNGInputStream *const input, Kind &kind, Type *const type, bool *const needsEncoding) try { if (!input) return CONFIDENCE_NONE; DetectionInfo info; if (input->isStructured()) { if (input->existsSubStream("CONTENTS")) { const boost::scoped_ptr content(input->getSubStreamByName("CONTENTS")); if (bool(content)) { if (probe(*content, info)) return pass(info, kind, type, needsEncoding); } } } return CONFIDENCE_NONE; } catch (...) { return CONFIDENCE_NONE; } SW602Document::Result SW602Document::parse(librevenge::RVNGInputStream *const input, librevenge::RVNGTextInterface *const document, const char *const password) { return parse(input, document, TYPE_UNKNOWN, ENCODING_UNSPECIFIED, password); } SW602Document::Result SW602Document::parse(librevenge::RVNGInputStream *const input, librevenge::RVNGTextInterface *const document, const Type type, const char *const password) { return parse(input, document, type, ENCODING_UNSPECIFIED, password); } SW602Document::Result SW602Document::parse(librevenge::RVNGInputStream *const input, librevenge::RVNGTextInterface *const document, const Encoding encoding, const char *const password) { return parse(input, document, TYPE_UNKNOWN, encoding, password); } SW602Document::Result SW602Document::parse(librevenge::RVNGInputStream *const input, librevenge::RVNGTextInterface *const document, Type type, Encoding /*encoding*/, const char * /*password*/) try { if (!document) return RESULT_OK; if (!input) return RESULT_PARSE_ERROR; if (type == TYPE_UNKNOWN) { Kind kind = KIND_UNKNOWN; const Confidence confidence = SW602Document::isSupported(input, kind, &type); if ((kind != KIND_TEXT) || ((confidence != CONFIDENCE_EXCELLENT) && (confidence != CONFIDENCE_SUPPORTED_ENCRYPTION))) return RESULT_UNSUPPORTED_FORMAT; } switch (type) { case TYPE_WINTEXT602: { if (!input->existsSubStream("CONTENTS")) return RESULT_UNSUPPORTED_FORMAT; const boost::shared_ptr content(input->getSubStreamByName("CONTENTS")); if (!content) return RESULT_UNSUPPORTED_FORMAT; WinText602Parser parser(content); parser.parse(document); return RESULT_OK; } default: return RESULT_UNSUPPORTED_FORMAT; } return RESULT_UNKNOWN_ERROR; } catch (...) { return RESULT_UNKNOWN_ERROR; } SW602Document::Result SW602Document::parse(librevenge::RVNGInputStream *input, librevenge::RVNGSpreadsheetInterface *document, const char *password) { return parse(input, document, TYPE_UNKNOWN, ENCODING_UNSPECIFIED, password); } SW602Document::Result SW602Document::parse(librevenge::RVNGInputStream *input, librevenge::RVNGSpreadsheetInterface *document, Type type, const char *password) { return parse(input, document, type, ENCODING_UNSPECIFIED, password); } SW602Document::Result SW602Document::parse(librevenge::RVNGInputStream *input, librevenge::RVNGSpreadsheetInterface *document, Encoding encoding, const char *password) { return parse(input, document, TYPE_UNKNOWN, encoding, password); } SW602Document::Result SW602Document::parse(librevenge::RVNGInputStream * /*input*/, librevenge::RVNGSpreadsheetInterface * /*document*/, Type /*type*/, Encoding /*encoding*/, const char * /*password*/) try { // TODO: implement me return RESULT_UNKNOWN_ERROR; } catch (...) { return RESULT_UNKNOWN_ERROR; } } // namespace libsw602 /* vim:set shiftwidth=2 softtabstop=2 expandtab: */