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