secureswitools/swisistools/source/interpretsislib/stringutils.cpp
branchRCL_3
changeset 24 5cc91383ab1e
parent 0 ba25891c3a9e
equal deleted inserted replaced
23:cd189dac02f7 24:5cc91383ab1e
     1 /*
     1 /*
     2 * Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
     2 * Copyright (c) 2007-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".
    28 
    28 
    29 // User includes
    29 // User includes
    30 #include "is_utils.h"
    30 #include "is_utils.h"
    31 #include "logger.h"
    31 #include "logger.h"
    32 #include "utility_interface.h"
    32 #include "utility_interface.h"
       
    33 #include "util.h"
    33 
    34 
    34 // Constants
    35 // Constants
    35 const int KInterpretSISExpectedSIDLength = 8;
    36 const int KInterpretSISExpectedSIDLength = 8;
    36 const int KInterpretSISCtlIndexLength = 4;
    37 const int KInterpretSISCtlIndexLength = 4;
    37 const std::string KStringUtilsWhiteSpace = "\t  \r\n";
    38 const std::string KStringUtilsWhiteSpace = "\t  \r\n";
    49 	{
    50 	{
    50 	// If path semantics is correct (as needed by sisx library)
    51 	// If path semantics is correct (as needed by sisx library)
    51 	// then the function will return 0
    52 	// then the function will return 0
    52 	int ret = 0; 
    53 	int ret = 0; 
    53 	int pos = 0;
    54 	int pos = 0;
    54 	if((pos = aPath.find(L"/", aIndex)) != std::wstring::npos)
    55 	#ifndef __TOOLS2_LINUX__
       
    56 	if((pos = aPath.find(L"//", aIndex)) != std::wstring::npos)
       
    57 		{
       
    58 		ret = 2;
       
    59 		}
       
    60 	#else
       
    61 	if((pos = aPath.find(L"\\\\", aIndex)) != std::wstring::npos)
       
    62 		{
       
    63 		ret = 2;
       
    64 		}
       
    65 	#endif
       
    66 
       
    67 	#ifndef __TOOLS2_LINUX__
       
    68 	else if((pos = aPath.find(L"/", aIndex)) != std::wstring::npos)
    55 		{
    69 		{
    56 		ret = 1;
    70 		ret = 1;
    57 		}
    71 		}
    58 	else if((pos = aPath.find(L"\\\\", aIndex)) != std::wstring::npos)
    72 	#else
    59 		{
    73 		else if((pos = aPath.find(L"\\", aIndex)) != std::wstring::npos)
    60 		ret = 2;
    74 		{
    61 		}
    75 		ret = 1;
       
    76 		}
       
    77 	#endif
    62 	aIndex = pos;
    78 	aIndex = pos;
    63 	return ret;
    79 	return ret;
    64 	}
    80 	}
    65 
    81 
    66 std::wstring StringUtils::FixPathDelimiters( const std::string& aString )
    82 std::wstring StringUtils::FixPathDelimiters( const std::string& aString )
    71     //
    87     //
    72 	wchar_t* buf = NULL ;
    88 	wchar_t* buf = NULL ;
    73 	int len = ConvertMultiByteToWideChar(src, -1, NULL, 0);
    89 	int len = ConvertMultiByteToWideChar(src, -1, NULL, 0);
    74 	buf = new wchar_t[len+1];
    90 	buf = new wchar_t[len+1];
    75 	len = ConvertMultiByteToWideChar(src, -1, buf, len);
    91 	len = ConvertMultiByteToWideChar(src, -1, buf, len);
    76 	ret = std::wstring( buf );
    92 
       
    93 	ret = std::wstring( buf ,len);
       
    94 
    77 	delete[] buf;
    95 	delete[] buf;
    78 	//
    96 	//
    79     std::wstring::size_type idx = 0;
    97     std::wstring::size_type idx = 0;
    80 	while(len = FirstInvalidDirSeparatorSize(ret, idx))
    98 	while(len = FirstInvalidDirSeparatorSize(ret, idx))
    81         {
    99         {
    82 		ret.replace(idx, len, KDirectorySeparator);
   100 		ret.replace(idx, len, KDirectorySeparator);
    83         }
   101         }
    84     //
   102     //
    85     return ret;
   103     return ret.c_str();
    86     }
   104     }
    87 
   105 
    88 
   106 
    89 std::string StringUtils::TrimWhiteSpace( const std::string& aString )
   107 std::string StringUtils::TrimWhiteSpace( const std::string& aString )
    90 	{
   108 	{
   142 	std::transform( ret.begin(), ret.end(), ret.begin(), tolower );
   160 	std::transform( ret.begin(), ret.end(), ret.begin(), tolower );
   143     //
   161     //
   144     return ret;
   162     return ret;
   145     }
   163     }
   146 
   164 
       
   165 std::wstring StringUtils::ToLower( const std::wstring& aString )
       
   166     {
       
   167 	std::wstring ret( aString );
       
   168 	std::transform( ret.begin(), ret.end(), ret.begin(), tolower );
       
   169 
       
   170     return ret;
       
   171     }
       
   172 
   147 bool StringUtils::IsLastCharacter( const std::wstring& aString, wchar_t aChar )
   173 bool StringUtils::IsLastCharacter( const std::wstring& aString, wchar_t aChar )
   148     {
   174     {
   149     bool isLast = false;
   175     bool isLast = false;
   150     //
   176     //
   151     if ( aString.length() )
   177     if ( aString.length() )
   162     {
   188     {
   163     bool startsWithDrive = false;
   189     bool startsWithDrive = false;
   164     //
   190     //
   165     if ( aText.length() >= 3 )
   191     if ( aText.length() >= 3 )
   166         {
   192         {
   167         const std::string prefix = ToUpper( Ucs2ToUtf8( aText.substr( 0, 3 ) ) );
   193         const std::string prefix = ToUpper( wstring2string( aText.substr( 0, 3 ) ) );
   168         //
   194         //
   169         const char drive = prefix[ 0 ];
   195         const char drive = prefix[ 0 ];
   170         const char colon = prefix[ 1 ];
   196         const char colon = prefix[ 1 ];
   171         const char backslash = prefix[ 2 ];
   197         const char backslash = prefix[ 2 ];
   172         //
   198         //
       
   199         #ifndef __TOOLS2_LINUX__
   173         if  ( colon == ':' && backslash == '\\' )
   200         if  ( colon == ':' && backslash == '\\' )
       
   201 		#else
       
   202 		if  ( colon == ':' && (backslash == '/' || backslash == '\\'))
       
   203 		#endif
   174             {
   204             {
   175             startsWithDrive = ( drive >= 'A' && drive <= 'Z' ) || (drive == '!') ;
   205             startsWithDrive = ( drive >= 'A' && drive <= 'Z' ) || (drive == '!') ;
   176             }
   206             }
   177         }
   207         }
   178     //
   208     //
   268 
   298 
   269  The function returns TRUE if a match is found, or FALSE.
   299  The function returns TRUE if a match is found, or FALSE.
   270  */
   300  */
   271 bool StringUtils::WildcardCompare(const std::wstring& aWildCardFileName, const std::wstring& aFileName)
   301 bool StringUtils::WildcardCompare(const std::wstring& aWildCardFileName, const std::wstring& aFileName)
   272 {
   302 {
       
   303 	#ifdef __TOOLS2_LINUX__
       
   304 	// In case the incoming data is a DB entry then it will be having windows
       
   305 	// specific paths due to the consistency of DB contents across WINDOWS
       
   306 	// and LINUX. So, we need to convert them to LINUX paths and then compare.
       
   307 
       
   308 	std::wstring::size_type idx = 0;
       
   309 
       
   310 	while( (idx = aWildCardFileName.find(L"\\", idx)) != std::wstring::npos)
       
   311         {
       
   312 		aWildCardFileName.replace( idx, 1, L"/" );
       
   313         }
       
   314 
       
   315 	idx = 0;
       
   316 
       
   317 	while( (idx = aFileName.find(L"\\", idx)) != std::wstring::npos)
       
   318         {
       
   319 		aFileName.replace( idx, 1, L"/" );
       
   320         }
       
   321 	#endif
       
   322 
       
   323 	aWildCardFileName = StringUtils::ToLower(aWildCardFileName);
       
   324 	aFileName = StringUtils::ToLower(aFileName);
       
   325 
   273 	std::wstring::const_iterator wildCurr = aWildCardFileName.begin();
   326 	std::wstring::const_iterator wildCurr = aWildCardFileName.begin();
   274 	std::wstring::const_iterator wildEnd = aWildCardFileName.end();
   327 	std::wstring::const_iterator wildEnd = aWildCardFileName.end();
   275 
   328 
   276 	std::wstring::const_iterator fileCurr = aFileName.begin();
   329 	std::wstring::const_iterator fileCurr = aFileName.begin();
   277 	std::wstring::const_iterator fileEnd = aFileName.end();
   330 	std::wstring::const_iterator fileEnd = aFileName.end();
   355 
   408 
   356 @return The filename and extension.
   409 @return The filename and extension.
   357 */
   410 */
   358 std::wstring StringUtils::NameAndExt( const std::wstring& aFile )
   411 std::wstring StringUtils::NameAndExt( const std::wstring& aFile )
   359 {
   412 {
       
   413 #ifndef __TOOLS2_LINUX__
   360 	int pos = aFile.find_last_of(L"\\");
   414 	int pos = aFile.find_last_of(L"\\");
       
   415 #else
       
   416 	// We should also check for backward slash since the caller could be
       
   417 	// passing a string containing a windows-specific paths within LINUX.
       
   418 	//
       
   419 	//    One instance being - an SCR database entry under LINUX, which has
       
   420 	//    the windows specific paths of the installed files to maintain
       
   421 	//    consistency across WINDOWS and LINUX platforms.
       
   422 
       
   423 	int pos = aFile.find_last_of(L"/");
       
   424 	pos = pos == std::wstring::npos ? aFile.find_last_of(L"\\") : pos;
       
   425 #endif
   361 	if (pos == std::wstring::npos)
   426 	if (pos == std::wstring::npos)
   362 	{
   427 	{
   363 		return L"";
   428 		return L"";
   364 	}
   429 	}
   365 	else 
   430 	else 
   380 
   445 
   381 @return The drive and path.
   446 @return The drive and path.
   382 */
   447 */
   383 std::wstring StringUtils::DriveAndPath( const std::wstring& aFile )
   448 std::wstring StringUtils::DriveAndPath( const std::wstring& aFile )
   384 {
   449 {
   385 	int pos = aFile.find_last_of(L"\\");
   450 #ifndef __TOOLS2_LINUX__
       
   451 		int pos = aFile.find_last_of(L"\\");
       
   452 #else
       
   453 		int pos = aFile.find_last_of(L"/");
       
   454 #endif
   386 	if (pos == std::wstring::npos)
   455 	if (pos == std::wstring::npos)
   387 	{
   456 	{
   388 		return L"";
   457 		return L"";
   389 	}
   458 	}
   390 	else 
   459 	else 
   403  
   472  
   404 @return The path. It always begins and ends in a backslash.
   473 @return The path. It always begins and ends in a backslash.
   405 */
   474 */
   406 std::wstring StringUtils::Path( const std::wstring& aFile )
   475 std::wstring StringUtils::Path( const std::wstring& aFile )
   407 {
   476 {
   408 	int firstPos = aFile.find_first_of(L"\\");
   477 #ifndef __TOOLS2_LINUX__
   409 	int lastPos = aFile.find_last_of(L"\\");
   478 		int firstPos = aFile.find_first_of(L"\\");
       
   479 		int lastPos = aFile.find_last_of(L"\\");
       
   480 #else
       
   481 		int firstPos = aFile.find_first_of(L"/");
       
   482 		int lastPos = aFile.find_last_of(L"/");
       
   483 #endif
   410 	
   484 	
   411 	if (lastPos >= firstPos)
   485 	if (lastPos >= firstPos)
   412 	{
   486 	{
   413 		std::wstring path(aFile.substr(firstPos,lastPos-1));
   487 		std::wstring path(aFile.substr(firstPos,lastPos-1));
   414 		return path;
   488 		return path;
   426  
   500  
   427 @return The filename.
   501 @return The filename.
   428 */
   502 */
   429 std::wstring StringUtils::Name( const std::wstring& aFile )
   503 std::wstring StringUtils::Name( const std::wstring& aFile )
   430 {
   504 {
       
   505 #ifndef __TOOLS2_LINUX__
   431 	int startPos = aFile.find_last_of(L"\\");
   506 	int startPos = aFile.find_last_of(L"\\");
       
   507 #else
       
   508 	int startPos = aFile.find_last_of(L"/");
       
   509 #endif
       
   510 	
   432 	int endPos = aFile.find_last_of(L".");
   511 	int endPos = aFile.find_last_of(L".");
   433 
   512 
   434 	if (endPos > startPos)
   513 	if (endPos > startPos)
   435     {
   514     {
   436 		std::wstring extension(aFile.substr(startPos+1,endPos));
   515 		std::wstring extension(aFile.substr(startPos+1,endPos));