3 * regexpl - Console Registry Explorer
5 * Copyright (C) 2000-2005 Nedko Arnaudov <nedko@users.sourceforge.net>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; see the file COPYING. If not, write to
19 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
23 // ShellCommandDir.cpp: implementation of the CShellCommandDir class.
25 //////////////////////////////////////////////////////////////////////
28 #include "RegistryExplorer.h"
29 #include "ShellCommandDir.h"
30 #include "RegistryTree.h"
31 #include "RegistryKey.h"
34 // *** THIS SHOULD GO IN A MINGW/ROS HEADER (tchar.h ???) - Begin
35 #if 1 // #ifndef _ui64tot ???
37 #define _ui64tot _ui64tow
39 #define _ui64tot _ui64toa
42 // *** THIS SHOULD GO IN A MINGW/ROS HEADER - End
44 #define DIR_CMD _T("DIR")
45 #define DIR_CMD_LENGTH COMMAND_LENGTH(DIR_CMD)
46 #define DIR_CMD_SHORT_DESC DIR_CMD _T(" command lists keys and values of any key.\n")
48 //////////////////////////////////////////////////////////////////////
49 // Construction/Destruction
50 //////////////////////////////////////////////////////////////////////
52 CShellCommandDir::CShellCommandDir(CRegistryTree
& rTree
):m_rTree(rTree
)
56 CShellCommandDir::~CShellCommandDir()
60 BOOL
CShellCommandDir::Match(const TCHAR
*pchCommand
)
62 if (_tcsicmp(pchCommand
,DIR_CMD
) == 0)
64 if (_tcsnicmp(pchCommand
,DIR_CMD
_T(".."),DIR_CMD_LENGTH
+2*sizeof(TCHAR
)) == 0)
66 if (_tcsnicmp(pchCommand
,DIR_CMD
_T("/"),DIR_CMD_LENGTH
+1*sizeof(TCHAR
)) == 0)
68 if (_tcsnicmp(pchCommand
,DIR_CMD
_T("\\"),DIR_CMD_LENGTH
+1*sizeof(TCHAR
)) == 0)
73 int CShellCommandDir::Execute(CConsole
&rConsole
, CArgumentParser
& rArguments
)
75 rArguments
.ResetArgumentIteration();
77 BOOL blnDo
= TRUE
,blnBadParameter
, blnHelp
= FALSE
;
79 TCHAR
*pszCommandItself
= rArguments
.GetNextArgument();
82 if ((_tcsnicmp(pszCommandItself
,DIR_CMD
_T(".."),DIR_CMD_LENGTH
+2*sizeof(TCHAR
)) == 0)||
83 (_tcsnicmp(pszCommandItself
,DIR_CMD
_T("\\"),DIR_CMD_LENGTH
+1*sizeof(TCHAR
)) == 0))
85 pszKey
= pszCommandItself
+ DIR_CMD_LENGTH
;
87 else if (_tcsnicmp(pszCommandItself
,DIR_CMD
_T("/"),DIR_CMD_LENGTH
+1*sizeof(TCHAR
)) == 0)
89 pszParameter
= pszCommandItself
+ DIR_CMD_LENGTH
;
90 goto CheckDirArgument
;
93 while((pszParameter
= rArguments
.GetNextArgument()) != NULL
)
96 blnBadParameter
= FALSE
;
97 if ((_tcsicmp(pszParameter
,_T("/?")) == 0)
98 ||(_tcsicmp(pszParameter
,_T("-?")) == 0))
101 blnDo
= pszKey
!= NULL
;
105 pszKey
= pszParameter
;
110 blnBadParameter
= TRUE
;
114 rConsole
.Write(_T("Bad parameter: "));
115 rConsole
.Write(pszParameter
);
116 rConsole
.Write(_T("\n"));
120 const TCHAR
*pszPattern
= PATTERN_MATCH_ALL
;
121 const TCHAR
*pszPath
= _T(".");
128 while(*pch
) // search end of string
131 if (pch
> pszKey
) // last non-null char
134 if (*pch
!= _T('\\'))
136 while ((pch
> pszKey
) && (*pch
!= _T('\\')))
139 if (*pch
== _T('\\'))
145 ASSERT(*pch
== _T('\\'));
148 else if (*pch
== _T('\\'))
163 if (!m_rTree
.GetKey(pszPath
,KEY_ENUMERATE_SUB_KEYS
|KEY_QUERY_VALUE
,Key
))
165 const TCHAR
*pszErrorMsg
= m_rTree
.GetLastErrorDescription();
166 rConsole
.Write(pszErrorMsg
);
172 rConsole
.Write(GetHelpString());
180 rConsole
.Write(_T("\n Key is "));
181 rConsole
.Write(Key
.GetKeyName());
185 rConsole
.Write(_T("\n Last modify time is "));
186 rConsole
.Write(Key
.GetLastWriteTime());
189 rConsole
.Write(_T("\n\n"));
190 unsigned __int64 nTotalItems
= 0;
194 ASSERT(nTotalItems
== 0);
195 rConsole
.Write(_T("\t(KEY)\t\t\t\t..\\\n")); // parent key abstraction
198 DWORD dwMaxSubkeyNameLength
;
199 nError
= Key
.GetSubkeyNameMaxLength(dwMaxSubkeyNameLength
);
200 if (nError
!= ERROR_SUCCESS
)
203 TCHAR
*pszSubkeyNameBuffer
= new TCHAR
[dwMaxSubkeyNameLength
];
204 if (!pszSubkeyNameBuffer
)
205 throw ERROR_OUTOFMEMORY
;
207 Key
.InitSubkeyEnumeration(pszSubkeyNameBuffer
,dwMaxSubkeyNameLength
);
208 while ((nError
= Key
.GetNextSubkeyName()) == ERROR_SUCCESS
)
210 if (PatternMatch(pszPattern
,pszSubkeyNameBuffer
))
212 rConsole
.Write(_T("\t(KEY)\t\t\t\t"));
213 rConsole
.Write(pszSubkeyNameBuffer
);
214 rConsole
.Write(_T("\\\n"));
219 delete pszSubkeyNameBuffer
;
221 if (nError
!= ERROR_NO_MORE_ITEMS
)
224 DWORD dwMaxValueNameBufferSize
;
225 nError
= Key
.GetMaxValueNameLength(dwMaxValueNameBufferSize
);
226 if (nError
!= ERROR_SUCCESS
)
229 TCHAR
*pchValueNameBuffer
= new TCHAR
[dwMaxValueNameBufferSize
];
230 if (!pchValueNameBuffer
)
231 throw ERROR_OUTOFMEMORY
;
235 Key
.InitValueEnumeration(pchValueNameBuffer
,
236 dwMaxValueNameBufferSize
,
241 DWORD dwValueNameActualLength
;
242 const TCHAR
*pszValueTypeName
;
243 unsigned int nTabSize
= rConsole
.GetTabWidth();
245 while((nError
= Key
.GetNextValue(&dwValueNameActualLength
)) == ERROR_SUCCESS
)
247 if (PatternMatch(pszPattern
,pchValueNameBuffer
))
249 rConsole
.Write(_T("\t"));
250 pszValueTypeName
= CRegistryKey::GetValueTypeName(Type
);
251 nTabs
= _tcslen(pszValueTypeName
)/nTabSize
;
252 nTabs
= (nTabs
< 4)?(4-nTabs
):1;
253 rConsole
.Write(pszValueTypeName
);
255 rConsole
.Write(_T("\t"));
256 rConsole
.Write((dwValueNameActualLength
== 0)?_T("(Default)"):pchValueNameBuffer
);
257 rConsole
.Write(_T("\n"));
262 delete pchValueNameBuffer
;
264 if (nError
!= ERROR_NO_MORE_ITEMS
)
270 rConsole
.Write(_T("Error "));
272 rConsole
.Write(_itoa(nError
,Buffer
,10));
273 rConsole
.Write(_T("\n"));
276 rConsole
.Write(_T("\n Total: "));
278 rConsole
.Write(_ui64tot(nTotalItems
,Buffer
,10));
279 rConsole
.Write(_T(" item(s) listed.\n"));
284 const TCHAR
* CShellCommandDir::GetHelpString()
286 return DIR_CMD_SHORT_DESC
287 _T("Syntax: ") DIR_CMD
_T(" [<PATH>\\][<PATTERN>] [/?]\n\n")
288 _T(" <PATH> - Optional relative path to the key on which command will be executed\n")
289 _T(" <PATTERN> - Optional pattern. Default is the all matching pattern.")
290 _T(" /? - This help.\n\n")
291 _T("Without parameters, command lists keys and values of current key.\n");
294 const TCHAR
* CShellCommandDir::GetHelpShortDescriptionString()
296 return DIR_CMD_SHORT_DESC
;