mmdevicefw/mdf/src/codecapi/codecapiresolverutils.cpp
changeset 0 40261b775718
equal deleted inserted replaced
-1:000000000000 0:40261b775718
       
     1 // Copyright (c) 2005-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 <mdf/codecapiresolverutils.h>
       
    17 #include "codecapiresolverconsts.h"
       
    18 
       
    19 
       
    20 /**
       
    21  Creates a new CCodecApiOpaqueData object.
       
    22  @param  aOpaqueData
       
    23  		 A reference to the opaque data value.
       
    24  @return A pointer to the newly constructed match data object.
       
    25  */
       
    26 EXPORT_C CCodecApiOpaqueData* CCodecApiOpaqueData::NewL(const TDesC8& aOpaqueData)
       
    27 	{
       
    28 	CCodecApiOpaqueData* result = CCodecApiOpaqueData::NewLC(aOpaqueData);
       
    29 	CleanupStack::Pop(result);
       
    30 	return result;
       
    31 	}
       
    32 
       
    33 
       
    34 /**
       
    35  Creates a new CCodecApiOpaqueData object and leaves a pointer to it on the cleanup stack.
       
    36  @param  aOpaqueData
       
    37  		 A reference to the opaque data value.
       
    38  @return A pointer to the newly constructed match data object.
       
    39  */
       
    40 EXPORT_C CCodecApiOpaqueData* CCodecApiOpaqueData::NewLC(const TDesC8& aOpaqueData)
       
    41 	{
       
    42 	CCodecApiOpaqueData* result = new (ELeave) CCodecApiOpaqueData(aOpaqueData);
       
    43 	CleanupStack::PushL(result);
       
    44 	result->ConstructL();
       
    45 	return result;
       
    46 	}
       
    47 
       
    48 
       
    49 /**
       
    50  Constructor.
       
    51  @param  aOpaqueData
       
    52  		 A reference to the opaque data value.
       
    53  */
       
    54 CCodecApiOpaqueData::CCodecApiOpaqueData(const TDesC8& aOpaqueData) :
       
    55 	iOpaqueData(aOpaqueData)
       
    56 	{
       
    57 	}
       
    58 
       
    59 /**
       
    60  Sets up the data inside the class by calling <code>ParseTaggedDataL()</code>. 
       
    61  */
       
    62 void CCodecApiOpaqueData::ConstructL()
       
    63 	{
       
    64 	ParseTaggedDataL();
       
    65 	}
       
    66 
       
    67 
       
    68 /**
       
    69  Destructor.
       
    70  */
       
    71 CCodecApiOpaqueData::~CCodecApiOpaqueData()
       
    72 	{
       
    73 	delete iInputDataFormat;
       
    74 	delete iOutputDataFormat;
       
    75 	}
       
    76 
       
    77 /*
       
    78  Parses the opaque data and calls <code>ProcessTaggedDataL()</code> to set the
       
    79  data member of the class.
       
    80  */
       
    81 void CCodecApiOpaqueData::ParseTaggedDataL()
       
    82 	{
       
    83 	TInt readPosition = 0;
       
    84 	TBool moreData = (iOpaqueData.Length()>0) ? ETrue : EFalse;
       
    85 	while (moreData)
       
    86 		{
       
    87 		// Assume that this segment will begin with a tag
       
    88 		TPtrC8 restOfData = iOpaqueData.Mid(readPosition);
       
    89 		TInt endPos = restOfData.MatchF(KTagMatch);
       
    90 		if (endPos == KErrNotFound)
       
    91 			{
       
    92 			User::Leave(KErrCorrupt);
       
    93 			}		
       
    94 		// Extract the tag
       
    95 		TPtrC8 tag = restOfData.Left(KTagLength);
       
    96 		readPosition += KTagLength;
       
    97 		// Find the next tag
       
    98 		restOfData.Set(iOpaqueData.Mid(readPosition));
       
    99 		endPos = restOfData.MatchF(KTagMatch);
       
   100 		TPtrC8 tagData;
       
   101 		if (endPos == KErrNotFound)
       
   102 			{
       
   103 			// If we didn't find a tag, we must be at the end of the data
       
   104 			tagData.Set(restOfData);
       
   105 			readPosition = restOfData.Length();
       
   106 			moreData = EFalse;
       
   107 			}
       
   108 		else
       
   109 			{
       
   110 			tagData.Set(restOfData.Left(endPos));
       
   111 			readPosition += endPos;
       
   112 			}
       
   113 		ProcessTaggedDataL(tag, tagData);		
       
   114 		}
       
   115 	}
       
   116 
       
   117 
       
   118 
       
   119 /**
       
   120  Sets member data to the value of the given opaque data, based upon the tag value provided. 
       
   121  @param  aTag
       
   122  	   	 A constant reference to a tag from the opaque data. Can be \<i\>, \<s\> and \<d\>.
       
   123  @param  aData
       
   124  	   	 The data associated with a tag.
       
   125  */
       
   126 void CCodecApiOpaqueData::ProcessTaggedDataL(const TDesC8& aTag, const TDesC8& aData)
       
   127 	{
       
   128 	if (aTag==KMediaUid)
       
   129 		{
       
   130 		SetMediaUidL(aData);
       
   131 		return;
       
   132 		}
       
   133 	if (aTag==KInputPortFormat)
       
   134 		{
       
   135 		HBufC8* dataFormat = aData.AllocL();
       
   136 		delete iInputDataFormat;
       
   137 		iInputDataFormat = dataFormat;
       
   138 		return;
       
   139 		}
       
   140 	if (aTag==KOutputPortFormat)
       
   141 		{
       
   142 		HBufC8* dataFormat = aData.AllocL();
       
   143 		delete iOutputDataFormat;
       
   144 		iOutputDataFormat = dataFormat;
       
   145 		return;
       
   146 		}
       
   147 	User::Leave(KErrCorrupt);	
       
   148 	}
       
   149 
       
   150 
       
   151 /**
       
   152  Sets the value of the media UID data.
       
   153  @param  aData
       
   154  		 The value to set the media uid to.
       
   155  */
       
   156 void CCodecApiOpaqueData::SetMediaUidL(const TDesC8& aData)
       
   157 	{
       
   158 	ConvertTextToUidL(aData, iTypeUid);
       
   159 	}
       
   160 
       
   161 
       
   162 /**
       
   163  Converts a given string to an integer.
       
   164  @param  aData
       
   165  		 The value to be converted.
       
   166  @return The converted value
       
   167  */
       
   168 TUint CCodecApiOpaqueData::ConvertTextToTUintL(const TDesC8& aData)
       
   169 	{
       
   170 	// Determine whether hex or decimal then parse as such
       
   171 	_LIT8(K0x, "0x");
       
   172 	TInt lenHexMarker = K0x().Length();
       
   173 	TUint value = 0;
       
   174 
       
   175 	// simple white space left trim (only handles spaces)
       
   176 	int index = 0;
       
   177 	while (aData[index]==' ')
       
   178 		{
       
   179 		index++;
       
   180 		}
       
   181 	TPtrC8 data = aData.Mid(index);
       
   182 	if (((data.Left(lenHexMarker).CompareF(K0x) == 0)) && (data.Length() >= 3))
       
   183 		{
       
   184 		// Only take the characters after "0x"
       
   185 		TLex8 lex(data.Mid(lenHexMarker));
       
   186 		User::LeaveIfError(lex.Val(value, EHex));
       
   187 		}
       
   188 	else if (data.Length() > 0)
       
   189 		{
       
   190 		TLex8 lex(data);
       
   191 		User::LeaveIfError(lex.Val(value, EDecimal));
       
   192 		}
       
   193 	else
       
   194 		{
       
   195 		User::Leave(KErrCorrupt);
       
   196 		}		
       
   197 	return value;
       
   198 	}
       
   199 
       
   200 
       
   201 /**
       
   202  Converts a given string to a UID.
       
   203  @param  aData
       
   204  		 The value to be converted.
       
   205  @param  aData
       
   206  		 The value after conversion.
       
   207  */
       
   208 void CCodecApiOpaqueData::ConvertTextToUidL(const TDesC8& aData, TUid& aUid)
       
   209 	{
       
   210 	_LIT8(K0x, "0x");	
       
   211 	if ((aData.Length() == 10) && (aData.FindF(K0x) == 0))
       
   212 		{
       
   213 		// only take the right 8 characters (ie discard the "0x")
       
   214 		TLex8 lex(aData.Right(8));
       
   215 		TUint32 value = 0;
       
   216 		User::LeaveIfError(lex.Val(value, EHex));
       
   217 		aUid.iUid = value;
       
   218 		}
       
   219 	else
       
   220 		{
       
   221 		User::Leave(KErrCorrupt);
       
   222 		}		
       
   223 	}
       
   224 
       
   225 
       
   226 /**
       
   227  Returns the value of the type UID data member.
       
   228  @return The value of the type UID data member.
       
   229  */
       
   230 EXPORT_C TUid CCodecApiOpaqueData::TypeUid() const
       
   231 	{
       
   232 	return iTypeUid;
       
   233 	}
       
   234 	
       
   235 
       
   236 /**
       
   237  Returns the value of the input data type.
       
   238  @return The value of the input data type.
       
   239  */	
       
   240 EXPORT_C const TDesC8& CCodecApiOpaqueData::InputDataType() const
       
   241 	{
       
   242 	if (iInputDataFormat)
       
   243 		{
       
   244 		return *iInputDataFormat;
       
   245 		}
       
   246 	return KNullDesC8;
       
   247 	}
       
   248 
       
   249 
       
   250 /**
       
   251  Returns the value of the output data type.
       
   252  @return The value of the output data type.
       
   253  */	
       
   254 EXPORT_C const TDesC8& CCodecApiOpaqueData::OutputDataType() const
       
   255 	{
       
   256 	if (iOutputDataFormat)
       
   257 		{
       
   258 		return *iOutputDataFormat;
       
   259 		}
       
   260 	return KNullDesC8;
       
   261 	}
       
   262 
       
   263 
       
   264 /**
       
   265  Compares the input data type with the argument of the method.
       
   266  @param  aDataType
       
   267  		 The value to be compared to.
       
   268  @return ETrue if the argument of the method and the input data 
       
   269  		 type are the same.
       
   270  */
       
   271 EXPORT_C TBool CCodecApiOpaqueData::CompareInputDataType(const TDesC8& aDataType)
       
   272 	{	
       
   273 	if(iInputDataFormat)
       
   274 		{
       
   275 		const TDesC8& i = *iInputDataFormat;
       
   276 		if(aDataType.CompareF(i) == 0)
       
   277 			{
       
   278 			return ETrue;
       
   279 			}			
       
   280 		}	
       
   281 	return EFalse;
       
   282 	}
       
   283 	
       
   284 	
       
   285 /**
       
   286  Compares the output data type with the argument of the method.
       
   287  @param  aDataType
       
   288  		 The value to be compared to.
       
   289  @return ETrue if the argument of the method and the output data
       
   290  		 type are the same.
       
   291  */	
       
   292 EXPORT_C TBool CCodecApiOpaqueData::CompareOutputDataType(const TDesC8& aDataType)
       
   293 	{
       
   294 	if(iOutputDataFormat)
       
   295 		{
       
   296 		if(aDataType.CompareF(*iOutputDataFormat) == 0)
       
   297 			{
       
   298 			return ETrue;
       
   299 			}		
       
   300 		}	
       
   301 	return EFalse;
       
   302 	}	
       
   303 	
       
   304