xref: /aosp_15_r20/external/parameter-framework/upstream/parameter/MappingData.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 #include "MappingData.h"
31*c33452fbSAndroid Build Coastguard Worker #include "Tokenizer.h"
32*c33452fbSAndroid Build Coastguard Worker #include "Utility.h"
33*c33452fbSAndroid Build Coastguard Worker #include <assert.h>
34*c33452fbSAndroid Build Coastguard Worker 
init(const std::string & rawMapping,std::string & error)35*c33452fbSAndroid Build Coastguard Worker bool CMappingData::init(const std::string &rawMapping, std::string &error)
36*c33452fbSAndroid Build Coastguard Worker {
37*c33452fbSAndroid Build Coastguard Worker     Tokenizer mappingTok(rawMapping, ",");
38*c33452fbSAndroid Build Coastguard Worker 
39*c33452fbSAndroid Build Coastguard Worker     std::string strMappingElement;
40*c33452fbSAndroid Build Coastguard Worker 
41*c33452fbSAndroid Build Coastguard Worker     for (const auto &strMappingElement : mappingTok.split()) {
42*c33452fbSAndroid Build Coastguard Worker 
43*c33452fbSAndroid Build Coastguard Worker         std::string::size_type iFistDelimiterOccurrence = strMappingElement.find_first_of(':');
44*c33452fbSAndroid Build Coastguard Worker 
45*c33452fbSAndroid Build Coastguard Worker         std::string strKey, strValue;
46*c33452fbSAndroid Build Coastguard Worker 
47*c33452fbSAndroid Build Coastguard Worker         if (iFistDelimiterOccurrence == std::string::npos) {
48*c33452fbSAndroid Build Coastguard Worker 
49*c33452fbSAndroid Build Coastguard Worker             // There is no delimiter in the mapping field,
50*c33452fbSAndroid Build Coastguard Worker             // it means that no value has been provided
51*c33452fbSAndroid Build Coastguard Worker             strKey = strMappingElement;
52*c33452fbSAndroid Build Coastguard Worker             strValue = "";
53*c33452fbSAndroid Build Coastguard Worker 
54*c33452fbSAndroid Build Coastguard Worker         } else {
55*c33452fbSAndroid Build Coastguard Worker 
56*c33452fbSAndroid Build Coastguard Worker             // Get mapping key
57*c33452fbSAndroid Build Coastguard Worker             strKey = strMappingElement.substr(0, iFistDelimiterOccurrence);
58*c33452fbSAndroid Build Coastguard Worker 
59*c33452fbSAndroid Build Coastguard Worker             // Get mapping value
60*c33452fbSAndroid Build Coastguard Worker             strValue = strMappingElement.substr(iFistDelimiterOccurrence + 1);
61*c33452fbSAndroid Build Coastguard Worker         }
62*c33452fbSAndroid Build Coastguard Worker 
63*c33452fbSAndroid Build Coastguard Worker         if (!addValue(strKey, strValue)) {
64*c33452fbSAndroid Build Coastguard Worker 
65*c33452fbSAndroid Build Coastguard Worker             error = "Unable to process Mapping element key = " + strKey + ", value = " + strValue +
66*c33452fbSAndroid Build Coastguard Worker                     ": Duplicate Mapping data";
67*c33452fbSAndroid Build Coastguard Worker 
68*c33452fbSAndroid Build Coastguard Worker             return false;
69*c33452fbSAndroid Build Coastguard Worker         }
70*c33452fbSAndroid Build Coastguard Worker     }
71*c33452fbSAndroid Build Coastguard Worker     return true;
72*c33452fbSAndroid Build Coastguard Worker }
73*c33452fbSAndroid Build Coastguard Worker 
getValue(const std::string & strkey,const std::string * & pStrValue) const74*c33452fbSAndroid Build Coastguard Worker bool CMappingData::getValue(const std::string &strkey, const std::string *&pStrValue) const
75*c33452fbSAndroid Build Coastguard Worker {
76*c33452fbSAndroid Build Coastguard Worker     auto it = _keyToValueMap.find(strkey);
77*c33452fbSAndroid Build Coastguard Worker 
78*c33452fbSAndroid Build Coastguard Worker     if (it != _keyToValueMap.end()) {
79*c33452fbSAndroid Build Coastguard Worker 
80*c33452fbSAndroid Build Coastguard Worker         pStrValue = &it->second;
81*c33452fbSAndroid Build Coastguard Worker 
82*c33452fbSAndroid Build Coastguard Worker         return true;
83*c33452fbSAndroid Build Coastguard Worker     }
84*c33452fbSAndroid Build Coastguard Worker     return false;
85*c33452fbSAndroid Build Coastguard Worker }
86*c33452fbSAndroid Build Coastguard Worker 
asString() const87*c33452fbSAndroid Build Coastguard Worker std::string CMappingData::asString() const
88*c33452fbSAndroid Build Coastguard Worker {
89*c33452fbSAndroid Build Coastguard Worker     return utility::asString(_keyToValueMap, ", ", ":");
90*c33452fbSAndroid Build Coastguard Worker }
91*c33452fbSAndroid Build Coastguard Worker 
addValue(const std::string & strkey,const std::string & strValue)92*c33452fbSAndroid Build Coastguard Worker bool CMappingData::addValue(const std::string &strkey, const std::string &strValue)
93*c33452fbSAndroid Build Coastguard Worker {
94*c33452fbSAndroid Build Coastguard Worker     if (_keyToValueMap.find(strkey) != _keyToValueMap.end()) {
95*c33452fbSAndroid Build Coastguard Worker 
96*c33452fbSAndroid Build Coastguard Worker         return false;
97*c33452fbSAndroid Build Coastguard Worker     }
98*c33452fbSAndroid Build Coastguard Worker     _keyToValueMap[strkey] = strValue;
99*c33452fbSAndroid Build Coastguard Worker 
100*c33452fbSAndroid Build Coastguard Worker     return true;
101*c33452fbSAndroid Build Coastguard Worker }
102