messagingfw/msgtestproduct/testutils/src/T_UtilsConfigFileParserUtility.cpp
changeset 0 8e480a14352b
child 58 6c34d0baa0b1
equal deleted inserted replaced
-1:000000000000 0:8e480a14352b
       
     1 // Copyright (c) 2006-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 // This is the CPP file which contains utility functions for parsing the config file
       
    15 // 
       
    16 //
       
    17 
       
    18 /**
       
    19  @file
       
    20 */
       
    21 
       
    22 
       
    23 // User include
       
    24 #include <t_utilsconfigfileparserutility.h>
       
    25 
       
    26 // epoc inlcude
       
    27 #include <f32file.h>
       
    28 
       
    29 // contants and literals used
       
    30 const TInt KFileBufferSize = 1024;
       
    31 _LIT8(KComment,"*#*");
       
    32 
       
    33 
       
    34 /**
       
    35 NewL()
       
    36 Allocates and creates a new CT_MsgUtilsConfigFileParserUtility object
       
    37 
       
    38 @param aFileName
       
    39 Name of the file to be parsed
       
    40 
       
    41 @leave KErrNoMemory
       
    42 @return
       
    43 The newly created CT_MsgUtilsConfigFileParserUtility object
       
    44 */
       
    45 EXPORT_C CT_MsgUtilsConfigFileParserUtility* CT_MsgUtilsConfigFileParserUtility::NewL(const TDesC& aFileName)
       
    46 	{
       
    47 	CT_MsgUtilsConfigFileParserUtility* self = new (ELeave) CT_MsgUtilsConfigFileParserUtility();
       
    48 	CleanupStack::PushL(self);
       
    49 	self->ConstructL(aFileName);
       
    50 	CleanupStack::Pop();
       
    51 	return self;
       
    52 	}
       
    53 	
       
    54 
       
    55 /**
       
    56 CT_MsgUtilsConfigFileParserUtility()
       
    57 Constructor
       
    58 */
       
    59 EXPORT_C CT_MsgUtilsConfigFileParserUtility::CT_MsgUtilsConfigFileParserUtility()
       
    60 	{
       
    61 	}
       
    62 
       
    63 
       
    64 /**
       
    65 ~CT_MsgUtilsConfigFileParserUtility()
       
    66 Destructor
       
    67 */
       
    68 CT_MsgUtilsConfigFileParserUtility::~CT_MsgUtilsConfigFileParserUtility()
       
    69 	{
       
    70 	iName.ResetAndDestroy();
       
    71 	iContent.ResetAndDestroy();
       
    72 	iContent8.ResetAndDestroy();
       
    73 	}
       
    74 
       
    75 
       
    76 /**
       
    77 ConstructL()
       
    78 Parses a .txt file and creates Arrays of fields and there values
       
    79 
       
    80 @param aFileName
       
    81 Name of the file to be parsed.
       
    82 */
       
    83 EXPORT_C void CT_MsgUtilsConfigFileParserUtility::ConstructL(const TDesC& aFileName)
       
    84 	{
       
    85 	RFs fileServerSession;
       
    86 
       
    87 	fileServerSession.Connect();
       
    88 
       
    89 	RFile file;
       
    90 	User::LeaveIfError(file.Open(fileServerSession, aFileName, EFileRead));
       
    91 
       
    92 	TInt eof = EFalse;
       
    93 	TInt fileOffset = 0;
       
    94 	TBuf8<KFileBufferSize> fileBuffer;
       
    95 
       
    96 	while (!eof)
       
    97 		{
       
    98 		fileBuffer.SetLength(0);
       
    99 		User::LeaveIfError(file.Read(fileOffset, fileBuffer, KFileBufferSize));
       
   100 		TInt read = fileBuffer.Length();
       
   101 
       
   102 		if (read < KFileBufferSize)
       
   103 			{
       
   104 			fileBuffer.Append('\n');
       
   105 			eof = ETrue;
       
   106 			}
       
   107 
       
   108 		TInt lineOverflow = fileBuffer.Locate('\n');
       
   109 		
       
   110 		if ((lineOverflow == KErrNotFound) && (read == KFileBufferSize))
       
   111 			{
       
   112 			User::Leave(KErrOverflow);
       
   113 			}
       
   114 
       
   115 		TInt eol = EFalse;
       
   116 		
       
   117 		while (!eol)
       
   118 			{
       
   119 			TInt lineFeedLocation = fileBuffer.Locate('\n');
       
   120 			
       
   121 			if (lineFeedLocation == KErrNotFound)
       
   122 				{
       
   123 				eol = ETrue;
       
   124 				}
       
   125 			
       
   126 			else
       
   127 				{
       
   128 				fileOffset += lineFeedLocation + 1;
       
   129 				TInt lineLength;
       
   130 				if ((lineFeedLocation != 0) && (fileBuffer[lineFeedLocation - 1] == '\r'))
       
   131 					{
       
   132 					lineLength = lineFeedLocation - 1;
       
   133 					}
       
   134 					
       
   135 				else
       
   136 					{
       
   137 					lineLength = lineFeedLocation;
       
   138 					}
       
   139 					
       
   140 				TPtrC8 line  = fileBuffer.Left(lineLength);
       
   141 				TInt commentLocation = line.Match(KComment);
       
   142 				
       
   143 				if (commentLocation != KErrNotFound)
       
   144 					{
       
   145 					TPtrC8 skipComment = line.Left(commentLocation);
       
   146 					line.Set(skipComment);
       
   147 					}
       
   148 					
       
   149 				TInt seperatorLocation = line.Locate('=');
       
   150 				
       
   151 				if (seperatorLocation != KErrNotFound)
       
   152 					{
       
   153 					if ((seperatorLocation == 0) || (seperatorLocation == line.Length() - 1))
       
   154 						{
       
   155 						seperatorLocation = KErrNotFound;
       
   156 						}
       
   157 					}
       
   158 					
       
   159 				if (seperatorLocation != KErrNotFound)
       
   160 					{
       
   161 					TPtrC8 namePtr = line.Left(seperatorLocation);
       
   162 					HBufC8* nameBuf8 = HBufC8::NewL(namePtr.Length());
       
   163 					CleanupStack::PushL(nameBuf8);
       
   164 					
       
   165 					TPtr8 name8 = nameBuf8->Des();
       
   166 					name8.Copy(namePtr);
       
   167 					name8.Trim();
       
   168 					HBufC* nameBuf16 = HBufC::NewL(namePtr.Length());
       
   169 					TPtr name16 = nameBuf16->Des();
       
   170 					name16.Copy(name8);
       
   171 					iName.Append(nameBuf16);
       
   172 					CleanupStack::PopAndDestroy(nameBuf8);
       
   173 
       
   174 					TPtrC8 contentPtr = line.Mid(seperatorLocation + 1);
       
   175 					HBufC8* contentBuf8 = HBufC8::NewL(contentPtr.Length());
       
   176 					CleanupStack::PushL(contentBuf8);
       
   177 					TPtr8 content8 = contentBuf8->Des();
       
   178 					content8.Copy(contentPtr);
       
   179 					content8.Trim();
       
   180 					
       
   181 					HBufC* contentBuf16 = HBufC::NewL(contentPtr.Length());
       
   182 					TPtr content16 = contentBuf16->Des();
       
   183 					content16.Copy(content8);
       
   184 					iContent.Append(contentBuf16);
       
   185 					iContent8.Append(contentBuf8);
       
   186 					CleanupStack::Pop(contentBuf8);
       
   187 					}
       
   188 				TPtrC8 theRest = fileBuffer.Mid(lineFeedLocation + 1);
       
   189 				fileBuffer.Copy(theRest);
       
   190 				}
       
   191 			}
       
   192 		}
       
   193 	file.Close();
       
   194 	fileServerSession.Close();
       
   195 	}
       
   196 
       
   197 
       
   198 /**
       
   199 GetFieldAsInteger()
       
   200 Retrives the content of a field name and Interpret it as an integer.
       
   201 
       
   202 @param aFieldName
       
   203 @param aValue
       
   204 @return
       
   205 Returns an integer corresponding to the field
       
   206 */
       
   207 EXPORT_C TInt CT_MsgUtilsConfigFileParserUtility::GetFieldAsInteger(const TDesC& aFieldName, TInt& aValue)
       
   208 	{
       
   209 	TInt count = iName.Count();
       
   210 
       
   211 	for (TInt i = 0; i<count; i++)
       
   212 		{
       
   213 		if (iName[i]->Compare(aFieldName) == 0)
       
   214 			{ 
       
   215 			TPtrC content = iContent[i]->Des();
       
   216 			TLex lex(content);
       
   217 			lex.Val(aValue);
       
   218 			return (KErrNone);
       
   219 			}
       
   220 		}
       
   221 	return (KErrNotFound);
       
   222 	}
       
   223 
       
   224 
       
   225 /**
       
   226 GetFieldAsString()
       
   227 Retrives the content of a field name and Interpret it as an string
       
   228 
       
   229 @param aFieldName
       
   230 @param aValue
       
   231 @return
       
   232 Returns an string corresponding to the field
       
   233 */
       
   234 EXPORT_C TInt CT_MsgUtilsConfigFileParserUtility::GetFieldAsString(const TDesC& aFieldName, TPtrC& aString)
       
   235 	{
       
   236 	TInt count = iName.Count();
       
   237 
       
   238 	for (TInt i = 0; i<count; i++)
       
   239 		{
       
   240 		if (iName[i]->Compare(aFieldName) == 0)
       
   241 			{ 
       
   242 			aString.Set(*iContent[i]);
       
   243 			return (KErrNone);
       
   244 			}
       
   245 		}
       
   246 	return (KErrNotFound);
       
   247 	}
       
   248 
       
   249 
       
   250 /**
       
   251 GetFieldAsString8()
       
   252 Retrives the content of a field name and Interpret it as an 8-bit descriptor
       
   253 
       
   254 @param aFieldName
       
   255 @param aValue
       
   256 @return
       
   257 Returns an 8-bit descriptor corresponding to the field
       
   258 */
       
   259 EXPORT_C TInt CT_MsgUtilsConfigFileParserUtility::GetFieldAsString8(const TDesC& aFieldName, TPtrC8& aString)
       
   260 	{
       
   261 	TInt count = iName.Count();
       
   262 	
       
   263 	for (TInt i = 0; i<count; i++)
       
   264 		{
       
   265 		if (iName[i]->Compare(aFieldName) == 0)
       
   266 			{ 
       
   267 			aString.Set(*iContent8[i]);
       
   268 			return (KErrNone);
       
   269 			}
       
   270 		}
       
   271 	return (KErrNotFound);
       
   272 	}
       
   273 
       
   274 
       
   275