tdf#154285 Check upper bound of arguments in SbRtl_Minute function
[LibreOffice.git] / stoc / source / implementationregistration / mergekeys.cxx
blob426caa2be9d6cbc5e4fc31104d676213914e27cb
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 <utility>
22 #include <vector>
24 #include <osl/diagnose.h>
26 #include <com/sun/star/registry/XRegistryKey.hpp>
28 #include "mergekeys.hxx"
30 using namespace css::uno;
31 using namespace ::com::sun::star;
33 namespace stoc_impreg
36 namespace {
38 struct Link
40 OUString m_name;
41 OUString m_target;
43 Link( OUString name, OUString target )
44 : m_name(std::move( name ))
45 , m_target(std::move( target ))
51 typedef ::std::vector< Link > t_links;
54 static void mergeKeys(
55 Reference< registry::XRegistryKey > const & xDest,
56 Reference< registry::XRegistryKey > const & xSource,
57 t_links & links )
58 // throw( registry::InvalidRegistryException, registry::MergeConflictException, RuntimeException )
60 if (!xSource.is() || !xSource->isValid()) {
61 throw registry::InvalidRegistryException(
62 u"source key is null or invalid!"_ustr );
64 if (!xDest.is() || !xDest->isValid()) {
65 throw registry::InvalidRegistryException(
66 u"destination key is null or invalid!"_ustr );
69 // write value
70 switch (xSource->getValueType())
72 case registry::RegistryValueType_NOT_DEFINED:
73 break;
74 case registry::RegistryValueType_LONG:
75 xDest->setLongValue( xSource->getLongValue() );
76 break;
77 case registry::RegistryValueType_ASCII:
78 xDest->setAsciiValue( xSource->getAsciiValue() );
79 break;
80 case registry::RegistryValueType_STRING:
81 xDest->setStringValue( xSource->getStringValue() );
82 break;
83 case registry::RegistryValueType_BINARY:
84 xDest->setBinaryValue( xSource->getBinaryValue() );
85 break;
86 case registry::RegistryValueType_LONGLIST:
87 xDest->setLongListValue( xSource->getLongListValue() );
88 break;
89 case registry::RegistryValueType_ASCIILIST:
90 xDest->setAsciiListValue( xSource->getAsciiListValue() );
91 break;
92 case registry::RegistryValueType_STRINGLIST:
93 xDest->setStringListValue( xSource->getStringListValue() );
94 break;
95 default:
96 OSL_ASSERT(false);
97 break;
100 // sub keys
101 Sequence< OUString > sourceKeys( xSource->getKeyNames() );
102 for ( sal_Int32 nPos = sourceKeys.getLength(); nPos--; )
104 // key name
105 OUString name( sourceKeys[ nPos ] );
106 sal_Int32 nSlash = name.lastIndexOf( '/' );
107 if (nSlash >= 0)
109 name = name.copy( nSlash +1 );
112 if (xSource->getKeyType( name ) == registry::RegistryKeyType_KEY)
114 // try to open existing dest key or create new one
115 Reference< registry::XRegistryKey > xDestKey( xDest->createKey( name ) );
116 Reference< registry::XRegistryKey > xSourceKey( xSource->openKey( name ) );
117 mergeKeys( xDestKey, xSourceKey, links );
118 xSourceKey->closeKey();
119 xDestKey->closeKey();
121 else // link
123 // remove existing key
124 Reference< registry::XRegistryKey > xDestKey( xDest->openKey( name ) );
125 if (xDestKey.is() && xDestKey->isValid()) // something to remove
127 xDestKey->closeKey();
128 if (xDest->getKeyType( name ) == registry::RegistryKeyType_LINK)
130 xDest->deleteLink( name );
132 else
134 xDest->deleteKey( name );
138 links.push_back( Link(
139 sourceKeys[ nPos ], // abs path
140 xSource->getResolvedName( name ) // abs resolved name
141 ) );
147 void mergeKeys(
148 Reference< registry::XRegistryKey > const & xDest,
149 Reference< registry::XRegistryKey > const & xSource )
150 // throw( registry::InvalidRegistryException, registry::MergeConflictException, RuntimeException )
152 if (!xDest.is() || !xDest->isValid()) {
153 throw registry::InvalidRegistryException(
154 u"destination key is null or invalid!"_ustr );
156 if (xDest->isReadOnly())
158 throw registry::InvalidRegistryException(
159 u"destination registry is read-only! cannot merge!"_ustr );
162 t_links links;
163 links.reserve( 16 );
164 mergeKeys( xDest, xSource, links );
166 for ( size_t nPos = links.size(); nPos--; )
168 Link const & r = links[ nPos ];
169 OSL_VERIFY( xDest->createLink( r.m_name, r.m_target ) );
175 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */