summaryrefslogtreecommitdiff
path: root/src/lib/T602Header.cpp
blob: 5ae759163c92b8abebb34d04eec31f07065a01e8 (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
/* -*- 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 "T602Header.h"

#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/spirit/include/qi_core.hpp>
#include <boost/spirit/include/qi_eol.hpp>

#include "SW602StreamIterator.h"

namespace libsw602
{

T602Header::T602Header()
  : m_valid(false)
  , m_encoding(SW602Document::ENCODING_UNSPECIFIED)
  , m_header()
  , m_footer()
  , m_lineHeight(1)
  , m_leftMargin(0)
  , m_rightMargin(0)
  , m_topMargin(0)
  , m_bottomMargin(0)
  , m_pageLength(0)
  , m_tabStr()
{
}

void T602Header::construct(librevenge::RVNGInputStream &input)
{
  namespace qi = boost::spirit::qi;
  namespace phx = boost::phoenix;
  using qi::_1;
  using qi::ascii::upper;
  using qi::ascii::char_;
  using qi::eol;
  using qi::lit;
  using qi::repeat;
  using qi::uint_;

  qi::symbols<char, SW602Document::Encoding> encoding;
  encoding.add
  ("0", SW602Document::ENCODING_KEYBCS2)
  ("1", SW602Document::ENCODING_LATIN2)
  ("2", SW602Document::ENCODING_KOI8CS)
  ;
  qi::symbols<char, double> lineHeight;
  lineHeight.add
  ("3", 2)
  ("4", 1.5)
  ("6", 1)
  ;

  bool known = false;
  SW602StreamIterator it(input);
  const bool result =
    qi::parse(it, SW602StreamIterator(),
              +(
                lit('@')
                > (
                  (
                    (lit("CT") > ' ' > encoding[phx::ref(m_encoding) = _1])
                    | (lit("HE") > ' ' > +(char_ - '\r')[phx::ref(m_header) += _1])
                    | (lit("FO") > ' ' > +(char_ - '\r')[phx::ref(m_footer) += _1])
                    | (lit("LH") > ' ' > lineHeight[phx::ref(m_lineHeight) = _1])
                    | (lit("LM") > ' ' > uint_[phx::ref(m_leftMargin) = _1])
                    | (lit("MT") > ' ' > uint_[phx::ref(m_topMargin) = _1])
                    | (lit("MB") > ' ' > uint_[phx::ref(m_bottomMargin) = _1])
                    | (lit("PL") > ' ' > uint_[phx::ref(m_pageLength) = _1])
                    | (lit("RM") > ' ' > uint_[phx::ref(m_rightMargin) = _1])
                    | (lit("TB") > ' ' > +(char_ - '\r')[phx::ref(m_tabStr) += _1])
                  )[phx::ref(known) = true]
                  | (repeat(2)[upper] > ' ' > *(char_ - '\r')) // unhandled controls
                )
                > eol
              )
             )
    ;

  m_valid = result && known;
}

bool T602Header::isValid() const
{
  return m_valid;
}

SW602Document::Encoding T602Header::encoding() const
{
  return m_encoding;
}

const std::string &T602Header::header() const
{
  return m_header;
}

const std::string &T602Header::footer() const
{
  return m_footer;
}

double T602Header::lineHeight() const
{
  return m_lineHeight;
}

unsigned T602Header::leftMargin() const
{
  return m_leftMargin;
}

unsigned T602Header::rightMargin() const
{
  return m_rightMargin;
}

unsigned T602Header::topMargin() const
{
  return m_topMargin;
}

unsigned T602Header::bottomMargin() const
{
  return m_bottomMargin;
}

unsigned T602Header::pageLength() const
{
  return m_pageLength;
}

const std::string &T602Header::tabs() const
{
  return m_tabStr;
}

} // namespace libsw602

/* vim:set shiftwidth=2 softtabstop=2 expandtab: */