LibreOffice Module svl (master) 1
macitem.cxx
Go to the documentation of this file.
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 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include <sal/config.h>
21
22#include <o3tl/safeint.hxx>
23#include <sal/log.hxx>
25#include <tools/stream.hxx>
26
27#include <svl/macitem.hxx>
28#include <stringio.hxx>
29#include <algorithm>
30#include <utility>
31
32SvxMacro::SvxMacro( OUString _aMacName, const OUString &rLanguage)
33 : aMacName(std::move( _aMacName )), aLibName( rLanguage),
35{
36 if ( rLanguage == SVX_MACRO_LANGUAGE_STARBASIC )
38 else if ( rLanguage == SVX_MACRO_LANGUAGE_JAVASCRIPT )
40}
41
42OUString SvxMacro::GetLanguage()const
43{
44 if(eType==STARBASIC)
45 {
47 }
48 else if(eType==JAVASCRIPT)
49 {
51 }
52 else if(eType==EXTENDED_STYPE)
53 {
55
56 }
57 return aLibName;
58}
59
61{
62 if (this != &rTbl)
63 {
64 aSvxMacroTable.clear();
65 aSvxMacroTable.insert(rTbl.aSvxMacroTable.begin(), rTbl.aSvxMacroTable.end());
66 }
67 return *this;
68}
69
71{
72 // Count different => odd in any case
73 // Compare single ones; the sequence matters due to performance reasons
74 return std::equal(aSvxMacroTable.begin(), aSvxMacroTable.end(),
75 rOther.aSvxMacroTable.begin(), rOther.aSvxMacroTable.end(),
76 [](const SvxMacroTable::value_type& rOwnEntry, const SvxMacroTable::value_type& rOtherEntry) {
77 const SvxMacro& rOwnMac = rOwnEntry.second;
78 const SvxMacro& rOtherMac = rOtherEntry.second;
79 return rOwnEntry.first == rOtherEntry.first
80 && rOwnMac.GetLibName() == rOtherMac.GetLibName()
81 && rOwnMac.GetMacName() == rOtherMac.GetMacName();
82 });
83}
84
86{
87 sal_uInt16 nVersion;
89
90 short nReadMacro(0);
91 rStrm.ReadInt16(nReadMacro);
92 if (nReadMacro < 0)
93 {
94 SAL_WARN("editeng", "Parsing error: negative value " << nReadMacro);
95 return;
96 }
97
98 auto nMacro = o3tl::make_unsigned(nReadMacro);
99
100 const size_t nMinStringSize = rStrm.GetStreamCharSet() == RTL_TEXTENCODING_UNICODE ? 4 : 2;
101 size_t nMinRecordSize = 2 + 2*nMinStringSize;
103 nMinRecordSize+=2;
104
105 const size_t nMaxRecords = rStrm.remainingSize() / nMinRecordSize;
106 if (nMacro > nMaxRecords)
107 {
108 SAL_WARN("editeng", "Parsing error: " << nMaxRecords <<
109 " max possible entries, but " << nMacro<< " claimed, truncating");
110 nMacro = nMaxRecords;
111 }
112
113 for (decltype(nMacro) i = 0; i < nMacro; ++i)
114 {
115 sal_uInt16 nCurKey, eType = STARBASIC;
116 OUString aLibName, aMacName;
117 rStrm.ReadUInt16( nCurKey );
118 aLibName = readByteString(rStrm);
119 aMacName = readByteString(rStrm);
120
123
124 aSvxMacroTable.emplace( SvMacroItemId(nCurKey), SvxMacro( aMacName, aLibName, static_cast<ScriptType>(eType) ) );
125 }
126}
127
129{
130 sal_uInt16 nVersion = SOFFICE_FILEFORMAT_31 == rStream.GetVersion()
133
135 rStream.WriteUInt16( nVersion );
136
137 rStream.WriteUInt16( aSvxMacroTable.size() );
138
139 for( const auto& rEntry : aSvxMacroTable )
140 {
141 if (rStream.GetError() != ERRCODE_NONE)
142 break;
143 const SvxMacro& rMac = rEntry.second;
144 rStream.WriteUInt16( static_cast<sal_uInt16>(rEntry.first) );
145 writeByteString(rStream, rMac.GetLibName());
146 writeByteString(rStream, rMac.GetMacName());
147
149 rStream.WriteUInt16( rMac.GetScriptType() );
150 }
151 return rStream;
152}
153
154// returns NULL if no entry exists, or a pointer to the internal value
156{
157 SvxMacroTable::const_iterator it = aSvxMacroTable.find(nEvent);
158 return it == aSvxMacroTable.end() ? nullptr : &(it->second);
159}
160
161// returns NULL if no entry exists, or a pointer to the internal value
163{
164 SvxMacroTable::iterator it = aSvxMacroTable.find(nEvent);
165 return it == aSvxMacroTable.end() ? nullptr : &(it->second);
166}
167
168// return true if the key exists
170{
171 SvxMacroTable::const_iterator it = aSvxMacroTable.find(nEvent);
172 return it != aSvxMacroTable.end();
173}
174
175// This stores a copy of the rMacro parameter
177{
178 return aSvxMacroTable.emplace( nEvent, rMacro ).first->second;
179}
180
181// If the entry exists, remove it from the map and release it's storage
183{
184 SvxMacroTable::iterator it = aSvxMacroTable.find(nEvent);
185 if ( it != aSvxMacroTable.end())
186 {
187 aSvxMacroTable.erase(it);
188 }
189}
190
191bool SvxMacroItem::operator==( const SfxPoolItem& rAttr ) const
192{
193 assert(SfxPoolItem::operator==(rAttr));
194
195 const SvxMacroTableDtor& rOwn = aMacroTable;
196 const SvxMacroTableDtor& rOther = static_cast<const SvxMacroItem&>(rAttr).aMacroTable;
197
198 return rOwn == rOther;
199}
200
202{
203 return new SvxMacroItem( *this );
204}
205
207(
208 SfxItemPresentation /*ePres*/,
209 MapUnit /*eCoreUnit*/,
210 MapUnit /*ePresUnit*/,
211 OUString& rText,
212 const IntlWrapper&
213) const
214{
229 rText.clear();
230 return false;
231}
232
233
234void SvxMacroItem::SetMacro( SvMacroItemId nEvent, const SvxMacro& rMacro )
235{
236 aMacroTable.Insert( nEvent, rMacro);
237}
238
239/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Base class for providers of defaults of SfxPoolItems.
Definition: itempool.hxx:51
SvStream & ReadInt16(sal_Int16 &rInt16)
SvStream & WriteUInt16(sal_uInt16 nUInt16)
sal_Int32 GetVersion() const
rtl_TextEncoding GetStreamCharSet() const
ErrCode GetError() const
SvStream & ReadUInt16(sal_uInt16 &rUInt16)
sal_uInt64 remainingSize()
SvxMacroTableDtor aMacroTable
Definition: macitem.hxx:132
virtual bool GetPresentation(SfxItemPresentation ePres, MapUnit eCoreMetric, MapUnit ePresMetric, OUString &rText, const IntlWrapper &) const override
This virtual method allows to get a textual representation of the value for the SfxPoolItem subclasse...
Definition: macitem.cxx:207
virtual SvxMacroItem * Clone(SfxItemPool *pPool=nullptr) const override
Definition: macitem.cxx:201
virtual bool operator==(const SfxPoolItem &) const override
Definition: macitem.cxx:191
SvxMacroItem(const sal_uInt16 nId)
Definition: macitem.hxx:137
void SetMacro(SvMacroItemId nEvent, const SvxMacro &)
Definition: macitem.cxx:234
SvxMacroTable aSvxMacroTable
Definition: macitem.hxx:80
void Erase(SvMacroItemId nEvent)
Definition: macitem.cxx:182
SvxMacro & Insert(SvMacroItemId nEvent, const SvxMacro &rMacro)
Definition: macitem.cxx:176
void Read(SvStream &)
Definition: macitem.cxx:85
SvStream & Write(SvStream &) const
Definition: macitem.cxx:128
bool IsKeyValid(SvMacroItemId nEvent) const
Definition: macitem.cxx:169
bool operator==(const SvxMacroTableDtor &rOther) const
Definition: macitem.cxx:70
SvxMacroTableDtor & operator=(const SvxMacroTableDtor &rCpy)
Definition: macitem.cxx:60
const SvxMacro * Get(SvMacroItemId nEvent) const
Definition: macitem.cxx:155
SvxMacro(OUString aMacName, const OUString &rLanguage)
Definition: macitem.cxx:32
OUString aLibName
Definition: macitem.hxx:47
ScriptType eType
Definition: macitem.hxx:48
const OUString & GetMacName() const
Definition: macitem.hxx:58
OUString GetLanguage() const
Definition: macitem.cxx:42
ScriptType GetScriptType() const
Definition: macitem.hxx:61
const OUString & GetLibName() const
Definition: macitem.hxx:57
#define ERRCODE_NONE
SvMacroItemId
sal_Int16 nVersion
#define SOFFICE_FILEFORMAT_31
DocumentType eType
#define SAL_WARN(area, stream)
ScriptType
Definition: macitem.hxx:38
@ EXTENDED_STYPE
Definition: macitem.hxx:41
@ JAVASCRIPT
Definition: macitem.hxx:40
@ STARBASIC
Definition: macitem.hxx:39
constexpr OUStringLiteral SVX_MACRO_LANGUAGE_JAVASCRIPT
Definition: macitem.hxx:33
constexpr OUStringLiteral SVX_MACRO_LANGUAGE_STARBASIC
Definition: macitem.hxx:34
#define SVX_MACROTBL_VERSION31
Definition: macitem.hxx:74
#define SVX_MACROTBL_VERSION40
Definition: macitem.hxx:75
constexpr OUStringLiteral SVX_MACRO_LANGUAGE_SF
Definition: macitem.hxx:35
MapUnit
int i
void SvStream & rStrm
constexpr std::enable_if_t< std::is_signed_v< T >, std::make_unsigned_t< T > > make_unsigned(T value)
SfxItemPresentation
Definition: poolitem.hxx:72
OUString readByteString(SvStream &rStream)
Read in a Unicode string from a streamed byte string representation.
Definition: stringio.cxx:24
void writeByteString(SvStream &rStream, std::u16string_view rString)
Write a byte string representation of a Unicode string into a stream.
Definition: stringio.cxx:29