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/.
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 .
20 #include <sal/config.h>
22 #include <sal/log.hxx>
23 #include <comphelper/fileformat.h>
24 #include <tools/stream.hxx>
26 #include <svl/macitem.hxx>
27 #include <stringio.hxx>
30 SvxMacro::SvxMacro( const OUString
&rMacName
, const OUString
&rLanguage
)
31 : aMacName( rMacName
), aLibName( rLanguage
),
32 eType( EXTENDED_STYPE
)
34 if ( rLanguage
== SVX_MACRO_LANGUAGE_STARBASIC
)
36 else if ( rLanguage
== SVX_MACRO_LANGUAGE_JAVASCRIPT
)
40 OUString
SvxMacro::GetLanguage()const
44 return OUString(SVX_MACRO_LANGUAGE_STARBASIC
);
46 else if(eType
==JAVASCRIPT
)
48 return OUString(SVX_MACRO_LANGUAGE_JAVASCRIPT
);
50 else if(eType
==EXTENDED_STYPE
)
52 return OUString(SVX_MACRO_LANGUAGE_SF
);
58 SvxMacro
& SvxMacro::operator=( const SvxMacro
& rBase
)
62 aMacName
= rBase
.aMacName
;
63 aLibName
= rBase
.aLibName
;
70 SvxMacroTableDtor
& SvxMacroTableDtor::operator=( const SvxMacroTableDtor
& rTbl
)
74 aSvxMacroTable
.clear();
75 aSvxMacroTable
.insert(rTbl
.aSvxMacroTable
.begin(), rTbl
.aSvxMacroTable
.end());
80 bool SvxMacroTableDtor::operator==( const SvxMacroTableDtor
& rOther
) const
82 // Count different => odd in any case
83 // Compare single ones; the sequence matters due to performance reasons
84 return std::equal(aSvxMacroTable
.begin(), aSvxMacroTable
.end(),
85 rOther
.aSvxMacroTable
.begin(), rOther
.aSvxMacroTable
.end(),
86 [](const SvxMacroTable::value_type
& rOwnEntry
, const SvxMacroTable::value_type
& rOtherEntry
) {
87 const SvxMacro
& rOwnMac
= rOwnEntry
.second
;
88 const SvxMacro
& rOtherMac
= rOtherEntry
.second
;
89 return rOwnEntry
.first
== rOtherEntry
.first
90 && rOwnMac
.GetLibName() == rOtherMac
.GetLibName()
91 && rOwnMac
.GetMacName() == rOtherMac
.GetMacName();
95 void SvxMacroTableDtor::Read( SvStream
& rStrm
)
98 rStrm
.ReadUInt16( nVersion
);
101 rStrm
.ReadInt16(nMacro
);
104 SAL_WARN("editeng", "Parsing error: negative value " << nMacro
);
108 const size_t nMinStringSize
= rStrm
.GetStreamCharSet() == RTL_TEXTENCODING_UNICODE
? 4 : 2;
109 size_t nMinRecordSize
= 2 + 2*nMinStringSize
;
110 if( SVX_MACROTBL_VERSION40
<= nVersion
)
113 const size_t nMaxRecords
= rStrm
.remainingSize() / nMinRecordSize
;
114 if (static_cast<size_t>(nMacro
) > nMaxRecords
)
116 SAL_WARN("editeng", "Parsing error: " << nMaxRecords
<<
117 " max possible entries, but " << nMacro
<< " claimed, truncating");
118 nMacro
= nMaxRecords
;
121 for (short i
= 0; i
< nMacro
; ++i
)
123 sal_uInt16 nCurKey
, eType
= STARBASIC
;
124 OUString aLibName
, aMacName
;
125 rStrm
.ReadUInt16( nCurKey
);
126 aLibName
= readByteString(rStrm
);
127 aMacName
= readByteString(rStrm
);
129 if( SVX_MACROTBL_VERSION40
<= nVersion
)
130 rStrm
.ReadUInt16( eType
);
132 aSvxMacroTable
.emplace( SvMacroItemId(nCurKey
), SvxMacro( aMacName
, aLibName
, static_cast<ScriptType
>(eType
) ) );
137 SvStream
& SvxMacroTableDtor::Write( SvStream
& rStream
) const
139 sal_uInt16 nVersion
= SOFFICE_FILEFORMAT_31
== rStream
.GetVersion()
140 ? SVX_MACROTBL_VERSION31
141 : SVX_MACROTBL_VERSION40
;
143 if( SVX_MACROTBL_VERSION40
<= nVersion
)
144 rStream
.WriteUInt16( nVersion
);
146 rStream
.WriteUInt16( aSvxMacroTable
.size() );
148 for( const auto& rEntry
: aSvxMacroTable
)
150 if (rStream
.GetError() != ERRCODE_NONE
)
152 const SvxMacro
& rMac
= rEntry
.second
;
153 rStream
.WriteUInt16( static_cast<sal_uInt16
>(rEntry
.first
) );
154 writeByteString(rStream
, rMac
.GetLibName());
155 writeByteString(rStream
, rMac
.GetMacName());
157 if( SVX_MACROTBL_VERSION40
<= nVersion
)
158 rStream
.WriteUInt16( rMac
.GetScriptType() );
163 // returns NULL if no entry exists, or a pointer to the internal value
164 const SvxMacro
* SvxMacroTableDtor::Get(SvMacroItemId nEvent
) const
166 SvxMacroTable::const_iterator it
= aSvxMacroTable
.find(nEvent
);
167 return it
== aSvxMacroTable
.end() ? nullptr : &(it
->second
);
170 // returns NULL if no entry exists, or a pointer to the internal value
171 SvxMacro
* SvxMacroTableDtor::Get(SvMacroItemId nEvent
)
173 SvxMacroTable::iterator it
= aSvxMacroTable
.find(nEvent
);
174 return it
== aSvxMacroTable
.end() ? nullptr : &(it
->second
);
177 // return true if the key exists
178 bool SvxMacroTableDtor::IsKeyValid(SvMacroItemId nEvent
) const
180 SvxMacroTable::const_iterator it
= aSvxMacroTable
.find(nEvent
);
181 return it
!= aSvxMacroTable
.end();
184 // This stores a copy of the rMacro parameter
185 SvxMacro
& SvxMacroTableDtor::Insert(SvMacroItemId nEvent
, const SvxMacro
& rMacro
)
187 return aSvxMacroTable
.emplace( nEvent
, rMacro
).first
->second
;
190 // If the entry exists, remove it from the map and release it's storage
191 void SvxMacroTableDtor::Erase(SvMacroItemId nEvent
)
193 SvxMacroTable::iterator it
= aSvxMacroTable
.find(nEvent
);
194 if ( it
!= aSvxMacroTable
.end())
196 aSvxMacroTable
.erase(it
);
201 bool SvxMacroItem::operator==( const SfxPoolItem
& rAttr
) const
203 assert(SfxPoolItem::operator==(rAttr
));
205 const SvxMacroTableDtor
& rOwn
= aMacroTable
;
206 const SvxMacroTableDtor
& rOther
= static_cast<const SvxMacroItem
&>(rAttr
).aMacroTable
;
208 return rOwn
== rOther
;
212 SfxPoolItem
* SvxMacroItem::Clone( SfxItemPool
* ) const
214 return new SvxMacroItem( *this );
218 bool SvxMacroItem::GetPresentation
220 SfxItemPresentation
/*ePres*/,
221 MapUnit
/*eCoreUnit*/,
222 MapUnit
/*ePresUnit*/,
228 SvxMacroTableDtor& rTbl = (SvxMacroTableDtor&)GetMacroTable();
229 SvxMacro* pMac = rTbl.First();
233 rText += pMac->GetLibName();
235 rText += pMac->GetMacName();
246 void SvxMacroItem::SetMacro( SvMacroItemId nEvent
, const SvxMacro
& rMacro
)
248 aMacroTable
.Insert( nEvent
, rMacro
);
251 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */