bump product version to 4.1.6.2
[LibreOffice.git] / writerfilter / source / rtftok / rtfsprm.cxx
blob7d09563c80f62f424023a977f60cd4a9b2401833
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/.
8 */
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 {
18 namespace rtftok {
20 RTFSprm::RTFSprm(Id nKeyword, RTFValue::Pointer_t& pValue)
21 : m_nKeyword(nKeyword),
22 m_pValue(pValue)
26 sal_uInt32 RTFSprm::getId() const
28 return m_nKeyword;
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()
53 return Sprm::UNKNOWN;
56 std::string RTFSprm::getName() const
58 return "RTFSprm";
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);
69 aBuf.append(" ('");
70 if (sResult.length() == 0)
71 aBuf.append(sal_Int32(m_nKeyword));
72 else
73 aBuf.append(sResult.c_str());
74 aBuf.append("', '");
75 aBuf.append(m_pValue->toString().c_str());
76 aBuf.append("')");
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)
87 if (bFirst)
88 return i->second;
89 else
90 pValue = i->second;
92 return pValue;
95 void RTFSprms::set(Id nKeyword, RTFValue::Pointer_t pValue, bool bOverwrite)
97 ensureCopyBeforeWrite();
98 if (bOverwrite)
100 for (RTFSprms::Iterator_t i = m_pSprms->begin(); i != m_pSprms->end(); ++i)
101 if (i->first == nKeyword)
103 i->second = pValue;
104 return;
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)
117 m_pSprms->erase(i);
118 return true;
121 return false;
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))
136 bIgnore = true;
138 if (bIgnore)
139 i = m_pSprms->erase(i);
140 else
141 ++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())));
151 m_pSprms = pClone;
155 RTFSprms::RTFSprms()
156 : m_pSprms(new RTFSprmsImpl())
160 RTFSprms::~RTFSprms()
164 RTFSprms::RTFSprms(const RTFSprms& rSprms)
166 *this = rSprms;
169 RTFSprms& RTFSprms::operator=(const RTFSprms& rOther)
171 m_pSprms = rOther.m_pSprms;
172 return *this;
175 void RTFSprms::clear()
177 if (m_pSprms->m_nRefCount == 1)
178 return m_pSprms->clear();
179 else
180 m_pSprms.reset(new RTFSprmsImpl());
183 } // namespace rtftok
184 } // namespace writerfilter
186 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */