1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
10 #include <rtfsprm.hxx>
11 #include <rtl/strbuf.hxx>
13 #include <resourcemodel/QNameToString.hxx>
14 #include <doctok/resourceids.hxx> // NS_rtf namespace
17 namespace writerfilter
{
20 RTFSprm::RTFSprm(Id nKeyword
, RTFValue::Pointer_t
& pValue
)
21 : m_nKeyword(nKeyword
),
26 sal_uInt32
RTFSprm::getId() const
31 Value::Pointer_t
RTFSprm::getValue()
33 return Value::Pointer_t(m_pValue
->Clone());
36 writerfilter::Reference
<BinaryObj
>::Pointer_t
RTFSprm::getBinary()
38 return m_pValue
->getBinary();
41 writerfilter::Reference
<Stream
>::Pointer_t
RTFSprm::getStream()
43 return m_pValue
->getStream();
46 writerfilter::Reference
<Properties
>::Pointer_t
RTFSprm::getProps()
48 return m_pValue
->getProperties();
51 Sprm::Kind
RTFSprm::getKind()
56 std::string
RTFSprm::getName() const
61 std::string
RTFSprm::toString() const
63 OStringBuffer
aBuf("RTFSprm");
65 std::string sResult
= (*QNameToString::Instance())(m_nKeyword
);
66 if (sResult
.length() == 0)
67 sResult
= (*SprmIdToString::Instance())(m_nKeyword
);
70 if (sResult
.length() == 0)
71 aBuf
.append(sal_Int32(m_nKeyword
));
73 aBuf
.append(sResult
.c_str());
75 aBuf
.append(m_pValue
->toString().c_str());
78 return aBuf
.makeStringAndClear().getStr();
81 RTFValue::Pointer_t
RTFSprms::find(Id nKeyword
, bool bFirst
)
83 RTFValue::Pointer_t pValue
;
84 for (RTFSprms::Iterator_t i
= m_pSprms
->begin(); i
!= m_pSprms
->end(); ++i
)
85 if (i
->first
== nKeyword
)
95 void RTFSprms::set(Id nKeyword
, RTFValue::Pointer_t pValue
, bool bOverwrite
)
97 ensureCopyBeforeWrite();
100 for (RTFSprms::Iterator_t i
= m_pSprms
->begin(); i
!= m_pSprms
->end(); ++i
)
101 if (i
->first
== nKeyword
)
107 m_pSprms
->push_back(std::make_pair(nKeyword
, pValue
));
110 bool RTFSprms::erase(Id nKeyword
)
112 ensureCopyBeforeWrite();
113 for (RTFSprms::Iterator_t i
= m_pSprms
->begin(); i
!= m_pSprms
->end(); ++i
)
115 if (i
->first
== nKeyword
)
124 void RTFSprms::deduplicate(RTFSprms
& rReference
)
126 ensureCopyBeforeWrite();
128 RTFSprms::Iterator_t i
= m_pSprms
->begin();
129 while (i
!= m_pSprms
->end())
131 bool bIgnore
= false;
132 if (i
->first
!= NS_rtf::LN_ISTD
)
134 RTFValue::Pointer_t
pValue(rReference
.find(i
->first
));
135 if (pValue
.get() && i
->second
->equals(*pValue
))
139 i
= m_pSprms
->erase(i
);
145 void RTFSprms::ensureCopyBeforeWrite()
147 if (m_pSprms
->m_nRefCount
> 1) {
148 boost::intrusive_ptr
<RTFSprmsImpl
> pClone(new RTFSprmsImpl());
149 for (std::vector
< std::pair
<Id
, RTFValue::Pointer_t
> >::const_iterator i
= m_pSprms
->begin(); i
!= m_pSprms
->end(); ++i
)
150 pClone
->push_back(std::make_pair(i
->first
, RTFValue::Pointer_t(i
->second
->Clone())));
156 : m_pSprms(new RTFSprmsImpl())
160 RTFSprms::~RTFSprms()
164 RTFSprms::RTFSprms(const RTFSprms
& rSprms
)
169 RTFSprms
& RTFSprms::operator=(const RTFSprms
& rOther
)
171 m_pSprms
= rOther
.m_pSprms
;
175 void RTFSprms::clear()
177 if (m_pSprms
->m_nRefCount
== 1)
178 return m_pSprms
->clear();
180 m_pSprms
.reset(new RTFSprmsImpl());
183 } // namespace rtftok
184 } // namespace writerfilter
186 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */