Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice 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 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #ifndef INCLUDED_PLCF_HXX
21 : #define INCLUDED_PLCF_HXX
22 :
23 : #include <boost/shared_ptr.hpp>
24 : #include "WW8StructBase.hxx"
25 :
26 : namespace writerfilter {
27 : namespace doctok
28 : {
29 :
30 : class Empty
31 : {
32 : public:
33 : typedef boost::shared_ptr<Empty> Pointer_t;
34 :
35 : Empty() {}
36 0 : virtual ~Empty() {}
37 :
38 : sal_uInt32 getSize() { return 0; }
39 : };
40 :
41 : template <class T>
42 : /**
43 : Plex in File
44 :
45 : A PLCF is a concatenation of two arrays. The first array contains
46 : file character positions. The second array contains elements of
47 : type T. If the first array contains N elements, the second contains
48 : N - 1 elements. The N-th element in the first array corresponds to
49 : the N-th element of the second array.
50 :
51 : The second array is referred to as the payload.
52 : */
53 0 : class PLCF : public WW8StructBase
54 : {
55 : /// number of entries
56 : sal_uInt32 nEntryCount;
57 :
58 : /// offset to payload
59 : sal_uInt32 nPayloadOffset;
60 :
61 : /// internal method to calculate the number of entries
62 : sal_uInt32 getEntryCount_() const;
63 :
64 : public:
65 : typedef boost::shared_ptr< PLCF< T > > Pointer_t;
66 :
67 : PLCF(sal_uInt32 nLength)
68 : : WW8StructBase(nLength), nEntryCount(getEntryCount_()),
69 : nPayloadOffset((nEntryCount + 1) * 4)
70 : {
71 : }
72 :
73 0 : PLCF(WW8Stream & rStream,
74 : sal_Int32 nOffset, sal_Int32 nCount)
75 : : WW8StructBase(rStream, nOffset, nCount),
76 : nEntryCount(getEntryCount_()),
77 0 : nPayloadOffset((nEntryCount + 1) * 4)
78 : {
79 0 : }
80 :
81 : PLCF(const Sequence & rSequence)
82 : : WW8StructBase(rSequence), nEntryCount(getEntryCount_()),
83 : nPayloadOffset((nEntryCount + 1) * 4)
84 : {
85 : }
86 :
87 : /**
88 : Return the number of elements in the PLCF-
89 : */
90 0 : sal_uInt32 getEntryCount() const { return nEntryCount; }
91 :
92 : /**
93 : Return the file character position of a certain element.
94 :
95 : @param nIndex the index of the element
96 : */
97 : sal_uInt32 getFc(sal_uInt32 nIndex) const;
98 :
99 : /**
100 : Return a C++ pointer to a certain payload entry.
101 :
102 : @param nIndex the index of the element
103 : */
104 : T * getEntryPointer(sal_uInt32 nIndex) const;
105 :
106 : /**
107 : Return a shared pointer to a certain payload element.
108 :
109 : @param nIndex the index of the element
110 : */
111 : typename T::Pointer_t getEntry(sal_uInt32 nIndex) const;
112 :
113 : /**
114 : Return a C++ pointer a certain payload element.
115 :
116 : @param nFc the file character position of the element
117 : */
118 : T * getEntryByFc(sal_uInt32 nFc) const;
119 :
120 : virtual void dump(OutputWithDepth<string> & out) const;
121 : };
122 :
123 : template <class T>
124 0 : sal_uInt32 PLCF<T>::getEntryCount_() const
125 : {
126 0 : return (getCount() - 4) / (T::getSize() + 4);
127 : }
128 :
129 : template <class T>
130 0 : sal_uInt32 PLCF<T>::getFc(sal_uInt32 nIndex) const
131 : {
132 0 : return getU32(nIndex * 4);
133 : }
134 :
135 : template <class T>
136 0 : T * PLCF<T>::getEntryPointer(sal_uInt32 nIndex) const
137 : {
138 : return new T(mSequence, nPayloadOffset + nIndex * T::getSize(),
139 0 : T::getSize());
140 : }
141 :
142 : template <class T>
143 0 : typename T::Pointer_t PLCF<T>::getEntry(sal_uInt32 nIndex) const
144 : {
145 0 : typename T::Pointer_t pResult(getEntryPointer(nIndex));
146 :
147 0 : return pResult;
148 : }
149 :
150 :
151 : template <class T>
152 0 : T * PLCF<T>::getEntryByFc(sal_uInt32 nFc) const
153 : {
154 0 : T * pResult = NULL;
155 :
156 0 : sal_uInt32 n = getEntryCount();
157 :
158 0 : while (getFc(n) > nFc)
159 0 : n--;
160 :
161 0 : pResult = getEntryPointer(n);
162 :
163 0 : return pResult;
164 : }
165 :
166 : template <class T>
167 0 : void PLCF<T>::dump(OutputWithDepth<string> & output_) const
168 : {
169 0 : output_.addItem("<plcf>");
170 0 : WW8StructBase::dump(output_);
171 :
172 0 : sal_uInt32 nCount = getEntryCount();
173 0 : for (sal_uInt32 n = 0; n < nCount; ++n)
174 : {
175 0 : Fc aFc = getFc(n);
176 0 : typename T::Pointer_t pT = getEntry(n);
177 :
178 0 : output_.addItem("<plcfentry cpandfc=\"" + aFc.toString() + "\">");
179 0 : pT->dump(output_);
180 0 : output_.addItem("</plcfentry>");
181 : }
182 0 : output_.addItem("</plcf>>");
183 0 : }
184 :
185 : }}
186 :
187 : #endif // INCLUDED_PLCF_HXX
188 :
189 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|