1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/lowlevellibsandfws/apputils/bsul/test/t_iniparser/LegacyParser.CPP Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,222 @@
1.4 +// Copyright (c) 2002-2009 Nokia Corporation and/or its subsidiary(-ies).
1.5 +// All rights reserved.
1.6 +// This component and the accompanying materials are made available
1.7 +// under the terms of "Eclipse Public License v1.0"
1.8 +// which accompanies this distribution, and is available
1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.10 +//
1.11 +// Initial Contributors:
1.12 +// Nokia Corporation - initial contribution.
1.13 +//
1.14 +// Contributors:
1.15 +//
1.16 +// Description:
1.17 +// INIFILE.CPP
1.18 +// Defines the class CIniFile for accessing ini file.
1.19 +// is used by the test harness in t_Usbman_dummyCC.mmp
1.20 +//
1.21 +//
1.22 +
1.23 +/**
1.24 + @file
1.25 +*/
1.26 +
1.27 +#include <f32file.h>
1.28 +#include "LegacyParser.h"
1.29 +
1.30 +const TUint KTokenSize = 32;
1.31 +_LIT(KDefaultIniFileDir,"\\");
1.32 +
1.33 +void CIniFile::Panic(TIniPanic aPanic)
1.34 + {
1.35 + _LIT(KIniData,"CIniFile");
1.36 + User::Panic(KIniData,aPanic);
1.37 + }
1.38 +
1.39 +CIniFile::CIniFile()
1.40 + : iPtr(NULL,0)
1.41 + {
1.42 + }
1.43 +
1.44 +CIniFile::~CIniFile()
1.45 + {
1.46 + delete (TText*)iPtr.Ptr();
1.47 + delete iToken;
1.48 + delete iName;
1.49 + }
1.50 +
1.51 +CIniFile* CIniFile::NewL(const TDesC& aName)
1.52 +/**
1.53 + * Factory function for CIniFile.
1.54 + *
1.55 + * @param aName The name of the ini file to be used, e.g. "GPRSBTT.INI".
1.56 + */
1.57 + {
1.58 + CIniFile* self = new(ELeave) CIniFile;
1.59 + CleanupStack::PushL(self);
1.60 + self->ConstructL(aName, KDefaultIniFileDir);
1.61 + CleanupStack::Pop(self);
1.62 + return self;
1.63 + }
1.64 +
1.65 +CIniFile* CIniFile::NewL(const TDesC& aName, const TDesC& aPath)
1.66 +/**
1.67 + * Factory function for CIniFile that allows the user to specify both filename
1.68 + * and path
1.69 + *
1.70 + * @param aName The name of the ini file to be used, e.g. "GPRSBTT.INI".
1.71 + * @param aPath The location of the file e.g. "\\system\\data\\".
1.72 + */
1.73 + {
1.74 + CIniFile* self = new(ELeave) CIniFile;
1.75 + CleanupStack::PushL(self);
1.76 + self->ConstructL(aName, aPath);
1.77 + CleanupStack::Pop(self);
1.78 + return self;
1.79 + }
1.80 +
1.81 +void CIniFile::ConstructL(const TDesC& aName, const TDesC& aPath)
1.82 +/**
1.83 + * Allocate a buffer and Read file's contents into iPtr
1.84 + *
1.85 + * @param aName is the name of the ini file to be used, e.g. "REFTSY.INI"
1.86 + */
1.87 + {
1.88 + iToken = HBufC::NewL(KTokenSize+2); // 2 extra chars for []
1.89 +
1.90 + RFs fs;
1.91 + User::LeaveIfError(fs.Connect());
1.92 + CleanupClosePushL(fs);
1.93 +
1.94 + TFindFile ff(fs);
1.95 +
1.96 +#ifdef BUILD_FOR_JETSTREAM
1.97 + // Jetstream
1.98 + // For security reasons ini files are in the private directory for the
1.99 + // comms-infras/rootserver (C32exe) process
1.100 + TBuf<KMaxPath> privatePath;
1.101 + User::LeaveIfError(fs.PrivatePath(privatePath));
1.102 + User::LeaveIfError(ff.FindByDir(aName, privatePath));
1.103 +#else
1.104 +
1.105 + User::LeaveIfError(ff.FindByDir(aName, aPath));
1.106 +
1.107 +#endif
1.108 +
1.109 + iName=ff.File().AllocL();
1.110 +
1.111 + RFile file;
1.112 + TInt size;
1.113 + User::LeaveIfError(file.Open(fs,*iName,EFileStreamText|EFileRead|EFileShareReadersOnly));
1.114 + CleanupClosePushL(file);
1.115 +
1.116 + User::LeaveIfError(file.Size(size));
1.117 +
1.118 +
1.119 + TText* data = REINTERPRET_CAST(TText*, User::AllocL(size));
1.120 + iPtr.Set(data, size/sizeof(TText), size/sizeof(TText));
1.121 + TPtr8 dest(REINTERPRET_CAST(TUint8*,data), 0, size);
1.122 + User::LeaveIfError(file.Read(dest));
1.123 +
1.124 + TUint8* ptr = REINTERPRET_CAST(TUint8*,data);
1.125 +
1.126 + //
1.127 + // This is orderred as FEFF assuming the processor is Little Endian
1.128 + // The data in the file is FFFE. PRR 28/9/98
1.129 + //
1.130 + if(size>= STATIC_CAST(TInt,sizeof(TText)) && iPtr[0]==0xFEFF)
1.131 + {
1.132 + Mem::Copy(ptr, ptr+sizeof(TText), size-sizeof(TText));
1.133 + iPtr.Set(data, size/sizeof(TText)-1, size/sizeof(TText)-1);
1.134 + }
1.135 + else if(size)
1.136 + {
1.137 + TText* newdata = REINTERPRET_CAST(TText*,
1.138 + User::AllocL(size*sizeof(TText)));
1.139 + iPtr.Set(newdata, size, size);
1.140 + TInt i;
1.141 + for(i=0 ; i<size ; ++i)
1.142 + iPtr[i]=ptr[i];
1.143 + delete data;
1.144 + }
1.145 +
1.146 + CleanupStack::PopAndDestroy(); // file
1.147 + CleanupStack::PopAndDestroy(); // fs
1.148 + }
1.149 +
1.150 +TBool CIniFile::FindVar(const TDesC &aSection,
1.151 + const TDesC &aVarName,
1.152 + TPtrC &aResult)
1.153 +//
1.154 +// Find a variable's value given a section name and a var name
1.155 +//
1.156 + {
1.157 + __ASSERT_DEBUG(aSection.Length()<=(TInt)KTokenSize,Panic(ESectionNameTooBig));
1.158 + __ASSERT_DEBUG(aVarName.Length()<=(TInt)KTokenSize,Panic(EVarNameTooBig));
1.159 +
1.160 + TPtr sectionToken=iToken->Des();
1.161 + _LIT(KSectionTokenString,"[%S]");
1.162 + sectionToken.Format(KSectionTokenString,&aSection);
1.163 + TInt sectionStart=iPtr.Find(sectionToken);
1.164 + TInt ret = ETrue;
1.165 + if (sectionStart==KErrNotFound)
1.166 + ret = EFalse;
1.167 + else
1.168 + {
1.169 + TPtrC section=iPtr.Mid(sectionStart);
1.170 + sectionStart+=section.Find(TPtrC(_S("]")));
1.171 + if (sectionStart==KErrNotFound)
1.172 + ret = EFalse;
1.173 + else
1.174 + {
1.175 + sectionStart++;
1.176 + section.Set(iPtr.Mid(sectionStart));
1.177 +
1.178 + TInt sectionEnd=section.Find(TPtrC(_S("[")));
1.179 + if (sectionEnd==KErrNotFound)
1.180 + sectionEnd=iPtr.Length()-sectionStart;
1.181 + else
1.182 + sectionEnd--;
1.183 + section.Set(iPtr.Mid(sectionStart,sectionEnd));
1.184 + TPtr varToken=iToken->Des();
1.185 + _LIT(KVarTokenString,"%S=");
1.186 + varToken.Format(KVarTokenString,&aVarName);
1.187 + TInt pos=section.Find(varToken);
1.188 + if (pos==KErrNotFound)
1.189 + ret = EFalse;
1.190 + else
1.191 + {
1.192 + // 'lex' points at the start of the data
1.193 + TPtrC lex(section.Mid(pos));
1.194 + TInt startpos = lex.Locate(TChar('='));
1.195 + startpos++; // startpos points immediately after the =.
1.196 + while ( TChar(lex[startpos]).IsSpace() )
1.197 + startpos++; // skip to start of data
1.198 + TInt endpos = lex.Locate(TChar('\n')); // assumes \n is after =.
1.199 + if ( endpos == KErrNotFound ) // may not be \n on last line
1.200 + endpos = section.Length()-1;
1.201 + aResult.Set(lex.Mid(startpos).Ptr(),endpos-startpos-1);
1.202 + }
1.203 + }
1.204 + }
1.205 +
1.206 + return ret;
1.207 + }
1.208 +
1.209 +TBool CIniFile::FindVar(const TDesC &aSection,const TDesC &aVarName,
1.210 + TInt &aResult)
1.211 + {
1.212 + TInt ret = EFalse;
1.213 + TPtrC ptr(NULL,0);
1.214 + if (FindVar(aSection,aVarName,ptr))
1.215 + {
1.216 + TLex lex(ptr);
1.217 + if (lex.Val(aResult)==KErrNone)
1.218 + ret = ETrue;
1.219 + }
1.220 +
1.221 + return ret;
1.222 + }
1.223 +
1.224 +//
1.225 +// End of file