lowlevellibsandfws/apputils/bsul/test/t_iniparser/LegacyParser.CPP
changeset 0 e4d67989cc36
equal deleted inserted replaced
-1:000000000000 0:e4d67989cc36
       
     1 // Copyright (c) 2002-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 // INIFILE.CPP
       
    15 // Defines the class CIniFile for accessing ini file.
       
    16 // is used by the test harness in t_Usbman_dummyCC.mmp
       
    17 // 
       
    18 //
       
    19 
       
    20 /**
       
    21  @file
       
    22 */
       
    23 
       
    24 #include <f32file.h>
       
    25 #include "LegacyParser.h"
       
    26 
       
    27 const TUint KTokenSize = 32;
       
    28 _LIT(KDefaultIniFileDir,"\\");
       
    29 
       
    30 void CIniFile::Panic(TIniPanic aPanic)
       
    31 	{
       
    32 	_LIT(KIniData,"CIniFile");
       
    33 	User::Panic(KIniData,aPanic);
       
    34 	}
       
    35 
       
    36 CIniFile::CIniFile() 
       
    37  :	iPtr(NULL,0)
       
    38 	{
       
    39 	}
       
    40 
       
    41 CIniFile::~CIniFile()
       
    42 	{
       
    43 	delete (TText*)iPtr.Ptr();
       
    44 	delete iToken;
       
    45 	delete iName;
       
    46 	}
       
    47 
       
    48 CIniFile* CIniFile::NewL(const TDesC& aName)
       
    49 /**
       
    50  * Factory function for CIniFile.
       
    51  *
       
    52  * @param aName The name of the ini file to be used, e.g. "GPRSBTT.INI".
       
    53  */
       
    54 	{
       
    55 	CIniFile* self = new(ELeave) CIniFile;
       
    56 	CleanupStack::PushL(self);
       
    57 	self->ConstructL(aName, KDefaultIniFileDir);
       
    58 	CleanupStack::Pop(self);
       
    59 	return self;
       
    60 	}
       
    61 
       
    62 CIniFile* CIniFile::NewL(const TDesC& aName, const TDesC& aPath)
       
    63 /**
       
    64  * Factory function for CIniFile that allows the user to specify both filename
       
    65  * and path
       
    66  *
       
    67  * @param aName The name of the ini file to be used, e.g. "GPRSBTT.INI".
       
    68  * @param aPath The location of the file e.g. "\\system\\data\\".
       
    69  */
       
    70  {
       
    71  	CIniFile* self = new(ELeave) CIniFile;
       
    72 	CleanupStack::PushL(self);
       
    73 	self->ConstructL(aName, aPath);
       
    74 	CleanupStack::Pop(self);
       
    75 	return self;	 	
       
    76  }
       
    77  
       
    78 void CIniFile::ConstructL(const TDesC& aName, const TDesC& aPath)
       
    79 /**
       
    80  * Allocate a buffer and Read file's contents into iPtr
       
    81  *
       
    82  * @param aName is the name of the ini file to be used, e.g. "REFTSY.INI"
       
    83  */
       
    84 	{
       
    85     iToken = HBufC::NewL(KTokenSize+2);	// 2 extra chars for []
       
    86 
       
    87 	RFs fs;
       
    88 	User::LeaveIfError(fs.Connect());
       
    89 	CleanupClosePushL(fs);
       
    90 
       
    91 	TFindFile ff(fs);
       
    92 
       
    93 #ifdef BUILD_FOR_JETSTREAM
       
    94 	// Jetstream
       
    95 	// For security reasons ini files are in the private directory for the
       
    96 	// comms-infras/rootserver (C32exe) process
       
    97 	TBuf<KMaxPath> privatePath;
       
    98 	User::LeaveIfError(fs.PrivatePath(privatePath));
       
    99 	User::LeaveIfError(ff.FindByDir(aName, privatePath));
       
   100 #else 
       
   101 
       
   102 	User::LeaveIfError(ff.FindByDir(aName, aPath));
       
   103 	
       
   104 #endif
       
   105 
       
   106 	iName=ff.File().AllocL();
       
   107 	
       
   108 	RFile file;
       
   109 	TInt size;
       
   110 	User::LeaveIfError(file.Open(fs,*iName,EFileStreamText|EFileRead|EFileShareReadersOnly));
       
   111 	CleanupClosePushL(file);
       
   112 	
       
   113 	User::LeaveIfError(file.Size(size));
       
   114 
       
   115 
       
   116 	TText* data = REINTERPRET_CAST(TText*, User::AllocL(size));
       
   117 	iPtr.Set(data, size/sizeof(TText), size/sizeof(TText));
       
   118 	TPtr8 dest(REINTERPRET_CAST(TUint8*,data), 0, size);
       
   119 	User::LeaveIfError(file.Read(dest)); 
       
   120 
       
   121 	TUint8* ptr = REINTERPRET_CAST(TUint8*,data);
       
   122 
       
   123 	//
       
   124 	// This is orderred as FEFF assuming the processor is Little Endian
       
   125 	// The data in the file is FFFE.		PRR 28/9/98
       
   126 	//
       
   127 	if(size>= STATIC_CAST(TInt,sizeof(TText)) && iPtr[0]==0xFEFF)
       
   128    		{
       
   129 		Mem::Copy(ptr, ptr+sizeof(TText), size-sizeof(TText));
       
   130 		iPtr.Set(data, size/sizeof(TText)-1, size/sizeof(TText)-1);
       
   131 		}
       
   132 	else if(size)
       
   133 		{
       
   134 		TText* newdata = REINTERPRET_CAST(TText*,
       
   135 			                              User::AllocL(size*sizeof(TText)));
       
   136 		iPtr.Set(newdata, size, size);
       
   137 		TInt i;
       
   138 		for(i=0 ; i<size ; ++i)
       
   139 			iPtr[i]=ptr[i];
       
   140 		delete data;
       
   141 		}
       
   142 
       
   143 	CleanupStack::PopAndDestroy(); // file
       
   144 	CleanupStack::PopAndDestroy(); // fs
       
   145 	}
       
   146 
       
   147 TBool CIniFile::FindVar(const TDesC &aSection,
       
   148 						const TDesC &aVarName,
       
   149 						TPtrC &aResult)
       
   150 //
       
   151 // Find a variable's value given a section name and a var name
       
   152 //
       
   153 	{
       
   154 	__ASSERT_DEBUG(aSection.Length()<=(TInt)KTokenSize,Panic(ESectionNameTooBig));
       
   155 	__ASSERT_DEBUG(aVarName.Length()<=(TInt)KTokenSize,Panic(EVarNameTooBig));
       
   156 
       
   157 	TPtr sectionToken=iToken->Des();
       
   158 	_LIT(KSectionTokenString,"[%S]");
       
   159 	sectionToken.Format(KSectionTokenString,&aSection);
       
   160 	TInt sectionStart=iPtr.Find(sectionToken);
       
   161 	TInt ret = ETrue;
       
   162 	if (sectionStart==KErrNotFound)
       
   163 		ret = EFalse;
       
   164 	else
       
   165 		{		
       
   166 		TPtrC section=iPtr.Mid(sectionStart);
       
   167 		sectionStart+=section.Find(TPtrC(_S("]")));
       
   168 		if (sectionStart==KErrNotFound)
       
   169 			ret = EFalse;
       
   170 		else
       
   171 			{
       
   172 			sectionStart++;
       
   173 			section.Set(iPtr.Mid(sectionStart));
       
   174 			
       
   175 			TInt sectionEnd=section.Find(TPtrC(_S("[")));
       
   176 			if (sectionEnd==KErrNotFound)
       
   177 				sectionEnd=iPtr.Length()-sectionStart;
       
   178 			else
       
   179 				sectionEnd--;
       
   180 			section.Set(iPtr.Mid(sectionStart,sectionEnd));
       
   181 			TPtr varToken=iToken->Des();
       
   182 			_LIT(KVarTokenString,"%S=");
       
   183 			varToken.Format(KVarTokenString,&aVarName);
       
   184 			TInt pos=section.Find(varToken);
       
   185 			if (pos==KErrNotFound)
       
   186 				ret = EFalse;
       
   187 			else
       
   188 				{
       
   189 				// 'lex' points at the start of the data
       
   190 				TPtrC lex(section.Mid(pos));
       
   191 				TInt startpos = lex.Locate(TChar('='));
       
   192 				startpos++; // startpos points immediately after the =.
       
   193 				while ( TChar(lex[startpos]).IsSpace() )
       
   194 					startpos++; // skip to start of data
       
   195 				TInt endpos = lex.Locate(TChar('\n')); // assumes \n is after =.
       
   196 				if ( endpos == KErrNotFound ) // may not be \n on last line
       
   197 					endpos = section.Length()-1;
       
   198 				aResult.Set(lex.Mid(startpos).Ptr(),endpos-startpos-1);
       
   199 				}
       
   200 			}
       
   201 		}
       
   202 
       
   203 	return ret;
       
   204 	}
       
   205 
       
   206 TBool CIniFile::FindVar(const TDesC &aSection,const TDesC &aVarName,
       
   207 						TInt &aResult)
       
   208 	{
       
   209 	TInt ret = EFalse;
       
   210 	TPtrC ptr(NULL,0);
       
   211 	if (FindVar(aSection,aVarName,ptr))
       
   212 		{
       
   213 		TLex lex(ptr);
       
   214 		if (lex.Val(aResult)==KErrNone)
       
   215 			ret = ETrue;
       
   216 		}
       
   217 
       
   218 	return ret;
       
   219 	}
       
   220 
       
   221 //
       
   222 // End of file