diff options
author | David Tardon <dtardon@redhat.com> | 2016-02-21 09:27:46 +0100 |
---|---|---|
committer | David Tardon <dtardon@redhat.com> | 2016-02-21 15:39:28 +0100 |
commit | 7f33506a73f655754f5ec35e1a1e290ac7c39356 (patch) | |
tree | e6a0c1cc98a152e6c6a71787b2302d0a2db5cf42 | |
parent | 1fae47f268a6aff3ac95fadf287a1b73f26a9e62 (diff) |
extend interface to cover all expected use cases
I.e., handling of multiple document formats (and kinds), password
protected documents, documents in unknown encoding.
-rw-r--r-- | inc/libsw602/Software602Document.h | 88 | ||||
-rw-r--r-- | src/conv/html/sw6022html.cpp | 12 | ||||
-rw-r--r-- | src/conv/raw/sw6022raw.cpp | 26 | ||||
-rw-r--r-- | src/conv/text/sw6022text.cpp | 13 | ||||
-rw-r--r-- | src/lib/Software602Document.cpp | 99 |
5 files changed, 204 insertions, 34 deletions
diff --git a/inc/libsw602/Software602Document.h b/inc/libsw602/Software602Document.h index 8e01902..663491a 100644 --- a/inc/libsw602/Software602Document.h +++ b/inc/libsw602/Software602Document.h @@ -32,8 +32,92 @@ namespace libsw602 class Software602Document { public: - static SW602API bool isSupported(librevenge::RVNGInputStream *input); - static SW602API bool parse(librevenge::RVNGInputStream *input, librevenge::RVNGTextInterface *document); + /** Likelihood that the file format is supported. + */ + enum Confidence + { + CONFIDENCE_NONE, //< not supported + CONFIDENCE_UNSUPPORTED_ENCRYPTION, //< encryption using unsupported encryption + CONFIDENCE_SUPPORTED_ENCRYPTION, //< encrypted using supported encryption + CONFIDENCE_EXCELLENT //< supported + }; + + /** Kind of document. + */ + enum Kind + { + KIND_UNKNOWN, + KIND_TEXT, + KIND_SPREADSHEET, + KIND_CHART, + KIND_DATABASE + }; + + /** Type of document. + */ + enum Type + { + TYPE_UNKNOWN, //< unrecognized format + TYPE_T602, //< T602 (text) + TYPE_C602, //< C602 (spreadsheet) + TYPE_C602_CHART, //< standalone C602 chart file + TYPE_K602, //< K602 (database) + TYPE_WINTEXT602, //< WinText602/602Text + TYPE_MAGICTAB, //< MagicTab/602Tab + + TYPE_RESERVED_0, + TYPE_RESERVED_1, + TYPE_RESERVED_2, + TYPE_RESERVED_3, + TYPE_RESERVED_4, + TYPE_RESERVED_5, + TYPE_RESERVED_6, + TYPE_RESERVED_7, + TYPE_RESERVED_8, + TYPE_RESERVED_9 + }; + + /** Text encoding, for formats which do not contain the information. + */ + enum Encoding + { + ENCODING_UNSPECIFIED, //< unspecified encoding, a format default will be used + ENCODING_LATIN2, //< PC Latin 2/CP852 + ENCODING_KEYBCS2, //< KEYBCS2/CP895(?) + ENCODING_KOI8CS, //< KOI-8 CS2 + ENCODING_WINEE //< WinEE/Windows-1250/CP1250 + }; + + /** Result of parsing the file. + */ + enum Result + { + RESULT_OK, //< parsed without any problem + RESULT_UNSUPPORTED_FORMAT, //< unsupported file format + RESULT_PASSWORD_MISMATCH, //< wrong password + RESULT_FILE_ACCESS_ERROR, //< problem when accessing the file + RESULT_PARSE_ERROR, //< problem when parsing the file + RESULT_UNKNOWN_ERROR //< an unspecified error + }; + +public: + /** Detect if the stream contains a valid Software602 document. + */ + static SW602API Confidence isSupported(librevenge::RVNGInputStream *input, Kind &kind, Type *type = 0, bool *needsEncoding = 0); + + /** Parse a text document. + */ + static SW602API Result parse(librevenge::RVNGInputStream *input, librevenge::RVNGTextInterface *document, const char *password = 0); + static SW602API Result parse(librevenge::RVNGInputStream *input, librevenge::RVNGTextInterface *document, Type type, const char *password = 0); + static SW602API Result parse(librevenge::RVNGInputStream *input, librevenge::RVNGTextInterface *document, Encoding encoding, const char *password = 0); + static SW602API Result parse(librevenge::RVNGInputStream *input, librevenge::RVNGTextInterface *document, Type type, Encoding encoding, const char *password = 0); + + /** Parse a spreadsheet. + */ + static SW602API Result parse(librevenge::RVNGInputStream *input, librevenge::RVNGSpreadsheetInterface *document, const char *password = 0); + static SW602API Result parse(librevenge::RVNGInputStream *input, librevenge::RVNGSpreadsheetInterface *document, Type type, const char *password = 0); + static SW602API Result parse(librevenge::RVNGInputStream *input, librevenge::RVNGSpreadsheetInterface *document, Encoding encoding, const char *password = 0); + static SW602API Result parse(librevenge::RVNGInputStream *input, librevenge::RVNGSpreadsheetInterface *document, Type type, Encoding encoding, const char *password = 0); }; } // namespace libsw602 diff --git a/src/conv/html/sw6022html.cpp b/src/conv/html/sw6022html.cpp index 247a669..1c08d76 100644 --- a/src/conv/html/sw6022html.cpp +++ b/src/conv/html/sw6022html.cpp @@ -76,14 +76,16 @@ int main(int argc, char *argv[]) librevenge::RVNGFileStream input(file); - librevenge::RVNGString document; - librevenge::RVNGHTMLTextGenerator documentGenerator(document); - - if (!Software602Document::isSupported(&input)) + Software602Document::Kind kind = Software602Document::KIND_UNKNOWN; + const Software602Document::Confidence confidence = Software602Document::isSupported(&input, kind); + if ((kind != Software602Document::KIND_TEXT) || (confidence != Software602Document::CONFIDENCE_EXCELLENT)) fprintf(stderr, "unsupported file format\n"); return 1; - if (!Software602Document::parse(&input, &documentGenerator)) + librevenge::RVNGString document; + + librevenge::RVNGHTMLTextGenerator documentGenerator(document); + if (Software602Document::parse(&input, &documentGenerator) != Software602Document::RESULT_OK) { fprintf(stderr, "parsing failed\n"); return 1; diff --git a/src/conv/raw/sw6022raw.cpp b/src/conv/raw/sw6022raw.cpp index e09dcd9..fae6dc7 100644 --- a/src/conv/raw/sw6022raw.cpp +++ b/src/conv/raw/sw6022raw.cpp @@ -83,12 +83,30 @@ int main(int argc, char *argv[]) librevenge::RVNGFileStream input(file); - librevenge::RVNGRawTextGenerator documentGenerator(printIndentLevel); + Software602Document::Kind kind = Software602Document::KIND_UNKNOWN; + const Software602Document::Confidence confidence = Software602Document::isSupported(&input, kind); + if (confidence != Software602Document::CONFIDENCE_EXCELLENT) + fprintf(stderr, "unsupported file format\n"); + return 1; - if (!Software602Document::isSupported(&input)) + switch (kind) + { + case Software602Document::KIND_TEXT: + { + librevenge::RVNGRawTextGenerator documentGenerator(printIndentLevel); + return Software602Document::parse(&input, &documentGenerator) == Software602Document::RESULT_OK; + } + case Software602Document::KIND_SPREADSHEET: + case Software602Document::KIND_CHART: + case Software602Document::KIND_DATABASE: + { + librevenge::RVNGRawSpreadsheetGenerator documentGenerator(printIndentLevel); + return Software602Document::parse(&input, &documentGenerator) == Software602Document::RESULT_OK; + } + default: + fprintf(stderr, "unsupported file format\n"); return 1; - - return Software602Document::parse(&input, &documentGenerator) ? 0 : 1; + } } /* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */ diff --git a/src/conv/text/sw6022text.cpp b/src/conv/text/sw6022text.cpp index 522f209..b6cbaa3 100644 --- a/src/conv/text/sw6022text.cpp +++ b/src/conv/text/sw6022text.cpp @@ -80,14 +80,16 @@ int main(int argc, char *argv[]) librevenge::RVNGFileStream input(szInputFile); - librevenge::RVNGString document; - librevenge::RVNGTextTextGenerator documentGenerator(document, isInfo); - - if (!Software602Document::isSupported(&input)) + Software602Document::Kind kind = Software602Document::KIND_UNKNOWN; + const Software602Document::Confidence confidence = Software602Document::isSupported(&input, kind); + if ((kind != Software602Document::KIND_TEXT) || (confidence != Software602Document::CONFIDENCE_EXCELLENT)) fprintf(stderr, "unsupported file format\n"); return 1; - if (!Software602Document::parse(&input, &documentGenerator)) + librevenge::RVNGString document; + + librevenge::RVNGTextTextGenerator documentGenerator(document, isInfo); + if (Software602Document::parse(&input, &documentGenerator) != Software602Document::RESULT_OK) { fprintf(stderr, "parsing failed\n"); return 1; @@ -95,7 +97,6 @@ int main(int argc, char *argv[]) printf("%s", document.cstr()); - return 0; } diff --git a/src/lib/Software602Document.cpp b/src/lib/Software602Document.cpp index 33c01a2..acc0670 100644 --- a/src/lib/Software602Document.cpp +++ b/src/lib/Software602Document.cpp @@ -35,47 +35,112 @@ RVNGInputStreamPtr getContent(librevenge::RVNGInputStream *const ip) } -bool Software602Document::isSupported(librevenge::RVNGInputStream *input) try +Software602Document::Confidence Software602Document::isSupported(librevenge::RVNGInputStream *const input, Kind &kind, Type *const type, bool *const needsEncoding) try { if (!input) - return false; + return CONFIDENCE_NONE; RVNGInputStreamPtr content = getContent(input); if (bool(content)) { content->seek(0, librevenge::RVNG_SEEK_SET); WinText602Header header(content.get()); - return header.isValid(); + // TODO: handle encryption + if (header.isValid()) + { + kind = KIND_TEXT; + if (type) + *type = TYPE_WINTEXT602; + if (needsEncoding) + *needsEncoding = false; + return CONFIDENCE_EXCELLENT; + } } - return false; + return CONFIDENCE_NONE; } catch (...) { - return false; + return CONFIDENCE_NONE; } -bool Software602Document::parse(librevenge::RVNGInputStream *input, librevenge::RVNGTextInterface *document) try +Software602Document::Result Software602Document::parse(librevenge::RVNGInputStream *const input, librevenge::RVNGTextInterface *const document, const char *const password) { - if (!input || !document) - return false; + return parse(input, document, TYPE_UNKNOWN, ENCODING_UNSPECIFIED, password); +} - if (!Software602Document::isSupported(input)) - return false; +Software602Document::Result Software602Document::parse(librevenge::RVNGInputStream *const input, librevenge::RVNGTextInterface *const document, const Type type, const char *const password) +{ + return parse(input, document, type, ENCODING_UNSPECIFIED, password); +} - RVNGInputStreamPtr content = getContent(input); - if (bool(content)) +Software602Document::Result Software602Document::parse(librevenge::RVNGInputStream *const input, librevenge::RVNGTextInterface *const document, const Encoding encoding, const char *const password) +{ + return parse(input, document, TYPE_UNKNOWN, encoding, password); +} + +Software602Document::Result Software602Document::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) { - content->seek(0, librevenge::RVNG_SEEK_SET); - WinText602Parser parser(content.get(), document); - return parser.parse(); + Kind kind = KIND_UNKNOWN; + const Confidence confidence = Software602Document::isSupported(input, kind, &type); + if ((kind != KIND_TEXT) || ((confidence != CONFIDENCE_EXCELLENT) && (confidence != CONFIDENCE_SUPPORTED_ENCRYPTION))) + return RESULT_UNSUPPORTED_FORMAT; + } + + switch (type) + { + case TYPE_WINTEXT602: + { + RVNGInputStreamPtr content = getContent(input); + if (bool(content)) + { + content->seek(0, librevenge::RVNG_SEEK_SET); + WinText602Parser parser(content.get(), document); + return parser.parse() ? RESULT_OK : RESULT_PARSE_ERROR; + } + break; } + default: + return RESULT_UNSUPPORTED_FORMAT; + } + + return RESULT_UNKNOWN_ERROR; +} +catch (...) +{ + return RESULT_UNKNOWN_ERROR; +} + +Software602Document::Result Software602Document::parse(librevenge::RVNGInputStream *input, librevenge::RVNGSpreadsheetInterface *document, const char *password) +{ + return parse(input, document, TYPE_UNKNOWN, ENCODING_UNSPECIFIED, password); +} + +Software602Document::Result Software602Document::parse(librevenge::RVNGInputStream *input, librevenge::RVNGSpreadsheetInterface *document, Type type, const char *password) +{ + return parse(input, document, type, ENCODING_UNSPECIFIED, password); +} - return false; +Software602Document::Result Software602Document::parse(librevenge::RVNGInputStream *input, librevenge::RVNGSpreadsheetInterface *document, Encoding encoding, const char *password) +{ + return parse(input, document, TYPE_UNKNOWN, encoding, password); +} + +Software602Document::Result Software602Document::parse(librevenge::RVNGInputStream * /*input*/, librevenge::RVNGSpreadsheetInterface * /*document*/, Type /*type*/, Encoding /*encoding*/, const char * /*password*/) try +{ + // TODO: implement me + return RESULT_UNKNOWN_ERROR; } catch (...) { - return false; + return RESULT_UNKNOWN_ERROR; } } // namespace libsw602 |