commands/iniedit/iniedit.cpp
changeset 0 7f656887cf89
equal deleted inserted replaced
-1:000000000000 0:7f656887cf89
       
     1 // iniedit.cpp
       
     2 // 
       
     3 // Copyright (c) 2009 - 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 <fshell/ioutils.h>
       
    14 #include <settings.h>
       
    15 
       
    16 using namespace IoUtils;
       
    17 using namespace LtkUtils;
       
    18 
       
    19 
       
    20 class CCmdIniEdit : public CCommandBase
       
    21 	{
       
    22 public:
       
    23 	static CCommandBase* NewLC();
       
    24 	~CCmdIniEdit();
       
    25 private:
       
    26 	CCmdIniEdit();
       
    27 private: // From CCommandBase.
       
    28 	virtual const TDesC& Name() const;
       
    29 	virtual void DoRunL();
       
    30 	virtual void ArgumentsL(RCommandArgumentList& aArguments);
       
    31 	virtual void OptionsL(RCommandOptionList& aOptions);
       
    32 private:
       
    33 	TFileName2 iIniFile;
       
    34 	TFileName2 iDescriptionFile;
       
    35 	TBool iDump;
       
    36 	TBool iForce;
       
    37 	HBufC* iSet;
       
    38 	HBufC* iGet;
       
    39 	HBufC* iRemove;
       
    40 	
       
    41 	RBuf iBuf;
       
    42 	CIniReader* iReader;
       
    43 	};
       
    44 
       
    45 
       
    46 CCommandBase* CCmdIniEdit::NewLC()
       
    47 	{
       
    48 	CCmdIniEdit* self = new(ELeave) CCmdIniEdit();
       
    49 	CleanupStack::PushL(self);
       
    50 	self->BaseConstructL();
       
    51 	return self;
       
    52 	}
       
    53 
       
    54 CCmdIniEdit::~CCmdIniEdit()
       
    55 	{
       
    56 	delete iSet;
       
    57 	delete iGet;
       
    58 	delete iRemove;
       
    59 	delete iReader;
       
    60 	iBuf.Close();
       
    61 	}
       
    62 
       
    63 CCmdIniEdit::CCmdIniEdit()
       
    64 	{
       
    65 	}
       
    66 
       
    67 const TDesC& CCmdIniEdit::Name() const
       
    68 	{
       
    69 	_LIT(KName, "iniedit");	
       
    70 	return KName;
       
    71 	}
       
    72 	
       
    73 _LIT(KEquals, "=");
       
    74 
       
    75 TPtrC Trim(const TDesC& aDes)
       
    76 	{
       
    77 	TPtrC des(aDes);
       
    78 	while (des.Length() && TChar(des[0]).IsSpace())
       
    79 		{
       
    80 		des.Set(des.Mid(1));
       
    81 		}
       
    82 	while (des.Length() && TChar(des[des.Length()-1]).IsSpace())
       
    83 		{
       
    84 		des.Set(des.Left(des.Length()-1));
       
    85 		}
       
    86 	return des;
       
    87 	}
       
    88 
       
    89 void CCmdIniEdit::DoRunL()
       
    90 	{
       
    91 	TBool interactive = ETrue;
       
    92 	if (iForce)
       
    93 		{
       
    94 		iReader = CIniReader::NewL(iIniFile);
       
    95 		}
       
    96 	else
       
    97 		{
       
    98 		iReader = CIniFile::NewL(iIniFile, iDescriptionFile);
       
    99 		}
       
   100 	TBool updated = EFalse;
       
   101 		
       
   102 	if (iSet)
       
   103 		{
       
   104 		TInt eqPos = iSet->Find(KEquals);
       
   105 		TPtrC value(KNullDesC);
       
   106 		TPtrC id(KNullDesC);
       
   107 		if (eqPos == KErrNotFound)
       
   108 			{
       
   109 			id.Set(*iSet);
       
   110 			
       
   111 			Stdin().SetReadModeL(RIoReadHandle::ELine);
       
   112 			iBuf.CreateL(0x100);
       
   113 			Stdin().ReadL(iBuf);
       
   114 			value.Set(iBuf);
       
   115 			}
       
   116 		else
       
   117 			{
       
   118 			id.Set(iSet->Left(eqPos));
       
   119 			value.Set(iSet->Mid(eqPos+1));
       
   120 			}
       
   121 		
       
   122 		iReader->SetValueL(Trim(id), value);
       
   123 		updated = ETrue;
       
   124 		interactive = EFalse;
       
   125 		}
       
   126 	if (iRemove)
       
   127 		{
       
   128 		iReader->RemoveValueL(*iRemove);
       
   129 		updated = ETrue;
       
   130 		interactive = EFalse;
       
   131 		}
       
   132 	
       
   133 	if (iGet)
       
   134 		{
       
   135 		const TDesC* value = iReader->GetValue(*iGet);
       
   136 		if (!value) LeaveIfErr(KErrNotFound, _L("id '%S' not found in %S"), iGet, &iIniFile);
       
   137 		Write(*value);
       
   138 		interactive = EFalse;
       
   139 		}
       
   140 	else if (iDump)
       
   141 		{
       
   142 		RPointerArray<CValue> values;
       
   143 		CleanupClosePushL(values);
       
   144 		iReader->GetValuesL(values);
       
   145 		for (TInt i=0; i<values.Count(); ++i)
       
   146 			{
       
   147 			if (iForce)
       
   148 				{
       
   149 				Printf(_L("%S: %S\n"), &values[i]->Id(), &values[i]->Value());
       
   150 				}
       
   151 			else
       
   152 				{
       
   153 				CSetting* setting = (CSetting*)values[i];
       
   154 				Printf(_L("%S: %S\n"), &setting->Name(), &setting->Value());
       
   155 				}
       
   156 			}
       
   157 		CleanupStack::PopAndDestroy(&values);
       
   158 		interactive = EFalse;
       
   159 		}
       
   160 	
       
   161 	if (interactive)
       
   162 		{
       
   163 		// TODO enter interactive mode
       
   164 		LeaveIfErr(KErrNotSupported, _L("Interactive mode not implemented yet."));
       
   165 		}
       
   166 
       
   167 	if (updated)
       
   168 		{
       
   169 		WriteIniFileL(iIniFile, *iReader);
       
   170 		}
       
   171 	}
       
   172 
       
   173 void CCmdIniEdit::ArgumentsL(RCommandArgumentList& aArguments)
       
   174 	{
       
   175 	_LIT(KIniFile, "inifile");
       
   176 	aArguments.AppendFileNameL(iIniFile, KIniFile);
       
   177 	}
       
   178 
       
   179 void CCmdIniEdit::OptionsL(RCommandOptionList& aOptions)
       
   180 	{
       
   181 	_LIT(KOptIdfFile, "idf");
       
   182 	aOptions.AppendFileNameL(iDescriptionFile, KOptIdfFile);
       
   183 	
       
   184 	_LIT(KOptDump, "dump");
       
   185 	aOptions.AppendBoolL(iDump, KOptDump);
       
   186 	
       
   187 	_LIT(KOptForce, "force");
       
   188 	aOptions.AppendBoolL(iForce, KOptForce);
       
   189 	
       
   190 	_LIT(KOptGet, "get");
       
   191 	aOptions.AppendStringL(iGet, KOptGet);
       
   192 
       
   193 	_LIT(KOptSet, "set");
       
   194 	aOptions.AppendStringL(iSet, KOptSet);
       
   195 	
       
   196 	_LIT(KOptRemove, "remove");
       
   197 	aOptions.AppendStringL(iRemove, KOptRemove);
       
   198 	}
       
   199 
       
   200 
       
   201 EXE_BOILER_PLATE(CCmdIniEdit)
       
   202