pkiutilities/ocsp/test/TEFparser.cpp
changeset 0 164170e6151a
equal deleted inserted replaced
-1:000000000000 0:164170e6151a
       
     1 //
       
     2 // Copyright (c) 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 #include <s32file.h>
       
    19 #include "bautils.h" 
       
    20 #include "TEFparser.h"
       
    21 #include "utf.h" 
       
    22 
       
    23 _LIT(KReturn, "\r");
       
    24 _LIT(KNewline, "\n");
       
    25 _LIT(KOpenBrk, "[");
       
    26 _LIT(KCloseBrk, "]");
       
    27 _LIT(KSpace, " ");
       
    28 _LIT(KTab, "\t");
       
    29 
       
    30 _LIT(KIniExtension, ".ini");
       
    31 _LIT(KScriptFile, ".script");
       
    32 _LIT(KDot, ".");
       
    33 
       
    34 #define KIniSectionIdLength 512
       
    35 
       
    36 
       
    37 EXPORT_C TBool TEFparser::FileType(const TDesC& aBuf)
       
    38 	{
       
    39 	
       
    40 	TInt pos1 = aBuf.Find(KDot);
       
    41 	
       
    42 	TPtrC fileType = aBuf.Mid(pos1);
       
    43 	
       
    44 	if(fileType == KScriptFile)
       
    45 		{
       
    46 		return ETrue;
       
    47 		}
       
    48 	else
       
    49 		{
       
    50 		return EFalse;
       
    51 		}
       
    52 	}
       
    53 
       
    54 EXPORT_C TInt TEFparser::ReadFileLC(RFs& aFs, 
       
    55 										const TDesC& aScriptFilepath,
       
    56 										TPtrC& aScriptFileContents )
       
    57 	{
       
    58 	
       
    59 	RFile scriptFile;
       
    60 	TInt err = scriptFile.Open(aFs, aScriptFilepath, EFileStream | EFileRead | EFileShareAny);
       
    61 	if (err != KErrNone)
       
    62 		{	
       
    63 		return KErrNotFound;
       
    64 		}
       
    65 	// gets size of ini file
       
    66 	TInt size;
       
    67 	scriptFile.Size(size);
       
    68 	scriptFile.Close();
       
    69 			
       
    70 	// reads ini into iTestInput
       
    71 	HBufC* scriptFileH16 = HBufC::NewLC(size);
       
    72 	HBufC8* scriptFileH = HBufC8::NewLC(size);
       
    73 	
       
    74 	//RBuf scriptFileR;
       
    75 	TPtr8 pInput = scriptFileH->Des(); 
       
    76 	TPtr pInput16 = scriptFileH16->Des(); 
       
    77 	
       
    78 	pInput.SetLength(size); 
       
    79 	
       
    80 	RFileReadStream stream;
       
    81 	User::LeaveIfError(stream.Open(aFs, aScriptFilepath, EFileStream | EFileRead | EFileShareReadersOnly));
       
    82 	CleanupClosePushL(stream);
       
    83 	stream.ReadL(pInput,size);
       
    84 	
       
    85 //	stream.ReadL(pInput, size);
       
    86 	
       
    87 	pInput16.Copy(pInput);
       
    88 	//scriptFileR.Assign(scriptFileH16);
       
    89 	
       
    90 	aScriptFileContents.Set(*scriptFileH16);
       
    91 	
       
    92 	CleanupStack::PopAndDestroy(2); // stream, testInput
       
    93 	
       
    94 	return KErrNone;
       
    95 	}
       
    96 
       
    97 EXPORT_C TPtrC TEFparser::GetRunTestStep(TPtrC& aBuf, 
       
    98 									const TDesC& aTag,
       
    99 									TInt& aPos,
       
   100 									TInt& aError)
       
   101 	{
       
   102 	TInt endPos = 0;
       
   103 	TInt startPos = 0;
       
   104 	TInt tempPos = 0;
       
   105 	
       
   106 	TPtrC data = aBuf.Mid(aPos);
       
   107 	
       
   108 	tempPos = data.Find(aTag);
       
   109 	
       
   110 	if (tempPos != KErrNotFound)
       
   111 		{
       
   112 		tempPos += aTag.Length();
       
   113 //			
       
   114 		TPtrC temprunStepData = data.Mid(tempPos);		
       
   115 //		
       
   116 		endPos = temprunStepData.Find(KNewline);
       
   117 		if (endPos == KErrNotFound)
       
   118 			{
       
   119 			endPos = temprunStepData.Find(KReturn);
       
   120 			}
       
   121 		if (endPos == KErrNotFound)
       
   122 			{
       
   123 			endPos = temprunStepData.Length();
       
   124 			}
       
   125 //		
       
   126 		TInt len = 0;
       
   127 		len = (endPos - startPos) + 1;
       
   128 		TPtrC runStepData = temprunStepData.Mid(startPos,len);
       
   129 		aPos += tempPos + runStepData.Length();
       
   130 		aError = KErrNone;
       
   131 		return Trim(runStepData);
       
   132 		}
       
   133 	else
       
   134 		{
       
   135 		aError = KErrNotFound;
       
   136 		return TPtrC();
       
   137 		}
       
   138 
       
   139 	}
       
   140 
       
   141 EXPORT_C TPtrC TEFparser::Trim(const TDesC& aBuf)
       
   142 	{
       
   143 	TInt startPos = 0;
       
   144 	TInt endPos = 0;
       
   145 	TInt i = 0, j = 0;
       
   146 		
       
   147 	for(i = 0; i < aBuf.Length() ; i ++)
       
   148 		{
       
   149 		TPtrC tmpChar = aBuf.Mid(i,1);
       
   150 		if(tmpChar != KSpace && tmpChar != KTab && tmpChar != KReturn && tmpChar != KNewline )
       
   151 			{
       
   152 			startPos = i;
       
   153 			break;
       
   154 			}
       
   155 		}
       
   156 	for(j = aBuf.Length()-1; j >= 0 ; j --)
       
   157 		{
       
   158 		TPtrC tmpChar1 = aBuf.Mid(j,1);
       
   159 		if(tmpChar1 != KSpace && tmpChar1 != KTab && tmpChar1 != KReturn && tmpChar1 != KNewline )
       
   160 			{
       
   161 			endPos = j;
       
   162 			break;
       
   163 			}
       
   164 		}
       
   165 	if(endPos < startPos)
       
   166 		{
       
   167 		endPos = aBuf.Length();
       
   168 		}
       
   169 	
       
   170 //	TInt len = aBuf.Length() - (startPos + endPos);
       
   171 	return aBuf.Mid(startPos, endPos - startPos + 1);
       
   172 	
       
   173 	}
       
   174 
       
   175 EXPORT_C TInt TEFparser::GetIniFileInfo(TDesC& aBuf, 
       
   176 											  TPtrC& aIniFileName, 
       
   177 											  TPtrC& aIniSectionName)
       
   178 	{
       
   179 	TInt pos =0;
       
   180 	TInt startPos = 0, endPos = 0;
       
   181 	
       
   182 	
       
   183 	TPtrC temp = aBuf.Mid(pos);
       
   184 	
       
   185 	endPos = temp.Find(KIniExtension);
       
   186 	endPos += 4;
       
   187 	
       
   188 	if (endPos != KErrNotFound)
       
   189 		{
       
   190 		TInt len = endPos - startPos;
       
   191 		TPtrC iniFileName = temp.Mid(startPos, len);
       
   192 		aIniFileName.Set(iniFileName);
       
   193 		
       
   194 		TPtrC iniSectionName = temp.Mid(iniFileName.Length());
       
   195 		aIniSectionName.Set(Trim(iniSectionName));
       
   196 		
       
   197 		return KErrNone;
       
   198 		}
       
   199 	else
       
   200 		{
       
   201 		return KErrNotFound;
       
   202 		}
       
   203 	
       
   204 	}
       
   205 
       
   206 EXPORT_C TInt TEFparser::GetiniPath( TDesC& aBuf, 
       
   207 									const TDesC& aScriptPath,
       
   208 									TDes& aIniFilePath)
       
   209 	{
       
   210 	
       
   211 	TInt err = KErrNone;	
       
   212 	TInt endPos = aScriptPath.LocateReverse('\\');
       
   213 	if (endPos == KErrNotFound)
       
   214 		{
       
   215 		err = KErrNotFound;
       
   216 		}
       
   217 	else
       
   218 		{
       
   219 		aIniFilePath.Copy(aBuf);
       
   220 		aIniFilePath.Insert(0, aScriptPath.Left(endPos+1));
       
   221 		}
       
   222 	return err;
       
   223 	}
       
   224 
       
   225 EXPORT_C TInt TEFparser::GetSectionData(TDesC& aScriptFilepath, TPtrC& aSectiontag, TDesC16 &aTocspTestFile, RFs& aFs)
       
   226 	{
       
   227 	
       
   228 	TInt err = KErrNone;	
       
   229 	TInt pos = 0;
       
   230 	RFile file;
       
   231 	
       
   232 	// open the .ini file
       
   233 	if (BaflUtils::FolderExists(aFs, aScriptFilepath))
       
   234 		{		
       
   235 		if (BaflUtils::FileExists( aFs, aScriptFilepath ))
       
   236 			{	
       
   237 			file.Open(aFs, aScriptFilepath, EFileRead | EFileShareAny);
       
   238 			
       
   239 			TFileText aLineReader;
       
   240 			TBuf<256> iLine;
       
   241 			TBuf<256> tempsectID;
       
   242 
       
   243 			// create the section name to search for
       
   244 			tempsectID.Copy(KOpenBrk);
       
   245 			tempsectID.Append(aSectiontag);
       
   246 			tempsectID.Append(KCloseBrk);
       
   247 			
       
   248 			// read the ini file a line at a time until you find the the section name
       
   249 			aLineReader.Set(file);		
       
   250 			TInt foundTag = -1;
       
   251 			while (err != KErrEof && foundTag != 0)
       
   252 				{
       
   253 				err = aLineReader.Read(iLine);
       
   254 				if (err != KErrEof)
       
   255 					foundTag =  iLine.Find(tempsectID);
       
   256 				}
       
   257 			
       
   258 			// create the next open bracket to search for		
       
   259 			TBuf<2> tempopenBrk;
       
   260 			tempopenBrk.Copy(KOpenBrk);
       
   261 			
       
   262 			RFile testfile;	
       
   263 			err = KErrNone;
       
   264 			foundTag = -1;
       
   265 
       
   266 			// while not at the end of the file and not found the next open bracket
       
   267 			while (err != KErrEof && foundTag != 0)
       
   268 				{
       
   269 
       
   270 				// get the next line of the .ini file
       
   271 				err = aLineReader.Read(iLine);
       
   272 				if (err != KErrEof)
       
   273 					{
       
   274 
       
   275 					// if the line of the file doesn't contain an open bracket, we are still in the section body
       
   276 					foundTag =  iLine.Find(tempopenBrk);
       
   277 					if (BaflUtils::FolderExists(aFs, aTocspTestFile) && foundTag != 0)
       
   278 						{		
       
   279 						// open the test file we are going to write all our section info into
       
   280 						if (BaflUtils::FileExists( aFs, aTocspTestFile ))
       
   281 							{	
       
   282 							testfile.Open(aFs, aTocspTestFile, EFileWrite|EFileShareAny);
       
   283 							testfile.Seek(ESeekEnd, pos);
       
   284 							}
       
   285 						else
       
   286 							{	
       
   287 							User::LeaveIfError(testfile.Create(aFs, aTocspTestFile, EFileWrite|EFileShareAny));
       
   288 							testfile.Open(aFs, aTocspTestFile, EFileWrite|EFileShareAny);
       
   289 							}
       
   290 						// append to line of the file end of line characters
       
   291 						iLine.Append(_L("\r\n"));
       
   292 
       
   293 						// write line of the code out to the test file in UNICODE format 
       
   294 						TPtrC8 tmpPoint((TText8*)iLine.Ptr(),iLine.Size());
       
   295 						testfile.Write(tmpPoint); 
       
   296 						
       
   297 						testfile.Flush();							
       
   298 						}
       
   299 					testfile.Close();
       
   300 					}
       
   301 				}
       
   302 			}
       
   303 		}
       
   304 		return KErrNone;
       
   305 
       
   306 	}
       
   307 
       
   308 EXPORT_C TPtrC TEFparser::ParseNthElement(const TDesC& aBuf, TInt aWordPos)
       
   309 
       
   310  {
       
   311  
       
   312 	 TInt startPos = KErrNotFound, endPos = KErrNotFound;
       
   313 	 TInt wordCounter =0 ;
       
   314 	 TBool inWord = EFalse;
       
   315 	 TInt i =0;
       
   316 	 
       
   317 	 for(i = 0; i < aBuf.Length() ; i ++)
       
   318 	  {
       
   319 	  	TPtrC tmpChar = aBuf.Mid(i,1);
       
   320 	  	if(tmpChar == KSpace || tmpChar == KTab || tmpChar == KReturn || tmpChar == KNewline )
       
   321 	  	{
       
   322 	  	if(inWord)
       
   323 	  		{
       
   324 	  		  if(wordCounter == aWordPos)
       
   325 	  		  {
       
   326 	    	   endPos =i-1;
       
   327 	    	   break;
       
   328 	    	   }
       
   329 	  		 inWord = EFalse;
       
   330 	    	}   
       
   331 	   }
       
   332 	  else
       
   333 	   {
       
   334 	   	if(inWord == EFalse)
       
   335 	   		{
       
   336 	   		wordCounter ++;
       
   337 	   		inWord = ETrue;
       
   338 	   		if(wordCounter == aWordPos)
       
   339 	   			{
       
   340 	   			startPos =i;
       
   341 	   			}
       
   342 	   		 }
       
   343 	   	 }
       
   344 	  } 
       
   345 	 if(startPos < 0 || endPos < 0)
       
   346 	 {
       
   347 	  endPos = aBuf.Length();
       
   348 	  return aBuf.Mid(startPos,(endPos-startPos));
       
   349 	 }
       
   350 	 else
       
   351 	 {
       
   352 	  return aBuf.Mid(startPos,(endPos-startPos+1));
       
   353 	 }
       
   354 }
       
   355 
       
   356