messagingfw/msgtest/testutils/base/src/msvtestconfig.cpp
changeset 62 db3f5fa34ec7
parent 0 8e480a14352b
equal deleted inserted replaced
60:9f5ae1728557 62:db3f5fa34ec7
       
     1 // Copyright (c) 2000-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 #include "msvtestconfig.h"
       
    17 
       
    18 EXPORT_C TInt TMsvConfigEntry::Set(const TDesC& aLine)
       
    19 	{
       
    20 	TInt pos = Find(aLine, _L("\t= "));
       
    21 	if (pos >= 0 && pos < aLine.Length() - 1)
       
    22 		{
       
    23 		iName.Copy(aLine.Left(pos));
       
    24 		iName.Trim();
       
    25 
       
    26 		iValue.Copy(aLine.Mid(pos + 1));
       
    27 		iValue.Trim();
       
    28 		}
       
    29 	else
       
    30 		return KErrArgument;
       
    31 
       
    32 	return KErrNone;
       
    33 	}
       
    34 
       
    35 TInt TMsvConfigEntry::Find(const TDesC& aString, const TDesC& aFind)
       
    36 	{
       
    37 	TInt result = -1;
       
    38 	for (TInt pos = 0; pos < aFind.Length(); pos++)
       
    39 		{
       
    40 		result = aString.Locate(aFind[pos]);
       
    41 		if (result >= 0)
       
    42 			break;
       
    43 		}
       
    44 	return result;
       
    45 	}
       
    46 
       
    47 EXPORT_C TInt TMsvConfigEntry::Int() const
       
    48 	{
       
    49 	return Int(iValue);
       
    50 	}
       
    51 
       
    52 TInt TMsvConfigEntry::Int(TPtrC aPtr) const
       
    53 	{
       
    54 	TRadix radix = EDecimal;
       
    55 
       
    56 	if (aPtr.Length() > 2 && aPtr[0] == '0' && (aPtr[1] == 'x' || aPtr[1] == 'X'))
       
    57 		{
       
    58 		aPtr.Set(aPtr.Mid(2));
       
    59 		radix = EHex;
       
    60 		}
       
    61 
       
    62 	TLex lex(aPtr);
       
    63 
       
    64 	TUint val = 0;
       
    65 	lex.Val(val, radix);
       
    66 
       
    67 	return val;
       
    68 	}
       
    69 
       
    70 EXPORT_C TVersion TMsvConfigEntry::Version() const
       
    71 	{
       
    72 	TUint major = 0;
       
    73 	TUint minor = 0;
       
    74 	TUint build = 0;
       
    75 
       
    76 	// Find first comma
       
    77 	TInt pos1 = iValue.Find(_L(","));
       
    78 	if (pos1 > 0)
       
    79 		major = Int(iValue.Left(pos1));
       
    80 
       
    81 	// Get position of second comma
       
    82 	TInt pos2 = pos1 + 1;
       
    83 	while(pos2 < iValue.Length() && iValue[pos2] != ',')
       
    84 		pos2++;
       
    85 	
       
    86 	// Check there is any data to read
       
    87 	if (pos1 + 1 < iValue.Length() - 1)
       
    88 		minor = Int(iValue.Mid(pos1 + 1, pos2 - pos1));
       
    89 
       
    90 	// Check there is any data to read
       
    91 	if (pos2 < iValue.Length() - 1)
       
    92 		build = Int(iValue.Mid(pos2 + 1));
       
    93 
       
    94 	return TVersion(major, minor, build);
       
    95 	}
       
    96 
       
    97 CMsvConfigEntrySelection::CMsvConfigEntrySelection()
       
    98 	: CArrayFixFlat<TMsvConfigEntry>(KConfigSelectionGranularity)
       
    99 	{
       
   100 	}
       
   101 
       
   102 EXPORT_C CMsvConfigEntrySelection* CMsvConfigEntrySelection::NewL(RFs& aFs, const TDesC& aFileName)
       
   103 	{
       
   104 	// Create the configuration selection
       
   105 	CMsvConfigEntrySelection* self = new(ELeave)CMsvConfigEntrySelection;
       
   106 	CleanupStack::PushL(self);
       
   107 
       
   108 	// Open the file
       
   109 	RFile file;
       
   110 	User::LeaveIfError(file.Open(aFs, aFileName, EFileShareReadersOnly | EFileRead));
       
   111 	
       
   112 	// Treat it as a text file (unicode)
       
   113 	TFileText textFile;
       
   114 	textFile.Set(file);
       
   115 
       
   116 	TInt error = KErrNone;
       
   117 	TBuf<256> buf;
       
   118 
       
   119 	while(error == KErrNone)
       
   120 		{
       
   121 		// Read a line of text
       
   122 		error = textFile.Read(buf);
       
   123 		buf.TrimLeft();
       
   124 
       
   125 		if (!error)
       
   126 			{
       
   127 			// Skip comments
       
   128 			if (buf.Length() >= 2 && buf[0] == '/' && buf[1] == '/')
       
   129 				continue;
       
   130 
       
   131 			// Create a configuration entry and add to selection
       
   132 			TMsvConfigEntry entry;
       
   133 			if (!entry.Set(buf))
       
   134 				self->AppendL(entry);
       
   135 			}
       
   136 		}
       
   137 
       
   138 	// Did an error occur?
       
   139 	if (error != KErrEof && !error)
       
   140 		User::Leave(error);
       
   141 
       
   142 	CleanupStack::Pop(); // self
       
   143 	return self;
       
   144 	}
       
   145 
       
   146 EXPORT_C TInt CMsvConfigEntrySelection::FindPos(const TDesC& aName) const
       
   147 	{
       
   148 	TInt count = Count();
       
   149 	while(count--)
       
   150 		{
       
   151 		if (At(count).Name().CompareF(aName) == 0)
       
   152 			return count;
       
   153 		}
       
   154 	return KErrNotFound;
       
   155 	}
       
   156 
       
   157 EXPORT_C const TMsvConfigEntry* CMsvConfigEntrySelection::Find(const TDesC& aName) const
       
   158 	{
       
   159 	TInt pos = FindPos(aName);
       
   160 	if (pos >= 0)
       
   161 		return &At(pos);
       
   162 
       
   163 	return NULL;
       
   164 	}