Avoid potential negative array index access to cached text.
[LibreOffice.git] / toolkit / source / helper / unopropertyarrayhelper.cxx
blobbc0f996d3d37bb0da408e253df75f6829880d891
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/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include <helper/property.hxx>
22 #include <map>
24 #include <helper/unopropertyarrayhelper.hxx>
28 UnoPropertyArrayHelper::UnoPropertyArrayHelper( const css::uno::Sequence<sal_Int32>& rIDs )
30 for ( const sal_Int32 nID : rIDs )
31 maIDs.insert( nID );
34 UnoPropertyArrayHelper::UnoPropertyArrayHelper( const std::vector< sal_uInt16 > &rIDs )
36 for (const auto& rId : rIDs)
37 maIDs.insert( rId );
40 bool UnoPropertyArrayHelper::ImplHasProperty( sal_uInt16 nPropId ) const
42 if ( ( nPropId >= BASEPROPERTY_FONTDESCRIPTORPART_START ) && ( nPropId <= BASEPROPERTY_FONTDESCRIPTORPART_END ) )
43 nPropId = BASEPROPERTY_FONTDESCRIPTOR;
45 return maIDs.find( nPropId ) != maIDs.end();
48 // ::cppu::IPropertyArrayHelper
49 sal_Bool UnoPropertyArrayHelper::fillPropertyMembersByHandle( OUString * pPropName, sal_Int16 * pAttributes, sal_Int32 nPropId )
51 sal_uInt16 id = sal::static_int_cast< sal_uInt16 >(nPropId);
52 bool bValid = ImplHasProperty( id );
53 if ( bValid )
55 if ( pPropName )
56 *pPropName = GetPropertyName( id );
57 if ( pAttributes )
58 *pAttributes = GetPropertyAttribs( id );
60 return bValid;
63 css::uno::Sequence< css::beans::Property > UnoPropertyArrayHelper::getProperties()
65 // Sort by names ...
67 std::map<OUString, sal_uInt16> aSortedPropsIds;
68 for (const auto& rId : maIDs)
70 sal_uInt16 nId = sal::static_int_cast< sal_uInt16 >(rId);
71 aSortedPropsIds.emplace(GetPropertyName( nId ), nId);
73 if ( nId == BASEPROPERTY_FONTDESCRIPTOR )
75 // single properties ...
76 for ( sal_uInt16 i = BASEPROPERTY_FONTDESCRIPTORPART_START; i <= BASEPROPERTY_FONTDESCRIPTORPART_END; i++ )
77 aSortedPropsIds.emplace(GetPropertyName( i ), i);
81 sal_uInt32 nProps = aSortedPropsIds.size(); // could be more now
82 css::uno::Sequence< css::beans::Property> aProps( nProps );
83 css::beans::Property* pProps = aProps.getArray();
85 sal_uInt32 n = 0;
86 for ( const auto& rPropIds : aSortedPropsIds )
88 sal_uInt16 nId = rPropIds.second;
89 pProps[n].Name = rPropIds.first;
90 pProps[n].Handle = nId;
91 pProps[n].Type = *GetPropertyType( nId );
92 pProps[n].Attributes = GetPropertyAttribs( nId );
93 ++n;
96 return aProps;
99 css::beans::Property UnoPropertyArrayHelper::getPropertyByName(const OUString& rPropertyName)
101 css::beans::Property aProp;
102 sal_uInt16 nId = GetPropertyId( rPropertyName );
103 if ( ImplHasProperty( nId ) )
105 aProp.Name = rPropertyName;
106 aProp.Handle = -1;
107 aProp.Type = *GetPropertyType( nId );
108 aProp.Attributes = GetPropertyAttribs( nId );
111 return aProp;
114 sal_Bool UnoPropertyArrayHelper::hasPropertyByName(const OUString& rPropertyName)
116 return ImplHasProperty( GetPropertyId( rPropertyName ) );
119 sal_Int32 UnoPropertyArrayHelper::getHandleByName( const OUString & rPropertyName )
121 sal_Int32 nId = static_cast<sal_Int32>(GetPropertyId( rPropertyName ));
122 return nId ? nId : -1;
125 sal_Int32 UnoPropertyArrayHelper::fillHandles( sal_Int32* pHandles, const css::uno::Sequence< OUString > & rPropNames )
127 const OUString* pNames = rPropNames.getConstArray();
128 sal_Int32 nValues = rPropNames.getLength();
129 sal_Int32 nValidHandles = 0;
131 for ( sal_Int32 n = 0; n < nValues; n++ )
133 sal_uInt16 nPropId = GetPropertyId( pNames[n] );
134 if ( nPropId && ImplHasProperty( nPropId ) )
136 pHandles[n] = nPropId;
137 nValidHandles++;
139 else
141 pHandles[n] = -1;
144 return nValidHandles;
148 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */