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