xref: /aosp_15_r20/external/parameter-framework/upstream/parameter/FormattedSubsystemObject.cpp (revision c33452fb792a5495ec310a9626f2638b053af5dd)
1*c33452fbSAndroid Build Coastguard Worker /*
2*c33452fbSAndroid Build Coastguard Worker  * Copyright (c) 2011-2015, Intel Corporation
3*c33452fbSAndroid Build Coastguard Worker  * All rights reserved.
4*c33452fbSAndroid Build Coastguard Worker  *
5*c33452fbSAndroid Build Coastguard Worker  * Redistribution and use in source and binary forms, with or without modification,
6*c33452fbSAndroid Build Coastguard Worker  * are permitted provided that the following conditions are met:
7*c33452fbSAndroid Build Coastguard Worker  *
8*c33452fbSAndroid Build Coastguard Worker  * 1. Redistributions of source code must retain the above copyright notice, this
9*c33452fbSAndroid Build Coastguard Worker  * list of conditions and the following disclaimer.
10*c33452fbSAndroid Build Coastguard Worker  *
11*c33452fbSAndroid Build Coastguard Worker  * 2. Redistributions in binary form must reproduce the above copyright notice,
12*c33452fbSAndroid Build Coastguard Worker  * this list of conditions and the following disclaimer in the documentation and/or
13*c33452fbSAndroid Build Coastguard Worker  * other materials provided with the distribution.
14*c33452fbSAndroid Build Coastguard Worker  *
15*c33452fbSAndroid Build Coastguard Worker  * 3. Neither the name of the copyright holder nor the names of its contributors
16*c33452fbSAndroid Build Coastguard Worker  * may be used to endorse or promote products derived from this software without
17*c33452fbSAndroid Build Coastguard Worker  * specific prior written permission.
18*c33452fbSAndroid Build Coastguard Worker  *
19*c33452fbSAndroid Build Coastguard Worker  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20*c33452fbSAndroid Build Coastguard Worker  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21*c33452fbSAndroid Build Coastguard Worker  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22*c33452fbSAndroid Build Coastguard Worker  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23*c33452fbSAndroid Build Coastguard Worker  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24*c33452fbSAndroid Build Coastguard Worker  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25*c33452fbSAndroid Build Coastguard Worker  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26*c33452fbSAndroid Build Coastguard Worker  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27*c33452fbSAndroid Build Coastguard Worker  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28*c33452fbSAndroid Build Coastguard Worker  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29*c33452fbSAndroid Build Coastguard Worker  */
30*c33452fbSAndroid Build Coastguard Worker 
31*c33452fbSAndroid Build Coastguard Worker #include "FormattedSubsystemObject.h"
32*c33452fbSAndroid Build Coastguard Worker #include "InstanceConfigurableElement.h"
33*c33452fbSAndroid Build Coastguard Worker #include "MappingContext.h"
34*c33452fbSAndroid Build Coastguard Worker #include <assert.h>
35*c33452fbSAndroid Build Coastguard Worker 
36*c33452fbSAndroid Build Coastguard Worker #define base CSubsystemObject
37*c33452fbSAndroid Build Coastguard Worker 
38*c33452fbSAndroid Build Coastguard Worker using std::string;
39*c33452fbSAndroid Build Coastguard Worker 
CFormattedSubsystemObject(CInstanceConfigurableElement * pInstanceConfigurableElement,core::log::Logger & logger)40*c33452fbSAndroid Build Coastguard Worker CFormattedSubsystemObject::CFormattedSubsystemObject(
41*c33452fbSAndroid Build Coastguard Worker     CInstanceConfigurableElement *pInstanceConfigurableElement, core::log::Logger &logger)
42*c33452fbSAndroid Build Coastguard Worker     : base(pInstanceConfigurableElement, logger)
43*c33452fbSAndroid Build Coastguard Worker {
44*c33452fbSAndroid Build Coastguard Worker }
45*c33452fbSAndroid Build Coastguard Worker 
CFormattedSubsystemObject(CInstanceConfigurableElement * pInstanceConfigurableElement,core::log::Logger & logger,const string & strMappingValue)46*c33452fbSAndroid Build Coastguard Worker CFormattedSubsystemObject::CFormattedSubsystemObject(
47*c33452fbSAndroid Build Coastguard Worker     CInstanceConfigurableElement *pInstanceConfigurableElement, core::log::Logger &logger,
48*c33452fbSAndroid Build Coastguard Worker     const string &strMappingValue)
49*c33452fbSAndroid Build Coastguard Worker     : base(pInstanceConfigurableElement, logger), _strFormattedMappingValue(strMappingValue)
50*c33452fbSAndroid Build Coastguard Worker {
51*c33452fbSAndroid Build Coastguard Worker }
52*c33452fbSAndroid Build Coastguard Worker 
CFormattedSubsystemObject(CInstanceConfigurableElement * pInstanceConfigurableElement,core::log::Logger & logger,const string & strMappingValue,size_t firstAmendKey,size_t nbAmendKeys,const CMappingContext & context)53*c33452fbSAndroid Build Coastguard Worker CFormattedSubsystemObject::CFormattedSubsystemObject(
54*c33452fbSAndroid Build Coastguard Worker     CInstanceConfigurableElement *pInstanceConfigurableElement, core::log::Logger &logger,
55*c33452fbSAndroid Build Coastguard Worker     const string &strMappingValue, size_t firstAmendKey, size_t nbAmendKeys,
56*c33452fbSAndroid Build Coastguard Worker     const CMappingContext &context)
57*c33452fbSAndroid Build Coastguard Worker     : base(pInstanceConfigurableElement, logger), _strFormattedMappingValue(strMappingValue)
58*c33452fbSAndroid Build Coastguard Worker {
59*c33452fbSAndroid Build Coastguard Worker     // Cope with quotes in the name
60*c33452fbSAndroid Build Coastguard Worker     if (strMappingValue[0] == '\'' && strMappingValue.length() >= 2) {
61*c33452fbSAndroid Build Coastguard Worker 
62*c33452fbSAndroid Build Coastguard Worker         _strFormattedMappingValue = strMappingValue.substr(1, strMappingValue.length() - 2);
63*c33452fbSAndroid Build Coastguard Worker     }
64*c33452fbSAndroid Build Coastguard Worker     _strFormattedMappingValue =
65*c33452fbSAndroid Build Coastguard Worker         formatMappingValue(_strFormattedMappingValue, firstAmendKey, nbAmendKeys, context);
66*c33452fbSAndroid Build Coastguard Worker }
67*c33452fbSAndroid Build Coastguard Worker 
getFormattedMappingValue() const68*c33452fbSAndroid Build Coastguard Worker string CFormattedSubsystemObject::getFormattedMappingValue() const
69*c33452fbSAndroid Build Coastguard Worker {
70*c33452fbSAndroid Build Coastguard Worker     return _strFormattedMappingValue;
71*c33452fbSAndroid Build Coastguard Worker }
72*c33452fbSAndroid Build Coastguard Worker 
isAmendKeyValid(size_t uiAmendKey)73*c33452fbSAndroid Build Coastguard Worker bool CFormattedSubsystemObject::isAmendKeyValid(size_t uiAmendKey)
74*c33452fbSAndroid Build Coastguard Worker {
75*c33452fbSAndroid Build Coastguard Worker 
76*c33452fbSAndroid Build Coastguard Worker     return (uiAmendKey > 0) && (uiAmendKey <= 9);
77*c33452fbSAndroid Build Coastguard Worker }
78*c33452fbSAndroid Build Coastguard Worker 
formatMappingValue(const string & strMappingValue,size_t firstAmendKey,size_t nbAmendKeys,const CMappingContext & context)79*c33452fbSAndroid Build Coastguard Worker string CFormattedSubsystemObject::formatMappingValue(const string &strMappingValue,
80*c33452fbSAndroid Build Coastguard Worker                                                      size_t firstAmendKey, size_t nbAmendKeys,
81*c33452fbSAndroid Build Coastguard Worker                                                      const CMappingContext &context)
82*c33452fbSAndroid Build Coastguard Worker {
83*c33452fbSAndroid Build Coastguard Worker     string strFormattedValue = strMappingValue;
84*c33452fbSAndroid Build Coastguard Worker 
85*c33452fbSAndroid Build Coastguard Worker     // Search for amendment (only one supported for now)
86*c33452fbSAndroid Build Coastguard Worker     string::size_type uiPercentPos = strFormattedValue.find('%', 0);
87*c33452fbSAndroid Build Coastguard Worker 
88*c33452fbSAndroid Build Coastguard Worker     // Amendment limited to one digit (values from 1 to 9)
89*c33452fbSAndroid Build Coastguard Worker     assert(isAmendKeyValid(nbAmendKeys));
90*c33452fbSAndroid Build Coastguard Worker 
91*c33452fbSAndroid Build Coastguard Worker     // Check we found one and that there's room for value
92*c33452fbSAndroid Build Coastguard Worker     if (uiPercentPos != string::npos && uiPercentPos < strFormattedValue.size() - 1) {
93*c33452fbSAndroid Build Coastguard Worker 
94*c33452fbSAndroid Build Coastguard Worker         // Get Amend number
95*c33452fbSAndroid Build Coastguard Worker         size_t uiAmendNumber = strFormattedValue[uiPercentPos + 1] - '0';
96*c33452fbSAndroid Build Coastguard Worker 
97*c33452fbSAndroid Build Coastguard Worker         // Check if current Amend number is Valid
98*c33452fbSAndroid Build Coastguard Worker         if ((uiAmendNumber > 0) && (uiAmendNumber <= nbAmendKeys)) {
99*c33452fbSAndroid Build Coastguard Worker 
100*c33452fbSAndroid Build Coastguard Worker             size_t uiAmendType = firstAmendKey + uiAmendNumber - 1;
101*c33452fbSAndroid Build Coastguard Worker 
102*c33452fbSAndroid Build Coastguard Worker             // Check if current Amend type is Set in the context
103*c33452fbSAndroid Build Coastguard Worker             if (context.iSet(uiAmendType)) {
104*c33452fbSAndroid Build Coastguard Worker 
105*c33452fbSAndroid Build Coastguard Worker                 // Make the amendment on the part of the string after the current Amend
106*c33452fbSAndroid Build Coastguard Worker                 string strEndOfLine = strFormattedValue.substr(
107*c33452fbSAndroid Build Coastguard Worker                     uiPercentPos + 2, strFormattedValue.size() - uiPercentPos - 2);
108*c33452fbSAndroid Build Coastguard Worker                 string strEndOfLineAmended =
109*c33452fbSAndroid Build Coastguard Worker                     formatMappingValue(strEndOfLine, firstAmendKey, nbAmendKeys, context);
110*c33452fbSAndroid Build Coastguard Worker 
111*c33452fbSAndroid Build Coastguard Worker                 // Get current Amend value
112*c33452fbSAndroid Build Coastguard Worker                 string strAmendValue = context.getItem(uiAmendType);
113*c33452fbSAndroid Build Coastguard Worker 
114*c33452fbSAndroid Build Coastguard Worker                 // Make the amendment
115*c33452fbSAndroid Build Coastguard Worker                 strFormattedValue =
116*c33452fbSAndroid Build Coastguard Worker                     strFormattedValue.substr(0, uiPercentPos) + strAmendValue + strEndOfLineAmended;
117*c33452fbSAndroid Build Coastguard Worker             }
118*c33452fbSAndroid Build Coastguard Worker         }
119*c33452fbSAndroid Build Coastguard Worker     }
120*c33452fbSAndroid Build Coastguard Worker     return strFormattedValue;
121*c33452fbSAndroid Build Coastguard Worker }
122