commsfwtools/preparedefaultcommsdatabase/Tools/ced/src/CIniFile.cpp
changeset 0 dfb7c4ff071f
equal deleted inserted replaced
-1:000000000000 0:dfb7c4ff071f
       
     1 // Copyright (c) 1997-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 //
       
    15 
       
    16 /**
       
    17  @file 
       
    18  @internalComponent
       
    19 */
       
    20 
       
    21 #include <f32file.h>
       
    22 #include "CIniFile.h"
       
    23 #include "dbdef.h"
       
    24 
       
    25 CIniData::CIniData() 
       
    26 /**
       
    27 Default constructor
       
    28 
       
    29 */
       
    30 	: iPtr(NULL,0), section(NULL,0), block(NULL,0), BlockState(E_UNKNOWN),
       
    31 	  blockStart(0), blockEnd(0), scanStart(0)
       
    32 	{
       
    33 	__DECLARE_NAME(_S("CIniData"));
       
    34 	}
       
    35 
       
    36 CIniData::~CIniData()
       
    37 /**
       
    38 Destructor
       
    39 
       
    40 */
       
    41 	{
       
    42 	delete (TText*)iPtr.Ptr();
       
    43 	delete iToken;
       
    44 	delete iName;
       
    45 	}
       
    46 
       
    47 CIniData* CIniData::NewL(const TDesC& aName)
       
    48 /**
       
    49 Allocates and constructs a new CIniData object
       
    50 
       
    51 @param aName Name of the ini file to be read
       
    52 @return A newly created CIniData object
       
    53 */
       
    54 	{
       
    55 	CIniData* p = new(ELeave) CIniData;
       
    56 	CleanupStack::PushL(p);
       
    57 	p->ConstructL(aName);
       
    58 	CleanupStack::Pop(p);
       
    59 	return p;
       
    60 	}
       
    61 
       
    62 
       
    63 void CIniData::ConstructL(const TDesC& aName)
       
    64 /**
       
    65 Allocates a buffer and reads file's contents into iPtr
       
    66 
       
    67 @param aName Name of the ini file to be read
       
    68 */
       
    69 	{
       
    70  	// Allocate space for token
       
    71 	iToken=HBufC::NewL(MAX_COL_NAME_LEN + 2);	// 2 extra chars for [tokenName]
       
    72 
       
    73 	// Connect to file server
       
    74 	TAutoClose<RFs> fs;
       
    75 	User::LeaveIfError(fs.iObj.Connect());
       
    76 	fs.PushL();
       
    77 
       
    78 	// Find file, given name
       
    79 	TFindFile ff(fs.iObj);
       
    80 	User::LeaveIfError(ff.FindByPath(aName, NULL));
       
    81 	iName=ff.File().AllocL();
       
    82 
       
    83 	// Open file
       
    84 	TAutoClose<RFile> file;
       
    85 	TInt size;
       
    86 	User::LeaveIfError(file.iObj.Open(fs.iObj,*iName,EFileStreamText|EFileRead));
       
    87 	file.PushL();
       
    88 
       
    89 	// Get file size and read in
       
    90 	User::LeaveIfError(file.iObj.Size(size));
       
    91 	TText* data=(TText*)User::AllocL(size);
       
    92 	iPtr.Set(data, TUint(size)/sizeof(TText), TUint(size)/sizeof(TText));
       
    93 	TPtr8 dest((TUint8*)data, 0, size);
       
    94 	User::LeaveIfError(file.iObj.Read(dest));
       
    95 	TUint8* ptr = (TUint8*)data;
       
    96 
       
    97 	//
       
    98 	// This is orderred as FEFF assuming the processor is Little Endian
       
    99 	// The data in the file is FFFE.		PRR 28/9/98
       
   100 	//
       
   101 	if(size>=(TInt)sizeof(TText) && iPtr[0]==0xFEFF)
       
   102 		{
       
   103 		// UNICODE Text file so lose the FFFE
       
   104 		Mem::Copy(ptr, ptr+sizeof(TText), size-sizeof(TText));
       
   105 		iPtr.Set(data, TUint(size)/sizeof(TText)-1, TUint(size)/sizeof(TText)-1);
       
   106 		}
       
   107 	else if(size)
       
   108 		{
       
   109 		// NON-UNICODE so convert to UNICODE
       
   110 		TText* newdata = (TText*)User::AllocL(size*sizeof(TText));
       
   111 		iPtr.Set(newdata, size, size);
       
   112 		TInt i;
       
   113 		for(i=0 ; i<size ; ++i)
       
   114 			{
       
   115 			iPtr[i]=ptr[i];
       
   116 			}
       
   117 		delete data;
       
   118 		}
       
   119 
       
   120 	file.Pop();
       
   121 	fs.Pop();
       
   122 	}
       
   123 
       
   124 
       
   125 TInt CIniData::OpenSetBlock(const TDesC &aSection)
       
   126 /**
       
   127 Locates and opens a section for 'updates'
       
   128 
       
   129 @param aSection Section to work with
       
   130 @return ETrue if successful or EFalse
       
   131 */
       
   132 	{
       
   133 	BlockState = E_SET;
       
   134 	return OpenBlock(aSection);
       
   135 	}
       
   136 
       
   137 
       
   138 TInt CIniData::OpenTemplateBlock(const TDesC &aSection)
       
   139 /**
       
   140 Locates and opens a section for 'template'
       
   141 
       
   142 @param aSection Section to work with
       
   143 @return ETrue if successful or EFalse
       
   144 */
       
   145 	{
       
   146 	BlockState = E_TEMPLATE;
       
   147 	return OpenBlock(aSection);
       
   148 	}
       
   149 
       
   150 
       
   151 TInt CIniData::OpenAddBlock(const TDesC &aSection)
       
   152 /**
       
   153 Locates and opens a section for 'adds'
       
   154 
       
   155 @param aSection Section to work with
       
   156 @return ETrue if successful or EFalse
       
   157 */
       
   158 	{
       
   159 	BlockState = E_ADD;
       
   160 	return OpenBlock(aSection);
       
   161 	}
       
   162 
       
   163 
       
   164 TInt CIniData::OpenBlock(const TDesC &aSection)
       
   165 /**
       
   166 Reads in the entire table section of all ADD / SET blocks into 'section'
       
   167 
       
   168 @param aSection Section to work with
       
   169 @return ETrue if successful or EFalse
       
   170 */
       
   171 	{
       
   172     if (BlockState != E_UNKNOWN)
       
   173 		{
       
   174 	    blockStart = 0;
       
   175 	    blockEnd = 0;
       
   176 	    scanStart = 0;
       
   177 
       
   178 	    TPtr sectionToken = iToken->Des();
       
   179 		_LIT(sectionTokenString,"[%S]");
       
   180 		sectionToken.Format(sectionTokenString,&aSection);
       
   181 
       
   182 	    // locate the section
       
   183 		TInt sectionStart = iPtr.FindF(sectionToken);
       
   184 	    if (sectionStart == KErrNotFound)
       
   185 			{
       
   186 	        return EFalse;
       
   187 			}
       
   188 
       
   189 		// step to the end of the section name
       
   190 		TPtrC tempSection = iPtr.Mid(sectionStart);
       
   191         sectionStart += tempSection.Find(TPtrC(_S("]")));
       
   192 
       
   193 		if (sectionStart == KErrNotFound)
       
   194 			{
       
   195 			return EFalse;
       
   196 			}
       
   197 		sectionStart++;
       
   198 
       
   199 		// we are now at the start of the section data
       
   200         tempSection.Set(iPtr.Mid(sectionStart));
       
   201 
       
   202 		// loop until we reach the end of the section
       
   203 		TUint32 i=0;
       
   204 		TUint32 lMaxLen = tempSection.Length();
       
   205 
       
   206         for (i=0;i<lMaxLen;i++)
       
   207 			{
       
   208 	        if (tempSection.Ptr()[i] == '[' &&
       
   209 		        tempSection.Ptr()[i - 1] != '\\')
       
   210 				{
       
   211 			    i--;
       
   212 			    break;
       
   213 				}
       
   214 			}
       
   215 		
       
   216 		// set the actual section to work with
       
   217         tempSection.Set(iPtr.Mid(sectionStart, i));
       
   218 		section.Set((unsigned short *)tempSection.Ptr(), tempSection.Length(), tempSection.Size());
       
   219 		}
       
   220 
       
   221 	return StepToNextBlock();	// find the first block
       
   222 	}
       
   223 
       
   224 
       
   225 TInt CIniData::StepToNextBlock()
       
   226 /**
       
   227 Locates the next available block of data within a section
       
   228 
       
   229 @return ETrue if successful or EFalse
       
   230 */
       
   231 	{
       
   232 	if (BlockState != E_UNKNOWN)
       
   233 		{
       
   234 		// get unscanned portion of the section
       
   235 		TPtrC temp = section.Mid(scanStart);
       
   236 
       
   237 		// find the start of the next block
       
   238 		if (BlockState == E_SET)
       
   239 			{
       
   240 			blockStart = temp.FindF(TPtrC(BEGIN_SET));
       
   241 			blockEnd = temp.FindF(TPtrC(END_SET));
       
   242 			}
       
   243 		else if (BlockState == E_TEMPLATE)
       
   244 			{
       
   245 			blockStart = temp.FindF(TPtrC(BEGIN_TEMPLATE));
       
   246 			blockEnd = temp.FindF(TPtrC(END_TEMPLATE));
       
   247 			}
       
   248 		else if (BlockState == E_ADD)
       
   249 			{
       
   250 			blockStart = temp.FindF(TPtrC(BEGIN_ADD));
       
   251 			blockEnd = temp.FindF(TPtrC(END_ADD));
       
   252 			}
       
   253 
       
   254 		if (blockStart != KErrNotFound && blockEnd != KErrNotFound)
       
   255 			{
       
   256 			// set the start point for the next block search
       
   257 			scanStart += blockEnd;
       
   258 			scanStart++;
       
   259 
       
   260 			// set the actual block to work with
       
   261 			blockEnd -= blockStart;
       
   262 			TPtrC tempBlock = temp.Mid(blockStart,blockEnd);
       
   263 			block.Set((unsigned short *)tempBlock.Ptr(), tempBlock.Length(), tempBlock.Size());
       
   264 			return ETrue;
       
   265 			}
       
   266 		}
       
   267 
       
   268 	return EFalse;
       
   269 	}
       
   270 
       
   271 
       
   272 TInt CIniData::GetSetting(const TDesC &aValue, TPtrC &aSetting)
       
   273 /**
       
   274 Retrieves the value for a particular setting within a block
       
   275 
       
   276 @param aValue The setting to be retrieved 
       
   277 @param aSetting On return it contains the value for the setting
       
   278 @return KErrNone if successful or KErrArgument for fields simply left blank. If the global BlockState is unknown returns KErrNotReady.
       
   279 */
       
   280 	{
       
   281 	TInt valid(KErrNotReady);	
       
   282 
       
   283 	if (BlockState != E_UNKNOWN)
       
   284 	    {
       
   285 		// first check for CONDITION field
       
   286 		// if we are NOT looking for this field, we need to trim off any condition
       
   287 		// field from the block because there may be a setting within it we 
       
   288 		// want to ignore during our search for this field
       
   289         TPtrC pBlock(block);
       
   290 		TInt tempEnd = blockEnd;
       
   291 		TPtrC Condition = CONDITION;
       
   292 		if (aValue.Compare(Condition) != 0)
       
   293 		    {
       
   294 			TPtr varCondToken = iToken->Des();
       
   295 			_LIT(varCondTokenString, "%S=");
       
   296 			varCondToken.Format(varCondTokenString, &Condition);
       
   297 			TInt CondPos = pBlock.FindF(varCondToken);
       
   298 			if (CondPos != KErrNotFound)
       
   299 				{
       
   300 				tempEnd = CondPos;
       
   301 				}
       
   302 		    }
       
   303 
       
   304 		// now look for the actual value
       
   305         TBool searchAgain = EFalse;
       
   306         _LIT(varTokenString, "%S=");
       
   307 		TPtr varToken = iToken->Des();
       
   308 		varToken.Format(varTokenString, &aValue);
       
   309 
       
   310         TInt pos = KErrNotFound;
       
   311         do
       
   312             {
       
   313             searchAgain = EFalse;
       
   314             pos = pBlock.FindF(varToken);
       
   315 		    if (pos != KErrNotFound && pos < tempEnd)
       
   316 		        {
       
   317                 // check that this is a complete match, not a substring
       
   318                 // match against another similar field, e.g. LoginScript
       
   319                 // must only match against "LoginScript" not "UseLoginScript"
       
   320                 if  (pos > 0)
       
   321                     {
       
   322                     // Previous character must be white space
       
   323                     const TChar previousCharacter = pBlock[pos - 1];
       
   324                     if  (previousCharacter.IsSpace())
       
   325                         {
       
   326 			            // make sure we haven't overrun our block
       
   327 			            TInt length = varToken.Length();
       
   328 			            if (pos + length < tempEnd)
       
   329 			                {
       
   330 				            TLex lex(pBlock.Mid(pos + length));
       
   331 
       
   332 				            // if enclosed by quotes, extract the text within
       
   333 				            if (lex.Peek() == '"')
       
   334 				                {
       
   335 					            lex.SkipAndMark(1);			// start of data
       
   336 
       
   337 					            // stop at end of quote or line
       
   338 					            while(lex.Peek() != '"' && lex.Peek() != 10 && lex.Peek() != 13)
       
   339 									{
       
   340 						            lex.Inc();
       
   341 									}
       
   342 				                }
       
   343 							else if(lex.Peek() == 10 || lex.Peek() == 13)	// skip empty or blank field value
       
   344 								{
       
   345 								return KErrArgument;
       
   346 								}
       
   347 							else	// skip any unwanted spaces or tabs in a field value
       
   348 								{
       
   349 								TBool fieldValFound=EFalse;
       
   350 								while(lex.Peek() != 10 && lex.Peek() != 13) 
       
   351 									{
       
   352 									if(!fieldValFound)
       
   353 										{
       
   354 										while(lex.Peek() == 9 || lex.Peek() == 32) // skip any space or a tab
       
   355 											{
       
   356 											lex.SkipAndMark(1);
       
   357 											}
       
   358 										if(lex.Peek() == 10 || lex.Peek() == 13) // field value simply filled with space or tab
       
   359 											{
       
   360 											return KErrArgument;
       
   361 											}
       
   362 										}
       
   363 									fieldValFound=ETrue;	// start of real data
       
   364 									lex.Inc();
       
   365 									}
       
   366 								}
       
   367 
       
   368 				            aSetting.Set(lex.MarkedToken().Ptr(),lex.MarkedToken().Length());
       
   369 							valid = KErrNone;
       
   370                             }
       
   371                         }
       
   372                     else
       
   373                         {
       
   374                         // E.g. LoginScript matched against UseLoginScript -> must look
       
   375                         // for another match after the current one
       
   376                         pBlock.Set(pBlock.Mid(pos+1));
       
   377                         searchAgain = ETrue;
       
   378                         }
       
   379                     }
       
   380 			    }
       
   381             }
       
   382         while(searchAgain);
       
   383 
       
   384 	    }
       
   385 
       
   386 	return valid;
       
   387     }