1 /* $Id: ShellCommandDir.cpp,v 1.2 2000/10/24 20:17:41 narnaoud Exp $
3 * regexpl - Console Registry Explorer
5 * Copyright (C) 2000 Nedko Arnaoudov <nedkohome@atia.com>
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"
33 // *** THIS SHOULD GO IN A MINGW/ROS HEADER (tchar.h ???) - Begin
34 #if 1 // #ifndef _ui64tot ???
36 #define _ui64tot _ui64tow
38 #define _ui64tot _ui64toa
41 // *** THIS SHOULD GO IN A MINGW/ROS HEADER - End
43 #define DIR_CMD _T("DIR")
44 #define DIR_CMD_LENGTH COMMAND_LENGTH(DIR_CMD)
45 #define DIR_CMD_SHORT_DESC DIR_CMD _T(" command lists keys and values of any key.\n")
47 //////////////////////////////////////////////////////////////////////
48 // Construction/Destruction
49 //////////////////////////////////////////////////////////////////////
51 CShellCommandDir::CShellCommandDir(CRegistryTree
& rTree
):m_rTree(rTree
)
55 CShellCommandDir::~CShellCommandDir()
59 BOOL
CShellCommandDir::Match(const TCHAR
*pchCommand
)
61 if (_tcsicmp(pchCommand
,DIR_CMD
) == 0)
63 if (_tcsnicmp(pchCommand
,DIR_CMD
_T(".."),DIR_CMD_LENGTH
+2*sizeof(TCHAR
)) == 0)
65 if (_tcsnicmp(pchCommand
,DIR_CMD
_T("/"),DIR_CMD_LENGTH
+1*sizeof(TCHAR
)) == 0)
67 if (_tcsnicmp(pchCommand
,DIR_CMD
_T("\\"),DIR_CMD_LENGTH
+1*sizeof(TCHAR
)) == 0)
72 int CShellCommandDir::Execute(CConsole
&rConsole
, CArgumentParser
& rArguments
)
74 rArguments
.ResetArgumentIteration();
76 const TCHAR
*pchKey
= NULL
;
77 BOOL blnDo
= TRUE
,blnBadParameter
, blnHelp
= FALSE
;
78 const TCHAR
*pchParameter
;
79 const TCHAR
*pchCommandItself
= rArguments
.GetNextArgument();
81 if ((_tcsnicmp(pchCommandItself
,DIR_CMD
_T(".."),DIR_CMD_LENGTH
+2*sizeof(TCHAR
)) == 0)||
82 (_tcsnicmp(pchCommandItself
,DIR_CMD
_T("\\"),DIR_CMD_LENGTH
+1*sizeof(TCHAR
)) == 0))
84 pchKey
= pchCommandItself
+ DIR_CMD_LENGTH
;
86 else if (_tcsnicmp(pchCommandItself
,DIR_CMD
_T("/"),DIR_CMD_LENGTH
+1*sizeof(TCHAR
)) == 0)
88 pchParameter
= pchCommandItself
+ DIR_CMD_LENGTH
;
89 goto CheckDirArgument
;
92 while((pchParameter
= rArguments
.GetNextArgument()) != NULL
)
95 blnBadParameter
= FALSE
;
96 if ((_tcsicmp(pchParameter
,_T("/?")) == 0)
97 ||(_tcsicmp(pchParameter
,_T("-?")) == 0))
100 blnDo
= pchKey
!= NULL
;
104 pchKey
= pchParameter
;
109 blnBadParameter
= TRUE
;
113 rConsole
.Write(_T("Bad parameter: "));
114 rConsole
.Write(pchParameter
);
115 rConsole
.Write(_T("\n"));
119 CRegistryTree
*pTree
= NULL
;
120 CRegistryKey
*pKey
= NULL
;
123 pTree
= new CRegistryTree(m_rTree
);
124 if ((_tcscmp(pTree
->GetCurrentPath(),m_rTree
.GetCurrentPath()) != 0)||
125 (!pTree
->ChangeCurrentKey(pchKey
)))
127 rConsole
.Write(_T("Cannot open key "));
128 rConsole
.Write(pchKey
);
129 rConsole
.Write(_T("\n"));
135 pKey
= pTree
->GetCurrentKey();
140 pKey
= m_rTree
.GetCurrentKey();
145 rConsole
.Write(GetHelpString());
152 rConsole
.Write(_T("\n Key is "));
153 // rConsole.Write(_T("\\"));
154 rConsole
.Write(pTree
?pTree
->GetCurrentPath():m_rTree
.GetCurrentPath());
157 rConsole
.Write(_T("\n Last modify time is "));
158 rConsole
.Write(pKey
->GetLastWriteTime());
160 rConsole
.Write(_T("\n\n"));
161 unsigned __int64 nTotalItems
= 0;
164 rConsole
.Write(_T("\t(KEY)\t\t\t\tHKEY_CLASSES_ROOT\\\n"));
165 rConsole
.Write(_T("\t(KEY)\t\t\t\tHKEY_CURRENT_USER\\\n"));
166 rConsole
.Write(_T("\t(KEY)\t\t\t\tHKEY_LOCAL_MACHINE\\\n"));
167 rConsole
.Write(_T("\t(KEY)\t\t\t\tHKEY_USERS\\\n"));
168 rConsole
.Write(_T("\t(KEY)\t\t\t\tHKEY_PERFORMANCE_DATA\\\n"));
169 rConsole
.Write(_T("\t(KEY)\t\t\t\tHKEY_CURRENT_CONFIG\\\n"));
170 rConsole
.Write(_T("\t(KEY)\t\t\t\tHKEY_DYN_DATA\\\n"));
175 dwError
= ERROR_SUCCESS
;
178 if (!pKey
->IsPredefined())
180 dwError
= pKey
->Open(KEY_QUERY_VALUE
|KEY_READ
);
181 if (dwError
!= ERROR_SUCCESS
) throw dwError
;
184 ASSERT(nTotalItems
== 0);
185 rConsole
.Write(_T("\t(KEY)\t\t\t\t..\\\n")); // parent key abstraction
188 pKey
->InitSubKeyEnumeration();
189 TCHAR
*pchSubKeyName
;
190 while ((pchSubKeyName
= pKey
->GetSubKeyName(dwError
)) != NULL
)
192 rConsole
.Write(_T("\t(KEY)\t\t\t\t"));
193 rConsole
.Write(pchSubKeyName
);
194 rConsole
.Write(_T("\\\n"));
197 if ((dwError
!= ERROR_SUCCESS
)&&(dwError
!= ERROR_NO_MORE_ITEMS
)) throw dwError
;
199 pKey
->InitValueEnumeration();
201 DWORD dwValueNameLength
, dwMaxValueNameLength
;
202 dwError
= pKey
->GetMaxValueNameLength(dwMaxValueNameLength
);
203 if (dwError
!= ERROR_SUCCESS
) throw dwError
;
204 dwMaxValueNameLength
++;
205 pchValueName
= new TCHAR
[dwMaxValueNameLength
];
209 dwValueNameLength
= dwMaxValueNameLength
;
210 //dwValueSize = dwMaxValueSize;
211 dwError
= pKey
->GetNextValue(pchValueName
,dwValueNameLength
,&Type
,
215 if (dwError
== ERROR_NO_MORE_ITEMS
) break;
216 if (dwError
!= ERROR_SUCCESS
) throw dwError
;
217 rConsole
.Write(_T("\t"));
218 rConsole
.Write(CRegistryKey::GetValueTypeName(Type
));
219 rConsole
.Write(_T("\t"));
220 rConsole
.Write((dwValueNameLength
== 0)?_T("(Default)"):pchValueName
);
221 rConsole
.Write(_T("\n"));
224 delete [] pchValueName
;
226 catch (DWORD dwError
)
228 rConsole
.Write(_T("Error "));
230 rConsole
.Write(_itot(dwError
,Buffer
,10));
231 rConsole
.Write(_T("\n"));
233 } // else (Tree.IsCurrentRoot())
235 rConsole
.Write(_T("\n Total: "));
237 rConsole
.Write(_ui64tot(nTotalItems
,Buffer
,10));
238 rConsole
.Write(_T(" item(s) listed.\n"));
239 if (pTree
) delete pTree
;
245 const TCHAR
* CShellCommandDir::GetHelpString()
247 return DIR_CMD_SHORT_DESC
248 _T("Syntax: ") DIR_CMD
_T(" [<KEY>] [/?]\n\n")
249 _T(" <KEY> - Optional relative path to the key on which command will be executed\n")
250 _T(" /? - This help.\n\n")
251 _T("Without parameters, command lists keys and values of current key.\n");
254 const TCHAR
* CShellCommandDir::GetHelpShortDescriptionString()
256 return DIR_CMD_SHORT_DESC
;