commands/find/find.cpp
changeset 0 7f656887cf89
equal deleted inserted replaced
-1:000000000000 0:7f656887cf89
       
     1 // find.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 
       
    15 using namespace IoUtils;
       
    16 
       
    17 class CCmdFind : public CCommandBase
       
    18 	{
       
    19 public:
       
    20 	static CCommandBase* NewLC();
       
    21 	~CCmdFind();
       
    22 private:
       
    23 	CCmdFind();
       
    24 	void FoundFile(const TDesC& aDir, const TDesC& aName, TBool aIsDir);
       
    25 private: // From CCommandBase.
       
    26 	virtual const TDesC& Name() const;
       
    27 	virtual void DoRunL();
       
    28 	virtual void ArgumentsL(RCommandArgumentList& aArguments);
       
    29 	virtual void OptionsL(RCommandOptionList& aOptions);
       
    30 private:
       
    31 	TFileName2 iPath;
       
    32 	HBufC* iName;
       
    33 	TBool iPrint;
       
    34 	TBool iAllDrives;
       
    35 	TFileName2 iTempName;
       
    36 	RPointerArray<HBufC> iSearchDirs;
       
    37 	};
       
    38 
       
    39 
       
    40 CCommandBase* CCmdFind::NewLC()
       
    41 	{
       
    42 	CCmdFind* self = new(ELeave) CCmdFind();
       
    43 	CleanupStack::PushL(self);
       
    44 	self->BaseConstructL();
       
    45 	return self;
       
    46 	}
       
    47 
       
    48 CCmdFind::~CCmdFind()
       
    49 	{
       
    50 	delete iName;
       
    51 	iSearchDirs.ResetAndDestroy();
       
    52 	}
       
    53 
       
    54 CCmdFind::CCmdFind()
       
    55 	{
       
    56 	}
       
    57 
       
    58 const TDesC& CCmdFind::Name() const
       
    59 	{
       
    60 	_LIT(KName, "find");
       
    61 	return KName;
       
    62 	}
       
    63 
       
    64 void CCmdFind::ArgumentsL(RCommandArgumentList& aArguments)
       
    65 	{
       
    66 	_LIT(KArgPath, "path");
       
    67 	aArguments.AppendFileNameL(iPath, KArgPath);
       
    68 	}
       
    69 
       
    70 void CCmdFind::OptionsL(RCommandOptionList& aOptions)
       
    71 	{
       
    72 	_LIT(KOptName, "name");
       
    73 	aOptions.AppendStringL(iName, KOptName);
       
    74 	//aOptions.AppendBoolL(iPrint, TChar('p'), _L("print"), _L("Print the paths of files that match the given conditions, one per line. This is the default if no other options are specified."));
       
    75 	}
       
    76 
       
    77 
       
    78 EXE_BOILER_PLATE(CCmdFind)
       
    79 
       
    80 
       
    81 void CCmdFind::DoRunL()
       
    82 	{
       
    83 	RFs& fs = FsL();
       
    84 	iPath.SetIsDirectoryL();
       
    85 	if (!iName)
       
    86 		{
       
    87 		LeaveIfErr(KErrArgument, _L("You must specify a name to match against"));
       
    88 		}
       
    89 
       
    90 	iSearchDirs.AppendL(iPath.AllocLC());
       
    91 	CleanupStack::Pop();
       
    92 
       
    93 	while (iSearchDirs.Count())
       
    94 		{
       
    95 		const TDesC& path = *iSearchDirs[0];
       
    96 
       
    97 		TInt err;
       
    98 		CDir* matchingFiles = NULL;
       
    99 		iTempName.Copy(path);
       
   100 		iTempName.AppendComponentL(*iName, TFileName2::EFile);
       
   101 		// Look for files in this directory first
       
   102 		err = fs.GetDir(iTempName, KEntryAttNormal|KEntryAttDir, ESortByName, matchingFiles);
       
   103 		if (!err)
       
   104 			{
       
   105 			for (TInt i = 0; i < matchingFiles->Count(); i++)
       
   106 				{
       
   107 				const TEntry& entry = (*matchingFiles)[i];
       
   108 				FoundFile(path, entry.iName, entry.IsDir());
       
   109 				}
       
   110 			}
       
   111 		delete matchingFiles;
       
   112 
       
   113 		// Then add all this dir's subdirectories to the list of ones to be scanned
       
   114 		CDir* dirsToRecurse = NULL;
       
   115 		err = fs.GetDir(path, KEntryAttDir|KEntryAttMatchExclusive, ESortNone, dirsToRecurse);
       
   116 		if (!err)
       
   117 			{
       
   118 			CleanupStack::PushL(dirsToRecurse);
       
   119 			for (TInt i = 0; i < dirsToRecurse->Count(); i++)
       
   120 				{
       
   121 				const TEntry& entry = (*dirsToRecurse)[i];
       
   122 				iTempName.Copy(path);
       
   123 				iTempName.AppendComponentL(entry);
       
   124 				iSearchDirs.AppendL(iTempName.AllocLC());
       
   125 				CleanupStack::Pop();
       
   126 				}
       
   127 			CleanupStack::PopAndDestroy(dirsToRecurse);
       
   128 			}
       
   129 		delete iSearchDirs[0];
       
   130 		iSearchDirs.Remove(0);
       
   131 		}
       
   132 	}
       
   133 
       
   134 void CCmdFind::FoundFile(const TDesC& aDir, const TDesC& aName, TBool aIsDir)
       
   135 	{
       
   136 	// For now, always print
       
   137 	_LIT(KBack, "\\");
       
   138 	Printf(_L("%S%S%S\n"), &aDir, &aName, aIsDir ? &KBack : &KNullDesC);
       
   139 	}