blob: 63502c77587baa44eab78d82735ba3e324edc60f [file] [log] [blame]
Frederic Boisnard6cae0ec2013-05-23 18:48:58 +02001/*
David Wagnerb76c9d62014-02-05 18:30:24 +01002 * Copyright (c) 2011-2014, Intel Corporation
3 * All rights reserved.
Frederic Boisnard6cae0ec2013-05-23 18:48:58 +02004 *
David Wagnerb76c9d62014-02-05 18:30:24 +01005 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
Frederic Boisnard6cae0ec2013-05-23 18:48:58 +02007 *
David Wagnerb76c9d62014-02-05 18:30:24 +01008 * 1. Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
Frederic Boisnard6cae0ec2013-05-23 18:48:58 +020010 *
David Wagnerb76c9d62014-02-05 18:30:24 +010011 * 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.
Frederic Boisnard6cae0ec2013-05-23 18:48:58 +020029 */
30
31#include "FormattedSubsystemObject.h"
32#include "InstanceConfigurableElement.h"
33#include "MappingContext.h"
34#include <assert.h>
35
36#define base CSubsystemObject
37
38CFormattedSubsystemObject::CFormattedSubsystemObject(
39 CInstanceConfigurableElement* pInstanceConfigurableElement)
40 : base(pInstanceConfigurableElement)
41{
42}
43
44CFormattedSubsystemObject::CFormattedSubsystemObject(
45 CInstanceConfigurableElement* pInstanceConfigurableElement,
46 const string& strMappingValue)
47 : base(pInstanceConfigurableElement), _strFormattedMappingValue(strMappingValue)
48{
49
50}
51
52
53CFormattedSubsystemObject::CFormattedSubsystemObject(
54 CInstanceConfigurableElement* pInstanceConfigurableElement,
55 const string& strMappingValue,
56 uint32_t uiFirstAmendKey,
57 uint32_t uiNbAmendKeys,
58 const CMappingContext& context)
59 : base(pInstanceConfigurableElement), _strFormattedMappingValue(strMappingValue)
60{
61 // Cope with quotes in the name
62 if (strMappingValue[0] == '\'' && strMappingValue.length() >= 2) {
63
64 _strFormattedMappingValue = strMappingValue.substr(1, strMappingValue.length() - 2);
65 }
66 _strFormattedMappingValue = formatMappingValue(_strFormattedMappingValue, uiFirstAmendKey,
67 uiNbAmendKeys, context);
68}
69
70CFormattedSubsystemObject::~CFormattedSubsystemObject()
71{
72}
73
74string CFormattedSubsystemObject::getFormattedMappingValue() const
75{
76 return _strFormattedMappingValue;
77}
78
79bool CFormattedSubsystemObject::isAmendKeyValid(uint32_t uiAmendKey)
80{
81
82 return (uiAmendKey > 0) && (uiAmendKey <= 9);
83}
84
85string CFormattedSubsystemObject::formatMappingValue(const string& strMappingValue,
86 uint32_t uiFirstAmendKey,
87 uint32_t uiNbAmendKeys,
88 const CMappingContext& context)
89{
90 string strFormattedValue = strMappingValue;
91
92 // Search for amendment (only one supported for now)
93 size_t uiPercentPos = strFormattedValue.find('%', 0);
94
95 // Amendment limited to one digit (values from 1 to 9)
96 assert(isAmendKeyValid(uiNbAmendKeys));
97
98 // Check we found one and that there's room for value
99 if (uiPercentPos != string::npos && uiPercentPos < strFormattedValue.size() - 1) {
100
101 // Get Amend number
102 uint32_t uiAmendNumber = strFormattedValue[uiPercentPos + 1] - '0';
103
104 // Check if current Amend number is Valid
105 if ((uiAmendNumber > 0) && (uiAmendNumber <= uiNbAmendKeys)) {
106
107 uint32_t uiAmendType = uiFirstAmendKey + uiAmendNumber - 1;
108
109 // Check if current Amend type is Set in the context
110 if (context.iSet(uiAmendType)) {
111
112 // Make the amendment on the part of the string after the current Amend
113 string strEndOfLine = strFormattedValue.substr(uiPercentPos + 2,
114 strFormattedValue.size()
115 - uiPercentPos - 2);
116 string strEndOfLineAmended = formatMappingValue(strEndOfLine, uiFirstAmendKey,
117 uiNbAmendKeys, context);
118
119 // Get current Amend value
120 string strAmendValue = context.getItem(uiAmendType);
121
122 // Make the amendment
123 strFormattedValue = strFormattedValue.substr(0, uiPercentPos) + strAmendValue
124 + strEndOfLineAmended;
125
126 }
127 }
128 }
129 return strFormattedValue;
130}