core/builtins/gobble.cpp
changeset 0 7f656887cf89
child 68 377ac716dabb
equal deleted inserted replaced
-1:000000000000 0:7f656887cf89
       
     1 // gobble.cpp
       
     2 // 
       
     3 // Copyright (c) 2007 - 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 
       
    13 #include "gobble.h"
       
    14 
       
    15 
       
    16 CCommandBase* CCmdGobble::NewLC()
       
    17 	{
       
    18 	CCmdGobble* self = new(ELeave) CCmdGobble();
       
    19 	CleanupStack::PushL(self);
       
    20 	self->BaseConstructL();
       
    21 	return self;
       
    22 	}
       
    23 
       
    24 CCmdGobble::~CCmdGobble()
       
    25 	{
       
    26 	}
       
    27 
       
    28 CCmdGobble::CCmdGobble()
       
    29 	: iBlockSize(512)
       
    30 	{
       
    31 	}
       
    32 
       
    33 const TDesC& CCmdGobble::Name() const
       
    34 	{
       
    35 	_LIT(KName, "gobble");
       
    36 	return KName;
       
    37 	}
       
    38 
       
    39 void CCmdGobble::DoRunL()
       
    40 	{
       
    41 	if (iAmount < iBlockSize)
       
    42 		{
       
    43 		LeaveIfErr(KErrArgument, _L("The amount to consume must be less than the block size (%d)"), iBlockSize);
       
    44 		}
       
    45 	if (iAmount & 0x80000000)
       
    46 		{
       
    47 		LeaveIfErr(KErrArgument, _L("The amount to consume is too large (maximum is %d)"), KMaxTInt);
       
    48 		}
       
    49 	if (iBlockSize & 0x80000000)
       
    50 		{
       
    51 		LeaveIfErr(KErrArgument, _L("The block size is too large (maximum is %d)"), KMaxTInt);
       
    52 		}
       
    53 	RFs& fs = FsL();
       
    54 	fs.MkDirAll(iFileName);
       
    55 	RFile file;
       
    56 	TInt err = file.Open(fs, iFileName, EFileWrite);
       
    57 	if (err == KErrNotFound)
       
    58 		{
       
    59 		err = file.Create(fs, iFileName, EFileWrite);
       
    60 		}
       
    61 	User::LeaveIfError(err);
       
    62 	CleanupClosePushL(file);
       
    63 	TInt pos = 0;
       
    64 	User::LeaveIfError(file.Seek(ESeekEnd, pos));
       
    65 
       
    66 	HBufC8* buf = HBufC8::NewLC(iBlockSize);
       
    67 	TPtr8 ptr(buf->Des());
       
    68 	ptr.Fill(TChar('x'), iBlockSize);
       
    69 	
       
    70 	TInt toWrite = static_cast<TInt>(iAmount);
       
    71 	do
       
    72 		{
       
    73 		TInt writeSize;
       
    74 		if (toWrite > static_cast<TInt>(iBlockSize))
       
    75 			{
       
    76 			writeSize = static_cast<TInt>(iBlockSize);
       
    77 			}
       
    78 		else
       
    79 			{
       
    80 			writeSize = toWrite;
       
    81 			}
       
    82 		ptr.SetLength(writeSize);
       
    83 		err = file.Write(ptr);
       
    84 		if (err == KErrNone)
       
    85 			{
       
    86 			if (iVerbose)
       
    87 				{
       
    88 				Printf(_L("\rWrote %d"), iAmount - toWrite);
       
    89 				}
       
    90 			toWrite -= writeSize;
       
    91 			}
       
    92 		}
       
    93 		while ((err == KErrNone) && (toWrite > 0));
       
    94 		if (iVerbose)
       
    95 			{
       
    96 			Printf(_L("\rWrote %d"), iAmount - toWrite);
       
    97 			}
       
    98 		
       
    99 	CleanupStack::PopAndDestroy(2, &file);
       
   100 	}
       
   101 
       
   102 void CCmdGobble::OptionsL(RCommandOptionList& aOptions)
       
   103 	{
       
   104 	_LIT(KCmdOptVerbose, "verbose");
       
   105 	aOptions.AppendBoolL(iVerbose, KCmdOptVerbose);
       
   106 	}
       
   107 
       
   108 void CCmdGobble::ArgumentsL(RCommandArgumentList& aArguments)
       
   109 	{
       
   110 	_LIT(KArgFileName, "file_name");
       
   111 	aArguments.AppendFileNameL(iFileName, KArgFileName);
       
   112 
       
   113 	_LIT(KArgAmount, "amount");
       
   114 	aArguments.AppendUintL(iAmount, KArgAmount);
       
   115 
       
   116 	_LIT(KArgBlockSize, "block_size");
       
   117 	aArguments.AppendUintL(iBlockSize, KArgBlockSize);
       
   118 	}
       
   119 
       
   120 
       
   121 #ifdef EXE_BUILD
       
   122 EXE_BOILER_PLATE(CCmdGobble)
       
   123 #endif
       
   124