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