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 :
10 : #include <rtfsprm.hxx>
11 : #include <rtl/strbuf.hxx>
12 :
13 : #include <resourcemodel/QNameToString.hxx>
14 :
15 :
16 : namespace writerfilter {
17 : namespace rtftok {
18 :
19 0 : RTFSprm::RTFSprm(Id nKeyword, RTFValue::Pointer_t& pValue)
20 : : m_nKeyword(nKeyword),
21 0 : m_pValue(pValue)
22 : {
23 0 : }
24 :
25 0 : sal_uInt32 RTFSprm::getId() const
26 : {
27 0 : return m_nKeyword;
28 : }
29 :
30 0 : Value::Pointer_t RTFSprm::getValue()
31 : {
32 0 : return Value::Pointer_t(m_pValue->Clone());
33 : }
34 :
35 0 : writerfilter::Reference<BinaryObj>::Pointer_t RTFSprm::getBinary()
36 : {
37 0 : return m_pValue->getBinary();
38 : }
39 :
40 0 : writerfilter::Reference<Stream>::Pointer_t RTFSprm::getStream()
41 : {
42 0 : return m_pValue->getStream();
43 : }
44 :
45 0 : writerfilter::Reference<Properties>::Pointer_t RTFSprm::getProps()
46 : {
47 0 : return m_pValue->getProperties();
48 : }
49 :
50 0 : Sprm::Kind RTFSprm::getKind()
51 : {
52 0 : return Sprm::UNKNOWN;
53 : }
54 :
55 0 : std::string RTFSprm::getName() const
56 : {
57 0 : return "RTFSprm";
58 : }
59 :
60 0 : std::string RTFSprm::toString() const
61 : {
62 0 : OStringBuffer aBuf("RTFSprm");
63 :
64 0 : std::string sResult = (*QNameToString::Instance())(m_nKeyword);
65 :
66 0 : aBuf.append(" ('");
67 0 : if (sResult.length() == 0)
68 0 : aBuf.append(sal_Int32(m_nKeyword));
69 : else
70 0 : aBuf.append(sResult.c_str());
71 0 : aBuf.append("', '");
72 0 : aBuf.append(m_pValue->toString().c_str());
73 0 : aBuf.append("')");
74 :
75 0 : return aBuf.makeStringAndClear().getStr();
76 : }
77 :
78 0 : RTFValue::Pointer_t RTFSprms::find(Id nKeyword, bool bFirst)
79 : {
80 0 : RTFValue::Pointer_t pValue;
81 0 : for (RTFSprms::Iterator_t i = m_pSprms->begin(); i != m_pSprms->end(); ++i)
82 0 : if (i->first == nKeyword)
83 : {
84 0 : if (bFirst)
85 0 : return i->second;
86 : else
87 0 : pValue = i->second;
88 : }
89 0 : return pValue;
90 : }
91 :
92 0 : void RTFSprms::set(Id nKeyword, RTFValue::Pointer_t pValue, RTFOverwrite eOverwrite)
93 : {
94 0 : ensureCopyBeforeWrite();
95 0 : bool bFound = false;
96 0 : if (eOverwrite == OVERWRITE_YES || eOverwrite == OVERWRITE_NO_IGNORE)
97 : {
98 0 : for (RTFSprms::Iterator_t i = m_pSprms->begin(); i != m_pSprms->end(); ++i)
99 0 : if (i->first == nKeyword)
100 : {
101 0 : if (eOverwrite == OVERWRITE_YES)
102 : {
103 0 : i->second = pValue;
104 0 : return;
105 : }
106 : else
107 : {
108 0 : bFound = true;
109 0 : break;
110 : }
111 : }
112 : }
113 0 : if (eOverwrite == OVERWRITE_NO_APPEND || !bFound)
114 0 : m_pSprms->push_back(std::make_pair(nKeyword, pValue));
115 : }
116 :
117 0 : bool RTFSprms::erase(Id nKeyword)
118 : {
119 0 : ensureCopyBeforeWrite();
120 0 : for (RTFSprms::Iterator_t i = m_pSprms->begin(); i != m_pSprms->end(); ++i)
121 : {
122 0 : if (i->first == nKeyword)
123 : {
124 0 : m_pSprms->erase(i);
125 0 : return true;
126 : }
127 : }
128 0 : return false;
129 : }
130 :
131 0 : void RTFSprms::deduplicate(RTFSprms& rReference)
132 : {
133 0 : ensureCopyBeforeWrite();
134 :
135 0 : RTFSprms::Iterator_t i = m_pSprms->begin();
136 0 : while (i != m_pSprms->end())
137 : {
138 0 : bool bIgnore = false;
139 0 : RTFValue::Pointer_t pValue(rReference.find(i->first));
140 0 : if (pValue.get() && i->second->equals(*pValue))
141 0 : bIgnore = true;
142 0 : if (bIgnore)
143 0 : i = m_pSprms->erase(i);
144 : else
145 0 : ++i;
146 0 : }
147 0 : }
148 :
149 0 : void RTFSprms::ensureCopyBeforeWrite()
150 : {
151 0 : if (m_pSprms->m_nRefCount > 1) {
152 0 : boost::intrusive_ptr<RTFSprmsImpl> pClone(new RTFSprmsImpl());
153 0 : for (std::vector< std::pair<Id, RTFValue::Pointer_t> >::const_iterator i = m_pSprms->begin(); i != m_pSprms->end(); ++i)
154 0 : pClone->push_back(std::make_pair(i->first, RTFValue::Pointer_t(i->second->Clone())));
155 0 : m_pSprms = pClone;
156 : }
157 0 : }
158 :
159 0 : RTFSprms::RTFSprms()
160 0 : : m_pSprms(new RTFSprmsImpl())
161 : {
162 0 : }
163 :
164 0 : RTFSprms::~RTFSprms()
165 : {
166 0 : }
167 :
168 0 : RTFSprms::RTFSprms(const RTFSprms& rSprms)
169 : {
170 0 : *this = rSprms;
171 0 : }
172 :
173 0 : RTFSprms& RTFSprms::operator=(const RTFSprms& rOther)
174 : {
175 0 : m_pSprms = rOther.m_pSprms;
176 0 : return *this;
177 : }
178 :
179 0 : void RTFSprms::clear()
180 : {
181 0 : if (m_pSprms->m_nRefCount == 1)
182 0 : return m_pSprms->clear();
183 : else
184 0 : m_pSprms.reset(new RTFSprmsImpl());
185 : }
186 :
187 : } // namespace rtftok
188 0 : } // namespace writerfilter
189 :
190 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|