1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/graphics/windowing/windowserver/test/PARSEINIDATA.CPP Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,351 @@
1.4 +// Copyright (c) 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 +// Parse the ini file
1.18 +//
1.19 +//
1.20 +
1.21 +/**
1.22 +@file
1.23 +@test
1.24 +@internalComponent
1.25 +*/
1.26 +
1.27 +#include "PARSEINIDATA.H"
1.28 +#include <f32file.h>
1.29 +#include <e32std.h>
1.30 +
1.31 +// Default directory to look for INI file
1.32 +_LIT(KIniFileDir,"Z:\\System\\Data\\");
1.33 +
1.34 +// Constant Value changed for DEF047130 fix
1.35 +const TInt KTokenSize = 256;
1.36 +
1.37 +enum TIniPanic
1.38 + {
1.39 + ESectionNameTooBig,
1.40 + EKeyNameTooBig,
1.41 + };
1.42 +
1.43 +//
1.44 +void Panic(TIniPanic aPanic)
1.45 + {
1.46 + _LIT(CIniData,"CIniData");
1.47 + User::Panic(CIniData,aPanic);
1.48 + }
1.49 +
1.50 +/**
1.51 +Constructor
1.52 +*/
1.53 +CIniData::CIniData() : iPtr(NULL,0)
1.54 + {
1.55 + __DECLARE_NAME(_S("CIniData"));
1.56 + }
1.57 +
1.58 +/**
1.59 +Destructor.
1.60 +Frees the resources located in second-phase constructor
1.61 +*/
1.62 +CIniData::~CIniData()
1.63 + {
1.64 + delete (TText*)iPtr.Ptr();
1.65 + delete iToken;
1.66 + delete iName;
1.67 + }
1.68 +
1.69 +/**
1.70 + Creates, and returns a pointer to CIniData object, leave on failure
1.71 + @param aName the name of the file which contains the ini data
1.72 + @param aDelimeter Delimiter used in wsini.ini file between variable and value
1.73 + @return A pointer to the CiniData object
1.74 +*/
1.75 +CIniData* CIniData::NewL(const TDesC& aName, char aDelimiter)
1.76 + {
1.77 + CIniData* p = new(ELeave) CIniData;
1.78 + CleanupStack::PushL(p);
1.79 + p->ConstructL(aName, aDelimiter);
1.80 + CleanupStack::Pop();
1.81 + return p;
1.82 + }
1.83 +
1.84 +/**
1.85 + Second-phase constructor.
1.86 + The function attempts to allocate a buffer and Read file's contents into iPtr
1.87 + @param aName the name of the file which contains the ini data
1.88 + @param aDelimeter Delimiter used in wsini.ini file between variable and value
1.89 + @leave One of the system-wide error codes
1.90 +*/
1.91 +void CIniData::ConstructL(const TDesC& aName, char aDelimiter)
1.92 + {
1.93 + //Set delimiter
1.94 + iDelimiter = aDelimiter;
1.95 +
1.96 + // Allocate space for token
1.97 + iToken=HBufC::NewL(KTokenSize+2); // 2 extra chars for [tokenName]
1.98 +
1.99 + // Connect to file server
1.100 + TAutoClose<RFs> fs;
1.101 + User::LeaveIfError(fs.iObj.Connect());
1.102 + fs.PushL();
1.103 +
1.104 + // Find file, given name
1.105 + TFindFile ff(fs.iObj);
1.106 + User::LeaveIfError(ff.FindByDir(aName, KIniFileDir));
1.107 + iName = ff.File().AllocL();
1.108 +
1.109 + // Open file
1.110 + TAutoClose<RFile> file;
1.111 + TInt size;
1.112 + User::LeaveIfError(file.iObj.Open(fs.iObj,*iName,EFileStreamText|EFileShareReadersOrWriters));
1.113 + file.PushL();
1.114 +
1.115 + // Get file size and read in
1.116 + User::LeaveIfError(file.iObj.Size(size));
1.117 + TText* data = (TText*)User::AllocL(size);
1.118 + iPtr.Set(data, size/sizeof(TText), size/sizeof(TText));
1.119 + TPtr8 dest((TUint8*)data, 0, size);
1.120 + User::LeaveIfError(file.iObj.Read(dest));
1.121 + TUint8* ptr = (TUint8*)data;
1.122 +
1.123 + //
1.124 + // This is orderred as FEFF assuming the processor is Little Endian
1.125 + // The data in the file is FFFE. PRR 28/9/98
1.126 + //
1.127 + if(size>=(TInt)sizeof(TText) && iPtr[0]==0xFEFF)
1.128 + {
1.129 + // UNICODE Text file so lose the FFFE
1.130 + Mem::Copy(ptr, ptr+sizeof(TText), size-sizeof(TText));
1.131 + iPtr.Set(data, size/sizeof(TText)-1, size/sizeof(TText)-1);
1.132 + }
1.133 + else if(size)
1.134 + {
1.135 + // NON-UNICODE so convert to UNICODE
1.136 + TText* newdata = (TText*)User::AllocL(size*sizeof(TText));
1.137 + iPtr.Set(newdata, size, size);
1.138 + TInt i;
1.139 + for(i = 0 ; i<size ; ++i)
1.140 + iPtr[i] = ptr[i];
1.141 + delete data;
1.142 + }
1.143 +
1.144 + file.Pop();
1.145 + fs.Pop();
1.146 +}
1.147 +
1.148 +/**
1.149 +Find a text value from given aKeyName regardless the section in the ini data file
1.150 +@param aKeyName Key being searched for
1.151 +@param aResult On return, contains the text result
1.152 +@return ETrue if found, otherwise EFalse
1.153 +*/
1.154 +TBool CIniData::FindVar(const TDesC& aKeyName, TPtrC& aResult)
1.155 + {
1.156 + // Call with no section, so starts at beginning
1.157 + if (FindVar((TDesC&)KNullDesC , aKeyName, aResult))
1.158 + return(ETrue);
1.159 + else
1.160 + return(EFalse);
1.161 + }
1.162 +
1.163 +/**
1.164 +Find a text value from given aKeyName and aSecName in the ini data file
1.165 +@param aSectName Section containing key
1.166 +@param aKeyName Key being searched for in aSectName
1.167 +@param aResult On return, contains the text result
1.168 +@return ETrue if found, otherwise EFalse
1.169 +*/
1.170 +TBool CIniData::FindVar(const TDesC& aSectName,const TDesC& aKeyName,TPtrC& aResult)
1.171 + {
1.172 +
1.173 + __ASSERT_DEBUG(aSectName.Length()<=KTokenSize,Panic(ESectionNameTooBig));
1.174 + __ASSERT_DEBUG(aKeyName.Length()<=KTokenSize,Panic(EKeyNameTooBig));
1.175 +
1.176 + TInt posStartOfSection(0);
1.177 + TInt posEndOfSection(iPtr.Length()); // Default to the entire length of the ini data
1.178 + TPtrC SearchBuf;
1.179 +
1.180 + // If we have a section, set pos to section start
1.181 + TInt posI(0); // Position in internal data Buffer
1.182 + if( aSectName.Length() > 0 )
1.183 + {
1.184 + TBool FoundSection(false);
1.185 + while ( ! FoundSection )
1.186 + {
1.187 + // Move search buffer to next area of interest
1.188 + SearchBuf.Set(iPtr.Mid(posI));
1.189 +
1.190 + // Make up token "[SECTIONNAME]", to search for
1.191 + TPtr sectionToken = iToken->Des();
1.192 + _LIT(sectionTokenFmtString,"[%S]");
1.193 + sectionToken.Format(sectionTokenFmtString,&aSectName);
1.194 +
1.195 + // Search for next occurrence of aSectName
1.196 + TInt posSB = SearchBuf.Find(sectionToken);
1.197 +
1.198 + if (posSB == KErrNotFound)
1.199 + return(EFalse);
1.200 +
1.201 + // Check this is at beginning of line (ie. non-commented)
1.202 + // ie. Check preceding char was LF
1.203 + if(posSB>0)
1.204 + {
1.205 + // Create a Buffer, starting one char before found subBuf
1.206 + TPtrC CharBefore(SearchBuf.Right(SearchBuf.Length()-posSB+1));
1.207 + // Check first char is end of prev
1.208 + if(CharBefore[0] == '\n')
1.209 + {
1.210 + FoundSection = ETrue; // found
1.211 + posI = posI + posSB;
1.212 + }
1.213 + else
1.214 + {
1.215 + posI = posI + posSB + 1; // try again
1.216 + }
1.217 + }
1.218 + else
1.219 + {
1.220 + FoundSection = ETrue;
1.221 + }
1.222 +
1.223 + } // while ( ! FoundSection )
1.224 +
1.225 + // Set start of section, after section name, (incl '[' and ']')
1.226 + posStartOfSection = posI + aSectName.Length() + 2;
1.227 +
1.228 + // Set end of section, by finding begin of next section or end
1.229 + SearchBuf.Set(iPtr.Mid(posI));
1.230 + _LIT(nextSectionBuf,"\n[");
1.231 + TInt posSB = SearchBuf.Find(nextSectionBuf);
1.232 + if(posSB != KErrNotFound)
1.233 + {
1.234 + posEndOfSection = posI + posSB;
1.235 + }
1.236 + else
1.237 + {
1.238 + posEndOfSection = iPtr.Length();
1.239 + }
1.240 +
1.241 + } // if( aSectName.Length() > 0 )
1.242 +
1.243 + // Look for key in ini file data Buffer
1.244 + posI = posStartOfSection;
1.245 + TBool FoundKey(false);
1.246 + while ( ! FoundKey )
1.247 + {
1.248 + // Search for next occurrence of aKeyName
1.249 + SearchBuf.Set(iPtr.Mid(posI,posEndOfSection-posI));
1.250 + TInt posSB = SearchBuf.Find(aKeyName);
1.251 +
1.252 + // If not found, return
1.253 + if (posSB == KErrNotFound)
1.254 + return(EFalse);
1.255 +
1.256 + // Check this is at beginning of line (ie. non-commented)
1.257 + // ie. Check preceding char was CR or LF
1.258 + if(posSB>0)
1.259 + {
1.260 + // Create a Buffer, starting one char before found subBuf
1.261 + TPtrC CharBefore(SearchBuf.Right(SearchBuf.Length()-posSB+1));
1.262 + // Check if the first char is end of prev and also check
1.263 + // if the token found is not a substring of another string
1.264 + TBool beginningOK = ((CharBefore[0] == '\n') || (CharBefore[0] == ' ') || (CharBefore[0] == '\t'));
1.265 + TBool endingOK = ((CharBefore[aKeyName.Length()+1] == iDelimiter) || (CharBefore[aKeyName.Length()+1] == ' ') || (CharBefore[aKeyName.Length()+1] == '\t'));
1.266 + if (beginningOK && endingOK)
1.267 + {
1.268 + FoundKey = ETrue;
1.269 + posI = posI + posSB;
1.270 + }
1.271 + else
1.272 + {
1.273 + posI = posI + posSB + 1;
1.274 + }
1.275 + }
1.276 + else
1.277 + {
1.278 + FoundKey = ETrue;
1.279 + }
1.280 + } // while ( ! FoundKey )
1.281 +
1.282 + // Set pos, to just after iDelimiter sign
1.283 + SearchBuf.Set(iPtr.Mid(posI));
1.284 + TInt posSB = SearchBuf.Locate(iDelimiter);
1.285 + if(posSB==KErrNotFound) // Illegal format, should flag this...
1.286 + return(EFalse);
1.287 +
1.288 + // Identify start and end of data (EOL or EOF)
1.289 + posI = posI + posSB + 1; // 1 char after iDelimiter
1.290 + TInt posValStart = posI;
1.291 + TInt posValEnd;
1.292 + SearchBuf.Set(iPtr.Mid(posI));
1.293 + posSB = SearchBuf.Locate('\r');
1.294 + if(posSB != KErrNotFound)
1.295 + {
1.296 + posValEnd = posI + posSB;
1.297 + }
1.298 + else
1.299 + {
1.300 + posValEnd = iPtr.Length();
1.301 + }
1.302 +
1.303 + // Check we are still in the section requested
1.304 + if( aSectName.Length() > 0 )
1.305 + {
1.306 + if( posValEnd > posEndOfSection )
1.307 + {
1.308 + return(EFalse);
1.309 + }
1.310 + }
1.311 + // Parse Buffer from posn of key
1.312 + // Start one space after '='
1.313 + TLex lex(iPtr.Mid(posValStart, posValEnd-posValStart));
1.314 + lex.SkipSpaceAndMark(); // Should be at the start of the data
1.315 + aResult.Set(lex.MarkedToken().Ptr(),posValEnd-posValStart - lex.Offset() );
1.316 + return(ETrue);
1.317 + }
1.318 +
1.319 +/**
1.320 +Find an integer value from given aKeyName regardless the section in the ini data file
1.321 +@param aKeyName Key being searched for
1.322 +@param aResult On return, contains the TInt result
1.323 +@return ETrue if found, otherwise EFalse
1.324 +*/
1.325 +TBool CIniData::FindVar(const TDesC& aKeyName, TInt& aResult)
1.326 + {
1.327 + TPtrC ptr(NULL,0);
1.328 + if (FindVar(aKeyName,ptr))
1.329 + {
1.330 + TLex lex(ptr);
1.331 + if (lex.Val(aResult) == KErrNone)
1.332 + return(ETrue);
1.333 + }
1.334 + return(EFalse);
1.335 + }
1.336 +
1.337 +/**
1.338 +Find an integer value from given aKeyName and aSecName in the ini data file
1.339 +@param aSectName Section containing key
1.340 +@param aKeyName Key being searched for in aSectName
1.341 +@param aResult On return, contains TInt result
1.342 +@return ETrue if found, otherwise EFalse
1.343 +*/
1.344 +TBool CIniData::FindVar(const TDesC& aSection,const TDesC& aKeyName,TInt& aResult)
1.345 + {
1.346 + TPtrC ptr(NULL,0);
1.347 + if (FindVar(aSection,aKeyName,ptr))
1.348 + {
1.349 + TLex lex(ptr);
1.350 + if (lex.Val(aResult) == KErrNone)
1.351 + return(ETrue);
1.352 + }
1.353 + return(EFalse);
1.354 +}