commsfwsupport/commselements/NetMeta/src/MetaBuffer.cpp
changeset 0 dfb7c4ff071f
equal deleted inserted replaced
-1:000000000000 0:dfb7c4ff071f
       
     1 // Copyright (c) 2004-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 /**
       
    17  @file
       
    18  @internalTechnology
       
    19 */
       
    20 
       
    21 #include <elements/metabuffer.h>
       
    22 #include <elements/metadata.h>
       
    23 
       
    24 
       
    25 #ifdef _DEBUG
       
    26 // Panic category for "absolutely impossible!" vanilla ASSERT()-type panics from this module
       
    27 // (if it could happen through user error then you should give it an explicit, documented, category + code)
       
    28 _LIT(KSpecAssert_ElemNetMetaMtBfr, "ElemNetMetaMtBfr");
       
    29 #endif
       
    30 
       
    31 using namespace Meta;
       
    32 
       
    33 TMetaBuf8::TMetaBuf8(const TAny* aData)
       
    34 	: TMetaVarLen8<RBuf8>(aData)
       
    35 	{
       
    36 	}
       
    37 
       
    38 EXPORT_C MMetaType* TMetaBuf8::NewL(const TAny* aMem, const TAny* aData)
       
    39 /**
       
    40  * Instantiates a meta type for RBuf8.
       
    41  * Used for attribure registration (in the data v-table).
       
    42  */
       
    43     {
       
    44     return ::new ((TUint8*)aMem) TMetaBuf8(aData);
       
    45     }
       
    46 
       
    47 EXPORT_C TInt TMetaBuf8::Load(TPtrC8& aBuffer)
       
    48 	{
       
    49 	TInt errlen = CheckBuf( aBuffer );
       
    50 	if (errlen < 0)
       
    51 		{
       
    52 		return errlen;
       
    53 		}
       
    54 	if ( iData->MaxLength() < errlen )
       
    55 		{
       
    56 		iData->Close();
       
    57 		TInt err = iData->Create(errlen);
       
    58 		if ( err != KErrNone )
       
    59 			return err;
       
    60 		}
       
    61 	//and copy whatever we can into the client descriptor
       
    62 	CopyBuf(aBuffer,errlen);
       
    63 
       
    64 	TInt padByte = errlen % 2;
       
    65 	aBuffer.Set(aBuffer.Ptr() + errlen + padByte, aBuffer.Length() - errlen - padByte); //update pointer
       
    66 	return KErrNone;
       
    67 	}
       
    68 
       
    69 EXPORT_C TInt TMetaBuf8::Store(TDes8& aBuffer) const
       
    70 	{
       
    71 	// Ask the descriptor how much data does it currently store
       
    72 	TUint32 size = this->iData->Length();
       
    73 
       
    74    	// The length variable and the data must fit
       
    75 	if ((TUint32)(aBuffer.MaxLength() - aBuffer.Length()) < (size + sizeof(TUint32)))
       
    76 		{
       
    77 		return KErrOverflow;
       
    78 		}
       
    79 
       
    80     // Store
       
    81 	aBuffer.Append((TUint8*)&size, sizeof(TUint32));
       
    82 	AppendToBuf(aBuffer);
       
    83 	if (size & 1)
       
    84 		{
       
    85 		// Pad odd lengths to an even number to avoid potential
       
    86 		// alignment issues on target
       
    87 		TUint8 pad(0);
       
    88 		aBuffer.Append(&pad, 1);
       
    89 		}
       
    90 	
       
    91 	return KErrNone;
       
    92 	}
       
    93 
       
    94 EXPORT_C void TMetaBuf8::Copy(const TAny* aData)
       
    95 	{
       
    96 	const RBuf8& var = *((RBuf8*)aData);
       
    97 	if ( iData->MaxLength() < var.Length() )
       
    98 		{
       
    99 		iData->Close();
       
   100 		TInt err = iData->Create(var.Length());
       
   101 		if ( err != KErrNone )
       
   102 			return;
       
   103 		}
       
   104 	TMetaVarLen8<RBuf8>::Copy(aData);
       
   105 	}
       
   106 
       
   107 EXPORT_C TInt TMetaBuf8::Length() const
       
   108 	{
       
   109 	return TMetaVarLen8<RBuf8>::Length();
       
   110 	}
       
   111 
       
   112 TMetaBuf16::TMetaBuf16(const TAny* aData)
       
   113 	: TMetaVarLen16<RBuf16>(aData)
       
   114 	{
       
   115 	}	
       
   116 
       
   117 EXPORT_C MMetaType* TMetaBuf16::NewL(const TAny* aMem, const TAny* aData)
       
   118 /**
       
   119  * Instantiates a meta type for RBuf8.
       
   120  * Used for attribure registration (in the data v-table).
       
   121  */
       
   122     {
       
   123     return ::new ((TUint8*)aMem) TMetaBuf16(aData);
       
   124     }	
       
   125 
       
   126 EXPORT_C TInt TMetaBuf16::Load(TPtrC8& aBuffer)
       
   127 	{
       
   128 	TInt errlen = CheckBuf( aBuffer );
       
   129 	if (errlen < 0)
       
   130 		{
       
   131 		return errlen;
       
   132 		}
       
   133 	
       
   134 	// errlen is in bytes we want unicode chars
       
   135 	__ASSERT_DEBUG((errlen & 1) == 0, User::Panic(KSpecAssert_ElemNetMetaMtBfr, 1));
       
   136 	if ( iData->MaxLength() < errlen / 2)
       
   137 		{
       
   138 		iData->Close();
       
   139 		TInt err = iData->Create(errlen / 2);
       
   140 		if ( err != KErrNone )
       
   141 			return err;
       
   142 		}
       
   143 	//and copy whatever we can into the client descriptor
       
   144 	CopyBuf(aBuffer,errlen);
       
   145 
       
   146 	aBuffer.Set(aBuffer.Ptr() + errlen, aBuffer.Length() - errlen); //update pointer
       
   147 	return KErrNone;
       
   148 	}
       
   149 
       
   150 EXPORT_C TInt TMetaBuf16::Store(TDes8& aBuffer) const
       
   151 	{
       
   152 	return TMetaVarLen16<RBuf16>::Store(aBuffer);
       
   153 	}
       
   154 
       
   155 EXPORT_C void TMetaBuf16::Copy(const TAny* aData)
       
   156 	{
       
   157 	const RBuf16& var = *((RBuf16*)aData);
       
   158 	if ( iData->MaxLength() < var.Length() )
       
   159 		{
       
   160 		iData->Close();
       
   161 		TInt err = iData->Create(var.Length());
       
   162 		if ( err != KErrNone )
       
   163 			return;
       
   164 		}
       
   165 	TMetaVarLen16<RBuf16>::Copy(aData);
       
   166 	}
       
   167 
       
   168 EXPORT_C TInt TMetaBuf16::Length() const
       
   169 	{
       
   170 	return TMetaVarLen16<RBuf16>::Length();
       
   171 	}
       
   172 
       
   173 
       
   174