imgtools/imglib/host/h_utl.cpp
changeset 0 044383f39525
child 590 360bd6b35136
equal deleted inserted replaced
-1:000000000000 0:044383f39525
       
     1 /*
       
     2 * Copyright (c) 1995-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 the License "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 
       
    20 #define __INCLUDE_CAPABILITY_NAMES__
       
    21 
       
    22 #if defined(_MSVCDOTNET__) || defined(__TOOLS2__)
       
    23 #include <string>
       
    24 #else //!__MSVCDOTNET__
       
    25 #include <string.h>
       
    26 #endif //__MSVCDOTNET__
       
    27 
       
    28 #include <stdarg.h>
       
    29 #include <stdlib.h>
       
    30 
       
    31 #include <e32std.h>
       
    32 #include <e32std_private.h>
       
    33 
       
    34 #include "h_utl.h"
       
    35 
       
    36 #ifdef __LINUX__
       
    37  
       
    38  // Convert the supplied string to uppercase, in-place 
       
    39  char* strupr(char *a)
       
    40     {
       
    41      char *ret = a;
       
    42  
       
    43     while (*a)
       
    44         {
       
    45         *a = toupper(*a);
       
    46         a++;
       
    47          }
       
    48  
       
    49      return ret;
       
    50      }
       
    51  
       
    52  off_t filelength(int filedes)
       
    53      {
       
    54      struct stat buf;
       
    55      if(!fstat(filedes, &buf))
       
    56          {
       
    57         return buf.st_size;
       
    58          }
       
    59     perror("fstat failed");
       
    60    return 0;
       
    61     }
       
    62  
       
    63 #endif
       
    64 
       
    65 TBool PVerbose=ETrue;
       
    66 
       
    67 HPrint H;
       
    68 
       
    69 HPrint::~HPrint()
       
    70 	{
       
    71 	iLogFile.close();
       
    72 	}
       
    73 
       
    74 void HPrint::SetLogFile(TText *aFileName)
       
    75 	{
       
    76 	iLogFile.open((const char *)aFileName);
       
    77 	}
       
    78 
       
    79 
       
    80 /**
       
    81 Closing the logfile.(since 'n' number of drive images can be created)
       
    82 
       
    83 @internalComponent
       
    84 @released
       
    85 
       
    86 */
       
    87 void HPrint::CloseLogFile()
       
    88 	{
       
    89 	if(iLogFile.is_open())
       
    90 		iLogFile.close();
       
    91 	}
       
    92 
       
    93 TInt HPrint::PrintString(TPrintType aType,const char *aFmt,...)
       
    94 //
       
    95 // Print text, noting where to send it.
       
    96 //
       
    97 	{
       
    98 
       
    99 	TInt r=KErrNone;
       
   100 	va_list list;
       
   101 	va_start(list,aFmt);
       
   102 	_vsnprintf((char *)iText,KMaxStringLength,aFmt,list);
       
   103 	va_end(list);
       
   104 	switch (aType)
       
   105 		{
       
   106 	case EAlways:
       
   107 		cout << iText;
       
   108 		iLogFile << iText;
       
   109 		break;
       
   110 	case EScreen:
       
   111 		cout << iText;
       
   112 		break;
       
   113 	case ELog:
       
   114 		if (iVerbose)
       
   115 			cout << iText;
       
   116 		iLogFile << iText;
       
   117 		break;
       
   118 	case EWarning:
       
   119 		cerr << "WARNING: " << iText;
       
   120 		iLogFile << "WARNING: "<<iText;
       
   121 		break;
       
   122 	case EError:
       
   123 		cerr << "ERROR: " << iText;
       
   124 		iLogFile << "ERROR: " << iText;
       
   125 		r=KErrGeneral;
       
   126 		break;
       
   127 	case EPeError:
       
   128 		if (PVerbose)
       
   129 			{
       
   130 			cerr << "ERROR: " << iText;
       
   131 			iLogFile << "ERROR: " << iText;
       
   132 			}
       
   133 		r=KErrGeneral;
       
   134 		break;
       
   135 	case ESevereError:
       
   136 		cerr << "ERROR: " << iText;
       
   137 		iLogFile << "ERROR: " << iText;
       
   138 		r=KErrGeneral;
       
   139 		break;
       
   140 	case EDiagnostic:
       
   141 		cerr << "DIAGNOSTIC MESSAGE: " << iText;
       
   142 		iLogFile << "DIAGNOSTIC MESSAGE: "<<iText;
       
   143 		break;
       
   144 	default:
       
   145 		cerr << "ERROR: Invalid print type" << endl;
       
   146 		r=KErrGeneral;
       
   147 		}
       
   148 	cout.flush();
       
   149 	iLogFile.flush();
       
   150 	return r;
       
   151     }
       
   152 
       
   153 TVersion::TVersion()
       
   154 	{}
       
   155 TVersion::TVersion(TInt aMajor, TInt aMinor, TInt aBuild)
       
   156 	: iMajor((TInt8)aMajor), iMinor((TInt8)aMinor), iBuild((TInt16)aBuild)
       
   157 	{}
       
   158 #ifdef __TOOLS2__
       
   159 istringstream &operator>>(istringstream &is, TVersion &aVersion)
       
   160 #else
       
   161 istrstream &operator>>(istrstream &is, TVersion &aVersion)
       
   162 #endif
       
   163 //
       
   164 // Input a TVersion with syntax: major[.minor][(build)]
       
   165 //	
       
   166 	{
       
   167 #ifdef __TOOLS2__
       
   168 string tmp = is.str();
       
   169 const char *str=tmp.c_str();
       
   170 #else
       
   171 #ifdef __LINUX__
       
   172     char *str = is.rdbuf()->str();
       
   173 #else
       
   174     char *str=is.str();
       
   175 #endif
       
   176 #endif
       
   177 
       
   178 
       
   179 	TInt build=0;
       
   180 	memset(&aVersion, sizeof(TVersion), 0);	
       
   181 	TInt i;
       
   182 	TInt len=strlen(str);
       
   183 	for (i=0; i<len; i++)
       
   184 		if (str[i]=='(')
       
   185 			break;
       
   186 	if (i<len)
       
   187 		build=atoi(str+i+1);
       
   188 	aVersion.iMajor = (TInt8)Min(KMaxTInt8, atoi(str));
       
   189 	int majorV = atoi(str);
       
   190 	// iMajor is defined as TInt8 so it should not be bigger than 127
       
   191 	if (majorV > 127)
       
   192 		{ 
       
   193 		cout << "\n Warning: major version must be in range 0 - 127 \n";
       
   194 		}
       
   195 	char* pMinor = strchr(str, '.');
       
   196 	if (pMinor)
       
   197 		{
       
   198 		pMinor++; 
       
   199 		aVersion.iMinor = (TInt8)Min(KMaxTInt8, atoi(pMinor));
       
   200 		int minorV = atoi(pMinor);
       
   201 		// iMinor is defined as TInt8 so it should not be bigger than 127
       
   202 		if (minorV > 127)
       
   203 			{ 
       
   204 			cout << "\n Warning: minor version must be in range 0 - 127 \n";
       
   205 			}
       
   206 		}	
       
   207 	aVersion.iBuild=(TInt16)build;
       
   208 	return is;
       
   209 	}
       
   210 
       
   211 TInt Locate(const char *aString, char aChar)
       
   212 //
       
   213 // Locate aChar in aString
       
   214 //
       
   215 	{
       
   216 
       
   217 	if (aString==NULL)
       
   218 		return KErrNotFound;
       
   219 	TInt i=0;
       
   220 	while (*aString!=0)
       
   221 		{
       
   222 		if (*aString==aChar)
       
   223 			return i;
       
   224 		aString++;
       
   225 		i++;
       
   226 		}
       
   227 	return KErrNotFound;
       
   228 	}
       
   229 
       
   230 
       
   231 #define KHoursToMicroSeconds	Int64(3600000000UL) 
       
   232 #define KDaysToMicroSeconds		(Int64(24)*KHoursToMicroSeconds)
       
   233 const TInt KMinutesToMicroSeconds = 60000000;
       
   234 const TInt KSecondsToMicroSeconds =  1000000;
       
   235 
       
   236 const TInt8 mTab[2][12]=
       
   237     {
       
   238     {31,28,31,30,31,30,31,31,30,31,30,31}, // 28 days in Feb
       
   239     {31,29,31,30,31,30,31,31,30,31,30,31}  // 29 days in Feb
       
   240     };
       
   241 
       
   242 TInt Time::LeapYearsUpTo(TInt aYear)
       
   243 //
       
   244 // from 0AD to present year according to the rule above
       
   245 //
       
   246 	{
       
   247 
       
   248 	if (aYear<=0)
       
   249 		return(aYear/4);
       
   250 	if (aYear<=1600)
       
   251 		return(1+((aYear-1)/4));
       
   252 	TInt num=401; // 1600/4+1
       
   253 	aYear-=1601;
       
   254 	num+=(aYear/4-aYear/100+aYear/400);
       
   255 	return(num);
       
   256 	}
       
   257 
       
   258 TBool Time::IsLeapYear(TInt aYear)
       
   259 //
       
   260 // up to and including 1600 leap years were every 4 years,since then leap years are every 4 years unless
       
   261 // the year falls on a century which is not divisible by 4 (ie 1900 wasnt,2000 will be)
       
   262 // for simplicity define year 0 as a leap year
       
   263 //
       
   264 	{
       
   265 
       
   266 	if (aYear>1600)
       
   267     	return(!(aYear%4) && (aYear%100 || !(aYear%400)));
       
   268 	return(!(aYear%4));
       
   269 	}
       
   270 
       
   271 
       
   272 Int64 ConvertTime(TInt aDay, TInt aMonth, TInt aYear, TInt aHour, TInt aMinute, TInt aSecond, TInt aMilliSeconds)
       
   273 //
       
   274 // converts TDateTime into a TTime, doesnt check for overflows
       
   275 //
       
   276 	{
       
   277 	
       
   278 	TInt days=365*aYear+Time::LeapYearsUpTo(aYear);
       
   279 	TBool isleap=Time::IsLeapYear(aYear);
       
   280 	for (TInt ii=0; ii<aMonth; ii++)
       
   281 	    days+=(mTab[isleap][ii]);	
       
   282 	days+=aDay;
       
   283 	TInt sum=aMilliSeconds+aSecond*KSecondsToMicroSeconds;
       
   284 	return((Int64(days)*KDaysToMicroSeconds+Int64(aHour)*KHoursToMicroSeconds) 
       
   285 			+(Int64(KMinutesToMicroSeconds)*Int64(aMinute)+Int64(sum))); 
       
   286 	}
       
   287 	
       
   288 TInt StringToTime(Int64 &aTime, char *aString)
       
   289 //
       
   290 // Convert string to time. String is in the format:
       
   291 //
       
   292 // dd/mm/yyyy hh:mm:ss.mmmmmmm
       
   293 //
       
   294 	{
       
   295 	TInt day=1;
       
   296 	TInt month=1;
       
   297 	TInt year=1997;
       
   298 	TInt hour=10;
       
   299 	TInt minute=10;
       
   300 	TInt sec=0;
       
   301 	TInt mill=0;
       
   302 	char ch;
       
   303 	#ifdef __TOOLS2__
       
   304 	istringstream val(aString);
       
   305 	#else
       
   306 	istrstream val(aString,strlen(aString));
       
   307 	#endif
       
   308 	val >> dec >> day; // locks istrstream in decimal mode for further extractions
       
   309 	val >> ch;
       
   310 	if (ch!='/')
       
   311 		return KErrGeneral;
       
   312 	val >> month;
       
   313 	val >> ch;
       
   314 	if (ch!='/')
       
   315 		return KErrGeneral;
       
   316 	val >> year;
       
   317 	val >> ch;
       
   318 
       
   319 	if (ch=='_')
       
   320 		{
       
   321 		// time too.
       
   322 		val >> hour;
       
   323 		val >> ch;
       
   324 		if (ch!=':')
       
   325 			return KErrGeneral;
       
   326 		val >> minute;
       
   327 		val >> ch;
       
   328 		if (ch!=':')
       
   329 			return KErrGeneral;
       
   330 		val >> sec;
       
   331 		val >> ch;
       
   332 		if (ch=='.')
       
   333 			{
       
   334 			val >> mill;
       
   335 			}
       
   336 		}
       
   337 
       
   338 	if (day<1 || day>31)
       
   339 		return KErrArgument;
       
   340 	if (month<1 || month>12)
       
   341 		return KErrArgument;
       
   342 	if (year<1970 || year>2060)
       
   343 		return KErrArgument;
       
   344 	if (hour<0 || hour>23)
       
   345 		return KErrArgument;
       
   346 	if (minute<0 || minute>59)
       
   347 		return KErrArgument;
       
   348 	if (sec<0 || sec>59)
       
   349 		return KErrArgument;
       
   350 	if (mill<0 || mill>999999)
       
   351 		return KErrArgument;
       
   352 
       
   353 	aTime=ConvertTime(day-1, month-1, year, hour, minute, sec, mill);
       
   354 	return KErrNone;
       
   355 	}
       
   356 
       
   357 void ByteSwap(TUint &aVal)
       
   358 	{
       
   359 	TUint t0=aVal & 0xff;
       
   360 	TUint t1=(aVal>>8)  & 0xff;
       
   361 	TUint t2=(aVal>>16) & 0xff;
       
   362 	TUint t3=aVal>>24;
       
   363 	aVal=(t0 << 24) | (t1 << 16) | (t2 << 8) | (t3);
       
   364 	}
       
   365 
       
   366 void ByteSwap(TUint16 &aVal)
       
   367 	{
       
   368 	TUint16 t0=(TUint16)((aVal >> 8) & 0xff);
       
   369 	TUint16 t1=(TUint16)(aVal & 0xff);
       
   370 	aVal=(TUint16)((t1 << 8) | t0);
       
   371 	}
       
   372 
       
   373 void ByteSwap(TUint *aPtr, TInt aSize)
       
   374 	{
       
   375 
       
   376 	while ((aSize-=4)>=0)
       
   377 		ByteSwap(*aPtr++);
       
   378 	}
       
   379 
       
   380 TBool IsBracketedHex(const char* s, const char* brackets, TInt digits, TUint32& aValue)
       
   381 	{
       
   382 	if (s[0]!=brackets[0] || s[1+digits]!=brackets[1])
       
   383 		return EFalse;
       
   384 	TInt i;
       
   385 	TUint32 x = 0;
       
   386 	for (i=1; i<=digits; ++i)
       
   387 		{
       
   388 		TInt c = s[i];
       
   389 		if (c>='a' && c<='z') c-=32;
       
   390 		if (c<'0' || (c>'9' && c<'A') || c>'F')
       
   391 			return EFalse;
       
   392 		c-='0';
       
   393 		if (c>9) c-=7;
       
   394 		x = (x<<4) | (TUint32)c;
       
   395 		}
       
   396 	aValue = x;
       
   397 	return ETrue;
       
   398 	}
       
   399 
       
   400 TInt CheckForDecimalVersion(const char* begin, const char* s, TUint32& aValue)
       
   401 	{
       
   402 	aValue = 0;
       
   403 	if (s <= begin || *s != '}')
       
   404 		return 0;
       
   405 	TUint32 v[2] = {0,0};
       
   406 	TUint32 m = 1;
       
   407 	TInt pos = 0;
       
   408 	const char* s0 = s + 1;
       
   409 	for (--s; s >= begin; --s)
       
   410 		{
       
   411 		int c = *s;
       
   412 		if (c >= '0' && c <= '9')
       
   413 			{
       
   414 			v[pos] += m * (c - '0');
       
   415 			if (v[pos] >= 65536u)
       
   416 				return 0;
       
   417 			m *= 10;
       
   418 			}
       
   419 		else if (c == '.')
       
   420 			{
       
   421 			m = 1;
       
   422 			if (++pos >= 2)
       
   423 				return 0;
       
   424 			}
       
   425 		else if (c == '{')
       
   426 			break;
       
   427 		else
       
   428 			return 0;
       
   429 		}
       
   430 	if (s < begin)
       
   431 		return 0;
       
   432 	aValue = (v[1] << 16) | v[0];
       
   433 	return s0 - s;
       
   434 	}
       
   435 
       
   436 // Decompose a name of the form NAME{MMMMmmmm}[UUUUUUUU].EXT where the bracketed
       
   437 // sections and extension are both optional.
       
   438 // Return a newly malloc-ed string containing NAME.EXT
       
   439 // Set aUid = 0xUUUUUUUU if present, 0 if not
       
   440 // Set aModuleVersion = 0xMMMMmmmm if present, 0 if not
       
   441 // Set aFlags according to which of these are present
       
   442 char* SplitFileName(const char* aName, TUint32& aUid, TUint32& aModuleVersion, TUint32& aFlags)
       
   443 	{
       
   444 	TFileNameInfo f(aName, ETrue);
       
   445 	aUid = f.iUid3;
       
   446 	aModuleVersion = f.iModuleVersion;
       
   447 	aFlags = f.iFlags;
       
   448 	TInt nl = f.iBaseLength;
       
   449 	TInt el = f.iTotalLength - f.iExtPos;
       
   450 	TInt tl = nl + el;
       
   451 	char* t = (char*)malloc(tl + 1);
       
   452 	if (t)
       
   453 		{
       
   454 		memcpy(t, aName, nl);
       
   455 		if (el)
       
   456 			memcpy(t + nl, aName + f.iExtPos, el);
       
   457 		t[tl] = 0;
       
   458 		}
       
   459 	return t;
       
   460 	}
       
   461 
       
   462 
       
   463 // Decompose a name of the form NAME{MMMMmmmm}.EXT where the bracketed
       
   464 // sections and extension are both optional.
       
   465 // Return a newly malloc-ed string containing NAME.EXT
       
   466 // Set aModuleVersion = 0xMMMMmmmm if present, 0 if not
       
   467 // Set aFlags according to whether version present
       
   468 char* SplitFileName(const char* aName, TUint32& aModuleVersion, TUint32& aFlags)
       
   469 	{
       
   470 	TFileNameInfo f(aName, EFalse);
       
   471 	aModuleVersion = f.iModuleVersion;
       
   472 	aFlags = f.iFlags;
       
   473 	TInt nl = f.iBaseLength;
       
   474 	TInt el = f.iTotalLength - f.iExtPos;
       
   475 	TInt tl = nl + el;
       
   476 	char* t = (char*)malloc(tl + 1);
       
   477 	if (t)
       
   478 		{
       
   479 		memcpy(t, aName, nl);
       
   480 		if (el)
       
   481 			memcpy(t + nl, aName + f.iExtPos, el);
       
   482 		t[tl] = 0;
       
   483 		}
       
   484 	return t;
       
   485 	}
       
   486 
       
   487 
       
   488 // Parse a filename and convert decimal version number to hex
       
   489 char* NormaliseFileName(const char* aName)
       
   490 {
       
   491 	//convert forward slashes into back slashes.
       
   492 	char* filename = strdup(aName);  //prevent violated access from stack.
       
   493 	char* fwdslashfinder = filename;
       
   494 	fwdslashfinder=strstr(fwdslashfinder, "/");
       
   495 	while(fwdslashfinder)
       
   496 	  {
       
   497 		*fwdslashfinder++ = '\\';
       
   498 		fwdslashfinder=strstr(fwdslashfinder, "/");
       
   499 	  }
       
   500 
       
   501 	//normalize filename.
       
   502 	TFileNameInfo f(filename, EFalse);
       
   503 	TInt nl = f.iBaseLength;
       
   504 	TInt el = f.iTotalLength - f.iExtPos;
       
   505 	TInt tl = nl + el;
       
   506 	if (f.iFlags & EVerPresent)
       
   507 		tl += 10;
       
   508 	char* t = (char*)malloc(tl + 1);
       
   509 	if (t)
       
   510 		{
       
   511 		memcpy(t, filename, nl);
       
   512 		if (f.iFlags & EVerPresent)
       
   513 			sprintf(t + nl, "{%08lx}%s", f.iModuleVersion, filename + f.iExtPos);
       
   514 		else if (el)
       
   515 			memcpy(t + nl, filename + f.iExtPos, el);
       
   516 		t[tl] = 0;
       
   517 		}
       
   518 	free(filename);
       
   519 
       
   520 	return t;
       
   521 }
       
   522 
       
   523 TFileNameInfo::TFileNameInfo(const char* aFileName, TBool aLookForUid)
       
   524 	{
       
   525 	iFileName = aFileName;
       
   526 	TInt l = strlen(aFileName);
       
   527 	iTotalLength = l;
       
   528 	TInt remain = l;
       
   529 	iFlags = 0;
       
   530 	iUid3 = 0;
       
   531 	iModuleVersion = 0;
       
   532 	iBaseLength = l;
       
   533 	iExtPos = l;
       
   534 	const char* s = iFileName + l;
       
   535 	for (; s>=iFileName && *s!='.' && *s!='}' && (!aLookForUid || *s!=']'); --s) {}
       
   536 	if (s<iFileName)
       
   537 		return;
       
   538 	if (*s == '.')
       
   539 		{
       
   540 		iExtPos = s - iFileName;
       
   541 		if (iExtPos == 0)
       
   542 			{
       
   543 			iBaseLength = 0;
       
   544 			return;
       
   545 			}
       
   546 		remain = iExtPos;
       
   547 		--s;
       
   548 		}
       
   549 	else if (s != iFileName + l)
       
   550 		return;
       
   551 	if (aLookForUid && remain>=10 && IsBracketedHex(s-9, "[]", 8, iUid3))
       
   552 		{
       
   553 		iFlags |= EUidPresent;
       
   554 		remain -= 10;
       
   555 		s -= 10;
       
   556 		}
       
   557 	if (remain>=10 && IsBracketedHex(s-9, "{}", 8, iModuleVersion))
       
   558 		{
       
   559 		iFlags |= EVerPresent;
       
   560 		remain -= 10;
       
   561 		s -= 10;
       
   562 		}
       
   563 	else
       
   564 		{
       
   565 		TInt n = CheckForDecimalVersion(iFileName, s, iModuleVersion);
       
   566 		if (n>0)
       
   567 			{
       
   568 			iFlags |= EVerPresent;
       
   569 			remain -= n;
       
   570 			s -= n;
       
   571 			}
       
   572 		}
       
   573 	iBaseLength = remain;
       
   574 	}
       
   575 
       
   576 
       
   577 #define PARSE_CAPABILITIES_ERROR(aMessage) Print(EError, "%s\n",aMessage)
       
   578 #define PARSE_CAPABILITIES_ERROR2(aMessage1,aMessage2) Print(EError, "%s%s\n",aMessage1,aMessage2)
       
   579 
       
   580 TInt ParseCapabilitiesArg(SCapabilitySet& aCapabilities, const char *aText)
       
   581 //
       
   582 // This is a cun'n'paste copy of the function in BASE\WINS\SPECIFIC\PROPERTY.CPP
       
   583 // Keep both of these versions up to date with each other
       
   584 //
       
   585 	{
       
   586 	memset(&aCapabilities,0,sizeof(aCapabilities));
       
   587 	char c;
       
   588 	while((c=*aText)!=0)
       
   589 		{
       
   590 		if(c<=' ')
       
   591 			{
       
   592 			++aText;
       
   593 			continue;
       
   594 			}
       
   595 		int invert=0;
       
   596 		if(c=='+')
       
   597 			{
       
   598 			++aText;
       
   599 			c=*aText;
       
   600 			}
       
   601 		if(c=='-')
       
   602 			{
       
   603 			invert=1;
       
   604 			++aText;
       
   605 			}
       
   606 		const char* name = aText;
       
   607 		while((c=*aText)>' ')
       
   608 			{
       
   609 			if(c=='-' || c=='+')
       
   610 				break;
       
   611 			++aText;
       
   612 			}
       
   613 		TUint n = aText-name; 
       
   614 		TInt i; 
       
   615 
       
   616 		if(n==3 && strnicmp("all",name,n)==0)
       
   617 			{
       
   618 			if(invert)
       
   619 				{
       
   620 				PARSE_CAPABILITIES_ERROR("Capability '-ALL' not allowed");
       
   621 				return KErrArgument;
       
   622 				}
       
   623 			for(i=0; i<ECapability_Limit; i++)
       
   624 				{
       
   625 				if(CapabilityNames[i])
       
   626 					aCapabilities[i>>5] |= (1<<(i&31));
       
   627 				}
       
   628 			continue;
       
   629 			}
       
   630 
       
   631 		if(n==4 && strnicmp("none",name,n)==0)
       
   632 			{
       
   633 			if(invert)
       
   634 				{
       
   635 				PARSE_CAPABILITIES_ERROR("Capability '-NONE' not allowed");
       
   636 				return KErrArgument;
       
   637 				}
       
   638 			memset(&aCapabilities,0,sizeof(aCapabilities));
       
   639 			continue;
       
   640 			}
       
   641 
       
   642 		for(i=0; i<ECapability_Limit; i++)
       
   643 			{
       
   644 			const char* cap = CapabilityNames[i];
       
   645 			if(!cap)
       
   646 				continue;
       
   647 			if((TUint)strlen(cap)!=n) 
       
   648 				continue;
       
   649 			if(strnicmp(cap,name,n)!=0)
       
   650 				continue;
       
   651 			break;
       
   652 			}
       
   653 		if(i>=ECapability_Limit)
       
   654 			{
       
   655 			char badName[32];
       
   656 			if(n>=sizeof(badName)) n=sizeof(badName)-1;
       
   657 			memcpy(badName,name,n);
       
   658 			badName[n]=0;
       
   659 			PARSE_CAPABILITIES_ERROR2("Unrecognised capability name: ",badName);
       
   660 			return KErrArgument;
       
   661 			}
       
   662 		if(invert)
       
   663 			aCapabilities[i>>5] &= ~(1<<(i&31));
       
   664 		else
       
   665 			aCapabilities[i>>5] |= (1<<(i&31));
       
   666 		}
       
   667 	return KErrNone;
       
   668 	}
       
   669 
       
   670 TInt ParseBoolArg(TBool& aValue, const char *aText)
       
   671 	{
       
   672 	if (_stricmp(aText, "on")==0 || _stricmp(aText, "yes")==0 || _stricmp(aText, "1")==0 || strlen(aText)==0)
       
   673 		{
       
   674 		aValue = ETrue;
       
   675 		return KErrNone;
       
   676 		}
       
   677 	if (_stricmp(aText, "off")==0 || _stricmp(aText, "no")==0 || _stricmp(aText, "0")==0 )
       
   678 		{
       
   679 		aValue = EFalse;
       
   680 		return KErrNone;
       
   681 		}
       
   682 	Print(EError, "Expected a boolean on/off value but found %s\n",aText);
       
   683 	return KErrArgument;
       
   684 	}