apicompatanamdw/compatanalysercmd/headeranalyser/src/Utils.cpp
changeset 0 638b9c697799
equal deleted inserted replaced
-1:000000000000 0:638b9c697799
       
     1 /*
       
     2 * Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "CmdGlobals.h"
       
    20 #ifdef __WIN__
       
    21 #pragma warning(disable:4786)
       
    22 #endif
       
    23 
       
    24 #include <string>
       
    25 #include <stdio.h>
       
    26 #include <stdarg.h>
       
    27 #include <iostream>
       
    28 #include <sstream>
       
    29 #include <locale>
       
    30 #include <map>
       
    31 #include <vector>
       
    32 #include "Utils.h"
       
    33 #include "HAException.h"
       
    34 #include "BBCFileUtils.h"
       
    35 #include "XMLUtils.h"
       
    36 
       
    37 using namespace std;
       
    38 
       
    39 // ----------------------------------------------------------------------------
       
    40 // itoa
       
    41 // Convert integer to string
       
    42 // ----------------------------------------------------------------------------
       
    43 //
       
    44 string itoa(int value, string& str, int radix)
       
    45 {
       
    46   ostringstream ostr;
       
    47   ostr << value;
       
    48   str = ostr.str();
       
    49   return str;
       
    50 }
       
    51 
       
    52 // ----------------------------------------------------------------------------
       
    53 // ltoa
       
    54 // Convert long integer to string
       
    55 // ----------------------------------------------------------------------------
       
    56 //
       
    57 string ltoa(long value, string& str, int radix)
       
    58 {
       
    59     ostringstream ostr;
       
    60     ostr << value;
       
    61     str = ostr.str();
       
    62     return str;
       
    63 }
       
    64 
       
    65 
       
    66 // ----------------------------------------------------------------------------
       
    67 // FindFromList
       
    68 // Find value from list
       
    69 // ----------------------------------------------------------------------------
       
    70 //
       
    71 list<string>::iterator FindFromList(const string& aValue, list<string>& aList, bool(*comparator)(const string&, const string&, bool), bool comparatorparam)
       
    72 {
       
    73     list<string>::iterator begin = aList.begin();
       
    74     list<string>::iterator end = aList.end();
       
    75     bool found = false;
       
    76     while(begin != end && found == false)
       
    77     {
       
    78         if (comparator != 0 && (*comparator)(*begin, aValue, comparatorparam))
       
    79         {
       
    80             found = true;
       
    81         } else if (comparator == 0 && *begin == aValue)
       
    82         {
       
    83             found = true;
       
    84         }
       
    85         else
       
    86         {
       
    87             begin++;
       
    88         }
       
    89     }
       
    90 
       
    91     return begin;
       
    92 }
       
    93 
       
    94 // ----------------------------------------------------------------------------
       
    95 // FindFromList
       
    96 // Find value from list
       
    97 // ----------------------------------------------------------------------------
       
    98 //
       
    99 list<pair<string,string> >::iterator FindFromList(const string& aValue, list<pair<string,string> >& aList, bool(*comparator)(const string&, const string&, bool), bool comparatorparam)
       
   100 {
       
   101     list<pair<string,string> >::iterator begin = aList.begin();
       
   102     list<pair<string,string> >::iterator end = aList.end();
       
   103     bool found = false;
       
   104     while(begin != end && found == false)
       
   105     {
       
   106 		if (comparator != 0 && (*comparator)(begin->first, aValue, comparatorparam))
       
   107         {
       
   108             found = true;
       
   109 		} else if (comparator == 0 && begin->first == aValue)
       
   110         {
       
   111             found = true;
       
   112         }
       
   113         else
       
   114         {
       
   115             begin++;
       
   116         }
       
   117     }
       
   118 
       
   119     return begin;
       
   120 }
       
   121 // ----------------------------------------------------------------------------
       
   122 // FindFromList
       
   123 // Find value from list
       
   124 // 
       
   125 // ----------------------------------------------------------------------------
       
   126 //
       
   127 list<pair<string, string> >::iterator FindFromList(const string& aValue, list<pair<string, string> >& aList, TValue compareTo, bool(*comparator)(const pair<string,string>&, const string&, bool), bool comparatorparam,bool comparewholeString)
       
   128 {
       
   129     list<pair<string, string> >::iterator begin = aList.begin();
       
   130 	list<pair<string, string> >::iterator begin1 = aList.begin();
       
   131     list<pair<string, string> >::iterator end = aList.end();
       
   132 	
       
   133     bool found = false;
       
   134 	while(comparewholeString == true && begin1 != end && found == false)
       
   135 	{// First check both the file matches or not
       
   136 		if(aValue.compare(begin1->second)== 0 )
       
   137 			found = true;
       
   138 	 // Now compare base file with  current file after sperating sub dirs of currrent file each time  
       
   139 		else if(compareWholeFileStrings(aValue,begin1->second) == true )
       
   140 		     found = true;
       
   141 		else
       
   142 			begin1++;
       
   143 
       
   144 		if(found == true)
       
   145 			return begin1;		
       
   146 	}
       
   147     while(begin != end && found == false)
       
   148     {
       
   149         if (comparator != 0 && (*comparator)(*begin, aValue, comparatorparam))
       
   150         {
       
   151             found = true;
       
   152         } else if (comparator == 0 && compareTo == ELeftValue && begin->first == aValue)
       
   153         {
       
   154             found = true;
       
   155         } else if (comparator == 0 && compareTo == ERightValue && begin->second == aValue)
       
   156         {
       
   157             found = true;
       
   158         }
       
   159         else
       
   160         {
       
   161             begin++;
       
   162         }
       
   163     }
       
   164 
       
   165     return begin;
       
   166 }
       
   167 
       
   168 // ----------------------------------------------------------------------------
       
   169 // FindFromList
       
   170 // Find value from list
       
   171 // 
       
   172 // ----------------------------------------------------------------------------
       
   173 //
       
   174 list<pair<pair<string, string>,string> >::iterator FindFromList(const string& aValue, list<pair<pair<string, string>,string> >& aList, TValue compareTo, bool(*comparator)(const pair<string,string>&, const string&, bool), bool comparatorparam,bool comparewholeString)
       
   175 {
       
   176     list<pair<pair<string, string>,string> >::iterator begin = aList.begin();
       
   177 	list<pair<pair<string, string>,string> >::iterator begin1 = aList.begin();
       
   178 	list<pair<pair<string, string>,string> >::iterator end = aList.end();
       
   179 	
       
   180     bool found = false;
       
   181 	while(comparewholeString == true && begin1 != end && found == false)
       
   182 	{// First check both the file matches or not
       
   183 		if(aValue.compare(begin1->first.second)== 0 )
       
   184 			found = true;
       
   185 	 // Now compare base file with  current file after sperating sub dirs of currrent file each time  
       
   186 		else if(compareWholeFileStrings(aValue,begin1->first.second) == true )
       
   187 		     found = true;
       
   188 		else
       
   189 			begin1++;
       
   190 
       
   191 		if(found == true)
       
   192 			return begin1;		
       
   193 	}
       
   194     while(begin != end && found == false)
       
   195     {
       
   196 		if (comparator != 0 && (*comparator)(begin->first, aValue, comparatorparam))
       
   197         {
       
   198             found = true;
       
   199 		} else if (comparator == 0 && compareTo == ELeftValue && begin->first.first == aValue)
       
   200         {
       
   201             found = true;
       
   202 		} else if (comparator == 0 && compareTo == ERightValue && begin->first.second == aValue)
       
   203         {
       
   204             found = true;
       
   205         }
       
   206         else
       
   207         {
       
   208             begin++;
       
   209         }
       
   210     }
       
   211 
       
   212     return begin;
       
   213 }
       
   214 
       
   215 // ----------------------------------------------------------------------------
       
   216 // trimWhiteSpace
       
   217 // Remove any white space from given string
       
   218 // startpos and endpos are optional parameters, defining the string under consideration
       
   219 // returns a NULL, if the input is empty or contains only '\t' or '\n' or ' '
       
   220 // ----------------------------------------------------------------------------
       
   221 //
       
   222 string trimWhiteSpace(string& aString, string::size_type aStartpos, string::size_type aEndpos)
       
   223 {
       
   224 	string ret = aString.substr( aStartpos, aEndpos);
       
   225 
       
   226 	string::size_type startpos = ret.find_first_not_of(" \t\n\r",aStartpos);
       
   227 	string::size_type endpos = ret.find_last_not_of(" \t\n\r");
       
   228 
       
   229 	//if the string is empty, either NULL or only newline character, return a NULL string
       
   230 	if( string::npos == startpos || KEmpty == ret)
       
   231 		return "";
       
   232 	//else return the substr without whitespace at start and end of given string
       
   233 	else
       
   234 		return ret.substr( startpos, endpos - startpos + 1);
       
   235 }
       
   236 
       
   237 
       
   238 // ----------------------------------------------------------------------------
       
   239 // printToString 
       
   240 // Substitute for printf 
       
   241 // ----------------------------------------------------------------------------
       
   242 //
       
   243 #if 0
       
   244 string printToString(string& aControlString, list<string> params)
       
   245 {
       
   246     istringstream input(aControlString);
       
   247     ostringstream output;
       
   248     string ret;
       
   249     char ch;
       
   250     list<string>::iterator paramlist = params.begin();
       
   251     list<string>::iterator paramend = params.end();
       
   252     while(input.get(ch))
       
   253     {
       
   254         if (ch == '%')
       
   255         {
       
   256             char newch = '\0';
       
   257             input.get(newch);
       
   258             if (newch != '\0')
       
   259             {
       
   260                 switch(newch)
       
   261                 {
       
   262                 case '%':
       
   263                     output << ch;
       
   264                     break;
       
   265                 case 's':
       
   266                 case 'd':
       
   267                 case 'f':
       
   268                     {
       
   269                         string temp;
       
   270                         if (paramlist == paramend)
       
   271                         {
       
   272                             throw HAException("Syntax error: Not enough parameters for given control string.");
       
   273                         }
       
   274                         temp = *paramlist;
       
   275                         output << temp;
       
   276                         paramlist++;
       
   277                         break;
       
   278                     }
       
   279                 default:
       
   280                     output << ch << newch;
       
   281                 }
       
   282             } else
       
   283             {
       
   284                 output << ch;
       
   285             }
       
   286         } else
       
   287         {
       
   288             output << ch;
       
   289         }
       
   290     }
       
   291     ret = output.str();
       
   292     return ret;
       
   293 }
       
   294 #endif
       
   295 // ----------------------------------------------------------------------------
       
   296 // getLine 
       
   297 // Get line from input stream
       
   298 // ----------------------------------------------------------------------------
       
   299 //
       
   300 string getLine(istream& aInput)
       
   301 {
       
   302     string line = KEmpty;
       
   303     char ch = 0;
       
   304     while(aInput.get(ch) && ch != '\n')
       
   305     {
       
   306         if (line == KEmpty)
       
   307         {
       
   308             line = "";
       
   309         }
       
   310         line += ch;
       
   311     }
       
   312     if (ch == '\n' && line == KEmpty)
       
   313     {
       
   314         line = "";
       
   315     }
       
   316     return line;
       
   317 }
       
   318 
       
   319 
       
   320 // ----------------------------------------------------------------------------
       
   321 // parseCommandlineParameters 
       
   322 // ----------------------------------------------------------------------------
       
   323 //
       
   324 list<string> parseCommandlineParameters(string args)
       
   325 {
       
   326     istringstream input(args);
       
   327     char ch;
       
   328     string str;
       
   329     bool careForSpace = true;
       
   330     bool strSpace = false;
       
   331     bool whitespace = true;
       
   332     list<string> parmlist;
       
   333     while (input.get(ch))
       
   334     {
       
   335         if (ch == ' ' && careForSpace == true)
       
   336         {
       
   337             if (whitespace == false)
       
   338             {
       
   339                 parmlist.push_back(str);
       
   340             }
       
   341             str = "";
       
   342             whitespace = false;
       
   343             careForSpace = false;
       
   344         }else if (ch == ' ' && strSpace == false)
       
   345         {
       
   346             // Do nothing
       
   347         }else if (ch == '"')
       
   348         {
       
   349             if (strSpace == false)
       
   350             {
       
   351                 careForSpace = false;
       
   352                 strSpace = true;
       
   353             } else
       
   354             {
       
   355                 careForSpace = true;
       
   356                 strSpace = false;
       
   357             }
       
   358             whitespace = false;
       
   359         } else if (ch == '\n')
       
   360         {
       
   361             throw HAException("Syntax error: Newline not accepted in parameter list.");
       
   362         } else
       
   363         {
       
   364             if (careForSpace == false && strSpace == false)
       
   365             {
       
   366                 careForSpace = true;
       
   367             }
       
   368             str += ch;
       
   369             whitespace = false;
       
   370         }
       
   371     }
       
   372     if (strSpace == true)
       
   373     {
       
   374         throw HAException("Syntax error: End of string before ending quotation mark.");
       
   375     }
       
   376     if (str.length() != 0)
       
   377     {
       
   378         parmlist.push_back(str);
       
   379     }
       
   380     return parmlist;
       
   381 }
       
   382 
       
   383 
       
   384 // ----------------------------------------------------------------------------
       
   385 // isNum 
       
   386 // Return if the given string is number
       
   387 // ----------------------------------------------------------------------------
       
   388 //
       
   389 bool isNum(string str)
       
   390 {
       
   391     istringstream strnum(str);
       
   392     char ch;
       
   393     bool ret = false;
       
   394     while(strnum.get(ch))
       
   395     {
       
   396         ret = (0 != isdigit(ch));
       
   397         if (ret == false)
       
   398         {
       
   399             break;
       
   400         }
       
   401     }
       
   402     return ret;
       
   403 }
       
   404 
       
   405 // ----------------------------------------------------------------------------
       
   406 // isAlpha 
       
   407 // Return if the given string is alphan
       
   408 // ----------------------------------------------------------------------------
       
   409 //
       
   410 bool isAlpha(string str)
       
   411 {
       
   412     istringstream strnum(str);
       
   413     char ch;
       
   414     bool ret = false;
       
   415     while(strnum.get(ch))
       
   416     {
       
   417         ret = (0 != isalpha(ch));
       
   418         if (ret == false)
       
   419         {
       
   420             break;
       
   421         }
       
   422     }
       
   423     return ret;
       
   424 }
       
   425 
       
   426 // ----------------------------------------------------------------------------
       
   427 // isAlphaNum 
       
   428 // Return if the given string is alphanumeric
       
   429 // ----------------------------------------------------------------------------
       
   430 //
       
   431 bool isAlphaNum(string str)
       
   432 {
       
   433     istringstream strnum(str);
       
   434     char ch;
       
   435     bool ret = false;
       
   436     while(strnum.get(ch))
       
   437     {
       
   438         ret = (0 != isalnum(ch));
       
   439         if (ret == false)
       
   440         {
       
   441             break;
       
   442         }
       
   443     }
       
   444     return ret;
       
   445 }
       
   446 
       
   447 // ----------------------------------------------------------------------------
       
   448 // toLowerCaseWin 
       
   449 // Convert given string to lower case
       
   450 // ----------------------------------------------------------------------------
       
   451 //
       
   452 string toLowerCaseWin(string aString)
       
   453 {
       
   454     string ret;
       
   455     char* tempvar;
       
   456     unsigned int len = (unsigned int)aString.length();
       
   457 
       
   458     tempvar = new char[len + 1];
       
   459     strcpy(tempvar, aString.c_str());
       
   460 #ifdef __WIN__
       
   461    TO_LOWER_CASE;
       
   462 #else
       
   463    ret=tempvar;
       
   464 #endif
       
   465 	delete[] tempvar;
       
   466     return ret;
       
   467 }
       
   468 
       
   469 // ----------------------------------------------------------------------------
       
   470 // toLowerCaseW
       
   471 // Convert given string to lower case
       
   472 // ----------------------------------------------------------------------------
       
   473 //
       
   474 string toLowerCase(string aString)
       
   475 {
       
   476     string ret;
       
   477     char* tempvar;
       
   478     unsigned int len = (unsigned int)aString.length();
       
   479 
       
   480     tempvar = new char[len + 1];
       
   481     strcpy(tempvar, aString.c_str());
       
   482 
       
   483     TO_LOWER_CASE;
       
   484 
       
   485     delete[] tempvar;
       
   486     return ret;
       
   487 }
       
   488 
       
   489 // ----------------------------------------------------------------------------
       
   490 // toUpperCase 
       
   491 // Convert given string to upper case
       
   492 // ----------------------------------------------------------------------------
       
   493 //
       
   494 string toUpperCase(string aString)
       
   495 {
       
   496     string ret;
       
   497     char* tempvar;
       
   498     unsigned int len = (unsigned int)aString.length();
       
   499 
       
   500     tempvar = new char[len + 1];
       
   501     strcpy(tempvar, aString.c_str());
       
   502 /*
       
   503 #ifdef __WIN__
       
   504     ret = strupr(tempvar);
       
   505 #else
       
   506     for(unsigned int i = 0; i < len; i++)
       
   507     {
       
   508       tempvar[i] = std::toupper(tempvar[i]);
       
   509     }
       
   510     ret = tempvar;
       
   511 #endif
       
   512 */
       
   513     TO_UPPER_CASE;
       
   514     return ret;
       
   515 }
       
   516 
       
   517 // ----------------------------------------------------------------------------
       
   518 // splitString 
       
   519 // Split string
       
   520 // ----------------------------------------------------------------------------
       
   521 //
       
   522 vector<string> splitString(const string& str, char separator)
       
   523 {
       
   524     vector<string> ret;
       
   525     string::size_type pos = str.find(separator);
       
   526     unsigned int lastpos = 0;
       
   527     while(pos != string::npos)
       
   528     {
       
   529         ret.push_back(str.substr(lastpos, pos - lastpos));
       
   530         lastpos = (unsigned int)pos + 1;
       
   531         pos = str.find(separator, lastpos);
       
   532     }
       
   533     if (!str.empty())
       
   534     {
       
   535         ret.push_back(str.substr(lastpos, pos));
       
   536     }
       
   537     return ret;
       
   538 }
       
   539 
       
   540 
       
   541 // ----------------------------------------------------------------------------
       
   542 // compareFiles 
       
   543 // ----------------------------------------------------------------------------
       
   544 //
       
   545 bool compareFiles(const string& tocompare, const string& comparestr, bool tryfilename)
       
   546 {
       
   547     bool ret = false;
       
   548     string tocomp(tocompare);
       
   549     string compstr(comparestr);
       
   550     toLower(tocomp);
       
   551     toLower(compstr);
       
   552     
       
   553     if (tocomp.compare(compstr) == 0)
       
   554     {
       
   555         ret = true;
       
   556     } else if (tryfilename == false)
       
   557     {
       
   558         // Do nothing
       
   559     } else
       
   560     {
       
   561         if (compstr == BBCFileUtils::StripPath(compstr))
       
   562         {
       
   563             tocomp = BBCFileUtils::StripPath(tocomp);
       
   564             if (tocomp.compare(compstr) == 0)
       
   565             {
       
   566                 ret = true;
       
   567             }
       
   568         }
       
   569     }
       
   570     return ret;
       
   571 }
       
   572 
       
   573 // ----------------------------------------------------------------------------
       
   574 // compareFiles 
       
   575 // ----------------------------------------------------------------------------
       
   576 //
       
   577 bool compareFiles(const pair<string, string>& tocompare, const string& comparestr, bool tryfilename)
       
   578 {
       
   579     bool ret = false;
       
   580     //string tempvar(tocompare.second);
       
   581     if (tocompare.second.compare(comparestr) == 0)
       
   582     {
       
   583         ret = true;
       
   584     } else if (tryfilename == false)
       
   585     {
       
   586         // Do nothing
       
   587     } else
       
   588     {
       
   589         if (rightmostDirSeparatorIndex(comparestr) == string::npos || rightmostDirSeparatorIndex(comparestr) == 0)
       
   590         {
       
   591             string tempcomparestr(BBCFileUtils::StripPath(comparestr));
       
   592             string tempvar(BBCFileUtils::StripPath(tocompare.second));           
       
   593             if (tempvar.compare(tempcomparestr) == 0)
       
   594             {
       
   595                 ret = true;
       
   596             }
       
   597         }
       
   598     }
       
   599     return ret;
       
   600 }
       
   601 
       
   602 
       
   603 // ----------------------------------------------------------------------------
       
   604 // rightmostDirSeparatorIndex 
       
   605 // Return the index of the rightmost directory separator
       
   606 // ----------------------------------------------------------------------------
       
   607 //
       
   608 string::size_type rightmostDirSeparatorIndex(const string& aFilename)
       
   609 {
       
   610     string::size_type idx = aFilename.rfind(DIR_SEPARATOR);
       
   611     return idx;
       
   612 }
       
   613 // ----------------------------------------------------------------------------
       
   614 // replaceChar 
       
   615 // Replaces all occurences of the character by another one.
       
   616 // Example: 
       
   617 // <code>
       
   618 // string myStr("c:/temp/config.txt");
       
   619 // replaceChar(myStr, '/', '\\');
       
   620 // std::cout << myStr << std::endl;
       
   621 // </code>
       
   622 // Above example will print out the following string:
       
   623 // c:\temp\config.txt
       
   624 // ----------------------------------------------------------------------------
       
   625 //
       
   626 void replaceChar(string& str, char replaceFrom, char replaceTo)
       
   627 {
       
   628     if( str.length() > 0 )
       
   629     {        
       
   630         string::size_type index = 0;
       
   631         while( (index = str.find(replaceFrom, index)) != string::npos )
       
   632         {     
       
   633             str.replace(index,1,1,replaceTo);
       
   634         }
       
   635     }
       
   636 }
       
   637 
       
   638 
       
   639 // ----------------------------------------------------------------------------
       
   640 // removeBase 
       
   641 // Remove base part from a string
       
   642 // ----------------------------------------------------------------------------
       
   643 //
       
   644 string removeBase(string aString, string aBase)
       
   645 {  
       
   646     string::size_type pos = toLowerCaseWin(aString).find(toLowerCaseWin(aBase),0);
       
   647     if ( pos == string::npos )
       
   648         return NULL;
       
   649     return aString.substr(aBase.length());
       
   650 }
       
   651 
       
   652 // ----------------------------------------------------------------------------
       
   653 // trimLeadingDirSeparator 
       
   654 // Remove any leading directory separators from a string
       
   655 // ----------------------------------------------------------------------------
       
   656 //
       
   657 string trimLeadingDirSeparator(string aString)
       
   658 {
       
   659     int pos = 0;       
       
   660     while ( aString[pos] == '\\' || aString[pos] == '/' )
       
   661         ++pos;
       
   662     if ( (aString.length() - pos) > 255 )
       
   663         return NULL;
       
   664     return aString.substr(pos);
       
   665 }
       
   666 //------------------------------------------------------------------
       
   667 //compareWholeFileStrings 
       
   668 // Comare base file with current file after excluding sub dirs each time until string ends
       
   669 // return true if base string matches with current string anytime after excluding sub dirs.Else false.
       
   670 //------------------------------------------------------------------
       
   671 
       
   672 bool compareWholeFileStrings(const string& aBasestr,const string& aCurstr)
       
   673 {
       
   674 	bool ret = false;
       
   675 	char separator = DIR_SEPARATOR;
       
   676 	unsigned int lastpos = 0;
       
   677 	string tmpStr;
       
   678     string::size_type pos = aCurstr.find(separator);
       
   679 	while(pos != string::npos && ret == false)
       
   680 	{
       
   681 		tmpStr.clear();
       
   682 		lastpos = (unsigned int)pos + 1;
       
   683 		pos = aCurstr.find(separator, lastpos);
       
   684 		if(pos!= string::npos)
       
   685 		{
       
   686 			tmpStr.append(aCurstr.substr(pos, string::npos));
       
   687 			if(tmpStr.compare(aBasestr) == 0)
       
   688 				ret = true;
       
   689 		}
       
   690 	}
       
   691 	return ret;
       
   692 }