1 /* 2 * Copyright (c) 2011-2014, 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 40 CFormattedSubsystemObject::CFormattedSubsystemObject( 41 CInstanceConfigurableElement* pInstanceConfigurableElement) 42 : base(pInstanceConfigurableElement) 43 { 44 } 45 46 CFormattedSubsystemObject::CFormattedSubsystemObject( 47 CInstanceConfigurableElement* pInstanceConfigurableElement, 48 const string& strMappingValue) 49 : base(pInstanceConfigurableElement), _strFormattedMappingValue(strMappingValue) 50 { 51 52 } 53 54 55 CFormattedSubsystemObject::CFormattedSubsystemObject( 56 CInstanceConfigurableElement* pInstanceConfigurableElement, 57 const string& strMappingValue, 58 uint32_t uiFirstAmendKey, 59 uint32_t uiNbAmendKeys, 60 const CMappingContext& context) 61 : base(pInstanceConfigurableElement), _strFormattedMappingValue(strMappingValue) 62 { 63 // Cope with quotes in the name 64 if (strMappingValue[0] == '\'' && strMappingValue.length() >= 2) { 65 66 _strFormattedMappingValue = strMappingValue.substr(1, strMappingValue.length() - 2); 67 } 68 _strFormattedMappingValue = formatMappingValue(_strFormattedMappingValue, uiFirstAmendKey, 69 uiNbAmendKeys, context); 70 } 71 72 CFormattedSubsystemObject::~CFormattedSubsystemObject() 73 { 74 } 75 76 string CFormattedSubsystemObject::getFormattedMappingValue() const 77 { 78 return _strFormattedMappingValue; 79 } 80 81 bool CFormattedSubsystemObject::isAmendKeyValid(uint32_t uiAmendKey) 82 { 83 84 return (uiAmendKey > 0) && (uiAmendKey <= 9); 85 } 86 87 string CFormattedSubsystemObject::formatMappingValue(const string& strMappingValue, 88 uint32_t uiFirstAmendKey, 89 uint32_t uiNbAmendKeys, 90 const CMappingContext& context) 91 { 92 string strFormattedValue = strMappingValue; 93 94 // Search for amendment (only one supported for now) 95 string::size_type uiPercentPos = strFormattedValue.find('%', 0); 96 97 // Amendment limited to one digit (values from 1 to 9) 98 assert(isAmendKeyValid(uiNbAmendKeys)); 99 100 // Check we found one and that there's room for value 101 if (uiPercentPos != string::npos && uiPercentPos < strFormattedValue.size() - 1) { 102 103 // Get Amend number 104 uint32_t uiAmendNumber = strFormattedValue[uiPercentPos + 1] - '0'; 105 106 // Check if current Amend number is Valid 107 if ((uiAmendNumber > 0) && (uiAmendNumber <= uiNbAmendKeys)) { 108 109 uint32_t uiAmendType = uiFirstAmendKey + uiAmendNumber - 1; 110 111 // Check if current Amend type is Set in the context 112 if (context.iSet(uiAmendType)) { 113 114 // Make the amendment on the part of the string after the current Amend 115 string strEndOfLine = strFormattedValue.substr(uiPercentPos + 2, 116 strFormattedValue.size() 117 - uiPercentPos - 2); 118 string strEndOfLineAmended = formatMappingValue(strEndOfLine, uiFirstAmendKey, 119 uiNbAmendKeys, context); 120 121 // Get current Amend value 122 string strAmendValue = context.getItem(uiAmendType); 123 124 // Make the amendment 125 strFormattedValue = strFormattedValue.substr(0, uiPercentPos) + strAmendValue 126 + strEndOfLineAmended; 127 128 } 129 } 130 } 131 return strFormattedValue; 132 } 133