Avoid potential negative array index access to cached text.
[LibreOffice.git] / scaddins / source / datefunc / datefunc.hxx
blob0dd6b5fa2f1c9086a97fb72fc4cfb995ea0afbf5
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 .
20 // date functions add in
22 #pragma once
24 #include <vector>
25 #include <memory>
26 #include <com/sun/star/lang/XServiceName.hpp>
27 #include <com/sun/star/lang/XServiceInfo.hpp>
28 #include <com/sun/star/sheet/XAddIn.hpp>
29 #include <com/sun/star/sheet/XCompatibilityNames.hpp>
30 #include <com/sun/star/sheet/addin/XDateFunctions.hpp>
31 #include <com/sun/star/sheet/addin/XMiscFunctions.hpp>
32 #include <cppuhelper/implbase.hxx>
33 #include <unotools/resmgr.hxx>
35 namespace com::sun::star::lang { class XMultiServiceFactory; }
37 enum class ScaCategory
39 DateTime,
40 Text,
41 Finance,
42 Inf,
43 Math,
44 Tech
47 struct ScaFuncDataBase
49 const char* pIntName; // internal name (get***)
50 TranslateId pUINameID; // resource ID to UI name
51 const TranslateId* pDescrID; // resource ID to description, parameter names and ~ description
52 const char** pCompListID; // list of valid names
53 sal_uInt16 nParamCount; // number of named / described parameters
54 ScaCategory eCat; // function category
55 bool bDouble; // name already exist in Calc
56 bool bWithOpt; // first parameter is internal
59 class ScaFuncData final
61 private:
62 OUString aIntName; // internal name (get***)
63 TranslateId pUINameID; // resource ID to UI name
64 const TranslateId* pDescrID; // leads also to parameter descriptions!
65 sal_uInt16 nParamCount; // num of parameters
66 std::vector<OUString> aCompList; // list of all valid names
67 ScaCategory eCat; // function category
68 bool bDouble; // name already exist in Calc
69 bool bWithOpt; // first parameter is internal
71 public:
72 ScaFuncData(const ScaFuncDataBase& rBaseData);
74 const TranslateId & GetUINameID() const { return pUINameID; }
75 const TranslateId* GetDescrID() const { return pDescrID; }
76 ScaCategory GetCategory() const { return eCat; }
77 bool IsDouble() const { return bDouble; }
79 sal_uInt16 GetStrIndex( sal_uInt16 nParam ) const;
80 bool Is( std::u16string_view rCompare ) const
81 { return aIntName == rCompare; }
83 const std::vector<OUString>& GetCompNameList() const { return aCompList; }
86 typedef std::vector<ScaFuncData> ScaFuncDataList;
88 // Predicate for use with std::find_if
89 struct FindScaFuncData
91 const OUString& m_rId;
92 explicit FindScaFuncData( const OUString& rId ) : m_rId(rId) {}
93 bool operator() ( ScaFuncData const & rCandidate ) const { return rCandidate.Is(m_rId); }
97 // THE AddIn class for date functions
99 class ScaDateAddIn : public ::cppu::WeakImplHelper<
100 css::sheet::XAddIn,
101 css::sheet::XCompatibilityNames,
102 css::sheet::addin::XDateFunctions,
103 css::sheet::addin::XMiscFunctions,
104 css::lang::XServiceName,
105 css::lang::XServiceInfo >
107 private:
108 css::lang::Locale aFuncLoc;
109 std::unique_ptr< css::lang::Locale[] > pDefLocales;
110 std::locale aResLocale;
111 std::unique_ptr< ScaFuncDataList > pFuncDataList;
114 void InitDefLocales();
115 const css::lang::Locale& GetLocale( sal_uInt32 nIndex );
116 void InitData();
118 /// @throws css::uno::RuntimeException
119 OUString GetFuncDescrStr(const TranslateId* pResId, sal_uInt16 nStrIndex);
121 public:
122 ScaDateAddIn();
124 OUString ScaResId(TranslateId aId);
126 // XAddIn
127 virtual OUString SAL_CALL getProgrammaticFuntionName( const OUString& aDisplayName ) override;
128 virtual OUString SAL_CALL getDisplayFunctionName( const OUString& aProgrammaticName ) override;
129 virtual OUString SAL_CALL getFunctionDescription( const OUString& aProgrammaticName ) override;
130 virtual OUString SAL_CALL getDisplayArgumentName( const OUString& aProgrammaticName, sal_Int32 nArgument ) override;
131 virtual OUString SAL_CALL getArgumentDescription( const OUString& aProgrammaticName, sal_Int32 nArgument ) override;
132 virtual OUString SAL_CALL getProgrammaticCategoryName( const OUString& aProgrammaticName ) override;
133 virtual OUString SAL_CALL getDisplayCategoryName( const OUString& aProgrammaticName ) override;
135 // XCompatibilityNames
136 virtual css::uno::Sequence< css::sheet::LocalizedName > SAL_CALL getCompatibilityNames( const OUString& aProgrammaticName ) override;
138 // XLocalizable
139 virtual void SAL_CALL setLocale( const css::lang::Locale& eLocale ) override;
140 virtual css::lang::Locale SAL_CALL getLocale() override;
142 // XServiceName
143 virtual OUString SAL_CALL getServiceName() override;
145 // XServiceInfo
146 virtual OUString SAL_CALL getImplementationName() override;
147 virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName ) override;
148 virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override;
150 // methods from own interfaces start here
152 // XDateFunctions
153 virtual sal_Int32 SAL_CALL getDiffWeeks(
154 const css::uno::Reference< css::beans::XPropertySet >& xOptions,
155 sal_Int32 nEndDate, sal_Int32 nStartDate,
156 sal_Int32 nMode ) override;
158 virtual sal_Int32 SAL_CALL getDiffMonths(
159 const css::uno::Reference< css::beans::XPropertySet >& xOptions,
160 sal_Int32 nEndDate, sal_Int32 nStartDate,
161 sal_Int32 nMode ) override;
163 virtual sal_Int32 SAL_CALL getDiffYears(
164 const css::uno::Reference< css::beans::XPropertySet >& xOptions,
165 sal_Int32 nEndDate, sal_Int32 nStartDate,
166 sal_Int32 nMode ) override;
168 virtual sal_Int32 SAL_CALL getIsLeapYear(
169 const css::uno::Reference< css::beans::XPropertySet >& xOptions,
170 sal_Int32 nDate ) override;
172 virtual sal_Int32 SAL_CALL getDaysInMonth(
173 const css::uno::Reference< css::beans::XPropertySet >& xOptions,
174 sal_Int32 nDate ) override;
176 virtual sal_Int32 SAL_CALL getDaysInYear(
177 const css::uno::Reference< css::beans::XPropertySet >& xOptions,
178 sal_Int32 nDate ) override;
180 virtual sal_Int32 SAL_CALL getWeeksInYear(
181 const css::uno::Reference< css::beans::XPropertySet >& xOptions,
182 sal_Int32 nDate ) override;
184 // XMiscFunctions
185 virtual OUString SAL_CALL getRot13(
186 const OUString& aSrcText ) override;
189 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */