Avoid potential negative array index access to cached text.
[LibreOffice.git] / cppuhelper / source / macro_expander.cxx
blobce227a7e76f47ca4624b084fb2c9c795e88430cc
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 <rtl/bootstrap.hxx>
23 #include <uno/lbnames.h>
24 #include <uno/mapping.hxx>
26 #include <cppuhelper/factory.hxx>
27 #include <compbase2.hxx>
28 #include <cppuhelper/supportsservice.hxx>
30 #include <com/sun/star/lang/XServiceInfo.hpp>
31 #include <com/sun/star/lang/XSingleComponentFactory.hpp>
32 #include <com/sun/star/util/XMacroExpander.hpp>
34 #include "macro_expander.hxx"
35 #include "paths.hxx"
37 constexpr OUString SERVICE_NAME_A = u"com.sun.star.lang.MacroExpander"_ustr;
38 constexpr OUString SERVICE_NAME_B = u"com.sun.star.lang.BootstrapMacroExpander"_ustr;
39 constexpr OUStringLiteral IMPL_NAME = u"com.sun.star.lang.comp.cppuhelper.BootstrapMacroExpander";
41 using namespace ::osl;
42 using namespace ::com::sun::star;
43 using namespace ::com::sun::star::uno;
45 using rtl::Bootstrap;
47 namespace cppu
50 static Bootstrap const & get_unorc()
52 static rtlBootstrapHandle s_bstrap = rtl_bootstrap_args_open(getUnoIniUri().pData);
53 return *reinterpret_cast<Bootstrap const *>(&s_bstrap);
58 namespace cppuhelper::detail {
60 OUString expandMacros(OUString const & text) {
61 OUString t(text);
62 rtl_bootstrap_expandMacros_from_handle(
63 cppu::get_unorc().getHandle(), &t.pData);
64 return t;
69 namespace
72 OUString s_impl_name()
74 return IMPL_NAME;
77 Sequence< OUString > const & s_get_service_names()
79 static const Sequence< OUString > IMPL_NAMES {
80 SERVICE_NAME_A,
81 SERVICE_NAME_B
83 return IMPL_NAMES;
86 typedef cppuhelper::WeakComponentImplHelper2<
87 util::XMacroExpander, lang::XServiceInfo > t_uno_impl;
89 class Bootstrap_MacroExpander : public t_uno_impl
91 public:
92 Bootstrap_MacroExpander()
95 // XMacroExpander impl
96 virtual OUString SAL_CALL expandMacros( OUString const & exp ) override;
97 // XServiceInfo impl
98 virtual OUString SAL_CALL getImplementationName() override;
99 virtual sal_Bool SAL_CALL supportsService( OUString const & serviceName ) override;
100 virtual Sequence< OUString > SAL_CALL getSupportedServiceNames() override;
104 // XServiceInfo impl
106 OUString Bootstrap_MacroExpander::getImplementationName()
108 return s_impl_name();
111 sal_Bool Bootstrap_MacroExpander::supportsService( OUString const & serviceName )
113 return cppu::supportsService(this, serviceName);
116 Sequence< OUString > Bootstrap_MacroExpander::getSupportedServiceNames()
118 return s_get_service_names();
121 // XMacroExpander impl
123 OUString Bootstrap_MacroExpander::expandMacros( OUString const & exp )
125 return cppuhelper::detail::expandMacros( exp );
129 Reference< XInterface > service_create(
130 SAL_UNUSED_PARAMETER Reference< XComponentContext > const & )
132 return static_cast< ::cppu::OWeakObject * >( new Bootstrap_MacroExpander );
137 namespace cppuhelper::detail {
139 Reference< lang::XSingleComponentFactory > create_bootstrap_macro_expander_factory()
141 Reference< lang::XSingleComponentFactory > free(::cppu::createSingleComponentFactory(
142 service_create,
143 s_impl_name(),
144 s_get_service_names() ));
146 uno::Environment curr_env(Environment::getCurrent());
147 uno::Environment target_env(CPPU_CURRENT_LANGUAGE_BINDING_NAME);
149 uno::Mapping target2curr(target_env, curr_env);
151 return Reference<lang::XSingleComponentFactory>(
152 static_cast<lang::XSingleComponentFactory *>(
153 target2curr.mapInterface(free.get(), cppu::UnoType<decltype(free)>::get())),
154 SAL_NO_ACQUIRE);
159 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */