secureswitools/swisistools/source/interpretsislib/expressionevaluator.cpp
branchRCL_3
changeset 34 741e5bba2bd1
parent 0 ba25891c3a9e
child 50 c6e8afe0ba85
child 62 5cc91383ab1e
equal deleted inserted replaced
28:98a43fae6e2b 34:741e5bba2bd1
     1 /*
     1 /*
     2 * Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
     2 * Copyright (c) 2006-2010 Nokia Corporation and/or its subsidiary(-ies).
     3 * All rights reserved.
     3 * All rights reserved.
     4 * This component and the accompanying materials are made available
     4 * This component and the accompanying materials are made available
     5 * under the terms of the License "Eclipse Public License v1.0"
     5 * under the terms of the License "Eclipse Public License v1.0"
     6 * which accompanies this distribution, and is available
     6 * which accompanies this distribution, and is available
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
    93 		throw std::runtime_error("null pointer");
    93 		throw std::runtime_error("null pointer");
    94     	}
    94     	}
    95     }
    95     }
    96 
    96 
    97 
    97 
    98 ExpressionResult ExpressionEvaluator::Evaluate(const CSISExpression* aExpression)
    98 ExpressionResult ExpressionEvaluator::Evaluate(const CSISExpression* aExpression, bool aLogInfo )
    99     {
    99     {
   100 	Require(aExpression);
   100 	Require(aExpression);
   101 	return Evaluate(*aExpression);
   101 	return Evaluate(*aExpression, aLogInfo);
   102     }
   102     }
   103 
   103 
   104 
   104 
   105 ExpressionResult ExpressionEvaluator::Evaluate(const CSISExpression& aExpression)
   105 ExpressionResult ExpressionEvaluator::Evaluate(const CSISExpression& aExpression, bool aLogInfo)
   106     {
   106     {
   107 	if (++iExpressionDepth > KMaxExpressionDepth)
   107 	if (++iExpressionDepth > KMaxExpressionDepth)
   108 	    {
   108 	    {
   109 		iExpressionDepth=0;
   109 		iExpressionDepth=0;
   110 		std::string error = "SIS File expression too complex\n";
   110 		std::string error = "SIS File expression too complex\n";
   120 	case CSISExpression::EBinOpGreaterThan:
   120 	case CSISExpression::EBinOpGreaterThan:
   121 	case CSISExpression::EBinOpLessThan:
   121 	case CSISExpression::EBinOpLessThan:
   122 	case CSISExpression::EBinOpGreaterThanOrEqual:
   122 	case CSISExpression::EBinOpGreaterThanOrEqual:
   123 	case CSISExpression::EBinOpLessThanOrEqual:
   123 	case CSISExpression::EBinOpLessThanOrEqual:
   124         {
   124         {
   125 		const ExpressionResult resultLeft = Evaluate( aExpression.LHS() );
   125 		const ExpressionResult resultLeft = Evaluate( aExpression.LHS(), aLogInfo );
   126 		const ExpressionResult resultRight = Evaluate( aExpression.RHS() );
   126 		const ExpressionResult resultRight = Evaluate( aExpression.RHS(), aLogInfo  );
   127         //
   127         //
   128 	    switch (aExpression.Operator())
   128 	    switch (aExpression.Operator())
   129 	        {
   129 	        {
   130 	    case CSISExpression::EBinOpEqual:
   130 	    case CSISExpression::EBinOpEqual:
   131             iTempResult = ( resultLeft == resultRight );
   131             iTempResult = ( resultLeft == resultRight );
   150 		break;
   150 		break;
   151         }
   151         }
   152     
   152     
   153     case CSISExpression::ELogOpAnd:
   153     case CSISExpression::ELogOpAnd:
   154 		{
   154 		{
   155 		ExpressionResult tmp1 = Evaluate(aExpression.LHS());
   155 		ExpressionResult tmp1 = Evaluate(aExpression.LHS(), aLogInfo );
   156 		ExpressionResult tmp2 = Evaluate(aExpression.RHS());
   156 		ExpressionResult tmp2 = Evaluate(aExpression.RHS(), aLogInfo );
   157 		iTempResult = ExpressionResult(tmp1.BoolValue() && tmp2.BoolValue());
   157 		iTempResult = ExpressionResult(tmp1.BoolValue() && tmp2.BoolValue());
   158 		break;
   158 		break;
   159 		}
   159 		}
   160 
   160 
   161 	case CSISExpression::ELogOpOr:
   161 	case CSISExpression::ELogOpOr:
   162 		{
   162 		{
   163 		ExpressionResult tmp1 = Evaluate(aExpression.LHS());
   163 		ExpressionResult tmp1 = Evaluate(aExpression.LHS(), aLogInfo );
   164 		if (tmp1.BoolValue())
   164 		if (tmp1.BoolValue())
   165     		{
   165     		{
   166 			iTempResult = ExpressionResult(true);
   166 			iTempResult = ExpressionResult(true);
   167 	    	}
   167 	    	}
   168 		else
   168 		else
   169 		    {
   169 		    {
   170 			iTempResult = ExpressionResult(Evaluate(aExpression.RHS())).BoolValue();
   170 			iTempResult = ExpressionResult(Evaluate(aExpression.RHS(), aLogInfo)).BoolValue();
   171 		    }
   171 		    }
   172 		break;
   172 		break;
   173 		}
   173 		}
   174 
   174 
   175 	case CSISExpression::EUnaryOpNot:
   175 	case CSISExpression::EUnaryOpNot:
   176 		iTempResult=!Evaluate(aExpression.RHS());
   176 		iTempResult=!Evaluate(aExpression.RHS(), aLogInfo );
   177 		break;
   177 		break;
   178 
   178 
   179 	case CSISExpression::EFuncAppProperties:
   179 	case CSISExpression::EFuncAppProperties:
   180         {
   180         {
   181         const TUint32 resultLeft = Evaluate( aExpression.LHS() ).IntegerValue();
   181         const TUint32 resultLeft = Evaluate( aExpression.LHS(), aLogInfo  ).IntegerValue();
   182         const TUint32 resultRight = Evaluate( aExpression.RHS() ).IntegerValue();
   182         const TUint32 resultRight = Evaluate( aExpression.RHS(), aLogInfo  ).IntegerValue();
   183         //
   183         //
   184 		iTempResult = iExpEnv.ApplicationProperty( resultLeft, resultRight );
   184 		iTempResult = iExpEnv.ApplicationProperty( resultLeft, resultRight );
   185 		break;
   185 		break;
   186         }
   186         }
   187 
   187 
   188 	case CSISExpression::EFuncDevProperties:
   188 	case CSISExpression::EFuncDevProperties:
   189 		iTempResult = ExpressionResult(iExpEnv.Package(Evaluate(aExpression.RHS()).IntegerValue()));
   189 		iTempResult = ExpressionResult(iExpEnv.Package(Evaluate(aExpression.RHS(), aLogInfo ).IntegerValue()));
   190 		break;
   190 		break;
   191 
   191 
   192 	case CSISExpression::EFuncExists:
   192 	case CSISExpression::EFuncExists:
   193 		{
   193 		{
   194 		const CSISString& pS = aExpression.String();
   194 		const CSISString& pS = aExpression.String();
   216 			{
   216 			{
   217 			iTempResult = ExpressionResult(iExpEnv.DeviceLanguage(pS.GetString().substr(KFuncSupportedLanguagePrefix.length())));
   217 			iTempResult = ExpressionResult(iExpEnv.DeviceLanguage(pS.GetString().substr(KFuncSupportedLanguagePrefix.length())));
   218 			}
   218 			}
   219 		else
   219 		else
   220 			{
   220 			{
   221 			iTempResult = ExpressionResult(iExpEnv.FindFile(pS.GetString()));
   221 			iTempResult = ExpressionResult(iExpEnv.FindFile(pS.GetString(), aLogInfo));
   222 			}
   222 			}
   223 		}
   223 		}
   224 		break;
   224 		break;
   225 
   225 
   226 	case CSISExpression::EPrimTypeString:
   226 	case CSISExpression::EPrimTypeString:
   236 		break;
   236 		break;
   237 
   237 
   238 	case CSISExpression::EPrimTypeVariable:
   238 	case CSISExpression::EPrimTypeVariable:
   239 		{
   239 		{
   240         const int variableId = aExpression.IntValue();
   240         const int variableId = aExpression.IntValue();
   241         const int variableValue = iExpEnv.Variable( variableId );
   241         const int variableValue = iExpEnv.Variable( variableId, aLogInfo);
   242         //
   242         //
   243 		iTempResult = ExpressionResult( variableValue );
   243 		iTempResult = ExpressionResult( variableValue );
   244 		break;
   244 		break;
   245 		}
   245 		}
   246 
   246 
   290     {
   290     {
   291 	return iSisFile.GetPackageName();
   291 	return iSisFile.GetPackageName();
   292     }
   292     }
   293 
   293 
   294 
   294 
   295 bool ExpressionEnvironment::FindFile( const std::wstring& aFileName )
   295 bool ExpressionEnvironment::FindFile( const std::wstring& aFileName, bool aLogInfo )
   296     {
   296     {
   297     bool fileExists = false;
   297     bool fileExists = false;
   298 
   298 
   299     // Fixed up file name
   299     // Fixed up file name
   300     std::wstring fileName( aFileName );
   300     std::wstring fileName( aFileName );
   306     // Filename length must be at least 3 characters, i.e. drive + semicolon + backslash + filename
   306     // Filename length must be at least 3 characters, i.e. drive + semicolon + backslash + filename
   307     //
   307     //
   308     if ( fileName.length() >= 1 && fileName[ 0 ] == L'\\' )
   308     if ( fileName.length() >= 1 && fileName[ 0 ] == L'\\' )
   309         {
   309         {
   310         // Bad file name?
   310         // Bad file name?
   311   		LWARN(L"\tAssuming file path \'" << aFileName << L"\' refers to Z:" );
   311 		if( aLogInfo )
       
   312 			{
       
   313 			LWARN(L"\tAssuming file path \'" << aFileName << L"\' refers to Z:" );
       
   314 			}  		
   312         fileName = L"Z:" + fileName;
   315         fileName = L"Z:" + fileName;
   313         }
   316         }
   314 
   317 
   315     // Require for invalid file exception (also helps with debugging)
   318     // Require for invalid file exception (also helps with debugging)
   316     std::string narrowFileName;
   319     std::string narrowFileName;
   353         {
   356         {
   354 		std::string error = "corrupt SIS file: bad \'EXISTS' filename: \'" + narrowFileName + "\'";
   357 		std::string error = "corrupt SIS file: bad \'EXISTS' filename: \'" + narrowFileName + "\'";
   355 		throw InvalidSis( "", error, INVALID_SIS );
   358 		throw InvalidSis( "", error, INVALID_SIS );
   356         }
   359         }
   357     //
   360     //
   358     std::ostringstream stream;
   361 	if(aLogInfo)
   359     stream << "\tIF EXISTS(\'" << narrowFileName << "\') => " << fileExists;
   362 		{
   360     std::string msg = stream.str();
   363 		std::ostringstream stream;
   361     std::wstring finalMessage = Utf8ToUcs2( msg );
   364 		stream << "\tIF EXISTS(\'" << narrowFileName << "\') => " << fileExists;
   362     LINFO( finalMessage );
   365 		std::string msg = stream.str();
       
   366 		std::wstring finalMessage = Utf8ToUcs2( msg );
       
   367 		LINFO( finalMessage );
       
   368 		}
   363     //
   369     //
   364     return fileExists;
   370     return fileExists;
   365     }
   371     }
   366 
   372 
   367 
   373 
   394 	// No property
   400 	// No property
   395 	return 0;
   401 	return 0;
   396     }
   402     }
   397 
   403 
   398 
   404 
   399 int ExpressionEnvironment::Variable( int aVariableId )
   405 int ExpressionEnvironment::Variable( int aVariableId, bool aLogInfo )
   400     {
   406     {
   401     int result = 0;
   407     int result = 0;
   402 
   408 
   403     // For debugging
   409     // For debugging
   404     std::string attributeName = ConfigManager::AttributeNameById( aVariableId );
   410     std::string attributeName = ConfigManager::AttributeNameById( aVariableId );
   415 		if (aVariableId == KVariableLanguage)
   421 		if (aVariableId == KVariableLanguage)
   416 			{
   422 			{
   417 			if (!iSisFile.IsSupportedLanguage((TUint32)result))
   423 			if (!iSisFile.IsSupportedLanguage((TUint32)result))
   418 				{
   424 				{
   419 				int firstLanguage = iSisFile.GetLanguage(); // get the first language
   425 				int firstLanguage = iSisFile.GetLanguage(); // get the first language
   420 				std::ostringstream stream;
   426 				if(aLogInfo)
   421 				stream << "Input language " << result << " is not supported by SIS file. Using first language " <<firstLanguage;
   427 					{
   422 				std::string msg = stream.str();
   428 					std::ostringstream stream;
   423 				std::wstring finalMessage = Utf8ToUcs2( msg );
   429 					stream << "Input language " << result << " is not supported by SIS file. Using first language " <<firstLanguage;
   424 				LWARN( finalMessage );	
   430 					std::string msg = stream.str();
       
   431 					std::wstring finalMessage = Utf8ToUcs2( msg );
       
   432 					LWARN( finalMessage );	
       
   433 					}
   425 				result = firstLanguage;
   434 				result = firstLanguage;
   426 				}
   435 				}
   427 			}
   436 			}
   428         std::ostringstream stream;
   437         if(aLogInfo)
   429         stream << "\tIF " << attributeName << " ... where [" << attributeName << " = " << result << "]";
   438 			{
   430         std::string msg = stream.str();
   439 			std::ostringstream stream;
   431         std::wstring finalMessage = Utf8ToUcs2( msg );
   440 			stream << "\tIF " << attributeName << " ... where [" << attributeName << " = " << result << "]";
   432         LINFO( finalMessage );
   441 			std::string msg = stream.str();
       
   442 			std::wstring finalMessage = Utf8ToUcs2( msg );
       
   443 			LINFO( finalMessage );
       
   444 			}
   433         }
   445         }
   434 	else if ( aVariableId == KVariableLanguage )
   446 	else if ( aVariableId == KVariableLanguage )
   435     	{
   447     	{
   436 		LWARN(L"Disregarding language selection. Using ELangEnglish");
   448 		if(aLogInfo)
       
   449 			{
       
   450 			LWARN(L"Disregarding language selection. Using ELangEnglish");
       
   451 			}
   437 		result = 1;
   452 		result = 1;
   438     	}
   453     	}
   439     else
   454     else
   440         {
   455         {
   441         std::string packageName;
   456         std::string packageName;