Avoid potential negative array index access to cached text.
[LibreOffice.git] / writerfilter / source / rtftok / rtfvalue.cxx
blob6654a3f2eca54ee53e324c3cacfb0c4cb526678e
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 "rtfreferenceproperties.hxx"
11 #include "rtfdocumentimpl.hxx"
12 #include <com/sun/star/embed/XEmbeddedObject.hpp>
14 using namespace com::sun::star;
16 namespace writerfilter::rtftok
18 RTFValue::RTFValue(int nValue, OUString sValue, const RTFSprms* pAttributes, const RTFSprms* pSprms,
19 uno::Reference<drawing::XShape> xShape, uno::Reference<io::XInputStream> xStream,
20 uno::Reference<embed::XEmbeddedObject> xObject, bool bForceString,
21 const RTFShape* pShape, const RTFPicture* pPicture)
22 : m_nValue(nValue)
23 , m_sValue(std::move(sValue))
24 , m_xShape(std::move(xShape))
25 , m_xStream(std::move(xStream))
26 , m_xObject(std::move(xObject))
27 , m_bForceString(bForceString)
29 if (pAttributes)
30 m_pAttributes = new RTFSprms(*pAttributes);
31 if (pSprms)
32 m_pSprms = new RTFSprms(*pSprms);
33 if (pShape)
34 m_pShape = new RTFShape(*pShape);
35 if (pPicture)
36 m_pPicture = new RTFPicture(*pPicture);
39 RTFValue::RTFValue() {}
41 RTFValue::RTFValue(int nValue)
42 : m_nValue(nValue)
46 RTFValue::RTFValue(OUString sValue, bool bForce)
47 : m_sValue(std::move(sValue))
48 , m_bForceString(bForce)
52 RTFValue::RTFValue(const RTFSprms& rAttributes)
53 : m_pAttributes(new RTFSprms(rAttributes))
57 RTFValue::RTFValue(const RTFSprms& rAttributes, const RTFSprms& rSprms)
58 : m_pAttributes(new RTFSprms(rAttributes))
59 , m_pSprms(new RTFSprms(rSprms))
63 RTFValue::RTFValue(uno::Reference<drawing::XShape> xShape)
64 : m_xShape(std::move(xShape))
68 RTFValue::RTFValue(uno::Reference<io::XInputStream> xStream)
69 : m_xStream(std::move(xStream))
73 RTFValue::RTFValue(uno::Reference<embed::XEmbeddedObject> xObject)
74 : m_xObject(std::move(xObject))
78 RTFValue::RTFValue(const RTFShape& aShape)
79 : m_pShape(new RTFShape(aShape))
83 RTFValue::RTFValue(const RTFPicture& rPicture)
84 : m_pPicture(new RTFPicture(rPicture))
88 RTFValue::RTFValue(text::GraphicCrop const& rCrop)
89 : m_oCrop(rCrop)
93 RTFValue::~RTFValue() = default;
95 int RTFValue::getInt() const { return m_nValue; }
97 OUString RTFValue::getString() const
99 if (!m_sValue.isEmpty() || m_bForceString)
100 return m_sValue;
102 return OUString::number(m_nValue);
105 void RTFValue::setString(const OUString& sValue) { m_sValue = sValue; }
107 uno::Any RTFValue::getAny() const
109 uno::Any ret;
110 if (!m_sValue.isEmpty() || m_bForceString)
111 ret <<= m_sValue;
112 else if (m_xShape.is())
113 ret <<= m_xShape;
114 else if (m_xStream.is())
115 ret <<= m_xStream;
116 else if (m_xObject.is())
117 ret <<= m_xObject;
118 else if (m_oCrop)
120 ret <<= *m_oCrop;
122 else
123 ret <<= static_cast<sal_Int32>(m_nValue);
124 return ret;
127 RTFShape& RTFValue::getShape() const
129 if (!m_pShape)
130 m_pShape = new RTFShape();
131 return *m_pShape;
134 RTFPicture& RTFValue::getPicture() const
136 if (!m_pPicture)
137 m_pPicture = new RTFPicture;
138 return *m_pPicture;
141 writerfilter::Reference<Properties>::Pointer_t RTFValue::getProperties()
143 return new RTFReferenceProperties(getAttributes(), getSprms());
146 writerfilter::Reference<BinaryObj>::Pointer_t RTFValue::getBinary()
148 return writerfilter::Reference<BinaryObj>::Pointer_t();
151 #ifdef DBG_UTIL
152 std::string RTFValue::toString() const
154 if (!m_sValue.isEmpty() || m_bForceString)
155 return std::string(OUStringToOString(m_sValue, RTL_TEXTENCODING_UTF8));
157 return std::string(OString::number(m_nValue));
159 #endif
161 RTFValue* RTFValue::Clone() const
163 return new RTFValue(m_nValue, m_sValue, m_pAttributes.get(), m_pSprms.get(), m_xShape,
164 m_xStream, m_xObject, m_bForceString, m_pShape.get(), m_pPicture.get());
167 RTFValue* RTFValue::CloneWithSprms(RTFSprms const& rAttributes, RTFSprms const& rSprms) const
169 return new RTFValue(m_nValue, m_sValue, &rAttributes, &rSprms, m_xShape, m_xStream, m_xObject,
170 m_bForceString, m_pShape.get(), m_pPicture.get());
173 bool RTFValue::equals(const RTFValue& rOther) const
175 if (m_nValue != rOther.m_nValue)
176 return false;
177 if (m_sValue != rOther.m_sValue)
178 return false;
180 if (m_pAttributes && rOther.m_pAttributes)
182 if (m_pAttributes->size() != rOther.m_pAttributes->size())
183 return false;
184 if (!m_pAttributes->equals(*rOther.m_pAttributes))
185 return false;
187 else if (m_pAttributes && m_pAttributes->size())
189 return false;
191 else if (rOther.m_pAttributes && rOther.m_pAttributes->size())
193 return false;
196 if (m_pSprms && rOther.m_pSprms)
198 if (m_pSprms->size() != rOther.m_pSprms->size())
199 return false;
200 if (!m_pSprms->equals(*rOther.m_pSprms))
201 return false;
203 else if (m_pSprms && m_pSprms->size())
205 return false;
207 else if (rOther.m_pSprms && rOther.m_pSprms->size())
209 return false;
212 return true;
215 RTFSprms& RTFValue::getAttributes() const
217 if (!m_pAttributes)
218 m_pAttributes = new RTFSprms();
219 return *m_pAttributes;
222 RTFSprms& RTFValue::getSprms() const
224 if (!m_pSprms)
225 m_pSprms = new RTFSprms();
226 return *m_pSprms;
229 } // namespace writerfilter::rtftok
231 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */