libraries/ltkutils/src/hexdump.cpp
changeset 0 7f656887cf89
equal deleted inserted replaced
-1:000000000000 0:7f656887cf89
       
     1 // hexdump.cpp
       
     2 // 
       
     3 // Copyright (c) 2010 Accenture. All rights reserved.
       
     4 // This component and the accompanying materials are made available
       
     5 // under the terms of the "Eclipse Public License v1.0"
       
     6 // which accompanies this distribution, and is available
       
     7 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 // 
       
     9 // Initial Contributors:
       
    10 // Accenture - Initial contribution
       
    11 //
       
    12 #include <fshell/ltkutils.h>
       
    13 #include <e32base.h>
       
    14 #include <fshell/iocli.h>
       
    15 #include <e32debug.h>
       
    16 
       
    17 static const TInt KLineSize = 16;
       
    18 // 00000000:  00 01 02 03 04 05 06 07 08  09 0A 0B 0C 0D 0E 0F  ................<CR><LF>
       
    19 // So line len is 8 + 3 + (3 * lineSize) + 1 + 1 + lineSize + 2
       
    20 static const TInt KTotalLen = 8 + 3 + (3 * KLineSize) + 1 + 1 + KLineSize + 2;
       
    21 
       
    22 void HexDumpLine(const TDesC8& aBuf, TDes& lineBuf, TInt aOffset)
       
    23 	{
       
    24 	TBuf<KLineSize> ascii;
       
    25 	lineBuf.NumFixedWidthUC(aOffset, EHex, 8);
       
    26 	_LIT(KColon, ":  ");
       
    27 	lineBuf.Append(KColon);
       
    28 	for (TInt i = 0; i < KLineSize; i++)
       
    29 		{
       
    30 		if (i < aBuf.Length())
       
    31 			{
       
    32 			TUint8 ch = aBuf[i];
       
    33 			lineBuf.AppendNumFixedWidthUC(ch, EHex, 2);
       
    34 			lineBuf.Append(' ');
       
    35 			if (ch < 32 || ch >= 128) ch = '.';
       
    36 			ascii.Append(ch);
       
    37 			}
       
    38 		else
       
    39 			{
       
    40 			_LIT(KFiller, "   ");
       
    41 			lineBuf.Append(KFiller);
       
    42 			}
       
    43 		if (i == 7) lineBuf.Append(' '); // The space between the two groups of 8
       
    44 		}
       
    45 	lineBuf.Append(' ');
       
    46 	lineBuf.Append(ascii);
       
    47 	lineBuf.Append('\r');
       
    48 	lineBuf.Append('\n');
       
    49 	}
       
    50 
       
    51 EXPORT_C HBufC* LtkUtils::HexDumpL(const TDesC8& aBuf)
       
    52 	{
       
    53 	TInt numLines = (aBuf.Length() + KLineSize-1) / KLineSize;
       
    54 	HBufC* result = HBufC::NewLC(numLines * KTotalLen);
       
    55 	TPtr resultPtr = result->Des();
       
    56 	for (TInt i = 0; i < aBuf.Length(); i += KLineSize)
       
    57 		{
       
    58 		TPtrC8 line = aBuf.Mid(i, Min(KLineSize, aBuf.Length()-i));
       
    59 		TPtr wptr((TUint16*)resultPtr.Ptr() + resultPtr.Length(), KTotalLen);
       
    60 		HexDumpLine(line, wptr, i);
       
    61 		resultPtr.SetLength(resultPtr.Length() + wptr.Length()); // MidTPtr doesn't update length of original
       
    62 		}
       
    63 	CleanupStack::Pop(result);
       
    64 	return result;
       
    65 	}
       
    66 
       
    67 EXPORT_C void LtkUtils::HexDumpToOutput(const TDesC8& aBuf, RIoWriteHandle& aHandle)
       
    68 	{
       
    69 	TInt offset = 0;
       
    70 	LtkUtils::HexDumpToOutput(aBuf, aHandle, offset);
       
    71 	}
       
    72 
       
    73 EXPORT_C void LtkUtils::HexDumpToOutput(const TDesC8& aBuf, RIoWriteHandle& aHandle, TInt& aOffset)
       
    74 	{
       
    75 	TBuf<KTotalLen> lineBuf;
       
    76 	const TInt len = aBuf.Length();
       
    77 	for (TInt i = 0; i < len; i += KLineSize)
       
    78 		{
       
    79 		TPtrC8 line = aBuf.Mid(i, Min(KLineSize, len-i));
       
    80 		HexDumpLine(line, lineBuf, aOffset+i);
       
    81 		aHandle.Write(lineBuf);
       
    82 		}
       
    83 	aOffset += len;
       
    84 	}
       
    85 
       
    86 EXPORT_C void LtkUtils::HexDumpToRDebug(const TDesC8& aBuf)
       
    87 	{
       
    88 	TInt offset = 0;
       
    89 	HexDumpToRDebug(aBuf, offset);
       
    90 	}
       
    91 
       
    92 EXPORT_C void LtkUtils::HexDumpToRDebug(const TDesC8& aBuf, TInt& aOffset)
       
    93 	{
       
    94 	TBuf<KTotalLen> lineBuf;
       
    95 	const TInt len = aBuf.Length();
       
    96 	for (TInt i = 0; i < len; i += KLineSize)
       
    97 		{
       
    98 		TPtrC8 line = aBuf.Mid(i, Min(KLineSize, len-i));
       
    99 		HexDumpLine(line, lineBuf, aOffset+i);
       
   100 		RDebug::RawPrint(lineBuf);
       
   101 		}
       
   102 	aOffset += len;
       
   103 	}
       
   104 
       
   105 EXPORT_C void LtkUtils::RawPrint(const TDesC8 &aDes)
       
   106 	{
       
   107 	TPtrC8 rem(aDes);
       
   108 	TBuf<256> buf;
       
   109 	while (rem.Length())
       
   110 		{
       
   111 		buf.Copy(rem.Left(256));
       
   112 		RDebug::RawPrint(buf);
       
   113 		rem.Set(rem.Mid(buf.Length()));
       
   114 		}
       
   115 	}