mmtestenv/mmtestfw/Source/TestFrameworkClient/TestIniData.cpp
changeset 0 40261b775718
equal deleted inserted replaced
-1:000000000000 0:40261b775718
       
     1 // Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // Local CTestIniData class, derived from CIniData.lib
       
    15 // 
       
    16 //
       
    17 
       
    18 #include <f32file.h>
       
    19 #include <e32std.h>
       
    20 #include "TestIniData.h"
       
    21 
       
    22 // Default directory to look for INI file
       
    23 _LIT(KIniFileDir,"C:\\System\\Data\\");
       
    24 
       
    25 const TInt KTokenSize=40;
       
    26 
       
    27 //
       
    28 void CTestIniData::Panic(TTestIniPanic aPanic)
       
    29         {
       
    30         _LIT(KIniData,"CTestIniData");
       
    31         User::Panic(KIniData,aPanic);
       
    32         }
       
    33 
       
    34 
       
    35 //
       
    36 CTestIniData::CTestIniData() 
       
    37         : iPtr(NULL,0)
       
    38         {
       
    39         __DECLARE_NAME(_S("CTestIniData"));
       
    40         }
       
    41 
       
    42 //
       
    43 CTestIniData::~CTestIniData()
       
    44         {
       
    45 
       
    46         delete (TText*)iPtr.Ptr();
       
    47         delete iToken;
       
    48         delete iName;
       
    49         }
       
    50 
       
    51 //
       
    52 CTestIniData* CTestIniData::NewL(const TDesC& aName)
       
    53         {
       
    54         CTestIniData* p=new(ELeave) CTestIniData;
       
    55         CleanupStack::PushL(p);
       
    56         p->ConstructL(aName);
       
    57         CleanupStack::Pop();
       
    58         return p;
       
    59         }
       
    60 
       
    61 //
       
    62 //
       
    63 // Allocate a buffer and Read file's contents into iPtr
       
    64 //
       
    65 void CTestIniData::ConstructL(const TDesC& aName)
       
    66         {
       
    67         // Allocate space for token
       
    68         iToken=HBufC::NewL(KTokenSize+2);       // 2 extra chars for [tokenName]
       
    69 
       
    70         // Connect to file server
       
    71         TAutoClose<RFs> fs;
       
    72         User::LeaveIfError(fs.iObj.Connect());
       
    73         fs.PushL();
       
    74 
       
    75         // Find file, given name
       
    76         TFindFile ff(fs.iObj);
       
    77         User::LeaveIfError(ff.FindByDir(aName, KIniFileDir));
       
    78         iName=ff.File().AllocL();
       
    79 
       
    80         // Open file
       
    81         TAutoClose<RFile> file;
       
    82         TInt size;
       
    83         User::LeaveIfError(file.iObj.Open(fs.iObj,*iName,EFileStreamText|EFileRead));
       
    84         file.PushL();
       
    85 
       
    86         // Get file size and read in
       
    87         User::LeaveIfError(file.iObj.Size(size));
       
    88         TText* data=(TText*)User::AllocL(size);
       
    89         iPtr.Set(data, size/sizeof(TText), size/sizeof(TText));
       
    90         TPtr8 dest((TUint8*)data, 0, size);
       
    91         User::LeaveIfError(file.iObj.Read(dest));
       
    92         TUint8* ptr = (TUint8*)data;
       
    93 
       
    94         //
       
    95         // This is orderred as FEFF assuming the processor is Little Endian
       
    96         // The data in the file is FFFE.                PRR 28/9/98
       
    97         //
       
    98         if(size>=(TInt)sizeof(TText) && iPtr[0]==0xFEFF)
       
    99         {
       
   100                 // UNICODE Text file so lose the FFFE
       
   101                 Mem::Copy(ptr, ptr+sizeof(TText), size-sizeof(TText));
       
   102                 iPtr.Set(data, size/sizeof(TText)-1, size/sizeof(TText)-1);
       
   103         }
       
   104         else if(size)
       
   105         {
       
   106                 // NON-UNICODE so convert to UNICODE
       
   107                 TText* newdata = (TText*)User::AllocL(size*sizeof(TText));
       
   108                 iPtr.Set(newdata, size, size);
       
   109                 TInt i;
       
   110                 for(i=0 ; i<size ; ++i)
       
   111                         iPtr[i]=ptr[i];
       
   112                 delete data;
       
   113         }
       
   114 
       
   115         file.Pop();
       
   116         fs.Pop();
       
   117 }
       
   118 
       
   119 //
       
   120 TBool CTestIniData::FindVar(const TDesC &aKeyName, TPtrC &aResult)
       
   121 {
       
   122         TDesC dummySection = _L("");
       
   123         // Call with no section, so starts at beginning
       
   124         if (FindVar(dummySection, aKeyName, aResult))
       
   125                 return(ETrue);
       
   126         else
       
   127                 return(EFalse);
       
   128 }
       
   129 //
       
   130 //
       
   131 // Find a key's value given a section name and a key name
       
   132 //
       
   133 TBool CTestIniData::FindVar(const TDesC &aSectName,const TDesC &aKeyName,TPtrC &aResult)
       
   134         {
       
   135 
       
   136         __ASSERT_DEBUG(aSectName.Length()<=KTokenSize,Panic(ESectionNameTooBig));
       
   137         __ASSERT_DEBUG(aKeyName.Length()<=KTokenSize,Panic(EKeyNameTooBig));
       
   138 
       
   139         TInt posStartOfSection(0);
       
   140 	TInt posEndOfSection(iPtr.Length()); // Default to the entire length of the ini data
       
   141         TPtrC SearchBuf;
       
   142 
       
   143         // If we have a section, set pos to section start
       
   144         TInt posI(0);   // Position in internal data Buffer
       
   145         if( aSectName.Length() > 0 )
       
   146         {
       
   147                 TBool FoundSection(false);
       
   148                 while ( ! FoundSection )
       
   149                 {
       
   150                         // Move search buffer to next area of interest
       
   151                         SearchBuf.Set(iPtr.Mid(posI));
       
   152 
       
   153                         // Make up token "[SECTIONNAME]", to search for
       
   154                         TPtr sectionToken=iToken->Des();
       
   155                         _LIT(sectionTokenFmtString,"[%S]");
       
   156                         sectionToken.Format(sectionTokenFmtString,&aSectName);
       
   157 
       
   158                         // Search for next occurrence of aSectName
       
   159                         TInt posSB = SearchBuf.Find(sectionToken);
       
   160 
       
   161                         // If not found, return
       
   162                         if (posSB==KErrNotFound)
       
   163                                 return(EFalse);
       
   164 
       
   165                         // Check this is at beginning of line (ie. non-commented)
       
   166                         // ie. Check preceding char was LF
       
   167                         if(posSB>0)
       
   168                         {
       
   169                                 // Create a Buffer, starting one char before found subBuf
       
   170                                 TPtrC CharBefore(SearchBuf.Right(SearchBuf.Length()-posSB+1));
       
   171                                 // Check first char is end of prev
       
   172                                 if(CharBefore[0] == '\n')
       
   173                                 {
       
   174                                         FoundSection = ETrue;           // found
       
   175                                         posI = posI + posSB;
       
   176                                 }
       
   177                                 else
       
   178 					{
       
   179                                         posI = posI + posSB + 1;        // try again
       
   180 					}
       
   181 				}
       
   182                         else
       
   183 				{
       
   184                                 FoundSection = ETrue;
       
   185 				}
       
   186 
       
   187                 }       // while ( ! FoundSection ) 
       
   188 
       
   189                 // Set start of section, after section name, (incl '[' and ']')
       
   190                 posStartOfSection = posI + aSectName.Length() + 2;
       
   191 
       
   192                 // Set end of section, by finding begin of next section or end
       
   193                 SearchBuf.Set(iPtr.Mid(posI));
       
   194                 _LIT(nextSectionBuf,"\n[");
       
   195                 TInt posSB = SearchBuf.Find(nextSectionBuf);
       
   196                 if(posSB != KErrNotFound)
       
   197 			{
       
   198                         posEndOfSection = posI + posSB;
       
   199 			}
       
   200                 else
       
   201 			{
       
   202                         posEndOfSection = iPtr.Length();
       
   203 			}
       
   204 
       
   205         }       // if( aSectName.Length() > 0 )
       
   206 
       
   207         // Look for key in ini file data Buffer
       
   208         posI = posStartOfSection;
       
   209         TBool FoundKey(false);
       
   210         while ( ! FoundKey )
       
   211         {
       
   212                 // Search for next occurrence of aKeyName
       
   213 		SearchBuf.Set(iPtr.Mid(posI,posEndOfSection-posI));
       
   214                 TInt posSB = SearchBuf.Find(aKeyName);
       
   215 
       
   216                 // If not found, return
       
   217                 if (posSB==KErrNotFound)
       
   218                         return(EFalse);
       
   219 
       
   220                 // Check this is at beginning of line (ie. non-commented)
       
   221                 // ie. Check preceding char was CR or LF
       
   222                 if(posSB>0)
       
   223                 {
       
   224                         // Create a Buffer, starting one char before found subBuf
       
   225                         TPtrC CharBefore(SearchBuf.Right(SearchBuf.Length()-posSB+1));
       
   226 			// Check if the first char is end of prev and also check 
       
   227 			// if the token found is not a substring of another string  
       
   228 			TBool beginningOK = ((CharBefore[0] == '\n') || (CharBefore[0] == ' ') || (CharBefore[0] == '\t'));
       
   229 			TBool endingOK = ((CharBefore[aKeyName.Length()+1] == '=') || (CharBefore[aKeyName.Length()+1] == ' ') || (CharBefore[aKeyName.Length()+1] == '\t'));
       
   230 			if (beginningOK && endingOK)
       
   231                         {
       
   232                                 FoundKey = ETrue;
       
   233                                 posI = posI + posSB;
       
   234                         }
       
   235                         else
       
   236 				{
       
   237                                 posI = posI + posSB + 1;
       
   238                 }
       
   239 			}
       
   240                 else
       
   241 			{
       
   242                         FoundKey = ETrue;
       
   243 			}
       
   244         }       // while ( ! FoundKey )
       
   245 
       
   246         // Set pos, to just after '=' sign
       
   247         SearchBuf.Set(iPtr.Mid(posI));
       
   248         TInt posSB = SearchBuf.Locate('=');
       
   249         if(posSB==KErrNotFound)         // Illegal format, should flag this...
       
   250                 return(EFalse);
       
   251 
       
   252         // Identify start and end of data (EOL or EOF)
       
   253         posI = posI + posSB + 1;        // 1 char after '='
       
   254         TInt posValStart = posI;
       
   255         TInt posValEnd;
       
   256         SearchBuf.Set(iPtr.Mid(posI));
       
   257         posSB = SearchBuf.Locate('\r');
       
   258         if(posSB!=KErrNotFound)
       
   259 		{
       
   260                 posValEnd = posI + posSB;
       
   261 		}
       
   262         else
       
   263 		{
       
   264                 posValEnd = iPtr.Length();
       
   265 		}
       
   266 
       
   267         // Check we are still in the section requested
       
   268         if( aSectName.Length() > 0 )
       
   269 		{
       
   270                 if( posValEnd > posEndOfSection )
       
   271 			{
       
   272                         return(EFalse);
       
   273 			}
       
   274 		}
       
   275         // Parse Buffer from posn of key
       
   276         // Start one space after '='
       
   277         TLex lex(iPtr.Mid(posValStart, posValEnd-posValStart));
       
   278         lex.SkipSpaceAndMark();         // Should be at the start of the data
       
   279         aResult.Set(lex.MarkedToken().Ptr(),posValEnd-posValStart - lex.Offset() );
       
   280         return(ETrue);
       
   281         }
       
   282 
       
   283 //
       
   284 TBool CTestIniData::FindVar(const TDesC &aKeyName, TInt &aResult)
       
   285         {
       
   286         TPtrC ptr(NULL,0);
       
   287         if (FindVar(aKeyName,ptr))
       
   288                 {
       
   289                 TLex lex(ptr);
       
   290                 if (lex.Val(aResult)==KErrNone)
       
   291                         return(ETrue);
       
   292                 }
       
   293         return(EFalse);
       
   294         }
       
   295 
       
   296 //
       
   297 TBool CTestIniData::FindVar(const TDesC &aSection,const TDesC &aKeyName,TInt &aResult)
       
   298         {
       
   299         TPtrC ptr(NULL,0);
       
   300         if (FindVar(aSection,aKeyName,ptr))
       
   301                 {
       
   302                 TLex lex(ptr);
       
   303                 if (lex.Val(aResult)==KErrNone)
       
   304                         return(ETrue);
       
   305                 }
       
   306         return(EFalse);
       
   307 		}
       
   308