summaryrefslogtreecommitdiff
path: root/src/lib/SW602Document.cpp
blob: d18fac683c315c6088c09d2e85e633a120e1beec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/* -*- 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 <libsw602/SW602Document.h>

#include <cassert>

#include <boost/scoped_ptr.hpp>
#include <boost/shared_ptr.hpp>

#include <librevenge-stream/librevenge-stream.h>

#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<class Header>
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<class Header>
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(m_kind != SW602Document::KIND_UNKNOWN);
    assert(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<RVNGInputStream> content(input->getSubStreamByName("CONTENTS"));
      if (bool(content))
      {
        if (probe<WinText602Header>(*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<RVNGInputStream> 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: */