/**
The method parses aFileName argument and constructs the full database file name (including the path) there.
The full file name will be constructed in aFileName input/output argument.

@param aDbFileName Input/Output. Database file name will be constructed there.
@param aSysDrivePrivatePath SQL server private path on the system drive.   
@param aDrive Output parameter. The drive number.
				
@leave KErrBadName Missing file name.

@panic SqlDb 7 In _DEBUG mode - no drive in the final file path.

@internalComponent
*/
static void DoFullFileNameL(TDes& aDbFileName, const TDesC& aSysDrivePrivatePath, TDriveNumber& aDrive)
	{
	TParse parse;
	__SQLLEAVE_IF_ERROR2(parse.Set(aDbFileName, &aSysDrivePrivatePath, NULL));
	if(!parse.NamePresent())
		{
		__SQLLEAVE2(KErrBadName);	
		}
	aDbFileName.Copy(parse.FullName());
	TPtrC driveName = parse.Drive();
	__ASSERT_DEBUG(driveName.Length() > 0, __SQLPANIC2(ESqlPanicInternalError));
	TInt driveNumber = -1;
	__SQLLEAVE_IF_ERROR2(RFs::CharToDrive(driveName[0], driveNumber));
	aDrive = static_cast <TDriveNumber> (driveNumber);
	}
/**
Protected constructor.

Extracts the initialisation data provided by the calling functions: ConstructSourceL() and 
ConstructSinkL(). Creates a file server session and sets up file name. If there is a file name and 
it cannot be found this function leaves. If there is no file name the function leaves. Does not 
attempt to open the file or check whether the file exists.

If aInitData contains a TMMFFileHandleParams instead of TMMFFileParams, the source/sink is constructed from 
the file handle provided by the caller

@param  aInitData
Initialisation data packaged in a TMMFFileParams or in a TMMFFileHandleParams (File Handle)
*/
void CFileMultimediaSource::ConstructL(const TDesC8& aInitData,TMMFileMode aFileMode)
    {
    User::LeaveIfError(iFsSession.Connect());
    // on IPCv2 we auto attach
    User::LeaveIfError(iFsSession.ShareAuto());
    
    User::LeaveIfError(iFsSession.ShareProtected());
    
    TBool fileInit = EFalse;
    HBufC* filename = NULL; 
    TBool filenamePushed = EFalse;
    
    iCAFParameters = new (ELeave) CCAFParameters;
    TBool drmContent = EFalse;
    RDesReadStream stream(aInitData);
    CleanupClosePushL(stream);

    TUid initUid;
    
    initUid = TUid::Uid(stream.ReadInt32L());
    
    if (initUid == KMMFileHandleSourceUid)
        {
        TPckgBuf<RFile*> fileptr;
        stream.ReadL(fileptr);
        
        iHandle.Duplicate(*fileptr());
        
        TInt length;
        length = stream.ReadInt32L();
        if (length>0)
            {
            iCAFParameters->iUniqueId = HBufC::NewL(length);
            TPtr16 ptr = iCAFParameters->iUniqueId->Des();
            stream.ReadL(ptr, length);
            }
        iFileHandle = ETrue;
        filename = HBufC::NewMaxL(KMaxFileName);
        TPtr ptr = filename->Des();
        iHandle.Name(ptr);
        fileInit = ETrue;
        drmContent = ETrue;
        
        iCAFParameters->iEnableUI = stream.ReadInt32L();
        }
    
    else if (initUid == KMMFileSourceUid)
        {
        TInt length;
        length = stream.ReadInt32L();
        filename = HBufC::NewMaxLC(length);
        TPtr ptr = filename->Des();
        stream.ReadL(ptr, length);
        
        length = stream.ReadInt32L();
        if (length>0)
            {
            iCAFParameters->iUniqueId = HBufC::NewMaxL(length);
            ptr.Set(iCAFParameters->iUniqueId->Des());
            stream.ReadL(ptr, length);
            }
        CleanupStack::Pop(filename);
        
        fileInit = ETrue;
        drmContent = ETrue;
        iFileHandle = EFalse; 
        iCAFParameters->iEnableUI = stream.ReadInt32L();
        }
    else
        {
        //		TODO If the UID is unknown we should reject, but  currently
        //		code also used for older calls that just supply filename.
        //		User::Leave(KErrNotSupported);
        }
    
    CleanupStack::PopAndDestroy(&stream);
    
    if (!fileInit && aInitData.Length() == sizeof(TMMFFileHandleParams))
        {
        TMMFFileHandleParams params;
        TPckgC<TMMFFileHandleParams> config(params);
        config.Set(aInitData);
        params = config();
        
        
        if (params.iUid == KFileHandleUid)
            {
            fileInit = ETrue;
            User::LeaveIfError(iHandle.Duplicate(*params.iFile));
            TInt pos = 0;
            // make sure the duplicate handle is at the start of the file - the usage of the file handle really requires this
            User::LeaveIfError(iHandle.Seek(ESeekStart, pos));
            iFileHandle = ETrue;
            filename = HBufC::NewMaxLC(KMaxFileName);
            filenamePushed = ETrue;
            TPtr ptr = filename->Des();
            User::LeaveIfError(iHandle.Name(ptr));
            }
        }
    
    if (!fileInit) // do old case as last resort
        {
        TMMFFileParams params;
        TPckgC<TMMFFileParams> config(params);
        config.Set(aInitData);
        params = config();
        
        filename = params.iPath.AllocL();
        fileInit = ETrue;
        }
    
    if (!filenamePushed)
        {
        // from now on it is assumed pushed.
        CleanupStack::PushL(filename);
        }
    
    TParse parser ;
    User::LeaveIfError(parser.Set(*filename, NULL, NULL));
    CleanupStack::PopAndDestroy(filename);
    if ( !( parser.NamePresent() ) && !( parser.ExtPresent() ) )
        User::Leave( KErrBadName ) ;
    
    iFullFileName.Copy( parser.FullName() ) ;	
    iFileName = parser.Name().AllocL() ;
    iFileExt = parser.Ext().AllocL() ;
    iFilePath = parser.Path().AllocL() ;
    iFileDrive = parser.Drive().AllocL() ;
    
    // in order to simulate old behaviour we are not passing error out
    // but will try to create Content again during PrimeL()
    if (fileInit && drmContent && aFileMode==ESourceMode)
        {
        TInt contentError;
        if (iFileHandle)
            {
            TRAP(contentError, 
                iFile = CContentFile::NewL(iHandle, UniqueId(), iCAFParameters->iEnableUI);
            );
            }
EXPORT_C void CMMFFormatSelectionParameters::SetMatchToFileNameL(const TDesC& aFileName)
	{
	delete iMatchReqData;
	iMatchReqData = NULL;
	iMatchDataType = EMatchAny;	
	// Extract the extension from the data passed in

	// Parse the path and extract the extension
	_LIT( KDot, "." ) ;
	_LIT8( KDot8, "." );

	// If there is no dot "." in aFileName then assume that we have been passed the extension only (if KMaxExtLen or less)
	if ( (aFileName.Length() <= KMaxExtLen) && (aFileName.Find( KDot ) == KErrNotFound) )
		{
		RBuf8 temp;
		CleanupClosePushL(temp);
		temp.CreateL(aFileName.Length()+1);
		User::LeaveIfError(CnvUtfConverter::ConvertFromUnicodeToUtf8(temp, aFileName));
		temp.Insert(0,KDot8);
		
		iMatchReqData = CMatchData::CreateL();
		iMatchReqData->SetMatchDataL(temp);
		
		CleanupStack::PopAndDestroy(&temp);
		
		}
	else if ( aFileName.Find( KDot ) == 0 )  // the first character is dot so assume extension only
		{
		RBuf8 temp;
		CleanupClosePushL(temp);
		temp.CreateL(aFileName.Length());
		User::LeaveIfError(CnvUtfConverter::ConvertFromUnicodeToUtf8(temp, aFileName));
			
		iMatchReqData = CMatchData::CreateL();
		iMatchReqData->SetMatchDataL(temp);
		
		CleanupStack::PopAndDestroy(&temp);
		
		}
	else // We have been given the whole filename.  Use TParse to extract the extension.
		{
		TParse parser ;
		parser.Set( aFileName, NULL, NULL ) ;
		if ( !( parser.NamePresent() ) )
			User::Leave( KErrBadName ) ;
		if ( !( parser.PathPresent() ) )
			{
			RFs fsSession ;
			User::LeaveIfError(fsSession.Connect());
			TInt error = fsSession.Parse(aFileName, parser);
			fsSession.Close();
			User::LeaveIfError(error);
			}
		// Parser should now have the full filename and path
		TPtrC extension = parser.Ext();
		
		RBuf8 temp;
		CleanupClosePushL(temp);
		temp.CreateL(extension.Length());
		User::LeaveIfError(CnvUtfConverter::ConvertFromUnicodeToUtf8(temp, extension));
			
		iMatchReqData = CMatchData::CreateL();
		iMatchReqData->SetMatchDataL(temp);
		
		CleanupStack::PopAndDestroy(&temp);
		
		}

	// If we're here, we must now have the file extension
	iMatchDataType = EMatchFileExtension;
	}