Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * Version: MPL 1.1 / GPLv3+ / LGPLv3+
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Initial Developer of the Original Code is
16 : * Miklos Vajna <vmiklos@frugalware.org>
17 : * Portions created by the Initial Developer are Copyright (C) 2011 the
18 : * Initial Developer. All Rights Reserved.
19 : *
20 : * Contributor(s):
21 : *
22 : * Alternatively, the contents of this file may be used under the terms of
23 : * either the GNU General Public License Version 3 or later (the "GPLv3+"), or
24 : * the GNU Lesser General Public License Version 3 or later (the "LGPLv3+"),
25 : * in which case the provisions of the GPLv3+ or the LGPLv3+ are applicable
26 : * instead of those above.
27 : */
28 :
29 : #include <rtfsprm.hxx>
30 : #include <rtl/strbuf.hxx>
31 :
32 : #include <resourcemodel/QNameToString.hxx>
33 : #include <doctok/resourceids.hxx> // NS_rtf namespace
34 :
35 : using rtl::OStringBuffer;
36 :
37 : namespace writerfilter {
38 : namespace rtftok {
39 :
40 18650 : RTFSprm::RTFSprm(Id nKeyword, RTFValue::Pointer_t& pValue)
41 : : m_nKeyword(nKeyword),
42 18650 : m_pValue(pValue)
43 : {
44 18650 : }
45 :
46 69296 : sal_uInt32 RTFSprm::getId() const
47 : {
48 69296 : return m_nKeyword;
49 : }
50 :
51 52279 : Value::Pointer_t RTFSprm::getValue()
52 : {
53 52279 : return Value::Pointer_t(m_pValue->Clone());
54 : }
55 :
56 0 : writerfilter::Reference<BinaryObj>::Pointer_t RTFSprm::getBinary()
57 : {
58 0 : return m_pValue->getBinary();
59 : }
60 :
61 0 : writerfilter::Reference<Stream>::Pointer_t RTFSprm::getStream()
62 : {
63 0 : return m_pValue->getStream();
64 : }
65 :
66 1375 : writerfilter::Reference<Properties>::Pointer_t RTFSprm::getProps()
67 : {
68 1375 : return m_pValue->getProperties();
69 : }
70 :
71 0 : Sprm::Kind RTFSprm::getKind()
72 : {
73 0 : return Sprm::UNKNOWN;
74 : }
75 :
76 0 : std::string RTFSprm::getName() const
77 : {
78 0 : return "RTFSprm";
79 : }
80 :
81 0 : std::string RTFSprm::toString() const
82 : {
83 0 : OStringBuffer aBuf("RTFSprm");
84 :
85 0 : std::string sResult = (*QNameToString::Instance())(m_nKeyword);
86 0 : if (sResult.length() == 0)
87 0 : sResult = (*SprmIdToString::Instance())(m_nKeyword);
88 :
89 0 : aBuf.append(" ('");
90 0 : if (sResult.length() == 0)
91 0 : aBuf.append(sal_Int32(m_nKeyword));
92 : else
93 0 : aBuf.append(sResult.c_str());
94 0 : aBuf.append("', '");
95 0 : aBuf.append(m_pValue->toString().c_str());
96 0 : aBuf.append("')");
97 :
98 0 : return aBuf.makeStringAndClear().getStr();
99 : }
100 :
101 14152 : RTFValue::Pointer_t RTFSprms::find(Id nKeyword)
102 : {
103 43480 : for (RTFSprms::Iterator_t i = m_pSprms->begin(); i != m_pSprms->end(); ++i)
104 32517 : if (i->first == nKeyword)
105 3189 : return i->second;
106 10963 : RTFValue::Pointer_t pValue;
107 10963 : return pValue;
108 : }
109 :
110 17221 : void RTFSprms::set(Id nKeyword, RTFValue::Pointer_t pValue, bool bOverwrite)
111 : {
112 17221 : ensureCopyBeforeWrite();
113 17221 : if (bOverwrite)
114 : {
115 48404 : for (RTFSprms::Iterator_t i = m_pSprms->begin(); i != m_pSprms->end(); ++i)
116 35126 : if (i->first == nKeyword)
117 : {
118 2690 : i->second = pValue;
119 17221 : return;
120 : }
121 : }
122 14531 : m_pSprms->push_back(std::make_pair(nKeyword, pValue));
123 : }
124 :
125 411 : bool RTFSprms::erase(Id nKeyword)
126 : {
127 411 : ensureCopyBeforeWrite();
128 3998 : for (RTFSprms::Iterator_t i = m_pSprms->begin(); i != m_pSprms->end(); ++i)
129 : {
130 3767 : if (i->first == nKeyword)
131 : {
132 180 : m_pSprms->erase(i);
133 180 : return true;
134 : }
135 : }
136 231 : return false;
137 : }
138 :
139 404 : void RTFSprms::deduplicate(RTFSprms& rReference)
140 : {
141 404 : ensureCopyBeforeWrite();
142 :
143 404 : RTFSprms::Iterator_t i = m_pSprms->begin();
144 2273 : while (i != m_pSprms->end())
145 : {
146 1465 : bool bIgnore = false;
147 1465 : if (i->first != NS_rtf::LN_ISTD)
148 : {
149 1370 : RTFValue::Pointer_t pValue(rReference.find(i->first));
150 1370 : if (pValue.get() && i->second->equals(*pValue))
151 999 : bIgnore = true;
152 : }
153 1465 : if (bIgnore)
154 999 : i = m_pSprms->erase(i);
155 : else
156 466 : ++i;
157 : }
158 404 : }
159 :
160 18036 : void RTFSprms::ensureCopyBeforeWrite()
161 : {
162 18036 : if (m_pSprms->m_nRefCount > 1) {
163 4079 : boost::intrusive_ptr<RTFSprmsImpl> pClone(new RTFSprmsImpl());
164 7705 : for (std::vector< std::pair<Id, RTFValue::Pointer_t> >::const_iterator i = m_pSprms->begin(); i != m_pSprms->end(); ++i)
165 3626 : pClone->push_back(std::make_pair(i->first, RTFValue::Pointer_t(i->second->Clone())));
166 4079 : m_pSprms = pClone;
167 : }
168 18036 : }
169 :
170 46582 : RTFSprms::RTFSprms()
171 46582 : : m_pSprms(new RTFSprmsImpl())
172 : {
173 46582 : }
174 :
175 458334 : RTFSprms::~RTFSprms()
176 : {
177 458334 : }
178 :
179 411752 : RTFSprms::RTFSprms(const RTFSprms& rSprms)
180 : {
181 411752 : *this = rSprms;
182 411752 : }
183 :
184 414641 : RTFSprms& RTFSprms::operator=(const RTFSprms& rOther)
185 : {
186 414641 : m_pSprms = rOther.m_pSprms;
187 414641 : return *this;
188 : }
189 :
190 0 : void RTFSprms::swap(RTFSprms& rOther)
191 : {
192 0 : boost::intrusive_ptr<RTFSprmsImpl> pTmp = rOther.m_pSprms;
193 0 : rOther.m_pSprms = m_pSprms;
194 0 : m_pSprms = pTmp;
195 0 : }
196 :
197 4601 : void RTFSprms::clear()
198 : {
199 4601 : if (m_pSprms->m_nRefCount == 1)
200 5327 : return m_pSprms->clear();
201 : else
202 3875 : m_pSprms.reset(new RTFSprmsImpl());
203 : }
204 :
205 : } // namespace rtftok
206 15 : } // namespace writerfilter
207 :
208 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|