resourcemgmt/hwresourcesmgr/common/src/inifile.cpp
changeset 0 4e1aa6a622a0
equal deleted inserted replaced
-1:000000000000 0:4e1aa6a622a0
       
     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 // Defines the class CIniFile for accessing ini file.
       
    15 // 
       
    16 //
       
    17 
       
    18 /**
       
    19  @file
       
    20 */
       
    21 
       
    22 #include "inifile.h"
       
    23 #include <f32file.h>
       
    24 
       
    25 CIniFile::CIniFile() 
       
    26  :	iPtr(NULL,0)
       
    27 	{
       
    28 	}
       
    29 
       
    30 EXPORT_C CIniFile::~CIniFile()
       
    31 	{
       
    32 	delete (TText*)iPtr.Ptr();
       
    33 	delete iToken;
       
    34 	delete iName;
       
    35 	}
       
    36 	
       
    37 EXPORT_C CIniFile* CIniFile::NewL(const TDesC& aName, const TDesC& aPath)
       
    38 /**
       
    39  * Factory function for CIniFile.which passes in the path of the file and its name.
       
    40  *
       
    41  * @param aName The name of the ini file to be used, e.g. "TRPAT.INI".
       
    42  */
       
    43 	{
       
    44 	CIniFile* p = new(ELeave) CIniFile();
       
    45 	CleanupStack::PushL(p);
       
    46 	p->ConstructL(aName, aPath);
       
    47 	CleanupStack::Pop(p);
       
    48 
       
    49 	return p;
       
    50 	}
       
    51 
       
    52 void CIniFile::ConstructL(const TDesC& aName, const TDesC& aPath)
       
    53 /**
       
    54  * Allocate a buffer and Read file's contents into iPtr
       
    55  *
       
    56  */
       
    57 	{
       
    58     iToken = HBufC::NewL(KTokenSize+2);	// 2 extra chars for []
       
    59 
       
    60 	TAutoClose<RFs> fs;
       
    61 	User::LeaveIfError(fs.iObj.Connect());
       
    62 	fs.PushL();
       
    63 
       
    64 	TFindFile ff(fs.iObj);
       
    65 
       
    66 	// For security reasons ini files are in the private directory for the
       
    67 	// comms-infras/rootserver (C32exe) process.
       
    68 	TBuf<KMaxPath> privatePath;
       
    69 	
       
    70 	privatePath = aPath;
       
    71 	User::LeaveIfError(ff.FindByDir(aName, privatePath));
       
    72 	
       
    73 	iName=ff.File().AllocL();
       
    74 
       
    75 	TAutoClose<RFile> file;
       
    76 	TInt size;
       
    77 	User::LeaveIfError(file.iObj.Open(fs.iObj,*iName,EFileStreamText|EFileRead|EFileShareReadersOnly));
       
    78 	file.PushL();
       
    79 
       
    80 	User::LeaveIfError(file.iObj.Size(size));
       
    81 
       
    82 
       
    83 	TText* data = REINTERPRET_CAST(TText*, User::AllocL(size));
       
    84 	iPtr.Set(data, size/sizeof(TText), size/sizeof(TText));
       
    85 	TPtr8 dest(REINTERPRET_CAST(TUint8*,data), 0, size);
       
    86 	User::LeaveIfError(file.iObj.Read(dest)); 
       
    87 
       
    88 	TUint8* ptr = REINTERPRET_CAST(TUint8*,data);
       
    89 
       
    90 	//
       
    91 	// This is orderred as FEFF assuming the processor is Little Endian
       
    92 	// The data in the file is FFFE.		PRR 28/9/98
       
    93 	//
       
    94 	if(size>= STATIC_CAST(TInt,sizeof(TText)) && iPtr[0]==0xFEFF)
       
    95    		{
       
    96 		Mem::Copy(ptr, ptr+sizeof(TText), size-sizeof(TText));
       
    97 		iPtr.Set(data, size/sizeof(TText)-1, size/sizeof(TText)-1);
       
    98 		}
       
    99 	else if(size)
       
   100 		{
       
   101 		TText* newdata = REINTERPRET_CAST(TText*,
       
   102 			                              User::AllocL(size*sizeof(TText)));
       
   103 		iPtr.Set(newdata, size, size);
       
   104 		TInt i;
       
   105 		for(i=0 ; i<size ; ++i)
       
   106 			iPtr[i]=ptr[i];
       
   107 		delete data;
       
   108 		}
       
   109 
       
   110 	file.Pop();
       
   111 	fs.Pop();
       
   112 	}
       
   113 
       
   114 EXPORT_C TBool CIniFile::FindVar(const TDesC &aSection,
       
   115 						const TDesC &aVarName,
       
   116 						TPtrC &aResult)
       
   117 //
       
   118 // Find a variable's value given a section name and a var name
       
   119 //
       
   120 	{
       
   121 	__ASSERT_DEBUG(aSection.Length()<=KTokenSize, User::Panic(_L("CIniFile"),ESectionNameTooBig));
       
   122 	__ASSERT_DEBUG(aVarName.Length()<=KTokenSize,User::Panic(_L("CIniFile"),EVarNameTooBig));
       
   123 
       
   124 	TPtr sectionToken=iToken->Des();
       
   125 	_LIT(KSectionTokenString,"[%S]");
       
   126 	sectionToken.Format(KSectionTokenString,&aSection);
       
   127 	TInt sectionStart=iPtr.Find(sectionToken);
       
   128 	TInt ret = ETrue;
       
   129 	if (sectionStart==KErrNotFound)
       
   130 		ret = EFalse;
       
   131 	else
       
   132 		{		
       
   133 		TPtrC section=iPtr.Mid(sectionStart);
       
   134 		sectionStart+=section.Find(TPtrC(_S("]")));
       
   135 		if (sectionStart==KErrNotFound)
       
   136 			ret = EFalse;
       
   137 		else
       
   138 			{
       
   139 			sectionStart++;
       
   140 			section.Set(iPtr.Mid(sectionStart));
       
   141 			
       
   142 			TInt sectionEnd=section.Find(TPtrC(_S("[")));
       
   143 			if (sectionEnd==KErrNotFound)
       
   144 				sectionEnd=iPtr.Length()-sectionStart;
       
   145 			else
       
   146 				sectionEnd--;
       
   147 			section.Set(iPtr.Mid(sectionStart,sectionEnd));
       
   148 			TPtr varToken=iToken->Des();
       
   149 			_LIT(KVarTokenString1,"%S=");
       
   150 			_LIT(KVarTokenString2,"%S =");
       
   151 			varToken.Format(KVarTokenString1,&aVarName);
       
   152 			TInt pos=section.Find(varToken);
       
   153 
       
   154 			if (pos == KErrNotFound)
       
   155 				{
       
   156 				varToken.Format(KVarTokenString2,&aVarName);
       
   157 				pos=section.Find(varToken);
       
   158 				}
       
   159 
       
   160 			if (pos==KErrNotFound)
       
   161 				{
       
   162 				ret = EFalse;
       
   163 				}
       
   164 			else
       
   165 				{
       
   166 				// 'lex' points at the start of the data
       
   167 				TPtrC lex(section.Mid(pos));
       
   168 				TInt startpos = lex.Locate(TChar('='));
       
   169 				startpos++; // startpos points immediately after the =.
       
   170 				while ( TChar(lex[startpos]).IsSpace() )
       
   171 					startpos++; // skip to start of data
       
   172 				TInt endpos = lex.Locate(TChar('\n')); // assumes \n is after =.
       
   173 				if ( endpos == KErrNotFound ) // may not be \n on last line
       
   174 					endpos = lex.Length() + 1;
       
   175 				aResult.Set(lex.Mid(startpos).Ptr(),endpos-startpos-1);
       
   176 				}
       
   177 			}
       
   178 		}
       
   179 
       
   180 	return ret;
       
   181 	}
       
   182 
       
   183 //
       
   184 // End of file